ó
    †~iÊ(  ã                   óø   • S r SSKrSSKJr  SSKJrJr  SSKJ	r	J
r
  / SQrS r\R                  " SSS	9S
 5       rSS jr\
" SSS9\R                  " SSS	9S 5       5       r\	" S5      \
" SSS9SS j5       5       rg)aP  Functions for reading and writing graphs in the *sparse6* format.

The *sparse6* file format is a space-efficient format for large sparse
graphs. For small graphs or large dense graphs, use the *graph6* file
format.

For more information, see the `sparse6`_ homepage.

.. _sparse6: https://users.cecs.anu.edu.au/~bdm/data/formats.html

é    N)ÚNetworkXError)Ú	data_to_nÚ	n_to_data)Únot_implemented_forÚ	open_file)Úfrom_sparse6_bytesÚread_sparse6Úto_sparse6_bytesÚwrite_sparse6c              #   ó  ^#   • [        U 5      nUS:¼  a  [        S5      eU(       a  Sv •  Sv •  [        U5       H&  n[        R	                  [        US-   5      5      v •  M(     SmST-  U:  a  TS-  mST-  U:  a  M  U4S jn[        S U R                  5        5       5      n/ nS	nU H½  u  pšX˜:X  a*  UR                  S	5        UR                  U" U
5      5        M4  X˜S-   :X  a/  US-  nUR                  S5        UR                  U" U
5      5        Mk  U	nUR                  S5        UR                  U" U	5      5        UR                  S	5        UR                  U" U
5      5        M¿     TS
:  aX  UST-  :X  aO  [        U5      * S
-  T:¼  a<  XƒS-
  :  a4  UR                  S	5        UR                  S/[        U5      * S
-  -  5        O"UR                  S/[        U5      * S
-  -  5        [        S	[        U5      S
5       Vs/ s HD  nX{S	-      S-  X{S-      S-  -   X{S-      S-  -   X{S-      S-  -   X{S-      S-  -   X{S-      S	-  -   PMF     nnU H&  n[        R	                  [        US-   5      5      v •  M(     Sv •  gs  snf 7f)aí  Yield bytes in the sparse6 encoding of a graph.

`G` is an undirected simple graph. `nodes` is the list of nodes for
which the node-induced subgraph will be encoded; if `nodes` is the
list of all nodes in the graph, the entire graph will be
encoded. `header` is a Boolean that specifies whether to generate
the header ``b'>>sparse6<<'`` before the remaining data.

This function generates `bytes` objects in the following order:

1. the header (if requested),
2. the encoding of the number of nodes,
3. each character, one-at-a-time, in the encoding of the requested
   node-induced subgraph,
4. a newline character.

This function raises :exc:`ValueError` if the graph is too large for
the graph6 format (that is, greater than ``2 ** 36`` nodes).

l       @ z?sparse6 is only defined if number of nodes is less than 2 ** 36ó   >>sparse6<<ó   :é?   é   c                 ón   >• [        T5       Vs/ s H  oSTS-
  U-
  -  -  (       a  SOSPM     sn$ s  snf )zBig endian k-bit encoding of xr   r   )Úrange)ÚxÚiÚks     €ÚW/home/mande/repo/quber/.venv/lib/python3.13/site-packages/networkx/readwrite/sparse6.pyÚencÚ$_generate_sparse6_bytes.<locals>.enc;   s6   ø€ ä<AÀ!¼HÓEºH°q˜!  A¡¨¡	Ñ*×*‘°Ò2¹HÑEÐEùÒEs   � 2c              3   óR   #   • U  H  u  p[        X5      [        X5      4v •  M     g 7f)N)ÚmaxÚmin)Ú.0ÚuÚvs      r   Ú	<genexpr>Ú*_generate_sparse6_bytes.<locals>.<genexpr>?   s   é € Ð@²i©d¨a”C˜“Iœs 1›yÕ)²iùs   ‚%'r   é   é   é   é   é   ó   
N)ÚlenÚ
ValueErrorr   ÚstrÚencodeÚchrÚsortedÚedgesÚappendÚextendr   )ÚGÚnodesÚheaderÚnÚdr   r-   ÚbitsÚcurvr   r   r   Údatar   s                @r   Ú_generate_sparse6_bytesr8      s‡  øé € ô* 	ˆA‹€AØˆEƒzÜØMó
ð 	
ö ØÒØ
‚JÜ�qŽ\ˆÜ�j‰jœ˜Q ™V›Ó%Ô%ñ ð 	
€AØ
ˆq‰&�1‹*Ø	ˆQ‰ˆð ˆq‰&�1�*õFô Ñ@°a·g±g´iÓ@Ó@€EØ€DØ€DÛ‰ˆØ‹9Ø�K‰K˜ŒNØ�K‰K™˜A›ÖØ˜‘(‹]Ø�A‰IˆDØ�K‰K˜ŒNØ�K‰K™˜A›ÖàˆDØ�K‰K˜ŒNØ�K‰K™˜A›ÔØ�K‰K˜ŒNØ�K‰K™˜A›Öñ ð 	ˆ1ƒu��q˜A‘v“¤c¨$£i Z°1Ñ$4¸Ó#:¸tÈ1Áu»~ð
 	�‰�AŒØ�‰�Q�CœS ›Y˜J¨!Ñ+Ñ,Õ-à�‰�Q�CœS ›Y˜J¨!Ñ+Ñ,Ô-ô �qœ#˜d›) QÔ'óò (ˆAð 
�!‰e‰˜Ñ	Ø�A‘‰;˜!Ññ	à�A‘‰;˜!Ññ	ð �A‘‰;˜!Ññ	ð �A‘‰;˜!Ññ		ð
 �A‘‰;˜!Ñô	ñ (ð 	ð ó ˆÜ�j‰jœ˜Q ™V›Ó%Ô%ñ à
ƒKùòùs   ƒA:JÁ?FJÈAJÉ8JT)ÚgraphsÚreturns_graphc                 ól  ^
^• U R                  S5      (       a  U SS n U R                  S5      (       d  [        S5      eU SS  Vs/ s H  oS-
  PM	     nn[        U5      u  nm
SmST-  U:  a  TS-  mST-  U:  a  M  U
U4S jnS	n[        R                  " 5       nUR                  [        U5      5        S
nU" 5        HN  u  p‰US:X  a  US-  nX“:¼  d  XS:¼  a    O6X•:”  a  U	nM%  UR                  X•5      (       a  SnUR                  X•5        MP     U(       d  [        R                  " U5      nU$ s  snf )aö  Read an undirected graph in sparse6 format from string.

Parameters
----------
string : string
   Data in sparse6 format

Returns
-------
G : Graph

Raises
------
NetworkXError
    If the string is unable to be parsed in sparse6 format

Examples
--------
>>> G = nx.from_sparse6_bytes(b":A_")
>>> sorted(G.edges())
[(0, 1), (0, 1), (0, 1)]

See Also
--------
read_sparse6, write_sparse6

References
----------
.. [1] Sparse6 specification
       <https://users.cecs.anu.edu.au/~bdm/data/formats.html>

r   é   Nr   z!Expected leading colon in sparse6r   r   c               3   ó>  >#   • [        T5      n SnSn US:  a   [        U 5      nSnUS-  nX-	  S-  nUSU-  S-
  -  nUnUT:  a#   [        U 5      nSnUS-  U-   nUS-  nUT:  a  M#  XET-
  -	  nUT-
  nX44v •  Mi  ! [         a     gf = f! [         a     gf = f7f)z6Returns stream of pairs b[i], x[i] for sparse6 format.Nr   r   r!   )ÚiterÚnextÚStopIteration)Úchunksr4   ÚdLenÚbr   ÚxLenr7   r   s         €€r   Ú	parseDataÚ%from_sparse6_bytes.<locals>.parseData–   sæ   øé € ä�d“ˆØˆØˆàØ�a‹xðÜ˜V›�Að �Ø�A‰IˆDØ‘˜a‘ˆAà�a˜4‘i 1‘_Ñ%ˆAØˆDØ˜“(ðÜ˜V›�Að �Ø˜!‘V˜q‘L�Ø˜‘	�ð ˜•(ð ˜Q‘h‘ˆAØ˜!‘8ˆDØ�$ŠJñ- øô %ó Ùðûô %ó ÙðüsQ   ƒB›A= ¦!BÁB ÁBÁ*BÁ=
B
ÂBÂ	B
Â
BÂ
BÂBÂBÂBr   FT)
Ú
startswithr   r   ÚnxÚ
MultiGraphÚadd_nodes_fromr   Úhas_edgeÚadd_edgeÚGraph)ÚstringÚcÚcharsr3   rE   r   r0   Ú
multigraphrC   r   r7   r   s             @@r   r   r   i   s.  ù€ ðD ×Ñ˜×(Ñ(Ø˜˜�ˆØ×Ñ˜T×"Ñ"ÜÐ?Ó@Ð@à# A B™ZÓ(šZ˜�ŒV™Z€EÐ(Ü˜Ó�G€A€tØ	€AØ
ˆq‰&�1‹*Ø	ˆQ‰ˆð ˆq‰&�1�*öð< 	
€Aä
�Š‹€AØ×Ñ”U˜1“XÔà€JÙ–‰ˆØ�‹6Ø�‰FˆAà‹6�Q“VÙØ‹UØŠAà�z‰z˜!×ÑØ!�
Ø�J‰J�qÖñ ö Ü�HŠH�Q‹KˆØ€Hùòq )s   ÁD1c                 óŠ   • Ub  U R                  U5      n [        R                  " U SS9n SR                  [	        XU5      5      $ )a{  Convert an undirected graph to bytes in sparse6 format.

Parameters
----------
G : Graph (undirected)

nodes: list or iterable
   Nodes are labeled 0...n-1 in the order provided.  If None the ordering
   given by ``G.nodes()`` is used.

header: bool
   If True add '>>sparse6<<' bytes to head of data.

Raises
------
NetworkXNotImplemented
    If the graph is directed.

ValueError
    If the graph has at least ``2 ** 36`` nodes; the sparse6 format
    is only defined for graphs of order less than ``2 ** 36``.

Examples
--------
>>> nx.to_sparse6_bytes(nx.path_graph(2))
b'>>sparse6<<:An\n'

See Also
--------
to_sparse6_bytes, read_sparse6, write_sparse6_bytes

Notes
-----
The returned bytes end with a newline character.

The format does not support edge or node labels.

References
----------
.. [1] Graph6 specification
       <https://users.cecs.anu.edu.au/~bdm/data/formats.html>

r,   ©Úorderingó    )ÚsubgraphrH   Úconvert_node_labels_to_integersÚjoinr8   )r0   r1   r2   s      r   r
   r
   Ë   sB   € ðX ÑØ�J‰J�uÓˆÜ
×*Ò*¨1°xÑ@€AØ�8‰8Ô+¨A°fÓ=Ó>Ð>rU   Úrb)Úmodec                 ó¼   • / nU  H?  nUR                  5       n[        U5      (       d  M%  UR                  [        U5      5        MA     [        U5      S:X  a  US   $ U$ )a¦  Read an undirected graph in sparse6 format from path.

Parameters
----------
path : file or string
   Filename or file handle to read.
   Filenames ending in .gz or .bz2 will be decompressed.

Returns
-------
G : Graph/Multigraph or list of Graphs/MultiGraphs
   If the file contains multiple lines then a list of graphs is returned

Raises
------
NetworkXError
    If the string is unable to be parsed in sparse6 format

Examples
--------
You can read a sparse6 file by giving the path to the file::

    >>> import tempfile
    >>> with tempfile.NamedTemporaryFile(delete=False) as f:
    ...     _ = f.write(b">>sparse6<<:An\n")
    ...     _ = f.seek(0)
    ...     G = nx.read_sparse6(f.name)
    >>> list(G.edges())
    [(0, 1)]

You can also read a sparse6 file by giving an open file-like object::

    >>> import tempfile
    >>> with tempfile.NamedTemporaryFile() as f:
    ...     _ = f.write(b">>sparse6<<:An\n")
    ...     _ = f.seek(0)
    ...     G = nx.read_sparse6(f)
    >>> list(G.edges())
    [(0, 1)]

See Also
--------
read_sparse6, from_sparse6_bytes

References
----------
.. [1] Sparse6 specification
       <https://users.cecs.anu.edu.au/~bdm/data/formats.html>

r   r   )Ústripr'   r.   r   )ÚpathÚglistÚlines      r   r	   r	   ý   sX   € ðj €EÛˆØ�z‰z‹|ˆÜ�4�y‰yÙØ�‰Ô'¨Ó-Ö.ñ	 ô
 ˆ5ƒz�QƒØ�Q‰xˆàˆrU   Údirectedr   Úwbc                 óž   • Ub  U R                  U5      n [        R                  " U SS9n [        XU5       H  nUR	                  U5        M     g)a¸  Write graph G to given path in sparse6 format.

Parameters
----------
G : Graph (undirected)

path : file or string
   File or filename to write.
   Filenames ending in .gz or .bz2 will be compressed.

nodes: list or iterable
   Nodes are labeled 0...n-1 in the order provided.  If None the ordering
   given by G.nodes() is used.

header: bool
   If True add '>>sparse6<<' string to head of data

Raises
------
NetworkXError
    If the graph is directed

Examples
--------
You can write a sparse6 file by giving the path to the file::

    >>> import tempfile
    >>> with tempfile.NamedTemporaryFile(delete=False) as f:
    ...     nx.write_sparse6(nx.path_graph(2), f.name)
    ...     print(f.read())
    b'>>sparse6<<:An\n'

You can also write a sparse6 file by giving an open file-like object::

    >>> with tempfile.NamedTemporaryFile() as f:
    ...     nx.write_sparse6(nx.path_graph(2), f)
    ...     _ = f.seek(0)
    ...     print(f.read())
    b'>>sparse6<<:An\n'

See Also
--------
read_sparse6, from_sparse6_bytes

Notes
-----
The format does not support edge or node labels.

References
----------
.. [1] Sparse6 specification
       <https://users.cecs.anu.edu.au/~bdm/data/formats.html>

Nr,   rS   )rV   rH   rW   r8   Úwrite)r0   r]   r1   r2   rC   s        r   r   r   >  sG   € ðr ÑØ�J‰J�uÓˆÜ
×*Ò*¨1°xÑ@€AÜ$ Q¨vÖ6ˆØ�
‰
�1Žò 7rU   )NT)Ú__doc__ÚnetworkxrH   Únetworkx.exceptionr   Únetworkx.readwrite.graph6r   r   Únetworkx.utilsr   r   Ú__all__r8   Ú_dispatchabler   r
   r	   r   © rU   r   Ú<module>rl      s§   ðñ
ó Ý ,ß :ß 9â
U€òOðd ×Ò˜¨TÑ2ñ^ó 3ð^ôB/?ñd ˆ1�4ÑØ×Ò˜¨TÑ2ñ<ó 3ó ð<ñ~ �ZÓ Ù
ˆ1�4Ñó;ó ó !ñ;rU   