Skip to content

Commit

Permalink
Fixed a comment and a test
Browse files Browse the repository at this point in the history
  • Loading branch information
nbro committed Feb 14, 2017
1 parent dea43cd commit 1ffd205
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
61 changes: 31 additions & 30 deletions ands/ds/LinearProbingHashTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class LinearProbingHashTable(HashTable):
"""Resizable hash table which uses linear probing,
which is a specific "open addressing" technique, to resolve collisions.
The process of re-sizing doubles the current capacity of the hash table each time (for now).
The process of resizing consists in doubling
the current capacity of the hash table each time (for now).
The hash function uses both the Python's built-in `hash` function and the `%` operator.
Expand Down Expand Up @@ -181,35 +182,6 @@ def get(self, key: object) -> object:
self.__invariants__()
return value

@staticmethod
def _get(key: object, keys: list, values: list, size: int) -> object:
"""Helper method of `self.get` and thus it's considered PRIVATE."""
assert not has_duplicates_ignore_nones(keys)

hash_value = LinearProbingHashTable._hash_code(key, size)

data = None
stop = False
found = False
position = hash_value

while keys[position] is not None and not found and not stop:

if keys[position] == key:
found = True
data = values[position]
else:
# Find a new possible position by rehashing
position = LinearProbingHashTable._rehash(position, size)

# We are at the initial slot,
# and thus nothing was found.
if position == hash_value:
stop = True

assert not has_duplicates_ignore_nones(keys)
return data

def delete(self, key: object) -> object:
"""Deletes the mapping between `key` and its corresponding associated value.
If there's no mapping, nothing is done."""
Expand Down Expand Up @@ -273,6 +245,35 @@ def _rehash(old_hash: int, size: int) -> int:
we want to have a new hash value from the old hash value."""
return (old_hash + 1) % size

@staticmethod
def _get(key: object, keys: list, values: list, size: int) -> object:
"""Helper method of `self.get` and thus it's considered PRIVATE."""
assert not has_duplicates_ignore_nones(keys)

hash_value = LinearProbingHashTable._hash_code(key, size)

data = None
stop = False
found = False
position = hash_value

while keys[position] is not None and not found and not stop:

if keys[position] == key:
found = True
data = values[position]
else:
# Find a new possible position by rehashing
position = LinearProbingHashTable._rehash(position, size)

# We are at the initial slot,
# and thus nothing was found.
if position == hash_value:
stop = True

assert not has_duplicates_ignore_nones(keys)
return data


def has_duplicates_ignore_nones(ls: list) -> bool:
"""Returns `True` if `ls` does contain duplicate elements, `False` otherwise.
Expand Down
5 changes: 5 additions & 0 deletions tests/ds/test_LinearProbingHashTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ def test_get_empty_table(self):
t = LinearProbingHashTable()
self.assertIsNone(t.get(3))

def test_get_with_syntactic_sugar(self):
t = LinearProbingHashTable()
t.put(5, 12)
self.assertEqual(t[5], 12)

def test_get_no_key_found(self):
t = LinearProbingHashTable()
t.put("three", 3)
Expand Down

0 comments on commit 1ffd205

Please sign in to comment.