This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def compare_hash(self, other: object) -> bool: | |
"""compare keys and hashes with different entry""" | |
if not isinstance(other, Entry): | |
return NotImplemented | |
return self.key == other.key and self.hash == other.hash |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __getitem__(self, key: str) -> Entry: | |
"""subscript method - object[key] | |
use hashing function to find the index""" | |
entry = Entry(key) | |
index = entry.hash % self.actual_size | |
found = self.entries[index] | |
while found and not entry.compare_hash(found): | |
index = (index + 1) % self.actual_size | |
found = self.entries[index] | |
if found == None: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __setitem__(self, key: str, value: Any) -> int: | |
"""allow insertion of items with object[key] = value""" | |
entry = Entry(key, value) | |
index = entry.hash % self.actual_size | |
existing_entry = self.entries[index] | |
while existing_entry and not entry.compare_hash(existing_entry): | |
# don't overlap existing values | |
index = (index + 1) % self.actual_size | |
existing_entry = self.entries[index] | |
self.entries[index] = entry |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __init__(self, size=10): | |
self.entries = [None for _ in range(size * 2)] | |
self.actual_size = size * 2 | |
self.size = size |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HashTable(object): | |
def __init__(self, size): | |
"""initialise hash table""" | |
def __getitem__(self, key: str) -> Entry: | |
"""subscript method - object[key] | |
use hashing function to find the index""" | |
def __setitem__(self, key: str, value: Any) -> int: | |
"""allow insertion of items with object[key] = value""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Entry(object): | |
def __init__(self, key: str, value=None): | |
self.key = key | |
self.value = value | |
@property | |
def hash(self) -> int: | |
"""return entry's hash""" | |
def __repr__(self): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* https://github.com/lattera/glibc/blob/895ef79e04a953cac1493863bcae29ad85657ee1/malloc/malloc.c#L1044 */ | |
struct malloc_chunk { | |
INTERNAL_SIZE_T mchunk_prev_size; /* Size of previous chunk (if free). */ | |
INTERNAL_SIZE_T mchunk_size; /* Size in bytes, including overhead. */ | |
struct malloc_chunk* fd; /* double links -- used only if free. */ | |
struct malloc_chunk* bk; | |
/* Only used for large blocks: pointer to next larger size. */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [25]: dis.dis("t[a] += b") | |
1 0 LOAD_NAME 0 (t) | |
2 LOAD_NAME 1 (a) | |
4 DUP_TOP_TWO | |
6 BINARY_SUBSCR | |
8 LOAD_NAME 2 (b) | |
10 INPLACE_ADD --> (value in t[a]) += b --> succeeds because list is mutable | |
12 ROT_THREE | |
14 STORE_SUBSCR --> Assign t[a] = our list --> Fails, t[a] is immutable. | |
16 LOAD_CONST 0 (None) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [22]: t = (1, 2, [3, 4]) | |
In [23]: t[2] += [30, 40] | |
--------------------------------------------------------------------------- | |
TypeError Traceback (most recent call last) | |
<ipython-input-25-af836a8d44a2> in <module> | |
----> 1 t[2] += [30, 40] | |
TypeError: 'tuple' object does not support item assignment | |
In [24]: t |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [18]: dict.fromkeys(["key1", "key2", "key3"], "DEFAULT_VALUE") | |
{'key1': 'DEFAULT_VALUE', 'key2': 'DEFAULT_VALUE', 'key3': 'DEFAULT_VALUE'} |