Ejemplo n.º 1
0
    def testCreate(self):
        ht = hashtable.HashTable(10)
        self.assertEqual(ht.capacity, 10)
        self.assertEqual(ht.size, 0)

        ht = hashtable.HashTable()
        self.assertEqual(ht.capacity, 1000)
Ejemplo n.º 2
0
 def test_initialization_with_invalid_size(self):
     with self.assertRaisesRegexp(
             TypeError, "size parameter must be a positive integer."):
         h = hashtable.HashTable(size=-1)
     with self.assertRaisesRegexp(
             TypeError, "size parameter must be a positive integer."):
         h = hashtable.HashTable(size=0)
Ejemplo n.º 3
0
 def test_initialization_with_invalid_max_load(self):
     with self.assertRaisesRegexp(
             TypeError,
             "max_load parameter must be a float between 0.0 and 1.0."):
         h = hashtable.HashTable(max_load=2)
     with self.assertRaisesRegexp(
             TypeError,
             "max_load parameter must be a float between 0.0 and 1.0."):
         h = hashtable.HashTable(max_load=-0.5)
Ejemplo n.º 4
0
    def test_constructor(self):
        hashmap = hashtable.HashTable(
        )  # hashmap constructors with some default fixed bin size > 1
        self.assertGreater(len(hashmap.bins), 1)

        hashmap = hashtable.HashTable(10)
        self.assertEqual(len(hashmap.bins), 10)

        hashmap = hashtable.HashTable(100)
        self.assertEqual(len(hashmap.bins), 100)
Ejemplo n.º 5
0
    def testSetAndGet(self):
        # Basic set and get
        ht = hashtable.HashTable(10)
        ht.set('a', 1)
        self.assertEqual(ht.get('a'), 1)
        self.assertEqual(ht.size, 1)

        # Check update functionality
        ht.set('a', 2)
        self.assertEqual(ht.get('a'), 2)
        self.assertEqual(ht.size, 1)

        # Make sure we can add a 2nd element
        ht.set('b', 10)
        self.assertEqual(ht.get('b'), 10)
        self.assertEqual(ht.get('a'), 2)
        self.assertEqual(ht.size, 2)

        # Assert ht.set returns itself (for fluent calls)
        self.assertEqual(ht.set('c', 5), ht)

        # Test fluent set functionality
        ht.set('d', 100).set('e', 200).set('f', 300)
        self.assertEqual(ht.get('d'), 100)
        self.assertEqual(ht.get('e'), 200)
        self.assertEqual(ht.get('f'), 300)
Ejemplo n.º 6
0
def analyseCountFunctionOfHashTable(iterable, word):
    _sum = 0
    hashtableHistogram = hashtable.HashTable()
    for i in range(100):
        _sum += hashtableHistogram.get(word)
    avg = _sum / 100
    return avg
Ejemplo n.º 7
0
 def test_set_get_delete_sequence(self):
     hashmap = hashtable.HashTable()
     hashmap.set('a', 1)
     hashmap.set('b', 2)
     self.assertEqual(hashmap.get('a'), 1)
     self.assertEqual(hashmap.delete('a'), 1)
     self.assertEqual(hashmap.delete('a'), None)
Ejemplo n.º 8
0
 def __init__(self, manifest_id):
     self.manifest_id = manifest_id
     self.packages = list()
     self.truck = 0
     self.driver = 0
     self.is_full = False
     self.route = list()
     self.packages_by_location = hashtable.HashTable()
Ejemplo n.º 9
0
    def test_valid_initialization(self):
        h = hashtable.HashTable(hash_func=my_hash)
        self.assertRegexpMatches(str(h.hash_func), r"<function my_hash at .*>")
        self.assertEqual(h.size, 4)  # defaults
        self.assertEqual(h.max_load, 0.5)  # defaults

        h = hashtable.HashTable(size=8)
        self.assertRegexpMatches(str(h.hash_func),
                                 r"<built-in function hash>")  # defaults
        self.assertEqual(h.size, 8)
        self.assertEqual(h.max_load, 0.5)  # defaults

        h = hashtable.HashTable(max_load=0.25)
        self.assertRegexpMatches(str(h.hash_func),
                                 r"<built-in function hash>")  # defaults
        self.assertEqual(h.size, 4)  # defaults
        self.assertEqual(h.max_load, 0.25)
Ejemplo n.º 10
0
    def testRemove(self):
        ht = hashtable.HashTable(10)
        self.assertRaises(KeyError, ht.remove, 'a')

        ht.set('a', 1)
        removed_item = ht.remove('a')
        self.assertEqual(removed_item, 1)
        self.assertEqual(ht.size, 0)
Ejemplo n.º 11
0
def analyseGenerationOfHashTable(iterable):
    start_time = time.time()
    _sum = 0
    hashtableHistogram = hashtable.HashTable()
    for i in range(100):
        for word in iterable:
            histogram = hashtableHistogram.set(word)
            _sum += time.time() - start_time
    avg = _sum / 100
    return avg
Ejemplo n.º 12
0
def test_hashtable1():
    """

    :return:
    """
    h = hashtable.HashTable()
    h.set(1, 1)
    h.set(11, 2)
    h.set(2, 3)
    assert h.get(1) == 1
Ejemplo n.º 13
0
    def test_delete(self):
        hashmap = hashtable.HashTable()
        hashmap.set('a', 1)
        hashmap.set('b', 2)
        hashmap.set('c', 3)

        self.assertEqual(hashmap.delete('c'), 3)
        self.assertEqual(hashmap.get('a'), 1)
        self.assertEqual(hashmap.get('b'), 2)
        self.assertEqual(hashmap.get('z'), None)
Ejemplo n.º 14
0
    def test_collision(self):
        # A hash function which returns 0 if the size of the string is even,
        # 1 otherwise
        hash_function = lambda x: len(x) % 2
        hs = hashtable.HashTable(2,
                                 TYPE_SEPARATE_CHAINING,
                                 hash_function=hash_function)
        hs.put("Alexis", 3)
        hs.put('Jerome', 4)
        hs.put('Ast', 5)

        self.assertEquals(5, hs.get('Ast'))
Ejemplo n.º 15
0
    def testPythonDictInterface(self):
        ht = hashtable.HashTable(10)

        ht['a'] = 10
        self.assertEqual(ht.get('a'), 10)

        ht['a'] = 20
        self.assertEqual(ht['a'], 20)

        self.assertIn('a', ht.keys())

        del ht['a']
        self.assertRaises(KeyError, ht.get, 'a')
Ejemplo n.º 16
0
    def test_set(self):
        hashmap = hashtable.HashTable()
        hashmap.set('a', 1)
        self.assertEqual(hashmap.size, 1)
        hashmap.set('b', 2)
        self.assertEqual(hashmap.size, 2)
        hashmap.set('c', 'carrot')
        self.assertEqual(hashmap.size, 3)

        with self.assertRaises(TypeError):
            hashmap.set(4, "keys should be strings")
        self.assertEqual(hashmap.size, 3)

        with self.assertRaises(ValueError):
            self.alphabet_map.set('aa', "fixed sized map is full")
            self.assertEqual(hashmap.size, 26)
Ejemplo n.º 17
0
def main():
    # a = Student.Student("Henry", "Jones", "123-45-7899", "*****@*****.**", "40")
    # b = Student.Student("Anakin", "Skywalker", "357-89-7642", "*****@*****.**", "30")
    # c = Student.Student("Anakin", "Skywalker", "357-89-7642", "*****@*****.**", "30")
    #
    # database = bst.BST()
    # database.Insert(a)
    # database.Insert(b)
    # database.Insert(c)

    database = hashtable.HashTable(30000)
    print "Insert Duration: " + str(
        timeSomething(InsertAllStudents, database, "InsertNames.txt"))
    print "Traverse Duration: " + str(
        timeSomething(TraverseAllStudents, database))
    print "Delete Duration: " + str(
        timeSomething(DeleteStudents, database, "DeleteNames.txt"))
    print "Retrieve Duration: " + str(
        timeSomething(RetrieveNames, database, "RetrieveNames.txt"))
Ejemplo n.º 18
0
    def test_load(self):
        self.assertAlmostEqual(self.alphabet_map.load(), 1.0)
        with self.assertRaises(ValueError):
            self.alphabet_map.set('aa', "i'm too full")
        self.alphabet_map.delete('a')
        self.assertTrue(
            math.isclose(self.alphabet_map.load(), 25 / 26, rel_tol=1e-6))

        hashmap = hashtable.HashTable(10)
        hashmap.set('thing1', 1)
        self.assertAlmostEqual(hashmap.load(), 1 / 10)
        hashmap.set('thing2', 2)
        self.assertAlmostEqual(hashmap.load(), 2 / 10)
        hashmap.set('thing3', 3)
        self.assertAlmostEqual(hashmap.load(), 3 / 10)
        hashmap.delete('thing1')
        self.assertAlmostEqual(hashmap.load(), 2 / 10)
        hashmap.delete('thing2')
        self.assertAlmostEqual(hashmap.load(), 1 / 10)
        hashmap.delete('thing3')
        self.assertEqual(hashmap.load(), 0)
Ejemplo n.º 19
0
 def __init__(self, node):
     self.address = node
     self.distances = hashtable.HashTable()
     self.neighbors_by_location = hashtable.HashTable()
     self.neighbors = []
Ejemplo n.º 20
0
 def setUp(self):
     self.alphabet_map = hashtable.HashTable(26)
     for i in range(1, 26 + 1):
         self.alphabet_map.set(chr(ord('a') + i - 1), i)
Ejemplo n.º 21
0
 def test_insert(self):
     testHashTable = h.HashTable()
     testHashTable.insert("abc")
     self.assertEqual(testHashTable.items[hash("abc") % 1024], "abc")
Ejemplo n.º 22
0
 def test_delete2(self):
     testHashTable = h.HashTable()
     testHashTable.insert("troll2")
     testHashTable.delete("troll3")
     self.assertTrue(testHashTable.search("troll2"))
Ejemplo n.º 23
0
 def test_delete(self):
     testHashTable = h.HashTable()
     testHashTable.insert("troll")
     testHashTable.delete("troll")
     self.assertFalse(testHashTable.search("troll"))
sys.path.insert(0, '/home/u/workspace/python-utility') 
import hashtable

if __name__ == '__main__':	
	#example usage:
	#python3 preprocessor.py folder_A folder_B lsfm_input_dir
	#merge the landmark file from folder_B into folder_A for all the files exist in folder_A

	#get folder name, and store them into a list
	lsfm_inputDir = sys.argv[3]
	directory_list_1 = list()
	directory_list_2 = list()

	#instanatiate a hash table
	ht = hashtable.HashTable(10)

	#scan the directories to get the folder path
	for r, d, f in walklevel(sys.argv[1]):
		for folder in d:
			directory_list_1.append(os.path.join(r, folder))			

	for r, d, f in walklevel(sys.argv[2]):
		for folder in d:
			directory_list_2.append(os.path.join(r, folder))
			obj_name = os.path.basename(os.path.normpath(os.path.join(r, folder)))			
			ht.set(obj_name, True)

	for path in directory_list_1:
		obj_name = os.path.basename(os.path.normpath(path))
		#target file is the source to be copied
Ejemplo n.º 25
0
 def test_put_1(self):
     hs = hashtable.HashTable(2, TYPE_SEPARATE_CHAINING)
     hs.put("Alexis", 3)
     hash = hs.hash_function('Alexis')
     self.assertEquals(('Alexis', 3), hs.array[hash].first.value)
     self.assertEquals(3, hs.get('Alexis'))
Ejemplo n.º 26
0
    N = int(sys.argv[1])
    m = int(sys.argv[2])
    func = sys.argv[3]
    filename = sys.argv[4]

# get our hashfunc
if func == "f1":
    h = hashtable.h_mod(m)
elif func == "f2":
    h = hashtable.h_mul(0.2, m)
elif func == "f3":
    h = hashtable.h_mul(0.618034, m)
else:
    h = hashtable.h_mul(0.8, m)

htable = hashtable.HashTable(m, h)

import random
for i in range(N):
    x = hashtable.htentry(random.randint(0, sys.maxint))
    htable.insert(x)

largest = 0
lengths = []
for i in range(m):
    length = len(htable.T[i])
    lengths.append(length)
    largest = length if length > largest else largest
    print "slot %d had %d collisions" % (i, length)
print "Largest collision was %d" % largest
Ejemplo n.º 27
0
# barebones.py by Zachary McNamara [email protected] ID#001182706

import csv
from cmath import inf
from collections import deque
from datetime import datetime, timedelta, tzinfo
import time

import package
import truck
import hashtable
from graph import Graph, Vertex
import operator

packages = hashtable.HashTable()
distances = hashtable.HashTable()
joined = hashtable.HashTable()
global at_station_packages
at_station_packages: package = []
vertices = {}
hub_address = "4001 South 700 East"


# Function create_package_hashtable populates the custom hashtable data structure for package info
#   receives and parses a csv file with wgups package info
def create_package_hashtable(filename):
    with open(filename) as csv_file:
        read_csv = csv.reader(csv_file, delimiter=',')

        for row in read_csv:  # For every row in CSV file
            key = row[0]
Ejemplo n.º 28
0
 def test_search(self):
     testHashTable = h.HashTable()
     testHashTable.insert("banan")
     res = testHashTable.search("banan")
     self.assertTrue(res)
Ejemplo n.º 29
0
def test_hashtable2():
    h = hashtable.HashTable()
    h.set(1, 1)
    h.set(11, 2)
    h.set(2, 3)
    assert h.get(2) == 3
Ejemplo n.º 30
0
 def test_search2(self):
     testHashTable = h.HashTable()
     testHashTable.insert("banan")
     res = testHashTable.search("baba")
     self.assertFalse(res)