Felix Brendel 7 лет назад
Родитель
Сommit
b9de82c0d8
3 измененных файлов: 50 добавлений и 20 удалений
  1. +0
    -0
     
  2. +45
    -5
      hashmap.hpp
  3. +5
    -15
      test.cpp

+ 45
- 5
hashmap.hpp Просмотреть файл

@@ -1,9 +1,18 @@
#pragma once #pragma once
#include <string.h> #include <string.h>
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "types.hpp" #include "types.hpp"


#define for_str_hash_map(hm) \
if (char* key = nullptr); else \
if (void* value = nullptr); else \
for(int i = 0; i < hm->current_capacity; ++i) \
if (!((key = hm->data[i].original_string) && \
(value = hm->data[i].object))); else



struct StringHashMapCell { struct StringHashMapCell {
char* original_string; char* original_string;
void* object; void* object;
@@ -17,6 +26,12 @@ struct StringHashMap {
StringHashMapCell* data; StringHashMapCell* data;
}; };


inline void print_hm(StringHashMap* hm) {
for_str_hash_map(hm) {
printf("index: %d key: %s value: %llu\n", i, key, (unsigned long long)value);
}
}

inline bool strings_match(char* a, char* b) { inline bool strings_match(char* a, char* b) {
return strcmp(a, b) == 0; return strcmp(a, b) == 0;
} }
@@ -61,15 +76,20 @@ u32 hash(char* str) {
} }


StringHashMap* create_hashmap(int initial_capacity = 8) { StringHashMap* create_hashmap(int initial_capacity = 8) {
printf("1aeaeae\n");
StringHashMap* hm = new StringHashMap; StringHashMap* hm = new StringHashMap;
hm->current_capacity = initial_capacity; hm->current_capacity = initial_capacity;
hm->cell_count = 0; hm->cell_count = 0;
// set all data to nullptr // set all data to nullptr
hm->data = (StringHashMapCell *)calloc(initial_capacity, sizeof(StringHashMapCell*)); hm->data = (StringHashMapCell *)calloc(initial_capacity, sizeof(StringHashMapCell*));
// printf("check: %s\n", hm->data[6].original_string);
printf("after creating...\n");
print_hm(hm);
return hm; return hm;
} }


int hm_get_index_of_living_cell_if_it_exists(StringHashMap* hm, char* key, u64 hash_val) { int hm_get_index_of_living_cell_if_it_exists(StringHashMap* hm, char* key, u64 hash_val) {
printf("2aeaeae\n");
int index = hash_val % hm->current_capacity; int index = hash_val % hm->current_capacity;
StringHashMapCell cell = hm->data[index]; StringHashMapCell cell = hm->data[index];
// test if cell exists at that index // test if cell exists at that index
@@ -130,13 +150,20 @@ inline void hm_delete_object(StringHashMap* hm, char* key) {
} }


void hm_set(StringHashMap* hm, char* key, void* obj) { void hm_set(StringHashMap* hm, char* key, void* obj) {
printf(" -------------\n");
printf("before...\n");
print_hm(hm);

u64 hash_val = hash(key); u64 hash_val = hash(key);
int index = index = hash_val % hm->current_capacity; int index = index = hash_val % hm->current_capacity;


// if we the desired cell is just empty, write to it and done :) // if we the desired cell is just empty, write to it and done :)
printf("index: %d\n");
printf(" setting: %s\n", key);
printf(" capacity: %d\n", hm->current_capacity);
printf(" index: %d\n", index);
if (!hm->data[index].original_string) { if (!hm->data[index].original_string) {
// insert new cell into desired slot // insert new cell into desired slot
printf("alles paletti\n");
++hm->cell_count; ++hm->cell_count;
} else { } else {
if (strings_match(key, hm->data[index].original_string)) { if (strings_match(key, hm->data[index].original_string)) {
@@ -146,7 +173,7 @@ void hm_set(StringHashMap* hm, char* key, void* obj) {
// collision, check resize // collision, check resize
++hm->cell_count; ++hm->cell_count;
if ((hm->cell_count*1.0f / hm->current_capacity) > 0.666f) { if ((hm->cell_count*1.0f / hm->current_capacity) > 0.666f) {
printf("raellocation\n");
printf("!!!reallocation\n");
StringHashMapCell* old_data = hm->data; StringHashMapCell* old_data = hm->data;
hm->data = (StringHashMapCell*)calloc(hm->current_capacity*4, sizeof(StringHashMapCell*)); hm->data = (StringHashMapCell*)calloc(hm->current_capacity*4, sizeof(StringHashMapCell*));
hm->cell_count = 0; hm->cell_count = 0;
@@ -165,23 +192,36 @@ void hm_set(StringHashMap* hm, char* key, void* obj) {
// preventing gotos using lambdas! // preventing gotos using lambdas!
[&](){ [&](){
for (int i = index; i < hm->current_capacity; ++i) { for (int i = index; i < hm->current_capacity; ++i) {
if (!hm->data[i].original_string) {
printf(" checking free index: %d\n", i);
if (!hm->data[i].original_string ||
strings_match(hm->data[i].original_string, key))
{
index = i; index = i;
return; return;
} else {
printf("-> not free, found %s\n", hm->data[i].original_string);
} }
} }
for (int i = 0; i < index; ++i) { for (int i = 0; i < index; ++i) {
if (!hm->data[i].original_string) {
printf(" checking free index: %d\n", i);
if (!hm->data[i].original_string ||
strings_match(hm->data[i].original_string, key))
{
index = i; index = i;
return; return;
} else {
printf("-> not free, found %s", hm->data[i].original_string);
} }
} }
}(); }();
} }
} }


printf (" writing at index: %d\n", index);
hm->data[index].deleted = false; hm->data[index].deleted = false;
hm->data[index].original_string = key; hm->data[index].original_string = key;
hm->data[index].hash = hash_val; hm->data[index].hash = hash_val;
hm->data[index].object = obj; hm->data[index].object = obj;
printf("after...\n");
print_hm(hm);
} }

+ 5
- 15
test.cpp Просмотреть файл

@@ -3,24 +3,14 @@


#include "profiler.hpp" #include "profiler.hpp"
#include "macros.hpp" #include "macros.hpp"

int var = 100;

void test() {
profile_this;
printf("var is %d\n", var);

fluid_let (var, 200) {
printf("var is %d\n", var);
}

}
#include "hashmap.hpp"


int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
profile_this;


test();
printf("var is %d\n", var);
StringHashMap* hm = create_hashmap();
hm_set(hm, (char*)"=", (void*)1);
hm_set(hm, (char*)"<", (void*)2);
hm_set(hm, (char*)"<=", (void*)2);


return 0; return 0;
} }

Загрузка…
Отмена
Сохранить