Tryag File Manager
Home
-
Turbo Force
Current Path :
/
usr
/
lib
/
python2.4
/
site-packages
/
rhsm
/
Upload File :
New :
File
Dir
//usr/lib/python2.4/site-packages/rhsm/huffman.pyc
mò Wx[Uc @ s, d k Z d k Z d e f d „ ƒ YZ d S( Nt HuffmanNodec B s€ t Z d Z e e e e d „ Z e d „ ƒ Z e d „ ƒ Z e d „ ƒ Z e d „ ƒ Z e d „ ƒ Z d „ Z d „ Z RS( s. Represents a node in a Huffman tree. c C s1 | | _ | | _ | | _ | | _ | | _ d S( sZ :param weight: number representing the weight/priority of this node :type weight: int :param value: any value carried by this node, such as a symbol to be used in reconstructing (uncompressing) some data. :param left: child node on the left, should have weight <= right :type left: rhsm.huffman.HuffmanNode :param right: child node on the right, should have weight >= left :type right: rhsm.huffman.HuffmanNode :param parent: parent node :type parent: rhsm.huffman.HuffmanNode N( t weightt selft valuet leftt rightt parent( R R R R R R ( ( t0 /usr/lib/python2.4/site-packages/rhsm/huffman.pyt __init__ s c C s5 | | i | i d | | ƒ } | | _ | | _ | S( s» Combine two nodes according to Huffman's tree-building algorithm. The weight of the left node should be <= that of the right node. If weights are equal, left should be the node that was in the queue longer. This creates a new node and sets it as the parent attribute of each child. :param left: child node on the left, should have weight <= right :type left: rhsm.huffman.HuffmanNode :param right: child node on the right, should have weight >= left :type right: rhsm.huffman.HuffmanNode :return: new node that is the combination of left and right :rtype: rhsm.huffman.HuffmanNode N( t clsR R R t Nonet nodeR ( R R R R ( ( R t combine* s c C s | i d j o | i d j S( sc :return: True iff left and right are None, else False :rtype: bool N( R R R R ( R ( ( R t is_leaf? s c C s= | i d j o t ‚ n | i i | j o d Sn d Sd S( ss :return: '0' if self is left of its parent, or '1' if right of parent. :rtype: str t 0t 1N( R R R t AttributeErrorR ( R ( ( R t direction_from_parentG s c C sx | i p t d ƒ ‚ n g } | } xB | d j o4 | i d j o | i d | i ƒ n | i } q) Wd i | ƒ S( sv :return: Huffman code for this node as a series of characters '0' and '1' :rtype: str s node is not a leafi t N( R R R t turnst next_nodeR R t insertR t join( R R R ( ( R t codeT s c C sÉ t i ƒ } g } | D] } | | | i ƒ f q ~ } t i | ƒ x| t ot t i | ƒ \ } } y t i | ƒ \ } } Wn t j o | Sn Xt i | | i | | ƒ | i ƒ f ƒ qI Wd S( s :param nodes: list of HuffmanNode instances that will become leaves in a Huffman tree. :type nodes: list :return: HuffmanNode instance that is the root node of the tree :rtype: rhsm.huffman.HuffmanNode N( t itertoolst countt countert _[1]t nodesR t nextt queuet heapqt heapifyt Truet heappopR R t IndexErrort heappushR R ( R R R R R R R R R ( ( R t build_treed s - c C s t | i | i ƒ S( N( t cmpR R t other( R R' ( ( R t __cmp__| s c C s d | i | i f S( Ns HuffmanNode(%d, "%s")( R R R ( R ( ( R t __repr__ s ( t __name__t __module__t __doc__R R t classmethodR t propertyR R R R% R( R) ( ( ( R R s ( R R t objectR ( R R R ( ( R t ? s