
    hK(                         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  pX:X  a*  UR                  S	5        UR                  U" U
5      5        M4  XS-   :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<  XS-
  :  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     L/var/www/html/env/lib/python3.13/site-packages/networkx/readwrite/sparse6.pyenc$_generate_sparse6_bytes.<locals>.enc;   s6    <A!HEHq!A	**2H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     @idaCIs1y)is   %'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EzM
 	
 
Jq\jjQV%%  	
A
q&1*	Q q&1*F @aggi@@EDD9KKNKKA(]AIDKKNKKADKKNKKAKKNKKA  	1uqAvc$iZ1$4#:t1u~
 	AQCSYJ!+,-QCSYJ!+,- q#d)Q' (A 
!e	A;!	A;!	 A;!	 A;!		
 A;!	 ( 	  jj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  pUS:X  a  US-  nX:  d  XS:  a    O6X:  a  U	nM%  UR                  X5      (       a  SnUR                  X5        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axVA AIDaAa4i1_%AD(VA !VqL	 ( QhA!8D$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""?@@#ABZ(ZVZE(GAt	A
q&1*	Q q&1*< 	
A
AU1XJ6FA6QVUAzz!!
JJq  HHQKHq )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 JJu
**1x@A88+Af=>>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$ )ag  Read an undirected graph in sparse6 format from path.

Parameters
----------
path : file or string
   File or filename to write.

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    h Ezz|4yy'-.	 
 5zQQx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

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    p JJu
**1x@A$Qv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 14T2; 3 ;| Z 
14:  !:rU   