Beispiel #1
0
    def update(self, *others):
        """
        :param iterable others: Other elemens.
        :type others: iterable or multiple iterables
        
        Update this set to the union of its own items and those in others.

        Example:
        
        >>> t = SortedSet([1, 3, 2])
        >>> t.update([20])        
        >>> t
        SortedSet([1, 2, 3, 20])
        >>> 
        >>> t = SortedSet([1, 3, 2])
        >>> t.update([20], [30, 3])
        >>> t
        SortedSet([1, 2, 3, 20, 30])
        """

        t = self.union(*others)
        SetTree.__init__(self, self._init_info.alg, t,
                         self._init_info.key_type, 1,
                         _updator_metadata(1, self._init_info),
                         self._init_info.key, self._init_info.compare, 1)
Beispiel #2
0
    def symmetric_difference_update(self, *others):
        """
        :param iterable others: Other elemens.
        :type others: iterable or multiple iterables
        
        Update this set to the symmetric difference of its own items and those in others.

        Example:
        
        >>> t = SortedSet([1, 3, 2])
        >>> t.symmetric_difference_update([2, 5])
        >>> t
        SortedSet([1, 3, 5])
        """

        t = self.symmetric_difference(*others)
        SetTree.__init__(
            self,
            self._init_info.alg,
            t,
            self._init_info.key_type,
            1,
            _updator_metadata(1, self._init_info),
            self._init_info.key,
            self._init_info.compare,
            1)                
Beispiel #3
0
    def intersection_update(self, *others):
        """
        :param iterable others: Other elemens.
        :type others: iterable or multiple iterables
        
        Update this set to the intersection of its own items and those in others.

        Example:
        
        >>> t = SortedSet([1, 3, 2])
        >>> t.intersection_update([20])
        >>> t
        SortedSet([])
        >>> t = SortedSet([1, 3, 2])
        >>> t.intersection_update([1, 3], [3])
        >>> t
        SortedSet([3])
        """

        t = self.intersection(*others)
        SetTree.__init__(
            self,
            self._init_info.alg,
            t,
            self._init_info.key_type,
            1,
            _updator_metadata(1, self._init_info),
            self._init_info.key,
            self._init_info.compare,
            1)                
Beispiel #4
0
    def __init__(self,
                 items=None,
                 key_type=None,
                 alg=RED_BLACK_TREE,
                 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:            

        >>> # Red-black tree sorted set
        >>> t = SortedSet()
        >>>
        >>> # (Red-black tree) sorted set with initial items
        >>> t = SortedSet([1, 3, 2])
        >>> list(t)
        [1, 2, 3]
        >>>
        >>> # Splay-tree sorted set
        >>> t = SortedSet(alg = SPLAY_TREE)
        >>>
        >>> # Splay-tree larger-first sorted set with initial items.
        >>> t = SortedSet([1, 3, 2], alg = SPLAY_TREE, compare = lambda x, y: y - x)
        >>> list(t)
        [3, 2, 1]

        Key-Type Example:
        
        >>> # Almost no change!
        >>> t = SortedSet([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)
        SetTree.__init__(self, alg, items, key_type, 1, metadata, key, compare,
                         0)
Beispiel #5
0
    def root(self):
        """
        :returns: The root :py:class:`Node` of the tree.
        """

        c_node = SetTree.root(self)
        return Node(c_node) if c_node is not None else None
Beispiel #6
0
 def root(self):
     """
     :returns: The root :py:class:`Node` of the tree.
     """
     
     c_node = SetTree.root(self)
     return Node(c_node) if c_node is not None else None    
Beispiel #7
0
 def __eq__(self, other):
     """
     :param iterable other: A sequence of items.
     :returns: Whether this set is equal to the set of other.
     
     Examples:
     
     >>> assert SortedSet([1, 3, 2]) == [1, 2, 3]
     """
         
     return SetTree._ext_cmp(self, other, 2)
Beispiel #8
0
    def isdisjoint(self, other):
        """
        Checks whether there are no common items in this set and other.
        
        :type other: iterable
        
        Examples:
        >>> assert SortedSet([1, 3, 2]).isdisjoint([42])
        """

        return SetTree._ext_cmp(self, other, 3)
Beispiel #9
0
 def isdisjoint(self, other):
     """
     Checks whether there are no common items in this set and other.
     
     :type other: iterable
     
     Examples:
     >>> assert SortedSet([1, 3, 2]).isdisjoint([42])
     """
     
     return SetTree._ext_cmp(self, other, 3)
Beispiel #10
0
    def symmetric_difference_update(self, *others):
        """
        :param iterable others: Other elemens.
        :type others: iterable or multiple iterables
        
        Update this set to the symmetric difference of its own items and those in others.

        Example:
        
        >>> t = SortedSet([1, 3, 2])
        >>> t.symmetric_difference_update([2, 5])
        >>> t
        SortedSet([1, 3, 5])
        """

        t = self.symmetric_difference(*others)
        SetTree.__init__(self, self._init_info.alg, t,
                         self._init_info.key_type, 1,
                         _updator_metadata(1, self._init_info),
                         self._init_info.key, self._init_info.compare, 1)
Beispiel #11
0
    def __eq__(self, other):
        """
        :param iterable other: A sequence of items.
        :returns: Whether this set is equal to the set of other.
        
        Examples:
        
        >>> assert SortedSet([1, 3, 2]) == [1, 2, 3]
        """

        return SetTree._ext_cmp(self, other, 2)
Beispiel #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:
        
        >>> SortedSet([1, 3, 2]) ^ [3, 1, 5]
        SortedSet([2, 5])
        """

        return self.__class__(SetTree._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)
Beispiel #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:
        
        >>> SortedSet([1, 3, 2]) & [3, 1]
        SortedSet([1, 3])
        """

        return self.__class__(SetTree._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)
Beispiel #14
0
    def issubset(self, other):
        """
        :param iterable other: A sequence of items.
        :returns: Whether this set is a subset of the set of other.
        
        Examples:
        
        >>> assert SortedSet([1, 3, 2]).issubset([1, 2, 3, 4])
        >>> assert SortedSet([1, 4, 3]).issubset([1, 4, 2, 3, 4])
        >>> assert not SortedSet([1, 3, 2]).issubset([])
        >>> assert SortedSet([1, 2, 3]).issubset([1, 2, 3])        
        >>> assert SortedSet([1, 2, 3]) <= [1, 2, 3, 4]
        >>> assert SortedSet([1, 2, 3]) <= [1, 4, 3, 2]
        """

        return SetTree._ext_cmp(self, other, 0)
Beispiel #15
0
 def issuperset(self, other):
     """
     :param iterable other: A sequence of items.
     :returns: Whether this set is a superset of the set of other.
     
     Examples:
     
     >>> assert not SortedSet([1, 3, 2]).issuperset([1, 2, 3, 4])
     >>> assert not SortedSet([1, 4, 3]).issuperset([1, 4, 2, 3, 4])
     >>> assert SortedSet([1, 3, 2]).issuperset([])
     >>> assert SortedSet([1, 2, 3]).issuperset([1, 2, 3])        
     >>> assert not SortedSet([1, 2, 3]) >= [1, 2, 3, 4]
     >>> assert not SortedSet([1, 2, 3]) >= [1, 4, 3, 2]
     """
     
     return SetTree._ext_cmp(self, other, 1)
Beispiel #16
0
 def __and__(self, other):
     """
     :param iterable other: Other items.
     :returns: The intersection of the items in this set and those in other. 
     
     Example:
     
     >>> SortedSet([1, 3, 2]) & [3, 1]
     SortedSet([1, 3])
     """
     
     return self.__class__(
         SetTree._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)
Beispiel #17
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:
     
     >>> SortedSet([1, 3, 2]) ^ [3, 1, 5]
     SortedSet([2, 5])
     """
     
     return self.__class__(
         SetTree._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)
Beispiel #18
0
    def __init__(
        self, 
        items = None, 
        key_type = None,
        alg = RED_BLACK_TREE, 
        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:            

        >>> # Red-black tree sorted set
        >>> t = SortedSet()
        >>>
        >>> # (Red-black tree) sorted set with initial items
        >>> t = SortedSet([1, 3, 2])
        >>> list(t)
        [1, 2, 3]
        >>>
        >>> # Splay-tree sorted set
        >>> t = SortedSet(alg = SPLAY_TREE)
        >>>
        >>> # Splay-tree larger-first sorted set with initial items.
        >>> t = SortedSet([1, 3, 2], alg = SPLAY_TREE, compare = lambda x, y: y - x)
        >>> list(t)
        [3, 2, 1]

        Key-Type Example:
        
        >>> # Almost no change!
        >>> t = SortedSet([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)
        SetTree.__init__(
            self,
            alg,
            items,
            key_type,
            1,
            metadata,
            key,
            compare,
            0)