
    UHj>                    ^   d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	  ee
      j                         j                  d   Zedz  ZdZd	Zd
ZdZdZddZd dZd!dZd"dZd#dZd#dZd$dZd$dZd$dZd%dZ	 	 d&	 	 	 	 	 	 	 d'dZddZd(dZ d(dZ!d)dZ"d*dZ#d+dZ$e%dk(  r e$        yy),zIClassify route-overlap secondaryBranchState fill roots by selector order.    )annotationsN)Path)Any   outmap1_01amap2_02dz0:0z1:0z2:0c                L    | j                  d       d| j                  d       S )Ngroup:slotgetrows    W/home/exedev/hwanse/tools/summarize_save_selector_secondary_route_overlap_candidates.pyselector_keyr      s&    gggq 122    c                    t        | j                  d      xs d      t        | j                  d      xs d      t        | j                  d      xs d      fS )Nr   r   r   _inputIndexintr   r   s    r   selector_sort_keyr      sM    CGGG!"CGGFO q!CGGM"'a( r   c                    g }t        |       D ].  \  }}|j                  d      s|j                  i |d|i       0 t        |t              S )N	fieldMapsr   key)	enumerater   appendsortedr   )	selectorsrowsindexr   s       r   ordered_field_selectorsr$   !   sT    D	* 7
s77;KK535u567 $-..r   c                ^    t        |       D ci c]  \  }}t        |      | c}}S c c}}w N)r   r   )r"   r#   r   s      r   selector_indexr'   )   s(    7@GLu$GGGs   )c                @    | D ci c]  }t        |      | c}S c c}w r&   )r   r"   r   s     r   selector_lookupr*   -   s     .23sLs"333s   c                p    | D ci c]&  }|j                  d      s|j                  d      |( c}S c c}w )NselectedPointerHexr   r)   s     r   root_lookupr-   1   s1    :>`3#''J^B_CGG()3.```s   33c                F    t        | xs i j                  d      xs g       S )Nfills)lenr   r   s    r   
fill_countr1   5   s    	rw'-2..r   c                T    t        d | xs i j                  d      xs g D              S )Nc              3  H   K   | ]  }|j                  d       du sd  yw)afterCurrentFrontierReaderTr   Nr   .0fills     r   	<genexpr>z'after_frontier_count.<locals>.<genexpr>:   s"     nTtxxHd?eim?mqn   ""r/   sumr   r   s    r   after_frontier_countr<   9   s%    nciR__W5;nnnr   c                T    t        d | xs i j                  d      xs g D              S )Nc              3  H   K   | ]  }|j                  d       du sd  yw)beforeCurrentFrontierReaderTr   Nr   r5   s     r   r8   z(before_frontier_count.<locals>.<genexpr>>   s"     oTtxxHe?fjn?nqor9   r/   r:   r   s    r   before_frontier_countr@   =   s%    ociR__W5;ooor   c                `    | t         k(  ry| t        k(  ry| t        k(  ry|||kD  ry|||k  ryy)Nzsource-side previous selectorz(only pre-current route-overlap fill rootz(current selector; fill is after frontierz$post-current route-overlap fill rootz)other pre-current route-overlap fill rootz unmapped route-overlap fill root)SOURCE_SELECTORPREDECESSOR_SELECTORCURRENT_SELECTOR)selectorr#   current_indexs      r   row_rolerG   A   sH    ?".''9##9U]25U]2:-r   c                	   |xs i }|xs i }t        |       }t        |      }t        |      }t        |      }|j	                  d      xs t
        }|j	                  d      xs t        |j	                  d      xs t        }||   }	|j	                  d      xs g D 
ci c]%  }
|
j	                  d      r|
j	                  d      |
' }}
|j	                  |      xs i }|j	                  |j	                  d            }g }|r|j                  ||j	                  |      |j	                  d      |j	                  d      xs g |j	                  d      xs g D cg c]$  }|t        |j	                  d      xs g       v r|& c}t        |      t        |      t        |      |xs i j	                  d	      xs g t        ||j	                  |      |	      d
d       |j	                  d      xs g D ]  }|j	                  d      }|j	                  |      xs i }|rt        |      n$|j	                  d       d|j	                  d       }||k(  rb|j	                  |      }t        |||	      }d}|k(  rd}n||k(  rd}n	|||	kD  rd}|j                  ||||j	                  d      xs g |j	                  d      xs g |j	                  dt        |            t        |      t        |      |j	                  d	      xs g ||d        |D 
cg c]R  }
|
j	                  d      |k7  r<|
j	                  d      +|
j	                  d      |	k  r|
j	                  dd      dkD  r|
T }}
|D 
cg c]  }
|
j	                  d      |k(  s|
 }}
|D 
cg c]>  }
|
j	                  d      +|
j	                  d      |	kD  r|
j	                  dd      dkD  r|
@ }}
t        |      }t        d |D              }t        d |D              }d}i dt         dt"        d|dd|d|	d|j	                  d      d |j	                  d       d!|j	                  d!t%        |            d"t%        |      d#t        fd$|D              d%|d&|d'|d(t%        |      d)t        d* |D              d+d,dt'        |d- .      g d/|d0S c c}
w c c}w c c}
w c c}
w c c}
w )1NsourceRoutePreviousSelectorpredecessorSelectorcurrentSelectorrouteOverlapRootsrootHexr,   r   	routeMapsr/   z?no secondary fill evidence in the source-side previous selector)rE   selectorIndexrM   r   routeMapOverlap	fillCountbeforeCurrentFrontierFillCountafterCurrentFrontierFillCountr/   rolepromotionUser   r   r   blockedz=candidate only after runtime order and persistence are provenzInot an initializer; all valid fills are after the current frontier readerzHpost-current selector table order cannot initialize current selector 2:0rP   rQ   rE   rO   r   c              3  @   K   | ]  }|j                  d d        yw)rS   r   Nr   r6   r   s     r   r8   z build_summary.<locals>.<genexpr>   s     bccgg&EqIb   c              3  @   K   | ]  }|j                  d d        yw)rR   r   Nr   rX   s     r   r8   z build_summary.<locals>.<genexpr>   s     dPSsww'GKdrY   a]  Route-overlap secondaryBranchState fills do not close the selector-merge gap. The source-side previous selector 0:0 has no secondary fill evidence, the only pre-current route-overlap fill root is predecessor selector 1:0, the current 2:0 fill is after the frontier reader, and later route-overlap fill roots appear after selector 2:0 in table order.sourcetargetsourceSelectorcurrentSelectorIndexrouteOrderProvenselectorMergeGapOpenrouteOverlapRootCount#preCurrentRouteOverlapFillRootCountcandidatePredecessorRootCountc              3  L   K   | ]  }|j                  d       k(  sd  yw)rE   r   Nr   )r6   r   predecessor_selectors     r   r8   z build_summary.<locals>.<genexpr>   s#     ,z3cggV`NaeyNyQ,zs   $$$sourceSidePreviousSecondaryFillCount"currentRootBeforeFrontierFillCount!currentRootAfterFrontierFillCount postCurrentRouteOverlapRootCount postCurrentRouteOverlapFillCountc              3  X   K   | ]"  }t        |j                  d       xs d       $ yw)rQ   r   Nr   rX   s     r   r8   z build_summary.<locals>.<genexpr>   s$     /lSVCGGK4H4MA0N/ls   (*"secondaryRouteOverlapPromotesRouteFc                    | j                  d      d u | j                  d      | j                  d      nd| j                  d      xs dfS )NrO   i'  rE    r   r   s    r   <lambda>zbuild_summary.<locals>.<lambda>   sH    (D0,/GGO,D,P(VZ
#)r r   r   )zUprove predecessor selector 1:0 executes before current selector 2:0 in the route pathzSprove secondaryBranchState is not globally reset before the current frontier readerz3find a strict map1_01a source coordinate or hotspot)promotionStatusr"   remainingProofs
conclusion)r$   r*   r-   r'   r   rB   rC   rD   r   setr1   r@   r<   rG   r   r;   SOURCETARGETr0   r    )r!   secondary_fill_rootspredecessor_persistence_gaporderedby_selectorby_rootindexessource_selectorcurrent_selectorrF   r   fill_roots_by_root
source_rowsource_fill_rootr"   item	fill_rootroot_hexselector_rowrE   r#   rT   promotion_usepre_current_rowscurrent_rowspost_current_rowssource_side_previous_fill_countcurrent_after_countcurrent_before_countrr   re   s                                 @r   build_summaryr   O   s   
 052"="C%i0G!'*K'"GW%G1556STgXgO6::;PQiUi2667HI]M],-M (++,?@FB779 		C 
 17RJ)--jnn=Q.RSD'$[[9!~~&:;#4:!+!<!B 3377DJKK   $$45.CDT.U-ABR-S&,"11':@b_gkk/.JMZ]
 	  *--.ABHb 	==+{{8,21=<-immT[F\E]]^_h_l_lms_t^uCv&H%%7!++[M))gM5=#8fM ""{39r(}}->?E2"{Jy4IJ.CI.N-A)-L]]7+1r)
 	: 77:/1GGO$0GGO$}4GGK#a'	 	  $(SC377:+>BR+RCSLS77?#/GGO$}4GGK#a' 	  '11A&B#bUabbdWcdd	W !&!&! 	/! 	3	!
 	+! 	! 	7;;<NO! 	 ; ? ?@V W! 	 !5!9!9:QSVWiSj!k! 	.s3C/D! 	(,z:J,z)z! 	/0O! 	-.B! 	,-@! 	+C0A,B!  	+C/lZk/l,l!!" 	-e#!$ %


 !A! !Y N Ts&   !*S)SAS6SSAS!c                h    | j                  d      xs g }|sydj                  d |d d D              S )Nr/   -, c              3  j   K   | ]+  }d |j                  d       d|j                  d       d  - yw)`vaHex=valueHexNr   r5   s     r   r8   zfill_brief.<locals>.<genexpr>   s3     [q'*+1TXXj-A,B!D[s   13   )r   join)r   r/   s     r   
fill_briefr      s:    GGG"E99[QVWYXYQZ[[[r   c                   ddd| d    d| d    d| d    d	| d
    d| d    d| d    d| d    d| d    d| d    d| d    d| d    dd| d   dddg}| d   D ]  }|j                  d|j                  d       d |j                  d!       d"|j                  d#       d |j                  d$       d%|j                  d&       d%|j                  d'       d%d(j                  |j                  d)      xs g       xs d* d%|j                  d+       d%|j                  d,       d%t        |       d-        |j	                  g d.       |j	                  d/ | d0   D               |j                  d       d1j                  |      S )2Nz2# Save Selector Secondary Route-Overlap Candidatesrn   z	- route: r[   z -> r\   z&- route-overlap secondary fill roots: ra   z(- pre-current route-overlap fill roots: rb   z- candidate predecessor roots: rc   z(- source-side previous secondary fills: rf   z&- current root before-frontier fills: rg   z%- current root after-frontier fills: rh   z$- post-current route-overlap roots: ri   z*- secondary route-overlap promotes route: rl   z- promotion status: `rp   r   rr   z{| selector | index | root | fills | before frontier | after frontier | route overlap | role | promotion use | first fills |zA| --- | ---: | --- | ---: | ---: | ---: | --- | --- | --- | --- |r"   z| `rE   z` | rO   z | `rM   rQ   z | rR   rS   r   rP   r   rT   rU   z |)rn   z## Remaining Proofsrn   c              3  &   K   | ]	  }d |   yw)z- N r6   r   s     r   r8   zmarkdown.<locals>.<genexpr>   s     D2dVDs   rq   
)r   r   r   r   extend)summarylinesr   s      r   markdownr      s   <

GH%&d78+<*=>
09P1Q0RS
27;`3a2bc
)'2Q*R)ST
27;a3b2cd
09]1^0_`
/8[0\/]^
.w7Y/Z.[\
4W=a5b4cd
(9 :;1=

 	FK#E& v 
#''*%&d377?+C*DDQZI[H\\`ww{#$C0P(Q'RRUww678DIIcggN_F`Ffdf<g<nkn;oorwwvs377>#:";3z#>OrS	

 
LL01	LLD1B)CDD	LL99Ur   c                .   dj                  d | d   D              }dj                  d | d   D              }dj                  g ddd	d
ddddddddddddddt        j                  | d          dt        j                  | d          d| d    d| d    d | d!    d"| d#    d$| d%    d&| d'    d(d)d*| d    d+d,| d!    d+d-| d#    d+d.| d%    d+d/d0t        j                  | d1          d2d3|d4d5| d6d7d8      S )9Nr   c              3  <  K   | ]  }d t        j                  t        |j                  d                   dt        j                  t        |j                  d                   dt        j                  t        |j                  d                   d|j                  d       d|j                  d       d|j                  d	       dt        j                  d
j	                  |j                  d      xs g       xs d       dt        j                  t        |j                  d                   dt        j                  t        |j                  d                   dd
j	                  d |j                  d      xs g dd D              xs d d  yw)z<tr><td><code>rE   z</code></td><td>rO   z</td><td><code>rM   rQ   z	</td><td>rR   rS   r   rP   r   rT   rU   c           	   3     K   | ]h  }d t        j                  t        |j                  d                  z   dz   t        j                  t        |j                  d                  z   dz    j yw)z<code>r   r   r   z</code>N)htmlescapestrr   r5   s     r   r8   z&html_page.<locals>.<genexpr>.<genexpr>  sm       m  HLDKKDHHW4E0F$GG#MPTP[P[\_`d`h`his`t\uPvv  zC  C  ms   A.A0r/   Nr   z
</td></tr>)r   r   r   r   r   rX   s     r   r8   zhtml_page.<locals>.<genexpr>   s      	[[SWWZ%8!9:; <{{3sww789: ;[[SWWY%7!89: ;ww{#$ %ww789 :ww678 9{{499SWW->%?%E2FM#NO P{{3swwv/01 2{{3sww~6789 :yy  m  QT  QX  QX  Y`  Qa  Qg  eg  ik  jk  Pl  m  m  t  qt  u u	s   FFr"   rn   c              3  N   K   | ]  }d t        j                  |       d  yw)z<li></li>N)r   r   r   s     r   r8   zhtml_page.<locals>.<genexpr>  s#     \tDKK-.e4\s   #%rq   z<!doctype html>z<html lang="en">z<head>z  <meta charset="utf-8">zF  <meta name="viewport" content="width=device-width, initial-scale=1">zA  <title>Save Selector Secondary Route-Overlap Candidates</title>z	  <style>zg    :root { color-scheme: dark; font-family: system-ui, sans-serif; background: #101010; color: #eee; }z&    body { margin: 0; padding: 24px; }z[    table { width: 100%; border-collapse: collapse; margin: 12px 0 20px; font-size: 13px; }zi    th, td { border-bottom: 1px solid #303030; padding: 7px 8px; text-align: left; vertical-align: top; }z,    th { background: #181818; color: #ddd; }z    code { color: #d7f0ff; }z
  </style>z</head>z<body>z;  <h1>Save Selector Secondary Route-Overlap Candidates</h1>z  <p>route r[   z -&gt; r\   z; route-overlap roots ra   z; pre-current roots rb   z; source-side fills rf   z; current after-frontier fills rh   z; post-current roots ri   z); secondary route-overlap promotes route rl   z.</p>z  <ul>z.    <li>pre-current route-overlap fill roots: r   z.    <li>source-side previous secondary fills: z+    <li>current root after-frontier fills: z*    <li>post-current route-overlap roots: z  </ul>z  <p>rr   z</p>z  <table><thead><tr><th>selector</th><th>index</th><th>root</th><th>fills</th><th>before frontier</th><th>after frontier</th><th>route overlap</th><th>role</th><th>promotion use</th><th>first fills</th></tr></thead><tbody>z  </tbody></table>z  <h2>Remaining Proofs</h2><ul>z</ul>z</body>z</html>)r   r   r   )r   	body_rowsproofss      r   	html_pager      s   		  6? I WW\IZA[\\F99     	  	#	 
 	Q  	L  	  	r  	1  	f  	t  	7  	'  	  	   	! " 	F# $ dkk'("345WT[[QYIZ=[<\\rsz  |S  tT  sU  Ui  jq  rW  jX  iY  Ym  nu  v\  n]  m^  ^}  ~E  Fi  ~j  }k  k@  AH  Ik  Al  @m  mV  W^  _C  WD  VE  EJ  	K% & 	' ( 9Af9g8hhmn) * 9Ag9h8iino+ , 6g>a6b5cchi- . 5W=_5`4aafg/ 0 	1 2 GL123483 4 	i5 6 	7 8 	9 : *&7; < 	= > 	?     r   c                    |j                  dd       |dz  j                  t        j                  | dd      dz   d	       |d
z  j                  t	        |       d	       y )NT)parentsexist_okz5save_selector_secondary_route_overlap_candidates.jsonF   )ensure_asciiindentr   utf-8encoding5save_selector_secondary_route_overlap_candidates.html)mkdir
write_textjsondumpsr   )r   out_dirs     r   write_outputsr   *  sg    MM$M.FFRR

7q9D@ S  FFRRS\]dSepwRxr   c                p    | j                         s|S t        j                  | j                  d            S )Nr   r   )existsr   loads	read_text)pathfallbacks     r   	load_jsonr   3  s*    ;;=::dnngn677r   c                 d   t        j                         } | j                  dt        t        dz         | j                  dt        t        dz         | j                  dt        t        dz         | j                  dt        t               | j                         }t        t        j                  |j                  j                  d	
            t        |j                  i       t        |j                  i             }t        ||j                         t!        d|j                  dz          y )Nz--selectorszsave_scene_selectors.json)typedefaultz--secondary-fill-rootsz'save_selector_secondary_fill_roots.jsonz--predecessor-persistence-gapz.save_selector_predecessor_persistence_gap.jsonz	--out-dirr   r   z,wrote secondary route-overlap candidates -> r   )argparseArgumentParseradd_argumentr   OUT
parse_argsr   r   r   r!   r   r   rv   rw   r   r   print)parserargsr   s      r   mainr   9  s   $$&F
D#@[:[\
0tSKtEtu
7dC  SC  MC  D
$<D

4>>++W+=>$++R0$22B7G
 '4<<(	8H9  9A  B  Cr   __main__)r   dictreturnr   )r   r   r   ztuple[int, int, int])r!   
list[dict]r   r   )r"   r   r   zdict[str, int])r"   r   r   zdict[str, dict])r   dict | Noner   r   )rE   r   r#   z
int | NonerF   r   r   r   )NN)r!   r   rv   r   rw   r   r   r   )r   r   r   r   )r   r   r   r   r   None)r   r   r   r   r   r   )r   r   )&__doc__
__future__r   r   r   r   pathlibr   typingr   __file__resolver   ROOTr   rt   ru   rB   rC   rD   r   r   r$   r'   r*   r-   r1   r<   r@   rG   r   r   r   r   r   r   r   __name__r   r   r   <module>r      s   O "      H~''*
Ul		  3/H4a/op.  )-/3||%| "-| 
	|~\B1hy8C  zF r   