def test_relevel_2(self):
        sl = SkipList( 3 )
        sl.insert( 1 )
        sl.insert( 2 )
        sl.insert( 3 )
        sl.insert( 4 )
        sl.insert( 5 )
        sl.insert( 6 )
        sl.insert( 7 )
        sl.insert( 8 )
        sl.insert( 9 )

        sl.relevel()

        self.assertEqual(sl.get_at(0).level, 3)
        self.assertEqual(sl.get_at(0).skiplist[0].data, 2)
        self.assertEqual(sl.get_at(0).skiplist[1].data, 3)
        self.assertEqual(sl.get_at(0).skiplist[2].data, 5)
        self.assertEqual(sl.get_at(0).skiplist[3].data, 9)
        self.assertEqual(sl.get_at(0).skipindex[0], 1)
        self.assertEqual(sl.get_at(0).skipindex[1], 2)
        self.assertEqual(sl.get_at(0).skipindex[2], 4)
        self.assertEqual(sl.get_at(0).skipindex[3], 8)

        self.assertEqual(sl.get_at(2).level, 1)
        self.assertEqual(sl.get_at(2).skiplist[1].data, 5)

        self.assertEqual(sl.get_at(4).level, 2)
        self.assertEqual(sl.get_at(4).skiplist[1].data, 7)
        self.assertEqual(sl.get_at(4).skiplist[2].data, 9)
        self.assertEqual(sl.get_at(4).skipindex[0], 1)
        self.assertEqual(sl.get_at(4).skipindex[1], 2)
        self.assertEqual(sl.get_at(4).skipindex[2], 4)
Example #2
0
    def test_relevel_2(self):
        sl = SkipList(3)
        sl.insert(1)
        sl.insert(2)
        sl.insert(3)
        sl.insert(4)
        sl.insert(5)
        sl.insert(6)
        sl.insert(7)
        sl.insert(8)
        sl.insert(9)

        sl.relevel()

        self.assertEqual(sl.get_at(0).level, 3)
        self.assertEqual(sl.get_at(0).skiplist[0].data, 2)
        self.assertEqual(sl.get_at(0).skiplist[1].data, 3)
        self.assertEqual(sl.get_at(0).skiplist[2].data, 5)
        self.assertEqual(sl.get_at(0).skiplist[3].data, 9)
        self.assertEqual(sl.get_at(0).skipindex[0], 1)
        self.assertEqual(sl.get_at(0).skipindex[1], 2)
        self.assertEqual(sl.get_at(0).skipindex[2], 4)
        self.assertEqual(sl.get_at(0).skipindex[3], 8)

        self.assertEqual(sl.get_at(2).level, 1)
        self.assertEqual(sl.get_at(2).skiplist[1].data, 5)

        self.assertEqual(sl.get_at(4).level, 2)
        self.assertEqual(sl.get_at(4).skiplist[1].data, 7)
        self.assertEqual(sl.get_at(4).skiplist[2].data, 9)
        self.assertEqual(sl.get_at(4).skipindex[0], 1)
        self.assertEqual(sl.get_at(4).skipindex[1], 2)
        self.assertEqual(sl.get_at(4).skipindex[2], 4)
 def test_insert_r_third_3(self):
     sl = SkipList()
     sl.insert( 1 )
     sl.insert( 2, 1 )
     sl.insert( 3, 0 )
     self.assertEqual(sl.get_at(0).skiplist.count(None), sl.max_height-1)
     self.assertEqual(sl.get_at(1).skiplist.count(None), 1)
     self.assertEqual(sl.get_at(2).skiplist.count(None), 1)
Example #4
0
 def test_insert_r_third_3(self):
     sl = SkipList()
     sl.insert(1)
     sl.insert(2, 1)
     sl.insert(3, 0)
     self.assertEqual(sl.get_at(0).skiplist.count(None), sl.max_height - 1)
     self.assertEqual(sl.get_at(1).skiplist.count(None), 1)
     self.assertEqual(sl.get_at(2).skiplist.count(None), 1)
 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 )
 def test_skip_2(self):
     sl = SkipList()
     sl.insert( 1, 1 )
     sl.insert( 2, 0 )
     sl.insert( 3, 1 )
     self.assertEqual(sl.head.data, 1)
     self.assertEqual(sl.head.skiplist[0].data, 2)
     self.assertEqual(sl.head.skiplist[1].data, 3)
 def test_insert_r_third_2(self):
     sl = SkipList()
     sl.insert( 1 )
     sl.insert( 2 )
     sl.insert( 3 )
     self.assertEqual(sl.head.data, 1)
     self.assertEqual(sl.head.skiplist[0].data, 2)
     self.assertEqual(sl.head.skiplist[0].skiplist[0].data, 3)
 def test_insert_r_second_2(self):
     sl = SkipList()
     sl.insert( 1, 0 )
     sl.insert( 2, 0 )
     self.assertEqual(sl.head.data, 1)
     self.assertEqual(sl.head.skiplist[0].data, 2)
     self.assertEqual(sl.get_at(0).skiplist.count(None), sl.max_height)
     self.assertEqual(sl.get_at(1).skiplist.count(None), 1)
Example #9
0
    def test_find_r_2(self):
        sl = SkipList()
        sl.insert(3, 0)
        sl.insert(1, 0)

        self.assertEqual(sl.find_r(1), 1)
        self.assertEqual(sl.find_r(3), 3)
        self.assertEqual(sl.find_r(2), None)
Example #10
0
 def test_skip_2(self):
     sl = SkipList()
     sl.insert(1, 1)
     sl.insert(2, 0)
     sl.insert(3, 1)
     self.assertEqual(sl.head.data, 1)
     self.assertEqual(sl.head.skiplist[0].data, 2)
     self.assertEqual(sl.head.skiplist[1].data, 3)
Example #11
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)
Example #12
0
 def test_insert_r_second_2(self):
     sl = SkipList()
     sl.insert(1, 0)
     sl.insert(2, 0)
     self.assertEqual(sl.head.data, 1)
     self.assertEqual(sl.head.skiplist[0].data, 2)
     self.assertEqual(sl.get_at(0).skiplist.count(None), sl.max_height)
     self.assertEqual(sl.get_at(1).skiplist.count(None), 1)
Example #13
0
 def test_insert_r_third_2(self):
     sl = SkipList()
     sl.insert(1)
     sl.insert(2)
     sl.insert(3)
     self.assertEqual(sl.head.data, 1)
     self.assertEqual(sl.head.skiplist[0].data, 2)
     self.assertEqual(sl.head.skiplist[0].skiplist[0].data, 3)
Example #14
0
    def test_find_r_2(self):
        sl = SkipList()
        sl.insert( 3, 0 )
        sl.insert( 1, 0 )

        self.assertEqual(sl.find_r(1), 1)
        self.assertEqual(sl.find_r(3), 3)
        self.assertEqual(sl.find_r(2), None)
Example #15
0
def test_insertion():
    skl = SkipList()
    skl.insert('A', 100)
    skl.insert('B', 200)
    assert skl.header.forward[0].key == 'A'
    assert skl.header.forward[0].val == 100
    assert skl.header.forward[0].forward[0].key == 'B'
    assert skl.header.forward[0].forward[0].val == 200
Example #16
0
 def test_insert_r_index_2(self):
     sl = SkipList()
     sl.insert(1, sl.max_height)
     sl.insert(2, 0)
     sl.insert(3, 1)
     self.assertEqual(sl.get_at(0).skipindex[0], 1)
     self.assertEqual(sl.get_at(0).skipindex[1], 2)
     self.assertEqual(sl.get_at(1).skipindex[0], 1)
     self.assertEqual(sl.get_at(2).skipindex[0], 1)
Example #17
0
 def test_insert_r_index_2(self):
     sl = SkipList()
     sl.insert( 1, sl.max_height )
     sl.insert( 2, 0 )
     sl.insert( 3, 1 )
     self.assertEqual(sl.get_at(0).skipindex[0], 1)
     self.assertEqual(sl.get_at(0).skipindex[1], 2)
     self.assertEqual(sl.get_at(1).skipindex[0], 1)
     self.assertEqual(sl.get_at(2).skipindex[0], 1)
Example #18
0
    def test_get_at_3(self):
        sl = SkipList()
        sl.insert( 1, 2 )
        sl.insert( 2, 0 )
        sl.insert( 3, 1 )

        self.assertEqual(sl.get_at(0).data, 1)
        self.assertEqual(sl.get_at(1).data, 2)
        self.assertEqual(sl.get_at(2).data, 3)
        self.assertIsNone(sl.get_at(3))
Example #19
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])
Example #20
0
 def test_skip_1(self):
     sl = SkipList()
     sl.insert(3, 3)
     sl.insert(2, 1)
     sl.insert(1, 3)
     self.assertEqual(sl.head.level, 3)
     self.assertEqual(sl.find(2), 2)
     self.assertEqual(sl.get_at(1).level, 3)
     self.assertEqual(sl.head.skiplist[0].data, 2)
     self.assertEqual(sl.head.skiplist[1].data, 2)
Example #21
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])
Example #22
0
    def test_get_at_3(self):
        sl = SkipList()
        sl.insert(1, 2)
        sl.insert(2, 0)
        sl.insert(3, 1)

        self.assertEqual(sl.get_at(0).data, 1)
        self.assertEqual(sl.get_at(1).data, 2)
        self.assertEqual(sl.get_at(2).data, 3)
        self.assertIsNone(sl.get_at(3))
Example #23
0
    def test_find_4(self):
        sl = SkipList()
        sl.insert( 3, 1 )
        sl.insert( 2, 0 )
        sl.insert( 1, 1 )

        self.assertEqual(sl.find(1), 1)
        self.assertEqual(sl.find(2), 2)
        self.assertEqual(sl.find(3), 3)
        self.assertEqual(sl.find(1.5), None)
Example #24
0
    def test_find_4(self):
        sl = SkipList()
        sl.insert(3, 1)
        sl.insert(2, 0)
        sl.insert(1, 1)

        self.assertEqual(sl.find(1), 1)
        self.assertEqual(sl.find(2), 2)
        self.assertEqual(sl.find(3), 3)
        self.assertEqual(sl.find(1.5), None)
Example #25
0
 def test_skip_1(self):
     sl = SkipList()
     sl.insert( 3, 3 )
     sl.insert( 2, 1 )
     sl.insert( 1, 3 )
     self.assertEqual(sl.head.level, 3)
     self.assertEqual(sl.find(2), 2)
     self.assertEqual(sl.get_at(1).level, 3)
     self.assertEqual(sl.head.skiplist[0].data, 2)
     self.assertEqual(sl.head.skiplist[1].data, 2)
Example #26
0
 def test_insert_r_third_1(self):
     sl = SkipList()
     sl.insert(3)
     sl.insert(2)
     sl.insert(1)
     self.assertEqual(sl.head.data, 1)
     self.assertEqual(sl.head.skiplist[0].data, 2)
     self.assertEqual(sl.head.skiplist[0].skiplist[0].data, 3)
     self.assertEqual(sl.get_at(0).skiplist.count(None), 0)
     self.assertEqual(sl.get_at(1).skiplist.count(None), 0)
     self.assertEqual(sl.get_at(2).skiplist.count(None), sl.max_height + 1)
Example #27
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])
Example #28
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])
Example #29
0
 def test_insert_r_third_1(self):
     sl = SkipList()
     sl.insert( 3 )
     sl.insert( 2 )
     sl.insert( 1 )
     self.assertEqual(sl.head.data, 1)
     self.assertEqual(sl.head.skiplist[0].data, 2)
     self.assertEqual(sl.head.skiplist[0].skiplist[0].data, 3)
     self.assertEqual(sl.get_at(0).skiplist.count(None), 0)
     self.assertEqual(sl.get_at(1).skiplist.count(None), 0)
     self.assertEqual(sl.get_at(2).skiplist.count(None), sl.max_height+1)
Example #30
0
    def test_relevel(self):
        sl = SkipList()
        sl.insert(1, 3)
        sl.insert(2, 3)
        sl.insert(3, 2)

        sl.relevel()

        self.assertEqual(sl.get_at(0).level, 3)
        self.assertEqual(sl.get_at(0).skiplist[0].data, 2)
        self.assertEqual(sl.get_at(0).skiplist[1].data, 3)
        self.assertEqual(sl.get_at(0).skiplist[2], None)
        self.assertEqual(sl.get_at(0).skiplist[3], None)
        self.assertEqual(sl.get_at(0).skipindex[0], 1)
        self.assertEqual(sl.get_at(0).skipindex[1], 2)

        self.assertEqual(sl.get_at(1).level, 0)
        self.assertEqual(sl.get_at(1).skiplist[0].data, 3)
        self.assertEqual(sl.get_at(1).skipindex[0], 1)

        self.assertEqual(sl.get_at(2).level, 1)
        self.assertEqual(sl.get_at(2).skiplist[0], None)
        self.assertEqual(sl.get_at(2).skiplist[1], None)
Example #31
0
    def test_relevel(self):
        sl = SkipList()
        sl.insert( 1, 3 )
        sl.insert( 2, 3 )
        sl.insert( 3, 2 )

        sl.relevel()

        self.assertEqual(sl.get_at(0).level, 3)
        self.assertEqual(sl.get_at(0).skiplist[0].data, 2)
        self.assertEqual(sl.get_at(0).skiplist[1].data, 3)
        self.assertEqual(sl.get_at(0).skiplist[2], None)
        self.assertEqual(sl.get_at(0).skiplist[3], None)
        self.assertEqual(sl.get_at(0).skipindex[0], 1)
        self.assertEqual(sl.get_at(0).skipindex[1], 2)

        self.assertEqual(sl.get_at(1).level, 0)
        self.assertEqual(sl.get_at(1).skiplist[0].data, 3)
        self.assertEqual(sl.get_at(1).skipindex[0], 1)

        self.assertEqual(sl.get_at(2).level, 1)
        self.assertEqual(sl.get_at(2).skiplist[0], None)
        self.assertEqual(sl.get_at(2).skiplist[1], None)
Example #32
0
    def test_insert_r_index_4(self):
        # middle insert
        sl = SkipList()
        sl.insert(1, sl.max_height)
        sl.insert(2, 0)
        sl.insert(4, 1)
        sl.insert(5, sl.max_height)
        sl.insert(3, 2)

        self.assertEqual(sl.get_at(3).skipindex[0], 1)
        self.assertEqual(sl.get_at(3).skipindex[1], 1)
        # *****
        self.assertEqual(sl.get_at(2).skipindex[0], 1)
        self.assertEqual(sl.get_at(2).skipindex[1], 1)
        self.assertEqual(sl.get_at(2).skipindex[2], 2)
        # *****
        self.assertEqual(sl.get_at(1).skipindex[0], 1)
        self.assertEqual(sl.get_at(0).skipindex[0], 1)
        self.assertEqual(sl.get_at(0).skipindex[1], 2)
        self.assertEqual(sl.get_at(0).skipindex[2], 2)
        self.assertEqual(sl.get_at(0).skipindex[sl.max_height], 4)
Example #33
0
    def test_insert_r_index_4(self):
        # middle insert
        sl = SkipList()
        sl.insert( 1, sl.max_height )
        sl.insert( 2, 0 )
        sl.insert( 4, 1 )
        sl.insert( 5, sl.max_height )
        sl.insert( 3, 2 )

        self.assertEqual(sl.get_at(3).skipindex[0], 1)
        self.assertEqual(sl.get_at(3).skipindex[1], 1)
        # *****
        self.assertEqual(sl.get_at(2).skipindex[0], 1)
        self.assertEqual(sl.get_at(2).skipindex[1], 1)
        self.assertEqual(sl.get_at(2).skipindex[2], 2)
        # *****
        self.assertEqual(sl.get_at(1).skipindex[0], 1)
        self.assertEqual(sl.get_at(0).skipindex[0], 1)
        self.assertEqual(sl.get_at(0).skipindex[1], 2)
        self.assertEqual(sl.get_at(0).skipindex[2], 2)
        self.assertEqual(sl.get_at(0).skipindex[sl.max_height], 4)
Example #34
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)
Example #35
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)
Example #36
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)
Example #37
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)
Example #38
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)
Example #39
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)
Example #40
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)
Example #41
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)
Example #42
0
 def test_insert_r_first(self):
     sl = SkipList()
     sl.insert( 1 )
     self.assertEqual(sl.head.data, 1)
     self.assertEqual(sl.head.skiplist.count(None), sl.max_height+1)
Example #43
0
 def test_str(self):
     sl = SkipList()
     sl.insert( 3 )
     sl.insert( 2 )
     sl.insert( 1 )
     self.assertEqual(str(sl), '[1,2,3]')
Example #44
0
 def test_insert_r_first(self):
     sl = SkipList()
     sl.insert(1)
     self.assertEqual(sl.head.data, 1)
     self.assertEqual(sl.head.skiplist.count(None), sl.max_height + 1)
Example #45
0
    def test_find_r_1(self):
        sl = SkipList()
        sl.insert(3, 0)
        sl.insert(1, 0)

        self.assertEqual(sl.find_r(1), 1)
Example #46
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
Example #47
0
def alphabet_list():
    skl = SkipList()
    [skl.insert(chr(65 + i), i) for i in range(26)]
    skl.print()
    return skl
Example #48
0
 def test_str(self):
     sl = SkipList()
     sl.insert(3)
     sl.insert(2)
     sl.insert(1)
     self.assertEqual(str(sl), '[1,2,3]')
Example #49
0
 def test_remove_r(self):
     sl = SkipList()
     sl.insert(1)
     sl.remove(1)
     sl.remove(1)
     self.assertIsNone(sl.head)
Example #50
0
 def test_remove_r(self):
     sl = SkipList()
     sl.insert( 1 )
     sl.remove( 1 )
     sl.remove( 1 )
     self.assertIsNone(sl.head)
Example #51
0
    def test_find_r_1(self):
        sl = SkipList()
        sl.insert( 3, 0 )
        sl.insert( 1, 0 )

        self.assertEqual(sl.find_r(1), 1)
class SkipListTest(unittest.TestCase):
    def setUp(self):
        '''
        Basic setup for the unittest module.
        '''
        self.skiplist = SkipList(4) #Generate a maxlevel 2 ordered map
        self.aNode = [None, None, None, None]

    def test_makeNode(self):
        '''
        Test if the _makeNode method can generate the right list [None, None, None, None]
        '''
        self.assertEqual(self.aNode, self.skiplist._makeNode(0, None, None))
        
    def test_makeNode_notEqual(self):
        '''
        Test if the list [None, None, None] generated by _makeNode method is not equal to list [None, None, None, None]
        '''
        self.assertNotEqual(self.aNode, self.skiplist._makeNode(-1, None, None))
    
    @patch('random.random')
    def test_randomLevel(self, mock_rand):
        '''
        Test when random mock result is 0, the return value by _randomLevel is 1
        '''
        mock_rand.return_value = 0
        self.assertEqual(1, self.skiplist._randomLevel())
        
    @patch('random.random')
    def test_randomLevel_notEqual(self, mock_rand):
        '''
        Test when random mock result is 0, the return value 1 by _randomLevel is not equal to 0
        '''
        mock_rand.return_value = 0
        self.assertNotEqual(0, self.skiplist._randomLevel())
        
    def test_findLess(self):
        '''
        Test when given the searchKey, the _findLess method returns the part of the array which contains the searched item
        '''
        self.skiplist.head = [1,[3,4],[5,6],[7,8,[None,0]]]
        result = self.skiplist._findLess(self.skiplist._update, 1)
        self.assertEqual([1, [3, 4], [5, 6], [7, 8, [None, 0]]], result)
       
    def test_items(self):
        '''
        Test when searchKey is not None (in this case is 1) and reverse is True,
        the method finds all the key value pairs in the ordered map
        '''
        test_array = []
        self.skiplist.head[3] = [1,2,3,[7,8,[None,9,10],[None,10,11]]]
        for item in self.skiplist.items(1, False):
            test_array.append(item)
        self.assertEqual([(1, 2), (7, 8)], test_array)
        
    def test_items_found3_is_nil(self):
        '''
        Test when the self._foundLess return the save object as self.skiplist.nil.
        The value of searchKey is 1 and reverse is False, but the last item of self.skiplist.head is self.skiplist.nil
        In Python, "is not" only return False when the two comparing items is referring to the same object.
        '''
        test_array = []
        self.skiplist.head[3] = self.skiplist.nil
        for item in self.skiplist.items(1, False):
            test_array.append(item)
        self.assertEqual([], test_array)
         
    def test_items_reverse_true(self):
        '''
        Test when not given a searchKey (None) and reverse is True,
        node should be [None, None, None] because self.tail is [None, None, None]
        the generator should not be called and returns nothing.
        '''
        test_array = []
        for item in self.skiplist.items(None, True):
            test_array.append(item)
        self.assertEqual([], test_array)
    
    @patch('skiplist.SkipList._randomLevel')    
    def test_insert_has_searchKey(self, mock_randomLevel):
        '''
        Test when the searchKey exists (in this case it is 1), 
        the insert method can overwrite the value None into 100 or not
        '''
        mock_randomLevel.return_value = 1
        self.skiplist.head[3][0] = 1
        self.skiplist.insert(1, 100)
        self.assertEqual([1, 100, None], self.skiplist.head[3])
        
    @patch('skiplist.SkipList._randomLevel')
    def test_insert_no_searchKey(self, mock_randomLevel):
        '''
        Test when the searchKey does not exist (in this case it is 0),
        the insert method inserts the new value pair at index 3
        '''
        mock_randomLevel.return_value = 0
        self.skiplist.insert(0,100)
        self.assertEqual(0, self.skiplist.head[3][0])
        self.assertEqual(100, self.skiplist.head[3][1])
        
    @patch('skiplist.SkipList._randomLevel')
    def test_insert_node3_not_nil(self, mock_randomLevel):
        '''
        Test when the searchKey does not exist (in this case it is 0),
        the insert method inserts the new value pair at index 3
        also defining new head array makes node[3] is not the same as self.skiplist.nil
        '''
        self.skiplist.head[3] = [1,[3,4],[5,6],[7,8,[None,0]]]
        mock_randomLevel.return_value = 0
        self.skiplist.insert(0,100)
        self.assertEqual(0, self.skiplist.head[3][0])
        self.assertEqual(100, self.skiplist.head[3][1])
        
    def test_delete(self):
        '''
        Test when given searchKey, the method can successfully delete the searchKey and its value or not
        '''
        self.skiplist.head[3] = [7,8,[9,0],[10,11,12]]
        self.assertTrue(self.skiplist.delete(7))
        
    def test_delete_add_level(self):
        '''
        Test when the ordered map contains another inner ordered map, the delete method can work properly or not
        '''
        self.skiplist.level = 1  
        self.skiplist.head[3] = [7,8,9,[10,11,12,[13,14,15]]]
        self.assertTrue(self.skiplist.delete(10))
        
    def test_delete_node_tail(self):
        '''
        Test when there is only one ordered map in the list (where the tail is pointing at),
        deleting the searchKey (10 in this case) will cause the self.skiplist.tail changes correspondingly.
        '''
        self.skiplist.head = [7,8,9,[10,11,12,[13,14,15]]]
        self.skiplist.tail = self.skiplist.head[3]
        self.assertTrue(self.skiplist.delete(10))
        
    def test_delete_no_searchKey(self):
        '''
        Test if the searchKey does not exist, the method should return None
        '''
        self.assertIsNone(self.skiplist.delete(0))
        
    def test_search(self):
        '''
        Test if there exists the searchKey (7 in this case), the method will return the value 8 associated with key 7
        '''
        self.skiplist.head[3] = [1,[3,4],[5,6],[7,8,[9,0],[10,11,12]]]
        self.assertEqual(8, self.skiplist.search(7))
         
    def test_search_no_searchKey(self):
        '''
        Test if the searchKey cannot match, the method should return None
        '''
        self.assertIsNone(self.skiplist.search(0))
Example #53
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