Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file added .vscode/ipch/4f1a86d870787839/mmap_address.bin
Binary file not shown.
Binary file added .vscode/ipch/7cdf94166c9fdc35/b_hashtables.ipch
Binary file not shown.
Binary file added .vscode/ipch/7cdf94166c9fdc35/mmap_address.bin
Binary file not shown.
Binary file added .vscode/ipch/7e1ed2a90ff053bf/hashtables.ipch
Binary file not shown.
Binary file added .vscode/ipch/7e1ed2a90ff053bf/mmap_address.bin
Binary file not shown.
Binary file not shown.
Binary file added .vscode/ipch/b7a07cdc6c6307db/mmap_address.bin
Binary file not shown.
20 changes: 20 additions & 0 deletions Sprint questions
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
What is an Array and how does it work?
- List of elements of same type in contiguous sequence of memory
- Can access the elements in constant time O(1) using this equation:
`memory_address = starting_address + index * data_size`


What is a Hash Table and how does it work?
- Underlying data structure is an.... Array!
- Values are indexed by... hashing a key
- Hash will return a large unsigned (large positive number) integer `%` (modulo, or mod) the size of the underlying array which you will use as an array index
- Hashing collisions, or when 2 keys hash to the same index, are unavoidable (birthday problem)
- Handle collisions using a Linked List (key/value pairs of the same index are chained together in the same bucket) or Open Addressing (key/value pairs of the same index are placed in the next available bucket)


What is a computer?
- Many way to answer this. Two things to touch on are `PROCESSOR` and `MEMORY`.
- Elaborate on one of the two... possiblities:
* Turing Machines: a theoretical construct that consists of a mechanical processor + an infinite spool of read/write tape which can solve any theoretically computable problem: a trait known as being "Turing Complete"
* The von Neumann architecture, which consists of a CPU (control + arithmetic/logic units), memory, input and output.
* Billions of transistors on a chip carefully and logically arranged to interface with memory (RAM), an operating system + file system (hard drive), input (keyboard, mouse, camera, microphone, internet) and output (screen, audio, internet).
64 changes: 49 additions & 15 deletions basic_hashtable/b_hashtables.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ void destroy_pair(Pair *pair)

/****
djb2 hash function

Do not modify this!
****/
unsigned int hash(char *str, int max)
Expand All @@ -64,71 +63,106 @@ unsigned int hash(char *str, int max)

/****
Fill this in.

All values in storage should be initialized to NULL
(hint: look up `calloc`)
****/
BasicHashTable *create_hash_table(int capacity)
{
BasicHashTable *ht;
BasicHashTable *ht = malloc(sizeof(BasicHashTable));

ht->capacity = capacity;
ht->storage = calloc(capacity, sizeof(Pair *));

return ht;
}

/****
Fill this in.

If you are overwriting a value with a different key, print a warning.

Don't forget to free any malloc'ed memory!
****/
void hash_table_insert(BasicHashTable *ht, char *key, char *value)
{

int index = hash(key, ht->capacity);
if (ht->storage[index]){
printf("Warning Key overwriting!\n");
destroy_pair(ht->storage[index]);
ht->storage[index] = NULL; }
ht->storage[index] = create_pair(key, value);
}

/****
Fill this in.

Don't forget to free any malloc'ed memory!
****/
void hash_table_remove(BasicHashTable *ht, char *key)
{

int index = hash(key, ht->capacity);
if (ht->storage[index]) {
destroy_pair(ht->storage[index]);
ht->storage[index] = NULL;
} else {
printf("Key not exist\n");
}
}

/****
Fill this in.

Should return NULL if the key is not found.
****/
char *hash_table_retrieve(BasicHashTable *ht, char *key)
{
int index = hash(key, ht->capacity);
if (ht->storage[index] != NULL) {

if (strcmp(ht->storage[index]->key, key) == 0) {
printf("KEY IS: %s\n", ht->storage[index]->value);
return ht->storage[index]->value;
} else {
printf("Keys don't match\n");
}
} else {
printf("Keys don't match\n");
}
return NULL;
}

/****
Fill this in.

Don't forget to free any malloc'ed memory!
****/
void destroy_hash_table(BasicHashTable *ht)
{

for (int i = 0; i < ht->capacity; i++)
{
if (ht->storage[i] != NULL)
{
destroy_pair(ht->storage[i]);
}
}
free(ht->storage);
free(ht);
printf("Hash table has been deleted\n");
}


#ifndef TESTING
int main(void)
{
struct BasicHashTable *ht = create_hash_table(16);

int index = hash("line", ht->capacity);
Pair *pair = create_pair("line", "Here today...\n");
ht->storage[index] = pair;

hash_table_insert(ht, "line", "Here today...\n");

printf("%s", hash_table_retrieve(ht, "line"));
hash_table_insert(ht, "test", "hello\n");
hash_table_insert(ht, "value", "world\n");
// hash_table_insert(ht, "key-0", "val-0");
// printf("%s\n", hash_table_retrieve(ht, "key-0"));
// printf("%s\n", hash_table_retrieve(ht, "line"));

hash_table_remove(ht, "line");

if (hash_table_retrieve(ht, "line") == NULL) {
printf("...gone tomorrow. (success)\n");
} else {
Expand All @@ -139,4 +173,4 @@ int main(void)

return 0;
}
#endif
#endif
Loading