Beispiel #1
0
    def put(self, key, value):
        index = hash_function(key, self.size)
        list = self.store[index]
        list.insert(key, value)
        self.data_count += 1

        if self.load_factor() > 0.7:
            self.increase_store_size()
Beispiel #2
0
 def insert(self , item):
     index = hash_function(self.length , item)
     print index
     if  self.my_hash_table[index] == None:
         singly_obj = Singly()
         self.my_hash_table[index] = singly_obj.insert_head(item)
     elif self.my_hash_table[index] != None:
         self.my_hash_table[index].insert_head(item)
Beispiel #3
0
 def delete(self , item):
     index = hash_function(self.length , item)
     target = self.my_hash_table[index]
     if target == None:
         return 'Nothing to deletepyto here'
     elif target.next.next == None:
         self.my_hash_table[index] = None
         return item
     else:
         return target.delete_item(item)
Beispiel #4
0
    def increase_store_size(self):
        new_store = []

        # The basic methodolgy here is:
        #   1. Double the size of store with each being a unique copy of linked list
        #   2. Get all existing key value pairs
        #   3. Since the store is a different size, buckets may be placed
        #      in different positions than they were previously. Re-bucket all
        #      key value pairs
        #   4. set the store of our instance to the new store

        for i in range(self.size * 2):
            new_store.append(LinkedList())

        for list in self.store:
            pairs = list.key_value_pairs()

            for pair in pairs:

                new_index = hash_function(pair[0], self.size * 2)
                new_store[new_index].insert(pair[0], pair[1])

        self.size = self.size * 2
        self.store = new_store
Beispiel #5
0
 def exists(self, key):
     index = hash_function(key, self.size)
     list = self.store[index]
     return bool(list.find_by_key(key))
Beispiel #6
0
 def delete(self, key):
     index = hash_function(key, self.size)
     list = self.store[index]
     deleted = list.delete(key)
     if deleted:
         self.data_count -= 1
Beispiel #7
0
 def get(self, key):
     index = hash_function(key, self.size)
     list = self.store[index]
     return list.find_by_key(key)
import numpy as np
import matplotlib.pyplot as plt
from random import choice, seed
from hash_function import hash_function


# ASCII chars that print nicely
ascii = ''.join([chr(i) for i in range(33, 127)])

seed(37)

found = {}
for j in range(5000):
    # Build a 4 byte random string
    s = bytes(''.join([choice(ascii) for _ in range(4)]), 'ascii')
    h = hash_function(s)
    if h in found:
        v = found[h]
        if v == s:
            # Same hash, but from the same source string
            continue
        print(h, found[h], s)
    found[h] = s


# Calculate the probability of collision for 100 keys using Birthday Attack
n = num_of_all_hashes = 2 ** 8  # 256
keys = 256
no_coll_probs = np.array([(n - k) / n for k in range(keys)])
keys_arr = np.array(range(keys))
no_collision = np.prod(no_coll_probs)
Beispiel #9
0
def test_int_input():
    assert (hash_function(188, 5)) < 5
Beispiel #10
0
def test_string_input():
    assert (hash_function("Hello", 5)) < 5
Beispiel #11
0
def test_float_input():
    assert (hash_function(1.2, 5)) < 5
Beispiel #12
0
def find_short_url():
    long_url = request.form['url']
    key = hash_function(long_url)
    URL_TABLE[key] = long_url
    short_url = HOST + key
    return render_template('shorturl.html', new_url=short_url)