コード例 #1
0
    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)
コード例 #2
0
ファイル: transfer.py プロジェクト: antonis-m/synnefo
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)
コード例 #3
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)
コード例 #4
0
    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)
コード例 #5
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])
コード例 #6
0
ファイル: hashmap_test.py プロジェクト: breezy1812/MyCodes
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')
コード例 #7
0
ファイル: main.py プロジェクト: tannershimanek/hw-cs2420
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)
コード例 #8
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)
コード例 #9
0
ファイル: tests.py プロジェクト: josefdaly/hashmap
 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)
コード例 #10
0
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
コード例 #11
0
ファイル: tests.py プロジェクト: josefdaly/hashmap
 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'))
コード例 #12
0
ファイル: test_hashmap.py プロジェクト: mikehaus/CS2420
 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)
コード例 #13
0
def test_get_set():
    hm = HashMap()
    with pytest.raises(KeyError):
        hm.get((0, 0))

    keys = [(r, r) for r in (range(10))]
    values = list(range(1, 11))
    for k, v in zip(keys, values):
        hm.set(k, v)
    assert hm.get((5, 5)) == 6
    assert hm.get((9, 9)) == 10
    hm.set((2, 2), 409)
    assert hm.get((2, 2)) == 409
コード例 #14
0
ファイル: test_hashmap.py プロジェクト: fyaretskiy/hashmap
    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')
コード例 #15
0
    def __init__(self, catSeq):

        # Histogram.frequenceCounts , is actually a hash table
        self._frequenceCounts = HashMap()

        for L_category in catSeq:

            # so it has a addkey() method, here "category" is single character-based key, "0" is value
            # the letter-based key with uppercase will be hash to integer-based key
            # and a empty slot will be fill with new entry, that is,
            # key-value(0) pair
            # the value(0) represent the initialized counter=0 for each category
            self._frequenceCounts.addkey(L_category, 0)
コード例 #16
0
class TestHashMapFunctions(unittest.TestCase):

    def setUp(self):
        self._ref = {}
        self._map = HashMap()

    def add(self, key, value):
        self._ref[key] = value
        self._map[key] = value

    def delete(self, key, value):
        del self._ref[key]
        del self._map[key]

    def test_add(self):
        for i in xrange(100):
            self.add("key_"+str(i), "value_"+str(i))
        self.assert_ref_equals_map()

    def test_delete(self):
        for i in xrange(100):
            self.add("key_"+str(i), "value_"+str(i))
        for i in xrange(0, 100, 2):
            self.delete("key_"+str(i), "value_"+str(i))
        self.assert_ref_equals_map()

    def test_get(self):
        for i in xrange(100):
            self.add("key_"+str(i), "value_"+str(i))
        for i in xrange(100):
            self.assertEqual(self._ref["key_"+str(i)], self._map["key_"+str(i)])

    def test_random(self):
        import random
        for i in xrange(10000):
            r = random.randint(0, 2)
            key = "key_"+str(random.randint(0, 9))
            value = "value_"+str(random.randint(0, 9))
            if r == 0:
                self.add(key, value)
            elif r == 1 and self._ref.get(key, None) is not None:
                self.delete(key, value)
            else:
                self.assertEqual(self._ref.get(key, None), self._map.get(key, None))



    def assert_ref_equals_map(self):
        self.assertItemsEqual(self._ref.keys(), self._map.keys())
        self.assertItemsEqual(self._ref.values(), self._map.values())
コード例 #17
0
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
コード例 #18
0
ファイル: tests.py プロジェクト: josefdaly/hashmap
 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']
コード例 #19
0
    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)
コード例 #20
0
ファイル: function.py プロジェクト: firegladtang/LanhuSaas-1
def get_scene_find_xml(xml, scene_id):
    """
    解析XML,判断是否有重复的监控项,如果正常保存场景与监控项的关联关系
    :param xml:
    :param scene_id:
    :return:
    """
    root_a = ElementTree.XML(xml)
    parent = root_a.find("root", "mxGraphModel")
    list = parent._children
    # 判断绑定的监控项是否有重复
    items_map = HashMap()
    # 批量入库数组
    add_scene_item_list = []
    for dto in list:
        if dto.tag == "object":
            dto_item_id = str(dto.attrib.get("item_id"))
            # 关联的监控项id非数字直接进入下一个循环
            if dto_item_id.isdigit() is False:
                continue
            int_item_id = int(dto_item_id)
            item_name = str(dto.attrib.get("label"))
            # 添加了重复的监控项,直接返回提示
            if items_map.get(int_item_id) is not None:
                return item_name
            items_map.add(int_item_id, item_name)
            temp_dto = Scene_monitor.objects.filter(scene_id=scene_id,
                                                    item_id=int_item_id)
            if temp_dto.count() == 0:
                # 只有等于零时才新增
                scene_dto = Scene_monitor()
                scene_dto.item_id = int_item_id
                scene_dto.scene_id = scene_id
                scene_dto.x = 0
                scene_dto.y = 0
                scene_dto.scale = 0.0
                scene_dto.score = 0
                scene_dto.order = 0
                add_scene_item_list.append(scene_dto)
                # scene_dto.save()
                print "场景 " + str(scene_id) + " " + dto_item_id + "保存成功"
            else:
                print "场景 " + str(scene_id) + " " + dto_item_id + "已存在,不处理"
    # 如果数组不为空,执行批量入库
    if add_scene_item_list.__len__() > 0:
        Scene_monitor.objects.bulk_create(add_scene_item_list)
    return "true"
コード例 #21
0
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
コード例 #22
0
    def test_combo_add_remove(self):
        hash_map = HashMap()
        hash_map["abc"] = True
        hash_map["moo"] = False
        hash_map["goo"] = True
        hash_map["foo"] = False

        assert len(hash_map) == 4

        hash_map.remove("goo")
        hash_map.remove("abc")

        assert len(hash_map) == 2

        hash_map["goo"] = False

        assert len(hash_map) == 3
コード例 #23
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"])
コード例 #24
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
コード例 #25
0
class Histogram(object):
	# Create a histogram containing the given categories.
	def __init__(self, catSeq):
		self._freqCounts = HashMap()
		for cat in catSeq:
			self._freqCounts.add(cat, 0)
			
	# Return the frequency count for the given category.
	def getCount(self, category):
		assert category in self._freqCounts, 'Invalid histogram category.'
		return self._freqCounts.valueOf(category)
		
	# Increment the counter for the given category.
	def incCount(self, category):
		assert category in self._freqCounts, 'Invalid histogram category.'
		value = self._freqCounts.valueOf(category)
		self._freqCounts.add(category, value + 1)
		
	# Return the sum of the frequency counts.
	def totalCount(self):
		total = 0
		for cat in self._freqCounts:
			total += self._freqCounts.valueOf(cat)
		return total
		
	# Return an iterator for traversing the categories.
	def __iter__(self):
		return iter(self._freqCounts)
コード例 #26
0
ファイル: maphist.py プロジェクト: storypku/tlpi
class Histogram:
    """ Implementation of the Histogram ADT using a Hash Map """

    def __init__(self, catSeq):
        """ Creates a histogram containing the given categories """

        self._freqCounts = HashMap()
        for cat in catSeq:
            self._freqCounts.add(cat, 0)

    def getCount(self, category):
        """ Returns the frequency count for the given category. """

        assert category in self._freqCounts, "Invalid histogram category."
        return self._freqCounts.valueOf(category)

    def incCount(self, category):
        """ Increments the counter for the given category """

        assert category in self._freqCounts, "Invalid histogram category."
        value = self._freqCounts.valueOf(category)
        self._freqCounts.add(category, value + 1)

    def totalCount(self):
        """ Returns the sum of the frequency counts. """

        total = 0
        for cat in self._freqCounts:
            total += self._freqCounts.valueOf(cat)
        return total

    def __iter__(self):
        """ Returns an iterator for traversing the categories """

        return iter(self._freqCounts)
コード例 #27
0
ファイル: test_hashmap.py プロジェクト: bharatnc/hash_map
    def test_sizeof(self):
        HM = HashMap()

        # Size for Normal Inputs
        self.assertTrue(HM.put(1, "one"))
        self.assertTrue(HM.put(2, "two"))
        self.assertEquals(HM.sizeof(), 2)

        # Size including colliding hashes
        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.assertEquals(HM.sizeof(), 6)
コード例 #28
0
ファイル: maphist.py プロジェクト: yprodev/python_ads
class Histogram:
	# Creates a histogram containing the given categories
	def __init__(self, catSeq):
		self._freqCounts = HashMap()
		for cat in catSeq:
			self._freqCounts.add(cat, 0)

	# Returns the frequency count for the given category
	def getCount(self, category):
		assert category in self._freqCounts, "Invalid histogram category"

		return self._freqCounts.valueOf(category)

	# Increments the counter for the given category
	def incCount(self, category):
		assert category in self._freqCounts, "Invalid histogram category"

		value = self._freqCounts.valueOf(category)
		self._freqCounts.add(category, value + 1)

	# Returns the sum of the frequency counts
	def totalCount(self):
		total = 0
		for cat in self._freqCounts:
			total += self._freqCounts.valueOf(cat)

		return total

	# Returns an iterator for traversing the categories
	def __iter__(self):
		return iter(self._freqCounts)
コード例 #29
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!'
コード例 #30
0
def main():
    """ Main driver to run program. """
    hashmap = HashMap()

    text_file = open("AliceInWonderland.txt", "r", encoding="utf8")

    for line in text_file:
        temp = clean_line(line)

        for word in temp:
            if hashmap.contains(word) is True:
                tempvalue = hashmap.get(word)
                hashmap.set(word, (tempvalue + 1))
            else:
                hashmap.set(word, 1)

    hashmap.print_top()
    text_file.close()
コード例 #31
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
コード例 #32
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")
コード例 #33
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)
コード例 #34
0
ファイル: transfer.py プロジェクト: antonis-m/synnefo
def upload(client, path, container, prefix, name=None, mimetype=None):

    meta = client.retrieve_container_metadata(container)
    blocksize = int(meta['x-container-block-size'])
    blockhash = meta['x-container-block-hash']

    size = os.path.getsize(path)
    hashes = HashMap(blocksize, blockhash)
    hashes.load(open(path))
    map = {'bytes': size, 'hashes': [hexlify(x) for x in hashes]}

    objectname = name if name else os.path.split(path)[-1]
    object = prefix + objectname
    kwargs = {'mimetype': mimetype} if mimetype else {}
    v = None
    try:
        v = client.create_object_by_hashmap(container, object, map, **kwargs)
    except Fault, fault:
        if fault.status != 409:
            raise
コード例 #35
0
ファイル: hashmap_test.py プロジェクト: Time1ess/MyCodes
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')
コード例 #36
0
ファイル: physics.py プロジェクト: travcunn/pyBrickMiner
class Environment(object):
    def __init__(self):
        self.__objects = [] # World objects
        self.__hashmap = HashMap()

    def addObject(self, object):
        """
        Add an object to the environment
        """
        object.setEnvironment(self)
        self.__objects.append(object)

    def removeObject(self, object):
        """
        Remove an object from the environment
        """
        self.__objects.remove(object)

    def step(self, time):
        """
        Step forward with controllers attached to each object
        """
        for object in self.__objects:
            object.processControllers(time)

        # After processing the controllers, check for collisions
        self.checkCollisions()

    def inputStep(self, mouse_x, mouse_y):
        """
        Pass inputs to the objects
        """
        for object in self.__objects:
            object.mouseEvent(mouse_x, mouse_y)
            #TODO: Add a keyboard event here

    def addBlock(self, x, y):
        self.__hashmap.insert((x, y))

    def clearMap(self):
        self.__hashmap.clear()

    def checkCollisions(self):
        position = self.__objects[0].getPosition()
        print "Character position: ", position.x, position.y
        print "Hash output: ", self.__hashmap.get((position.x, position.y))
        if self.__hashmap.get((position.x, position.y)):
            print "collision!"
            self.__objects[0].gravity.stop()
            self.__objects[0].gravity.reset()

            self.__objects[0].jump.stop()
        else:
            self.__objects[0].gravity.start()
コード例 #37
0
ファイル: test_hashmap.py プロジェクト: mosdragon/PythonDS
	def test_put(self):
		hmap = HashMap()
		keys = [(x + 1) for x in range(HashMap.CAPACITY_DEFAULT * 2)]
		vals = [key ** 3 for key in keys]

		threshold = HashMap.THRESHOLD
		size = 0

		# Stop just before the hashmap needs to be resized
		while float(size + 1) / float(hmap.capacity) < threshold:
			hmap.put(keys[size], vals[size])
			size += 1


		self.assertEquals(hmap.size, size)
		self.assertEquals(hmap.capacity, HashMap.CAPACITY_DEFAULT)
		self.assertEquals(len(hmap.mapping), HashMap.CAPACITY_DEFAULT)

		old_capacity = hmap.capacity
		# Will first resize itself
		# Then it will stop before the next resize
		while float(size + 1) / float(old_capacity * 2) < threshold:
			hmap.put(keys[size], vals[size])
			size += 1

		self.assertEquals(hmap.size, size)
		self.assertEquals(hmap.capacity, HashMap.CAPACITY_DEFAULT * 2)
		self.assertEquals(len(hmap.mapping), HashMap.CAPACITY_DEFAULT * 2)

		keys_size = len(keys)

		# Finish adding everything
		while size < keys_size:
			hmap.put(keys[size], vals[size])
			size += 1

		self.assertEquals(hmap.size, size)
		self.assertEquals(hmap.capacity, HashMap.CAPACITY_DEFAULT * 4)
		self.assertEquals(len(hmap.mapping), HashMap.CAPACITY_DEFAULT * 4)
コード例 #38
0
class Histogram:
    def __init__( self, catSeq ):
        self._freqCounts = HashMap()
        for cat in catSeq:
            self._freqCounts.add( cat, 0 )

    def getCount( self, category ):
        assert category in self._freqCounts, "Invalid histogram category."
        return self._freqCounts.valueOf( category )

    def incCount( self, category ):
        assert category in self._freqCounts, "Invalid histogram category."
        value = self._freqCounts.valueOf( category )
        self._freqCounts.add( category, value + 1 )

    def totalCount( self ):
        total = 0
        for cat in self._freqCounts:
            total += self._freqCounts.valueOf( cat )
        return total

    def __iter__( self ):
        return iter( self._freqCounts )
コード例 #39
0
ファイル: test_hashmap.py プロジェクト: mosdragon/PythonDS
	def test_init(self):
		hmap = HashMap()
		self.assertEquals(hmap.capacity, HashMap.CAPACITY_DEFAULT)
		
		self.assertEquals(hmap.size, 0)
		self.assertEquals(hmap.getsize(), 0)

		self.assertTrue(hmap.isempty())
		self.assertEquals(hmap.mapping, [None for _ in range(HashMap.CAPACITY_DEFAULT)])



		start_capacity = random.randint(0, 100)
		hmap = HashMap(start_capacity)
		self.assertEquals(hmap.capacity, start_capacity)
		
		self.assertEquals(hmap.getsize(), 0)
		self.assertEquals(hmap.size, 0)

		self.assertTrue(hmap.isempty())
		self.assertEquals(hmap.mapping, [None for _ in range(start_capacity)])
コード例 #40
0
ファイル: maphist.py プロジェクト: storypku/tlpi
    def __init__(self, catSeq):
        """ Creates a histogram containing the given categories """

        self._freqCounts = HashMap()
        for cat in catSeq:
            self._freqCounts.add(cat, 0)
コード例 #41
0
ファイル: testhash.py プロジェクト: alexwade13/AlexsGit
from hashmap import HashMap
import random

"""Test program for hashmap implementation.
First run some small tests to make sure basic operations in hashmap
work correctly. Then run a large test to examine the number of collisions
when different hash and collision resolution methods are used.
"""

# Create a hashmap
myMap = HashMap()

# Keys and values for testing
keys = [ 6, 13, 100, 29, 12, 5, 15, 18, 101, 56 ]
values = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

# Test insertion
for i in range( len( keys ) ):
    print( 'inserting ', keys[ i] , '/', values[ i]  )
    myMap.add( keys[ i ], values[ i ] )    # (key, value) pair

# Test the iterator
print( 'original map : ' )
for v in myMap:
    print( '[', v.key, '/', v.value, ']', end = ' ' )
print()

# Test search
print( 'value of key 6 should be 0, it is ', myMap.valueOf( 6 ) )
print( 'value of key 56 should be 9, it is ', myMap.valueOf( 56 ) )
print( 'value of key 12 should be 4, it is ', myMap.valueOf( 12 ) )
コード例 #42
0
 def setUp(self):
     self._ref = {}
     self._map = HashMap()
コード例 #43
0
 def __init__( self, catSeq ):
     self._freqCounts = HashMap()
     for cat in catSeq:
         self._freqCounts.add( cat, 0 )
コード例 #44
0
ファイル: instance.py プロジェクト: erkanay/data-structures
#!/usr/bin/python
# -*- coding: utf-8 -*-

from hashmap import HashMap

h = HashMap(4)  #  4 cells

h.add("erkan", "444-44-44")
h.add("maxime", "333-33-33")
h.add("bob", "555-55-55")
h.add("mike", "666-66-66")
h.add("aude", "777-77-77")
h.add("kosta", "888-88-88")

h.get("mike")
h.delete("bob")

h.show()
コード例 #45
0
ファイル: physics.py プロジェクト: travcunn/pyBrickMiner
 def __init__(self):
     self.__objects = [] # World objects
     self.__hashmap = HashMap()