Ë
    D^(hK(  ã                   ó  — 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y)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              #   óð  ‡K  — t        | «      }|dk\  rt        d«      ‚|rd–— d–— t        |«      D ]%  }t        j	                  t        |dz   «      «      –— Œ' dŠd‰z  |k  r‰dz  Šd‰z  |k  rŒˆfd„}t        d„ | j                  «       D «       «      }g }d	}|D ]»  \  }	}
|	|k(  r)|j                  d	«       |j                   ||
«      «       Œ4|	|dz   k(  r.|dz  }|j                  d«       |j                   ||
«      «       Œj|	}|j                  d«       |j                   ||	«      «       |j                  d	«       |j                   ||
«      «       Œ½ ‰d
k  rV|d‰z  k(  rNt        |«       d
z  ‰k\  r<||dz
  k  r4|j                  d	«       |j                  dgt        |«       d
z  z  «       n"|j                  dgt        |«       d
z  z  «       t        d	t        |«      d
«      D �cg c]I  }||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   ‘ŒK }}|D ]%  }t        j	                  t        |dz   «      «      –— Œ' d–— yc c}w ­w)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                 ó`   •— t        ‰«      D �cg c]  }| d‰dz
  |z
  z  z  rdnd‘Œ c}S c c}w )zBig endian k-bit encoding of xr   r   )Úrange)ÚxÚiÚks     €úX/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/networkx/readwrite/sparse6.pyÚencz$_generate_sparse6_bytes.<locals>.enc;   s4   ø€ ä<AÀ!»HÖE°q�a˜!  A¡¨¡	Ñ*Ò*‘°Ñ2ÒEÐEùÒEs   �+c              3   óR   K  — | ]  \  }}t        ||«      t        ||«      f–— Œ! y ­w)N)ÚmaxÚmin)Ú.0ÚuÚvs      r   ú	<genexpr>z*_generate_sparse6_bytes.<locals>.<genexpr>?   s%   è ø€ Ò@©d¨a°”C˜˜1“Iœs 1 a›yÔ)Ñ@ù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_bytesr6      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Øò  ‰ˆˆ1Ø�Š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ÀqÈ1Áuº~ð
 	�‰�AŒØ�‰�Q�CœS ›Y˜J¨!Ñ+Ñ,Õ-à�‰�Q�CœS ›Y˜J¨!Ñ+Ñ,Ô-ô �qœ#˜d›) QÓ'öð ð 
ˆa�!‰e‰˜Ñ	Ø��A‘‰;˜!Ññ	à��A‘‰;˜!Ññ	ð ��A‘‰;˜!Ññ	ð ��A‘‰;˜!Ññ		ð
 ��A‘‰;˜!Ñó	ð€Dð ð ò &ˆÜ�j‰jœ˜Q ™V›Ó%Ó%ð&à
ƒKùòùs   ƒA0I6Á4E>I6Ç2AI1É 6I6T)ÚgraphsÚreturns_graphc                 ó2  ‡
‡— | j                  d«      r| dd } | j                  d«      st        d«      ‚| dd D �cg c]  }|dz
  ‘Œ	 }}t        |«      \  }Š
dŠd‰z  |k  r‰dz  Šd‰z  |k  rŒˆ
ˆfd„}d	}t        j                  «       }|j                  t        |«      «       d
} |«       D ]I  \  }}	|dk(  r|dz  }|	|k\  s||k\  r n0|	|kD  r|	}Œ$|j                  |	|«      rd}|j                  |	|«       ŒK |st        j                  |«      }|S c c}w )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 sparse6r   r   c               3   ó6  •K  — t        ‰«      } d}d}	 |dk  r	 t        | «      }d}|dz  }||z	  dz  }|d|z  dz
  z  }|}|‰k  r!	 t        | «      }d}|dz  |z   }|dz  }|‰k  rŒ!||‰z
  z	  }|‰z
  }||f–— Œg# t        $ r Y yw xY w# t        $ r Y yw xY w­w)z6Returns stream of pairs b[i], x[i] for sparse6 format.Nr   r   r   )ÚiterÚnextÚStopIteration)Úchunksr2   ÚdLenÚbr   ÚxLenr5   r   s         €€r   Ú	parseDataz%from_sparse6_bytes.<locals>.parseData–   sí   øè ø€ ä�d“ˆØˆØˆàØ�aŠxðÜ˜V›�Að �Ø�A‰IˆDØ�d‘˜a‘ˆAà�a˜4‘i 1‘_Ñ%ˆAØˆDØ˜’(ðÜ˜V›�Að �Ø˜!‘V˜q‘L�Ø˜‘	�ð ˜“(ð �d˜Q‘h‘ˆAØ˜!‘8ˆDØ�Q�$Š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Úcharsr1   rC   r   r.   Ú
multigraphrA   r   r5   r   s             @@r   r   r   i   s7  ù€ ðD ×Ñ˜Ô(Ø˜˜�ˆØ×Ñ˜TÔ"ÜÐ?Ó@Ð@à# A B˜ZÖ(˜ˆQ�‹VÐ(€EÐ(Ü˜Ó�G€A€tØ	€AØ
ˆq‰&�1Š*Ø	ˆQ‰ˆð ˆq‰&�1‹*õð< 	
€Aä
�‰‹€AØ×Ñ”U˜1“XÔà€JÙ“ò ‰ˆˆ1Ø�Š6Ø�‰FˆAà�Š6�Q˜!’VÙØ�ŠUØ‰Aà�z‰z˜!˜QÔØ!�
Ø�J‰J�q˜!Õðñ Ü�H‰H�Q‹KˆØ€Hùòq )s   ¼Dc                 óŽ   — |�| j                  |«      } t        j                  | d¬«      } dj                  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>

    r*   ©Úorderingó    )ÚsubgraphrE   Úconvert_node_labels_to_integersÚjoinr6   )r.   r/   r0   s      r   r
   r
   Ë   sD   € ðX ÐØ�J‰J�uÓˆÜ
×*Ñ*¨1°xÔ@€AØ�8‰8Ô+¨A¨u°fÓ=Ó>Ð>rR   Úrb)Úmodec                 óª   — g }| D ]8  }|j                  «       }t        |«      sŒ|j                  t        |«      «       Œ: t        |«      dk(  r|d   S |S )aÿ  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	   ý   s[   € ðh €EØò /ˆØ�z‰z‹|ˆÜ�4ŒyØØ�‰Ô'¨Ó-Õ.ð	/ô
 ˆ5ƒz�Q‚Ø�Q‰xˆàˆrR   Údirectedr   Úwbc                 óž   — |�| j                  |«      } t        j                  | d¬«      } t        | ||«      D ]  }|j	                  |«       Œ y)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*   rP   )rS   rE   rT   r6   Úwrite)r.   rZ   r/   r0   rA   s        r   r   r   =  sN   € ðp ÐØ�J‰J�uÓˆÜ
×*Ñ*¨1°xÔ@€AÜ$ Q¨¨vÓ6ò ˆØ�
‰
�1�ñrR   )NT)Ú__doc__ÚnetworkxrE   Únetworkx.exceptionr   Únetworkx.readwrite.graph6r   r   Únetworkx.utilsr   r   Ú__all__r6   Ú_dispatchabler   r
   r	   r   © rR   r   ú<module>ri      s«   ðñ
ó Ý ,ß :ß 9â
U€òOðd €×Ñ˜¨TÔ2ñ^ó 3ð^óB/?ñd ˆ1�4ÔØ€×Ñ˜¨TÔ2ñ;ó 3ó ð;ñ| �ZÓ Ù
ˆ1�4Ôò:ó ó !ñ:rR   