Skip to content

Instantly share code, notes, and snippets.

View AdamGold's full-sized avatar
🌏
Trust the process

Adam Goldschmidt AdamGold

🌏
Trust the process
  • Israel
View GitHub Profile
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
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:
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
def __init__(self, size=10):
self.entries = [None for _ in range(size * 2)]
self.actual_size = size * 2
self.size = size
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"""
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):
@AdamGold
AdamGold / malloc_struct.c
Last active March 4, 2024 18:17
glibc malloc struct
/* 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. */
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)
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
In [18]: dict.fromkeys(["key1", "key2", "key3"], "DEFAULT_VALUE")
{'key1': 'DEFAULT_VALUE', 'key2': 'DEFAULT_VALUE', 'key3': 'DEFAULT_VALUE'}