Exemplo n.º 1
0
 def test_stress(self):
     limit = 3000
     sl = SkipList()
     items = list( range(limit) )
     shuffle( items )
     for i in range(limit):
         sl.insert( items[i] )
         sl.remove( i / 2 )
Exemplo n.º 2
0
 def test_stress(self):
     limit = 3000
     sl = SkipList()
     items = list(range(limit))
     shuffle(items)
     for i in range(limit):
         sl.insert(items[i])
         sl.remove(i / 2)
Exemplo n.º 3
0
    def test_remove_r_last(self):
        sl = SkipList()
        sl.insert(2)
        sl.insert(1)

        sl.remove(2)

        self.assertEqual(sl.head.data, 1)
        for i in range(sl.max_height + 1):
            self.assertIsNone(sl.head.skiplist[i])
Exemplo n.º 4
0
    def test_remove_r_last(self):
        sl = SkipList()
        sl.insert( 2 )
        sl.insert( 1 )

        sl.remove( 2 )

        self.assertEqual(sl.head.data, 1)
        for i in range(sl.max_height+1):
            self.assertIsNone(sl.head.skiplist[i])
Exemplo n.º 5
0
    def test_remove_r_first(self):
        sl = SkipList()
        sl.insert(1)
        sl.insert(2, 0)

        sl.remove(1)

        self.assertEqual(sl.head.data, 2)
        self.assertEqual(sl.head.level, sl.max_height)
        for i in range(sl.max_height + 1):
            self.assertIsNone(sl.head.skiplist[i])
Exemplo n.º 6
0
    def test_remove_r_first(self):
        sl = SkipList()
        sl.insert( 1 )
        sl.insert( 2, 0 )

        sl.remove( 1 )

        self.assertEqual(sl.head.data, 2)
        self.assertEqual(sl.head.level, sl.max_height)
        for i in range(sl.max_height+1):
            self.assertIsNone(sl.head.skiplist[i])
Exemplo n.º 7
0
    def test_remove_r_middle_2(self):
        sl = SkipList()
        sl.insert(1, 3)
        sl.insert(2, 0)
        sl.insert(3, 0)
        sl.insert(4, 2)
        sl.insert(5, 0)
        sl.insert(6, 3)

        sl.remove(4)

        self.assertEqual(sl.head.data, 1)
        self.assertEqual(sl.get_at(0).skiplist[2].data, 6)
        self.assertEqual(sl.get_at(0).skiplist[1].data, 6)
        self.assertEqual(sl.get_at(2).skiplist[0].data, 5)
Exemplo n.º 8
0
    def test_remove_r_middle_2(self):
        sl = SkipList()
        sl.insert( 1, 3 )
        sl.insert( 2, 0 )
        sl.insert( 3, 0 )
        sl.insert( 4, 2 )
        sl.insert( 5, 0 )
        sl.insert( 6, 3 )

        sl.remove( 4 )

        self.assertEqual(sl.head.data, 1)
        self.assertEqual(sl.get_at(0).skiplist[2].data, 6)
        self.assertEqual(sl.get_at(0).skiplist[1].data, 6)
        self.assertEqual(sl.get_at(2).skiplist[0].data, 5)
Exemplo n.º 9
0
    def test_remove_r_index_1(self):
        sl = SkipList()
        sl.insert(1, 2)
        sl.insert(2, 0)
        sl.insert(3, 1)
        sl.insert(4, 0)
        sl.insert(5, 2)

        sl.remove(3)

        self.assertEqual(sl.get_at(0).skipindex[0], 1)
        self.assertEqual(sl.get_at(0).skipindex[1], 3)
        self.assertEqual(sl.get_at(0).skipindex[2], 3)

        self.assertEqual(sl.get_at(1).skipindex[0], 1)
        self.assertEqual(sl.get_at(2).skipindex[0], 1)
Exemplo n.º 10
0
    def test_remove_r_index_1(self):
        sl = SkipList()
        sl.insert( 1, 2 )
        sl.insert( 2, 0 )
        sl.insert( 3, 1 )
        sl.insert( 4, 0 )
        sl.insert( 5, 2 )

        sl.remove( 3 )

        self.assertEqual(sl.get_at(0).skipindex[0], 1)
        self.assertEqual(sl.get_at(0).skipindex[1], 3)
        self.assertEqual(sl.get_at(0).skipindex[2], 3)

        self.assertEqual(sl.get_at(1).skipindex[0], 1)
        self.assertEqual(sl.get_at(2).skipindex[0], 1)
Exemplo n.º 11
0
    def test_remove_r_middle_1(self):
        sl = SkipList()
        sl.insert( 3 )
        sl.insert( 2 )
        sl.insert( 1 )

        sl.remove( 2 )

        self.assertEqual(sl.head.data, 1)
        self.assertEqual(sl.get_at(1).data, 3)

        self.assertIsNone(sl.find(2))

        sl.insert( 2 )
        self.assertEqual(sl.get_at(1).data, 2)
        self.assertEqual(sl.get_at(2).data, 3)
Exemplo n.º 12
0
    def test_remove_r_middle_1(self):
        sl = SkipList()
        sl.insert(3)
        sl.insert(2)
        sl.insert(1)

        sl.remove(2)

        self.assertEqual(sl.head.data, 1)
        self.assertEqual(sl.get_at(1).data, 3)

        self.assertIsNone(sl.find(2))

        sl.insert(2)
        self.assertEqual(sl.get_at(1).data, 2)
        self.assertEqual(sl.get_at(2).data, 3)
Exemplo n.º 13
0
    def test_remove_r_index_5(self):
        # remove last
        sl = SkipList()
        sl.insert( 1, 2 )
        sl.insert( 2, 1 )
        sl.insert( 3, 0 )
        sl.insert( 4, 2 )

        sl.remove( 4 )
        sl.insert( 4, 2 )

        self.assertEqual(sl.get_at(0).skipindex[0], 1)
        self.assertEqual(sl.get_at(0).skipindex[1], 1)
        self.assertEqual(sl.get_at(0).skipindex[2], 3)

        self.assertEqual(sl.get_at(1).skipindex[0], 1)
        self.assertEqual(sl.get_at(1).skipindex[1], 2)

        self.assertEqual(sl.get_at(2).skipindex[0], 1)
Exemplo n.º 14
0
    def test_remove_r_index_5(self):
        # remove last
        sl = SkipList()
        sl.insert(1, 2)
        sl.insert(2, 1)
        sl.insert(3, 0)
        sl.insert(4, 2)

        sl.remove(4)
        sl.insert(4, 2)

        self.assertEqual(sl.get_at(0).skipindex[0], 1)
        self.assertEqual(sl.get_at(0).skipindex[1], 1)
        self.assertEqual(sl.get_at(0).skipindex[2], 3)

        self.assertEqual(sl.get_at(1).skipindex[0], 1)
        self.assertEqual(sl.get_at(1).skipindex[1], 2)

        self.assertEqual(sl.get_at(2).skipindex[0], 1)
Exemplo n.º 15
0
    def test_remove(self):
        skip = SkipList([1, 2, 3])
        with self.assertRaises(KeyError):
            skip.remove(100)

        x = 1
        skip.remove(x)
        self.assertNotIn(x, skip)

        items = list(range(100))
        random.shuffle(items)
        skip = SkipList()
        std = set()

        for i, x in enumerate(items):
            skip.add(x)
            std.add(x)

        self.assertEqual(sorted(list(skip)), sorted(list(std)))

        for i, x in enumerate(items):
            skip.remove(x)
            std.remove(x)
            self.assertNotIn(x, skip)
            self.assertEqual(sorted(list(skip)), sorted(list(std)))
Exemplo n.º 16
0
 def test_remove_r(self):
     sl = SkipList()
     sl.insert( 1 )
     sl.remove( 1 )
     sl.remove( 1 )
     self.assertIsNone(sl.head)
Exemplo n.º 17
0
 def test_remove_r(self):
     sl = SkipList()
     sl.insert(1)
     sl.remove(1)
     sl.remove(1)
     self.assertIsNone(sl.head)
Exemplo n.º 18
0
class MultiSet(object):
    """ A class of MultiSet which is a collection of object, like regular set
        but it can contain duplicate elements. It uses SkipList to implement.
    """
    
    def __init__(self):
        '''(MultiSet)-> Nonetype
        Initialize a MultiSet.
        '''
        self.sk = SkipList()
        
        
    def __contains__(self, data):
        '''(MultiSet, object)-> bool
        Return True when the MultiSet contains data, otherwise return False
        '''
        return (data in self.sk)       
                
    def count(self, data):
        '''(Multiset, object)-> int
        Return the number of occurrences of data in the MultiSet.
        '''
        return self.sk.count(data)
        
    def insert(self, data):
        '''(MultiSet, object) -> Nonetype
        Add element to MultiSet in None descending order.
        ex: {1,3,4,6,8}
        '''
        self.sk.insert(data)
    
    def __repr__(self):
        '''(MultiSet)-> str
        Return a string that represent the MultiSet. 
        the orders is not matters.
        '''
        res = repr(self.sk)
        res = "MultiSet([" + res + "])"
        return res
    
    def remove(self, data):
        '''(MultiSet, object)-> Nonetype
        Remove the object from MultiSet, if it not in MultiSet, do nothing.
        '''
        self.sk.remove(data)   
            
    def clear(self):
        '''(MultiSet)->Nonetype
        Remove all the elements from MultiSet.
        '''
        self.sk.clear()
    
    def __len__(self):
        '''(MultiSet)->int
        Retunr the lenght of MultiSet.
        '''
        return len(self.sk)
    
    def __eq__(self, other):
        '''(MultiSet, MultiSet)->bool
        Return True when multiset are the same as self MultiSet, 
        the same elements and same occurences times.
        otherwise return False
        '''
        if type(self) != type(other):
            return False        
        return (self.sk == other.sk)
        
    def __le__(self, other):
        '''(MultiSet, MultiSet) -> bool
        Return True when the self is the subset of multiset.
        otherwise return False
        '''
        if type(self) != type(other):
            return False
        return (self.sk <= other.sk)
        
    def __sub__(self, other):
        '''(MultiSet, MultiSet) -> MultiSet
        Return a new multiset that contains every element 
        that belongs to multiset s1 but not to multiset s2;
        in others word is the difference between self and multiset,
        which is self - multiset
        '''
        if type(self) == type(other):
            res = MultiSet()
            for i in self.sk:
                num1 = self.count(i)
                num2 = other.count(i)
                num3 = res.count(i)
                if num1 - num2- num3 > 0:
                    res.insert(i)
            return res
    
    def __isub__(self, other):
        '''(MultiSet, MultiSet) -> MultiSet
        Update self so that remove every element in other is remove from 
        self, which is self -= multiset
        '''
        if type(self) == type(other):
            if len(self) != 0:
                for i in other.sk:
                    if i in self:
                        self.remove(i)
            return self
        
    
    def __add__(self, other):
        '''(MultiSet, MultiSet) -> MultiSet
        return a new MultiSet which contains all the elements in self and
        multiset.
        '''
        if type(self) == type(other):
            res = MultiSet()
            if len(self) !=0:
                for i in self.sk: 
                    res.insert(i)
            if len(other) !=0:
                for i in other.sk:
                    res.insert(i)
            return res
    
    def __iadd__(self, other):
        '''(MultiSet, MultiSet) -> MultiSet
        update self which all the elements in other multiset are adding to self.
        '''
        if type(self) == type(other):
            if len(other) !=0:
                for i in other.sk:
                    self.insert(i)
            return self
    
    def __and__(self, other):
        '''(MultiSet, MultiSet) -> MultiSet
        return a new multiset that contains the elements belong to both self
        and other. which is meaned the intersection of self and multiset.
        '''
        if type(self) == type(other):
            res = MultiSet()
            temp = self
            if len(other) != 0 and len(temp) != 0:
                for i in other.sk:
                    if temp.__contains__(i):
                        res.insert(i)
                        temp.remove(i)
            return res
    
    def __iand__(self, other):
        '''(MultiSet, MultiSet) -> MultiSet
        Update self so that it contians only the common of self and other.
        '''
        if type(self) == type(other):
            self = self.__and__(other)
            return self
    
    def isdisjoint(self, other):
        '''(MultiSet, MultiSet) -> MultiSet
        Return True if self have no element in common of other.
        '''
        if type(self) == type(other):
            res = self.__and__(other)
            if len(res) != 0:
                return False
        return True
Exemplo n.º 19
0
class MultiSet(object):
    """ A class of MultiSet which is a collection of object, like regular set
        but it can contain duplicate elements. It uses SkipList to implement.
    """
    def __init__(self):
        '''(MultiSet)-> Nonetype
        Initialize a MultiSet.
        '''
        self.sk = SkipList()

    def __contains__(self, data):
        '''(MultiSet, object)-> bool
        Return True when the MultiSet contains data, otherwise return False
        '''
        return (data in self.sk)

    def count(self, data):
        '''(Multiset, object)-> int
        Return the number of occurrences of data in the MultiSet.
        '''
        return self.sk.count(data)

    def insert(self, data):
        '''(MultiSet, object) -> Nonetype
        Add element to MultiSet in None descending order.
        ex: {1,3,4,6,8}
        '''
        self.sk.insert(data)

    def __repr__(self):
        '''(MultiSet)-> str
        Return a string that represent the MultiSet. 
        the orders is not matters.
        '''
        res = repr(self.sk)
        res = "MultiSet([" + res + "])"
        return res

    def remove(self, data):
        '''(MultiSet, object)-> Nonetype
        Remove the object from MultiSet, if it not in MultiSet, do nothing.
        '''
        self.sk.remove(data)

    def clear(self):
        '''(MultiSet)->Nonetype
        Remove all the elements from MultiSet.
        '''
        self.sk.clear()

    def __len__(self):
        '''(MultiSet)->int
        Retunr the lenght of MultiSet.
        '''
        return len(self.sk)

    def __eq__(self, other):
        '''(MultiSet, MultiSet)->bool
        Return True when multiset are the same as self MultiSet, 
        the same elements and same occurences times.
        otherwise return False
        '''
        if type(self) != type(other):
            return False
        return (self.sk == other.sk)

    def __le__(self, other):
        '''(MultiSet, MultiSet) -> bool
        Return True when the self is the subset of multiset.
        otherwise return False
        '''
        if type(self) != type(other):
            return False
        return (self.sk <= other.sk)

    def __sub__(self, other):
        '''(MultiSet, MultiSet) -> MultiSet
        Return a new multiset that contains every element 
        that belongs to multiset s1 but not to multiset s2;
        in others word is the difference between self and multiset,
        which is self - multiset
        '''
        if type(self) == type(other):
            res = MultiSet()
            for i in self.sk:
                num1 = self.count(i)
                num2 = other.count(i)
                num3 = res.count(i)
                if num1 - num2 - num3 > 0:
                    res.insert(i)
            return res

    def __isub__(self, other):
        '''(MultiSet, MultiSet) -> MultiSet
        Update self so that remove every element in other is remove from 
        self, which is self -= multiset
        '''
        if type(self) == type(other):
            if len(self) != 0:
                for i in other.sk:
                    if i in self:
                        self.remove(i)
            return self

    def __add__(self, other):
        '''(MultiSet, MultiSet) -> MultiSet
        return a new MultiSet which contains all the elements in self and
        multiset.
        '''
        if type(self) == type(other):
            res = MultiSet()
            if len(self) != 0:
                for i in self.sk:
                    res.insert(i)
            if len(other) != 0:
                for i in other.sk:
                    res.insert(i)
            return res

    def __iadd__(self, other):
        '''(MultiSet, MultiSet) -> MultiSet
        update self which all the elements in other multiset are adding to self.
        '''
        if type(self) == type(other):
            if len(other) != 0:
                for i in other.sk:
                    self.insert(i)
            return self

    def __and__(self, other):
        '''(MultiSet, MultiSet) -> MultiSet
        return a new multiset that contains the elements belong to both self
        and other. which is meaned the intersection of self and multiset.
        '''
        if type(self) == type(other):
            res = MultiSet()
            temp = self
            if len(other) != 0 and len(temp) != 0:
                for i in other.sk:
                    if temp.__contains__(i):
                        res.insert(i)
                        temp.remove(i)
            return res

    def __iand__(self, other):
        '''(MultiSet, MultiSet) -> MultiSet
        Update self so that it contians only the common of self and other.
        '''
        if type(self) == type(other):
            self = self.__and__(other)
            return self

    def isdisjoint(self, other):
        '''(MultiSet, MultiSet) -> MultiSet
        Return True if self have no element in common of other.
        '''
        if type(self) == type(other):
            res = self.__and__(other)
            if len(res) != 0:
                return False
        return True