Add containers hashing implementation

This commit is contained in:
Gregory Lirent 2022-08-14 21:05:33 +03:00
parent 4e6d83c4cf
commit 5c39f4970c
3 changed files with 61 additions and 9 deletions

View File

@ -4,17 +4,20 @@
#include "include.h"
#include "../__internal/assert.h"
size_t array_size (const arr_t* x) {
return x->size;
}
/*#####################################################################################################################*/
size_t array_nmemb(const arr_t* x) {
return x->size*vtype_size(x->type);
}
hash_t array_hash(const arr_t* s) {
hash_t hash = 0;
void* array_at(const arr_t* x, ssize_t i) {
if (i < 0 && (i += x->size) < 0) i = 0;
return x->mem + i*vtype_size(x->type);
if (s->size > 0)
hash = vtype_hash(s->mem, s->type);
if (s->size > 1)
hash += vtype_hash(array_internal_at(s, s->size - 1), s->type);
hash ^= s->size;
return hash + VTYPE_ARRAY;
}
void array_init(arr_t* x, vtype t) {
@ -52,6 +55,23 @@ void array_free(arr_t* x) {
memset(x, 0, sizeof(*x));
}
/*#####################################################################################################################*/
size_t array_size (const arr_t* x) {
return x->size;
}
size_t array_nmemb(const arr_t* x) {
return x->size*vtype_size(x->type);
}
void* array_at(const arr_t* x, ssize_t i) {
if (i < 0 && (i += x->size) < 0) i = 0;
return x->mem + i*vtype_size(x->type);
}
/*#####################################################################################################################*/
int array_compare(const arr_t* s0, const arr_t* s1) {
void *e;

View File

@ -5,6 +5,22 @@
/*#####################################################################################################################*/
hash_t list_hash(const list_t* s) {
hash_t hash = 0;
if (!is_null(s->first)) {
hash = vnode_hash(&s->first->node, s->first->type);
if (s->first != s->last) {
hash += vnode_hash(&s->first->node, s->first->type);
hash ^= list_size(s);
} else hash ^= 1;
}
return hash + VTYPE_LIST;
}
void list_init(list_t* x) {
memset(x, 0, sizeof(*x));
}

View File

@ -3,6 +3,22 @@
#include "include.h"
hash_t string_hash(const str_t* s) {
hash_t hash = 0;
size_t nmemb = string_nmemb(s);
if (nmemb > 0)
hash = s->buffer[0];
if (nmemb > 1)
hash += s->buffer[nmemb-1];
hash ^= nmemb;
return hash + VTYPE_STRING;
}
size_t string_nmemb(const str_t* s) {
return (!is_null(s->buffer)) ? strlen(s->buffer) : 0;
}