Example #1
0
    def __init__(self,
                 items=None,
                 key_type=None,
                 alg=SORTED_LIST,
                 key=None,
                 compare=None,
                 updator=None):
        """
        :param items: Sequence of initial items 
        :type items: iterable or ``None``        
        :param key_type: Optional keys' type, or ``None`` to show it is unknown or undefined 
            (specifying the key type can greatly improve performance).
        :type key_type: ``int``, ``float``, ``str``, ``bytes``, ``unicode`` (for pre Py3)   , ``(int, int)``, ``(float, float)``, or ``None``
        :param string alg: Underlying algorithm string. Should be one of 
            RED_BLACK_TREE, SPLAY_TREE, or SORTED_LIST.  
        :param function key: Key function: transforms the set's keys into something that 
            can be compared.                               
        :param compare: Comparison function. Should take two parameters, say x and y, 
            and return the a negative number, 0, or positive number, for the cases 
            x < y, x == y, and x > y, respectively.
        :type compare: function or ``None``
        :param updator: Node updator
        :type updator: function or ``None``        
                
        .. Note::
            
            The compare fuction is deprecated in favor of the key function.

        Examples:            

        >>> # (Sorted-list) frozen sorted set with initial items
        >>> t = FrozenSortedSet([1, 3, 2])
        >>> list(t)
        [1, 2, 3]
        >>> assert 1 in t
        >>> assert 4 not in t
        >>> t.add(130)
        Traceback (most recent call last):
            ...
        AttributeError: 'FrozenSortedSet' object has no attribute 'add'

        Key-Type Example:
        
        >>> # Almost no change!
        >>> t = FrozenSortedSet([1, 3, 2], key_type = int)
        >>> # Identical from here.
        """

        self._init_info = _CommonInitInfo(key_type, alg, key, compare, updator)
        metadata = _updator_metadata(True, self._init_info)
        _adopt_updator_methods(self, updator)
        FrozenSetTree.__init__(self, alg, items, key_type, 1, metadata, key,
                               compare, 0)
Example #2
0
 def root(self):
     """
     :returns: The root :py:class:`Node` of the set.
     """
     
     c_node = FrozenSetTree.root(self)
     return Node(c_node) if c_node is not None else None    
Example #3
0
    def root(self):
        """
        :returns: The root :py:class:`Node` of the set.
        """

        c_node = FrozenSetTree.root(self)
        return Node(c_node) if c_node is not None else None
Example #4
0
 def isdisjoint(self, other):
     """
     Checks whether there are no common items in this set and other.
     
     :type other: iterable
     
     Examples:
     >>> assert FrozenSortedSet([1, 3, 2]).isdisjoint([42])
     """
     
     return FrozenSetTree._ext_cmp(self, other, 3)
Example #5
0
    def isdisjoint(self, other):
        """
        Checks whether there are no common items in this set and other.
        
        :type other: iterable
        
        Examples:
        >>> assert FrozenSortedSet([1, 3, 2]).isdisjoint([42])
        """

        return FrozenSetTree._ext_cmp(self, other, 3)
Example #6
0
 def __eq__(self, other):
     """
     :param other: A sequence of items.
     :type other: iterable
     :returns: Whether this set is equal to the set of other.
     
     Examples:
     
     >>> assert FrozenSortedSet([1, 3, 2]) == [1, 2, 3]
     """
         
     return FrozenSetTree._ext_cmp(self, other, 2)
Example #7
0
    def __eq__(self, other):
        """
        :param other: A sequence of items.
        :type other: iterable
        :returns: Whether this set is equal to the set of other.
        
        Examples:
        
        >>> assert FrozenSortedSet([1, 3, 2]) == [1, 2, 3]
        """

        return FrozenSetTree._ext_cmp(self, other, 2)
Example #8
0
    def __xor__(self, other):
        """
        :param iterable other: Other items.
        :returns: The symmetric difference of the items in this set and those in other. 
        
        Example:
        
        >>> FrozenSortedSet([1, 3, 2]) ^ [3, 1, 5]
        FrozenSortedSet([2, 5])
        """

        return self.__class__(FrozenSetTree._ext_union(self, other, 3),
                              self._init_info.key_type, self._init_info.alg,
                              self._init_info.key, self._init_info.compare,
                              self._init_info.updator)
Example #9
0
    def __and__(self, other):
        """
        :param iterable other: Other items.
        :returns: The intersection of the items in this set and those in other. 
        
        Example:
        
        >>> FrozenSortedSet([1, 3, 2]) & [3, 1]
        FrozenSortedSet([1, 3])
        """

        return self.__class__(FrozenSetTree._ext_union(self, other, 1),
                              self._init_info.key_type, self._init_info.alg,
                              self._init_info.key, self._init_info.compare,
                              self._init_info.updator)
Example #10
0
 def issubset(self, other):
     """
     :param other: A sequence of items.
     :type other: iterable
     :returns: Whether this set is a subset of the set of other.
     
     Examples:
     
     >>> assert FrozenSortedSet([1, 3, 2]).issubset([1, 2, 3, 4])
     >>> assert FrozenSortedSet([1, 4, 3]).issubset([1, 4, 2, 3, 4])
     >>> assert not FrozenSortedSet([1, 3, 2]).issubset([])
     >>> assert FrozenSortedSet([1, 2, 3]).issubset([1, 2, 3])        
     >>> assert FrozenSortedSet([1, 2, 3]) <= [1, 2, 3, 4]
     >>> assert FrozenSortedSet([1, 2, 3]) <= [1, 4, 3, 2]
     """
     
     return FrozenSetTree._ext_cmp(self, other, 0)
Example #11
0
    def issubset(self, other):
        """
        :param other: A sequence of items.
        :type other: iterable
        :returns: Whether this set is a subset of the set of other.
        
        Examples:
        
        >>> assert FrozenSortedSet([1, 3, 2]).issubset([1, 2, 3, 4])
        >>> assert FrozenSortedSet([1, 4, 3]).issubset([1, 4, 2, 3, 4])
        >>> assert not FrozenSortedSet([1, 3, 2]).issubset([])
        >>> assert FrozenSortedSet([1, 2, 3]).issubset([1, 2, 3])        
        >>> assert FrozenSortedSet([1, 2, 3]) <= [1, 2, 3, 4]
        >>> assert FrozenSortedSet([1, 2, 3]) <= [1, 4, 3, 2]
        """

        return FrozenSetTree._ext_cmp(self, other, 0)
Example #12
0
 def __xor__(self, other):
     """
     :param iterable other: Other items.
     :returns: The symmetric difference of the items in this set and those in other. 
     
     Example:
     
     >>> FrozenSortedSet([1, 3, 2]) ^ [3, 1, 5]
     FrozenSortedSet([2, 5])
     """
     
     return self.__class__(
         FrozenSetTree._ext_union(self, other, 3),
         self._init_info.key_type,
         self._init_info.alg,
         self._init_info.key,
         self._init_info.compare,
         self._init_info.updator)
Example #13
0
 def __and__(self, other):
     """
     :param iterable other: Other items.
     :returns: The intersection of the items in this set and those in other. 
     
     Example:
     
     >>> FrozenSortedSet([1, 3, 2]) & [3, 1]
     FrozenSortedSet([1, 3])
     """
     
     return self.__class__(
         FrozenSetTree._ext_union(self, other, 1),
         self._init_info.key_type,
         self._init_info.alg,
         self._init_info.key,
         self._init_info.compare,
         self._init_info.updator)
Example #14
0
    def __init__(
            self, 
            items = None, 
            key_type = None,
            alg = SORTED_LIST, 
            key = None,
            compare = None, 
            updator = None):
            
        """
        :param items: Sequence of initial items 
        :type items: iterable or ``None``        
        :param key_type: Optional keys' type, or ``None`` to show it is unknown or undefined 
            (specifying the key type can greatly improve performance).
        :type key_type: ``int``, ``float``, ``str``, ``bytes``, ``unicode`` (for pre Py3)   , ``(int, int)``, ``(float, float)``, or ``None``
        :param string alg: Underlying algorithm string. Should be one of 
            RED_BLACK_TREE, SPLAY_TREE, or SORTED_LIST.  
        :param function key: Key function: transforms the set's keys into something that 
            can be compared.                               
        :param compare: Comparison function. Should take two parameters, say x and y, 
            and return the a negative number, 0, or positive number, for the cases 
            x < y, x == y, and x > y, respectively.
        :type compare: function or ``None``
        :param updator: Node updator
        :type updator: function or ``None``        
                
        .. Note::
            
            The compare fuction is deprecated in favor of the key function.

        Examples:            

        >>> # (Sorted-list) frozen sorted set with initial items
        >>> t = FrozenSortedSet([1, 3, 2])
        >>> list(t)
        [1, 2, 3]
        >>> assert 1 in t
        >>> assert 4 not in t
        >>> t.add(130)
        Traceback (most recent call last):
            ...
        AttributeError: 'FrozenSortedSet' object has no attribute 'add'

        Key-Type Example:
        
        >>> # Almost no change!
        >>> t = FrozenSortedSet([1, 3, 2], key_type = int)
        >>> # Identical from here.
        """

        self._init_info = _CommonInitInfo(key_type, alg, key, compare, updator)
        metadata = _updator_metadata(True, self._init_info)
        _adopt_updator_methods(self, updator)
        FrozenSetTree.__init__(
            self,
            alg,
            items,
            key_type,
            1,
            metadata,
            key,
            compare,
            0)