Example #1
0
    def test_init(self):
        hash_map = HashMap()
        self.assertEqual(len(hash_map._bucket_array), 10)

        hash_map = HashMap(('key', 'value'))
        self.assertEqual(len(hash_map._bucket_array), 10)

        hash_map = HashMap([
            (self.key_1, self.value_1),
            (self.key_2, self.value_2)
        ])
        self.assertEqual(len(hash_map._bucket_array), 10)
        self.assertRaises(TypeError, HashMap, 'string')
    def test_insert(self):
        d = HashMap()
        d.insert(lower_to_upper.items())
        self.assertEqual(len(d), len(lowercase))

        for ll, ul in zip(lowercase, uppercase):
            self.assertEqual(d[ll], ul)
Example #3
0
def main():
    m = HashMap()

    ops = [
        ('insert', (5, 6), (True, )),
        ('lookup', (5, ), (6, )),
        ('insert', (7, 3), (True, )),
        ('lookup', (7, ), (3, )),
        ('lookup', (5, ), (6, )),
        ('insert', (5, 2), (False, )),
        ('lookup', (5, ), (2, )),
        ('erase', (5, ), (True, )),
        ('lookup', (5, ), (None, )),
        ('erase', (3, ), (False, )),
    ]

    for op, input, expected_output in ops:
        if op == 'insert':
            output = (m.insert(*input), )
        elif op == 'lookup':
            output = (m.lookup(*input), )
        else:
            output = (m.erase(*input), )
        assert output == expected_output, 'Expected {} == {}'.format(
            output, expected_output)
        print('Succeed')
    def test_update(self):
        d = HashMap()
        d.update(lower_to_upper)
        self.assertEqual(len(d), len(lowercase))

        for ll, ul in lower_to_upper.items():
            self.assertEqual(d[ll], ul)
Example #5
0
 def test_rehashing(self):
     keys = [
         "first", "second", "third", "fourth", "fifth", "sixth", "seventh",
         "eighth", "ninth", "tenth"
     ]
     values = list(range(1, 11))
     hm = HashMap()
     self.assertEqual(hm.size(), 0)
     self.assertEqual(hm.capacity(), 8)
     for i in range(0, 5):
         hm.set(keys[i], values[i])
     self.assertEqual(hm.size(), 5)
     self.assertEqual(hm.capacity(), 8)
     output = hm.keys()
     self.assertEqual(len(output), 5)
     for key in output:
         self.assertIn(key, keys)
     for i in range(5, 10):
         hm.set(keys[i], values[i])
     self.assertEqual(hm.size(), 10)
     self.assertEqual(hm.capacity(), 16)
     output = hm.keys()
     for key in output:
         self.assertIn(key, keys)
     for key in keys:
         self.assertIn(key, output)
Example #6
0
def download(client, container, object, path):

    res = client.retrieve_object_hashmap(container, object)
    blocksize = int(res['block_size'])
    blockhash = res['block_hash']
    bytes = res['bytes']
    map = res['hashes']

    if os.path.exists(path):
        h = HashMap(blocksize, blockhash)
        h.load(open(path))
        hashes = [hexlify(x) for x in h]
    else:
        open(path, 'w').close()     # Create an empty file
        hashes = []

    with open(path, 'a+') as fp:
        if bytes != 0:
            for i, h in enumerate(map):
                if i < len(hashes) and h == hashes[i]:
                    continue
                start = i * blocksize
                end = '' if i == len(map) - 1 else ((i + 1) * blocksize) - 1
                data = client.retrieve_object(
                    container, object, range='bytes=%s-%s' % (start, end))
                if i != len(map) - 1:
                    data += (blocksize - len(data)) * '\x00'
                fp.seek(start)
                fp.write(data)
        fp.truncate(bytes)
Example #7
0
 def test_getitem_magic_method_raises_value_error_when_target_key_not_extant(
         self):
     hashmap = HashMap()
     hashmap['key'] = 'val'
     self.assertEqual(hashmap['key'], 'val')
     with self.assertRaises(ValueError):
         hashmap['not_key']
    def test_multiprocessing_update(self):
        d = HashMap(key_type=ctypes.c_int,
                    value_type=ctypes.c_int,
                    lock=mp.Lock())

        def update_1():
            d.update({1: 1, 11: 11, 111: 111})

        def update_2():
            d.update({2: 2, 22: 22, 222: 222})

        p1 = mp.Process(target=update_1)
        p2 = mp.Process(target=update_2)
        p1.start()
        p2.start()
        p1.join()
        p2.join()

        for n in [1, 11, 111]:
            self.assertTrue(n in d)

        for n in [2, 22, 222]:
            self.assertTrue(n in d)

        self.assertEqual(len(d), 6)
Example #9
0
 def test_can_set_and_get_basic_case(self):
     hashmap = HashMap()
     self.assertEqual(hashmap._number_of_elements, 0)
     hashmap.set('key', 'val')
     self.assertEqual(hashmap._number_of_elements, 1)
     self.assertEqual(hashmap.get('key'), 'val')
     self.assertIsNone(hashmap.get('otherval'))
Example #10
0
    def test_has(self):
        # New HashMap object
        HM = HashMap()

        # For Normal Inputs

        # Putting Value
        self.assertTrue(HM.put(1, "one"))
        self.assertTrue(HM.put(2, "two"))

        # Testing has()
        self.assertTrue(HM.has(1))
        self.assertTrue(HM.has(2))

        # For Inputs with colliding hash values
        self.assertTrue(HM.put(3, "three"))
        self.assertTrue(HM.put(25, "twenty-five"))
        self.assertTrue(HM.put(36, "thirty-six"))
        self.assertTrue(HM.put(47, "forty-seven"))
        self.assertTrue(HM.has(3))
        self.assertTrue(HM.has(25))
        self.assertTrue(HM.has(36))
        self.assertTrue(HM.has(47))
        self.assertTrue(HM.has(1))
        self.assertTrue(HM.has(2))
        self.assertFalse(HM.has(4))
Example #11
0
def main():
    """Runs the main program"""
    hash_map = HashMap()

    def count(key):
        """Function to return the value attached to the key given"""
        number = hash_map.get(key, 0)
        return number

    with open("AliceInWonderland.txt", 'r') as alice:
        for line in alice:
            words = clean_line(line)
            for i in words:
                hash_map.set(i, count(i) + 1)

    ranking = []
    position = 0
    while hash_map.lyst[position] is not None:
        ranking.append(hash_map.lyst[position])
        position += 1
    ranking.sort(key=lambda x: x[1], reverse=True)

    top_15 = []
    for i in range(15):
        top_15.append(ranking[i])

    print("The most common words are:")
    for i in top_15:
        if len(i[0]) >= 3:
            print(i[0], '\t', '\t', '\t', '\t', i[1])
        else:
            print(i[0], '\t', '\t', '\t', '\t', '\t', i[1])
def test_rehashing():
    keys = [(r, r) for r in (range(10))]
    values = list(range(1, 11))
    hm = HashMap()
    for k, v in zip(keys, values):
        hm.set(k, v)
    assert hm.size() == 10
    assert hm.capacity() == 13
Example #13
0
    def test_init(self):
        hash_map = HashMap()
        assert hash_map.capacity == 17
        assert len(hash_map.array) == 17
        assert len(hash_map) == 0

        for item in hash_map.array:
            assert item is None
Example #14
0
 def test_key_present(self):
     hm = HashMap()
     self.assertEqual(hm.get("asdf"), None)
     self.assertEqual(hm.get("asdf", 0), 0)
     hm.set("qwerty", 12345)
     self.assertEqual(hm.get("qwerty"), 12345)
     self.assertEqual(hm.size(), 1)
     self.assertEqual(hm.capacity(), 8)
Example #15
0
    def test_hash_map_updates_with_other_hashmap(self):

        self.hashmap["FirstKey"] = "FirstValue"
        other_hashmap = HashMap()
        other_hashmap["FirstKey"] = "OtherFirstValue"
        other_hashmap["SeondKey"] = "SecondValue"
        self.hashmap.update(other_hashmap)
        self.assertEqual("OtherFirstValue", self.hashmap["FirstKey"])
Example #16
0
def main():
    '''Driver function for hashmap.py.'''
    hm = HashMap()
    with open('AliceInWonderland.txt', 'r') as file:
        for line in file:
            for word in clean_line(line):
                hm.set(word, 1)
    print('The most common words are:')
    get_most_used_words(hm)
def test_clear():
    hm = HashMap()
    keys = [(r, r) for r in (range(10))]
    values = list(range(1, 11))
    for k, v in zip(keys, values):
        hm.set(k, v)
    hm.clear()
    assert hm.capacity() == 7
    assert hm.size() == 0
Example #18
0
    def test_memory_error(self):
        d = HashMap(capacity=4)
        d[b'a'] = b"a"
        d[b'b'] = b"tester"
        d[b'c'] = b"something new"
        d[b'd'] = b"DEE"

        with self.assertRaises(MemoryError):
            d[b'oh no'] = b'too many!'
Example #19
0
 def __init__(self, hash_function, initial_size=10, max_load_factor=0.7):
     """
     This function is used to initialize the hashtable based on the hashfunction provided with a certain load factor
     which is used to increase the size of the hash table
     :param hash_function: a particular hash value calculating function
     :param initial_size: initial size of the table
     :param max_load_factor: loac factor for a table
     """
     self.hash_table = HashMap(initsz=initial_size, hash_func=hash_function, maxload=max_load_factor)
def test_keys():
    hm = HashMap()
    keys = [(r, r) for r in (range(10))]
    values = list(range(1, 11))
    for k, v in zip(keys, values):
        hm.set(k, v)
    keys2 = hm.keys()
    keys2.sort()
    assert keys == keys2
Example #21
0
 def test_can_set_and_get_many_values(self):
     hashmap = HashMap()
     for number in range(0, 20):
         key, val = str(number), number
         hashmap.set(key, val)
     self.assertEqual(hashmap._number_of_elements, 20)
     for number in range(0, 20):
         key, expected_val = str(number), number
         self.assertEqual(hashmap.get(key), expected_val)
Example #22
0
def build_data(dat):
    for restaurant in dat:
        resto = resto_styles.retrieve(restaurant[0])
        resto.insert_beginning(HashMap(4))
        node = resto.get_head_node().get_value()
        node.assign('Name', restaurant[1])
        node.assign('Price', restaurant[2])
        node.assign('Rating', restaurant[3])
        node.assign('Address', restaurant[4])
    return resto_styles
Example #23
0
    def test_clear(self):
        d = HashMap(upper_to_i.items(),
                    key_type=ctypes.c_char_p,
                    value_type=ctypes.c_int)

        d.clear()
        self.assertEqual(len(d), 0)

        d.update(upper_to_i)
        self.assertEqual(len(d), len(upper_to_i))
Example #24
0
    def test_setting(self):
        d = HashMap(capacity=4)
        d[b'a'] = b'A'
        d[b'b'] = b'B'
        self.assertEqual(d[b'a'], b'A', "Failed to insert A")
        self.assertEqual(d[b'b'], b'B', "Failed to insert B")

        d[b'a'] = b'H'
        self.assertEqual(d[b'a'], b'H', "Failed to change a")
        self.assertEqual(d[b'b'], b'B', "Changing a changed b")
Example #25
0
    def test_contains(self):
        d = HashMap(capacity=4)
        d[b'a'] = b'hi'
        d[b'b'] = b'Bee'

        self.assertTrue(b'a' in d)
        self.assertTrue(b'b' in d)
        self.assertTrue(b'nope' not in d)

        d = HashMap(key_type=ctypes.c_char_p, value_type=ctypes.c_int)
        for i, letter in enumerate(uppercase):
            d[letter] = i

        for i, letter in enumerate(uppercase):
            self.assertTrue(letter in d, "Didn't contain letter: %s" % letter)

        for i, letter in enumerate(lowercase):
            self.assertTrue(letter not in d,
                            "Somehow contains letter: %s" % letter)
Example #26
0
 def test_can_set_and_get_multiple_values(self):
     hashmap = HashMap()
     self.assertEqual(hashmap._number_of_elements, 0)
     hashmap.set('key', 'val')
     self.assertEqual(hashmap._number_of_elements, 1)
     self.assertEqual(hashmap.get('key'), 'val')
     hashmap.set('otherkey', 'otherval')
     self.assertEqual(hashmap._number_of_elements, 2)
     self.assertEqual(hashmap.get('otherkey'), 'otherval')
     self.assertEqual(hashmap.get('key'), 'val')
     self.assertIsNone(hashmap.get('blah'))
    def setUpClass(cls):
        # class variable run before the test suite.
        # We'll be making changes to this with each
        # test.

        cls.hashmap = HashMap()
        cls.key_list = [
            ''.join(random.choice(string.lowercase) for i in xrange(100))
            for j in xrange(26)
        ]  # 26 random keys
        cls.value_list = random.sample(range(1000), 26)  # 26 random values
def test_remove():
    hm = HashMap()
    keys = [(r, r) for r in (range(10))]
    values = list(range(1, 11))
    for k, v in zip(keys, values):
        hm.set(k, v)
    hm.remove((3, 3))
    print(hm)
    with pytest.raises(KeyError):
        hm.get((3, 3))
    assert hm.get((5, 5)) == 6
Example #29
0
def performance_test():
    import time
    import dbm
    from itertools import permutations

    def test_dict(d):
        for p in [''.join(p) for p in permutations('1234567')]:
            d[p.encode()] = p.encode()

    def test_lookup(d):
        for p in [''.join(p) for p in permutations('1234567')]:
            if p.encode() in d:
                pass

    d = {}
    t = time.time()
    test_dict(d)
    print("dict insert: %s" % (time.time() - t))

    t = time.time()
    test_lookup(d)
    print("dict lookup: %s" % (time.time() - t))

    d = HashMap(key_type=ctypes.c_char_p,
                value_type=ctypes.c_char_p,
                capacity=1000000)
    t = time.time()
    test_dict(d)
    print("Shared-Memory HashMap insert: %s" % (time.time() - t))

    t = time.time()
    test_lookup(d)
    print("Shared-Memory HashMap lookup: %s" % (time.time() - t))

    d = mp.Manager().dict()
    t = time.time()
    test_dict(d)
    print("Manager().dict insert: %s" % (time.time() - t))

    t = time.time()
    test_lookup(d)
    print("Manager().dict lookup: %s" % (time.time() - t))

    db = dbm.open('cache', 'c')
    t = time.time()
    test_dict(db)
    print("dbm insert time: %s" % (time.time() - t))

    t = time.time()
    test_lookup(db)
    print("dbm lookup time: %s" % (time.time() - t))

    db.close()
Example #30
0
    def test_add_collision(self):
        key1 = "hysh"
        key2 = "vwtuyy"

        hash_map = HashMap()
        hash_map[key1] = True
        hash_map[key2] = False

        assert len(hash_map) == 2

        assert hash_map[key1] == True
        assert hash_map[key2] == False