ui id strategy and core_hash.c

This commit is contained in:
Krzosa Karol
2025-01-12 17:27:53 +01:00
parent 16125f5605
commit dd796b12f4
5 changed files with 65 additions and 54 deletions

View File

@@ -4,7 +4,7 @@ struct random_seed_t {
};
// xorshift64
u64 get_random_u64(random_seed_t *seed) {
fn u64 get_random_u64(random_seed_t *seed) {
u64 x = seed->a;
x ^= x << 13;
x ^= x >> 7;
@@ -12,7 +12,7 @@ u64 get_random_u64(random_seed_t *seed) {
return seed->a = x;
}
u64 get_random_in_range_u64(random_seed_t *seed, u64 start, u64 one_past_end) {
fn u64 get_random_in_range_u64(random_seed_t *seed, u64 start, u64 one_past_end) {
u64 r = get_random_u64(seed);
u64 diff = one_past_end - start;
u64 rr = r % diff;
@@ -20,7 +20,7 @@ u64 get_random_in_range_u64(random_seed_t *seed, u64 start, u64 one_past_end) {
return rrr;
}
i64 get_random_in_range_i64(random_seed_t *seed, i64 start, i64 one_past_end) {
fn i64 get_random_in_range_i64(random_seed_t *seed, i64 start, i64 one_past_end) {
u64 r = get_random_u64(seed);
i64 diff = one_past_end - start;
u64 rr = r % diff;
@@ -28,8 +28,25 @@ i64 get_random_in_range_i64(random_seed_t *seed, i64 start, i64 one_past_end) {
return rrr;
}
f64 get_random_f64(random_seed_t *seed) {
fn f64 get_random_f64(random_seed_t *seed) {
f64 random = (f64)get_random_in_range_u64(seed, 0, UINT32_MAX);
f64 result = random / (f64)(UINT32_MAX - 1u);
return result;
}
}
fn u64 hash_data(s8_t string) {
u8 *data = (u8 *)string.str;
u64 hash = (u64)14695981039346656037ULL;
for (i64 i = 0; i < string.len; i++) {
hash = hash ^ (u64)(data[i]);
hash = hash * (u64)1099511628211ULL;
}
return hash;
}
fn u64 hash_mix(u64 x, u64 y) {
x ^= y;
x *= 0xff51afd7ed558ccd;
x ^= x >> 32;
return x;
}

View File

@@ -33,16 +33,6 @@ struct ht_dict_t {
ht_bucket_t *buckets;
};
u64 ht_hash_data(s8_t string) {
u8 *data = (u8 *)string.str;
uint64_t hash = (u64)14695981039346656037ULL;
for (i64 i = 0; i < string.len; i++) {
hash = hash ^ (u64)(data[i]);
hash = hash * (u64)1099511628211ULL;
}
return hash;
}
ht_dict_t *ht_create(ma_arena_t *arena, i32 size) {
ht_dict_t *result = ma_push_type(arena, ht_dict_t);
result->buckets = ma_push_array(arena, ht_bucket_t, size);
@@ -61,7 +51,7 @@ ht_node_t *ht_insert_kv(ht_dict_t *ht, u64 hash, ht_key_value_t kv) {
}
ht_node_t *ht_insert_u64_u64(ht_dict_t *ht, u64 key, u64 value) {
u64 hash = ht_hash_data(s8((char *)&key, sizeof(key)));
u64 hash = hash_data(s8_struct(key));
return ht_insert_kv(ht, hash, (ht_key_value_t){.key_u64 = key, .value_u64 = value});
}
@@ -70,12 +60,12 @@ ht_node_t *ht_insert_ptr(ht_dict_t *ht, void *key, void *value) {
}
ht_node_t *ht_insert_string(ht_dict_t *ht, s8_t key, s8_t value) {
u64 hash = ht_hash_data(key);
u64 hash = hash_data(key);
return ht_insert_kv(ht, hash, (ht_key_value_t){.key_string = key, .value_string = value});
}
ht_node_t *ht_search_u64(ht_dict_t *ht, u64 key) {
u64 hash = ht_hash_data(s8((char *)&key, sizeof(key)));
u64 hash = hash_data(s8_struct(key));
i64 idx = hash % ht->bucket_count;
ht_bucket_t *bucket = ht->buckets + idx;
for (ht_node_t *it = bucket->first; it; it = it->next) {
@@ -91,7 +81,7 @@ ht_node_t *ht_search_ptr(ht_dict_t *ht, void *key_ptr) {
}
ht_node_t *ht_search_string(ht_dict_t *ht, s8_t key) {
u64 hash = ht_hash_data(key);
u64 hash = hash_data(key);
i64 idx = hash % ht->bucket_count;
ht_bucket_t *bucket = ht->buckets + idx;
for (ht_node_t *it = bucket->first; it; it = it->next) {

View File

@@ -44,5 +44,5 @@
#include "core_log.c"
#include "core_lexer.c"
#include "core_type_info.c"
#include "core_random.c"
#include "core_hash.c"
#include "core_hash_table.c"

View File

@@ -155,6 +155,7 @@ void *sbin_read_data(stream_t *stream, i64 size);
//
// other
#define s8_fmtspec(string) (int)(string).len, (string).str
#define s8_struct(DATA) (s8_t){.str = (char *)&(DATA), .len = sizeof(DATA)}
#define S8_CODE(...) s8_lit(#__VA_ARGS__)
#define S8_FILE s8_lit(__FILE__)
#define S8_FILE_AND_LINE s8_lit(FILE_AND_LINE)