ó
|£*^c           @   só   d  Z  d d l Z d d l m Z d d l m Z m Z d j d d g ƒ Z	 d d	 d
 d g Z
 d „  Z e d d d ƒd „  ƒ Z e d d ƒ d e d „ ƒ Z e d d d ƒd e d „ ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   s>  Graph6

Read and write graphs in graph6 format.

Format
------

"graph6 and sparse6 are formats for storing undirected graphs in a
compact manner, using only printable ASCII characters. Files in these
formats have text type and contain one line per graph."

See http://cs.anu.edu.au/~bdm/data/formats.txt for details.
iÿÿÿÿN(   t   NetworkXError(   t	   open_filet   not_implemented_fors   
s    Tomas Gavenciak <gavento@ucw.cz>s#   Aric Hagberg <aric.hagberg@lanl.govt   read_graph6t   parse_graph6t   generate_graph6t   write_graph6c            s>  ‡  f d †  } |  j  d ƒ r+ |  d }  n  t |  ƒ ‰  t ˆ  ƒ \ } ‰  | | d d d d } t ˆ  ƒ | k r¤ t d | | d d t ˆ  ƒ d f ƒ ‚ n  t j ƒ  } | j t | ƒ ƒ xt t	 g  t d | ƒ D]% } t | ƒ D] } | | f ^ qé qÙ | ƒ  ƒ D]+ \ \ } } } | r| j
 | | ƒ qqW| S(	   sB  Read a simple undirected graph in graph6 format from string.

    Parameters
    ----------
    string : string
       Data in graph6 format

    Returns
    -------
    G : Graph

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

    Examples
    --------
    >>> G = nx.parse_graph6('A_')
    >>> sorted(G.edges())
    [(0, 1)]

    See Also
    --------
    generate_graph6, read_graph6, write_graph6

    References
    ----------
    Graph6 specification:
    http://cs.anu.edu.au/~bdm/data/formats.txt for details.
    c          3   sE   x> ˆ  D]6 }  x- d d d d d d g D] } |  | ?d @Vq& Wq Wd S(   sT   Return sequence of individual bits from 6-bit-per-value
        list of data values.i   i   i   i   i   i    N(    (   t   dt   i(   t   data(    sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/graph6.pyt   bits>   s    s
   >>graph6<<i
   i   i   i   i   s%   Expected %d bits but got %d in graph6(   t
   startswitht   graph6_to_datat	   data_to_nt   lenR    t   nxt   Grapht   add_nodes_fromt   ranget   zipt   add_edge(   t   stringR
   t   nt   ndt   Gt   jR   t   b(    (   R	   sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/graph6.pyR      s     ,Zi    t   modet   rtc         C   sj   g  } x? |  D]7 } | j  ƒ  } t | ƒ s1 q n  | j t | ƒ ƒ q Wt | ƒ d k rb | d S| Sd S(   sä  Read simple undirected graphs in graph6 format from path.

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

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

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

    Examples
    --------
    >>> nx.write_graph6(nx.Graph([(0,1)]), 'test.g6')
    >>> G = nx.read_graph6('test.g6')
    >>> sorted(G.edges())
    [(0, 1)]

    See Also
    --------
    generate_graph6, parse_graph6, write_graph6

    References
    ----------
    Graph6 specification:
    http://cs.anu.edu.au/~bdm/data/formats.txt for details.
    i   i    N(   t   stripR   t   appendR   (   t   patht   glistt   line(    (    sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/graph6.pyR   V   s    #t   directedt
   multigraphc   
         s  | d k	 r t | ƒ ‰ n t ˆ  ƒ ‰ ‡  ‡ ‡ f d †  } ˆ  j ƒ  ‰ t ˆ ƒ } d } t } xr t t ˆ ˆ ƒ | ƒ  ƒ D]T \ } } | | d | d >O} t } | d d k r} | j | ƒ d } t } q} q} W| rë | j | ƒ n  t	 | ƒ }	 | r
d |	 }	 n  |	 S(   sÃ  Generate graph6 format string from a simple undirected graph.

    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 '>>graph6<<' string to head of data

    Returns
    -------
    s : string
       String in graph6 format

    Raises
    ------
    NetworkXError
        If the graph is directed or has parallel edges

    Examples
    --------
    >>> G = nx.Graph([(0, 1)])
    >>> nx.generate_graph6(G)
    '>>graph6<<A_'

    See Also
    --------
    read_graph6, parse_graph6, write_graph6

    Notes
    -----
    The format does not support edge or node labels, parallel edges or
    self loops.  If self loops are present they are silently ignored.

    References
    ----------
    Graph6 specification:
    http://cs.anu.edu.au/~bdm/data/formats.txt for details.
    c          3   si   xb g  t  d ˆ ƒ D]% }  t  |  ƒ D] } | |  f ^ q# q D]% \ } }  ˆ  j ˆ | ˆ |  ƒ Vq< Wd  S(   Ni   (   R   t   has_edge(   R   R   (   R   R   t   ns(    sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/graph6.pyR
   ¶   s    Hi    i   i   s
   >>graph6<<N(
   t   Nonet   listt   ordert	   n_to_datat   FalseR   R   t   TrueR   t   data_to_graph6(
   R   t   nodest   headerR
   R	   R   t   flushR   R   t   string_data(    (   R   R   R%   sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/graph6.pyR   „   s*    -)i   t   wtc         C   s0   | j  t |  d | d | ƒƒ | j  d ƒ d S(   s­  Write a simple undirected graph to path in graph6 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 '>>graph6<<' string to head of data

    Raises
    ------
    NetworkXError
        If the graph is directed or has parallel edges

    Examples
    --------
    >>> G = nx.Graph([(0, 1)])
    >>> nx.write_graph6(G, 'test.g6')

    See Also
    --------
    generate_graph6, parse_graph6, read_graph6

    Notes
    -----
    The format does not support edge or node labels, parallel edges or
    self loops.  If self loops are present they are silently ignored.

    References
    ----------
    Graph6 specification:
    http://cs.anu.edu.au/~bdm/data/formats.txt for details.
    R-   R.   s   
N(   t   writeR   (   R   R   R-   R.   (    (    sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/graph6.pyR   Î   s    *c         C   sa   g  |  D] } t  | ƒ d ^ q } t | ƒ d k r] t | ƒ d k  sY t | ƒ d k r] d S| S(   s4   Convert graph6 character sequence to 6-bit integers.i?   i    N(   t   ordR   t   mint   maxR&   (   R   t   ct   v(    (    sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/graph6.pyR   ý   s    #6c         C   so   t  |  ƒ d k rE t |  ƒ d k  s6 t |  ƒ d k rE t d ƒ ‚ n  d j g  |  D] } t | d ƒ ^ qR ƒ S(   s<   Convert 6-bit integer sequence to graph6 character sequence.i    i?   s&   graph6 data units must be within 0..63t    (   R   R4   R5   R    t   joint   chr(   R	   R   (    (    sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/graph6.pyR,     s    6c         C   sª   |  d d k r" |  d |  d f S|  d d k r\ |  d d >|  d d >|  d |  d f S|  d d	 >|  d d
 >|  d d >|  d d >|  d d >|  d |  d f S(   sp   Read initial one-, four- or eight-unit value from graph6
    integer sequence.

    Return (value, rest of seq.)i    i>   i   i   i   i   i   i   i   i   i   i   i   i   (    (   R	   (    (    sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/graph6.pyR   
  s
    *c      	   C   sÂ   |  d k  r t  d ƒ ‚ n  |  d k r. |  g S|  d k r^ d |  d ?d @|  d ?d @|  d @g S|  d k r² d d |  d	 ?d @|  d
 ?d @|  d ?d @|  d ?d @|  d ?d @|  d @g St  d ƒ ‚ d S(   s@   Convert an integer to one-, four- or eight-unit graph6 sequence.i    s.   Numbers in graph6 format must be non-negative.i>   iÿï i?   i   i   Iÿÿÿÿ   i   i   i   s5   Numbers above 68719476735 are not supported by graph6N(   R    (   R   (    (    sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/graph6.pyR)     s    $!!c         C   s2   d d  l  } | j j d ƒ r. | j d ƒ n  d  S(   Niÿÿÿÿs   test.g6(   t   osR   t   isfilet   unlink(   t   moduleR;   (    (    sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/graph6.pyt   teardown_module%  s    (   t   __doc__t   networkxR   t   networkx.exceptionR    t   networkx.utilsR   R   R9   t
   __author__t   __all__R   R   R&   R+   R   R   R   R,   R   R)   R?   (    (    (    sm   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/readwrite/graph6.pyt   <module>   s"   
		8.I.				