
    \Mh(                     6   d Z ddlZddlmZ ddlmZmZ ddlm	Z	m
Z
 g dZd Z ej        dd	          d
             ZddZ e
dd           ej        dd	          d                         Z e	d           e
dd          dd                        ZdS )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              #   H  K   t          |           }|dk    rt          d          |rdV  dV  t          |          D ].}t                              t          |dz                       V  /ddz  |k     rdz  dz  |k     fd}t          d |                                 D                       }g d	}|D ]\  }}	||k    r4                    d	           	                     ||	                     ?||dz   k    r9|dz  }                    d           	                     ||	                     |}                    d           	                     ||                                         d	           	                     ||	                     d
k     ri|dz  k    r`t                     d
z  k    rI||dz
  k     r@                    d	           	                    dgt                     d
z  z             n*	                    dgt                     d
z  z             fdt          d	t                    d
          D             }
|
D ].}t                              t          |dz                       V  /dV  dS )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                 >      fdt                    D             S )zBig endian k-bit encoding of xc                 4    g | ]}d d z
  |z
  z  z  rd ndS )r   r    ).0ikxs     Z/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/networkx/readwrite/sparse6.py
<listcomp>z8_generate_sparse6_bytes.<locals>.enc.<locals>.<listcomp>=   s4    EEEqa!A	**2EEE    )range)r   r   s   `r   encz$_generate_sparse6_bytes.<locals>.enc;   s'    EEEEEE!HHEEEEr   c              3   \   K   | ]'\  }}t          ||          t          ||          fV  (d S )N)maxmin)r   uvs      r   	<genexpr>z*_generate_sparse6_bytes.<locals>.<genexpr>?   s;      @@daC1IIs1ayy)@@@@@@r   r      c                     g | ][}|d z            dz  |dz            dz  z   |dz            dz  z   |dz            dz  z   |dz            dz  z   |dz            d z  z   \S )r      r            r   )r   r   bitss     r   r   z+_generate_sparse6_bytes.<locals>.<listcomp>Z   s         
a!e	A;!	A;!	 A;!	 A;!		
 A;!	  r      
N)len
ValueErrorr   strencodechrsortededgesappendextendr   )Gnodesheaderndr   r1   curvr!   r    datar)   r   s              @@r   _generate_sparse6_bytesr;      s     * 	AAEzzM
 
 	
  
JJJq\\ & &jjQV%%%%%%	A
q&1**	Q q&1**F F F F F @@aggii@@@@@EDD    199KKNNNKKA$(]]AIDKKNNNKKADKKNNNKKAKKNNNKKA1uuqAvc$iiZ1$4#:#:tq1u~~
 	AQCSYYJ!+,----QCSYYJ!+,---    q#d))Q''  D  & &jjQV%%%%%%
KKKKKr   T)graphsreturns_graphc                 x  	
 |                      d          r
| dd         } |                      d          st          d          d | dd         D             }t          |          \  }	d
d
z  |k     r
dz  
d
z  |k     	
fd}d	}t          j                    }|                    t          |                     d
} |            D ]U\  }}|dk    r|dz  }||k    s||k    r n8||k    r|}'|                    ||          rd}|                    ||           V|st          j	        |          }|S )aV  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 sparse6c                     g | ]}|d z
  S )r   r   )r   cs     r   r   z&from_sparse6_bytes.<locals>.<listcomp>   s    (((QV(((r   r   c               3   Z  K   t                    } d}d}	 |dk     r$	 t          |           }n# t          $ r Y dS w xY wd}|dz  }||z	  dz  }|d|z  dz
  z  }|}|k     r7	 t          |           }n# t          $ r Y dS w xY wd}|dz  |z   }|dz  }|k     7||z
  z	  }|z
  }||fV  )z6Returns stream of pairs b[i], x[i] for sparse6 format.Nr   r   r#   )iternextStopIteration)chunksr8   dLenbr   xLenr:   r   s         r   	parseDataz%from_sparse6_bytes.<locals>.parseData   s     d	axxVAA$   FFAIDdaAa4i1_%AD((VAA$   FF!VqL	 (( dQhA!8DQ$JJJ-	s   / 
==#A3 3
B Br   FT)

startswithr   r   nx
MultiGraphadd_nodes_fromr   has_edgeadd_edgeGraph)stringcharsr7   rJ   r!   r4   
multigraphrH   r   r:   r   s            @@r   r   r   i   s   D (( T"" A?@@@((VABBZ(((EGAt	A
q&1**	Q q&1**     < 	
A
AU1XXJ	  166FA66Q!VVEUUAAzz!Q "!
JJq! HQKKHr   c                     ||                      |          } t          j        | d          } d                    t	          | ||                    S )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>

    Nr0   orderingr   )subgraphrL   convert_node_labels_to_integersjoinr;   )r4   r5   r6   s      r   r
   r
      sP    X JJu
*1x@@@A88+Auf==>>>r   rb)modec                     g }| D ]H}|                                 }t          |          s&|                    t          |                     It          |          dk    r|d         S |S )aB  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+   r2   r   )pathglistlines      r   r	   r	      sr    j E / /zz||4yy 	'--....
5zzQQxr   directedr   wbc                     ||                      |          } t          j        | d          } t          | ||          D ]}|                    |           dS )aX  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>

    Nr0   rV   )rX   rL   rY   r;   write)r4   r_   r5   r6   rH   s        r   r   r   >  sg    r JJu
*1x@@@A$Qv66  

1 r   )NT)__doc__networkxrL   networkx.exceptionr   networkx.readwrite.graph6r   r   networkx.utilsr   r   __all__r;   _dispatchabler   r
   r	   r   r   r   r   <module>rm      s]  
 
     , , , , , , : : : : : : : : 9 9 9 9 9 9 9 9
U
U
UO O Od T222^ ^ 32^B/? /? /? /?d 14T222< < 32 <~ Z  
14; ; ;  ! ; ; ;r   