blob: 690527f5d73f3c9c899eb3ed2d2dfd1bfddf1434 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#include "../include/value.h"
#include <string.h>
static uint64_t hash_range(const uint8_t *data, size_t size) {
uint64_t result = 0xdec05eba;
while(size) {
result = ((result << 5) + result) + *data;
++data;
--size;
}
return result;
}
uint64_t tsl_value_hash(const TslValue *key) {
switch(key->type) {
case TSL_TYPE_NULL:
return 0;
case TSL_TYPE_NUMBER:
return *(uint64_t*)&key->data.number;
case TSL_TYPE_STRING:
return hash_range(key->data.string->data, key->data.string->size);
case TSL_TYPE_BOOL:
return key->data.boolean;
case TSL_TYPE_LIST:
return (uint64_t)key->data.list;
case TSL_TYPE_MAP:
return (uint64_t)key->data.map;
case TSL_TYPE_USERDATA:
return (uint64_t)key->data.userdata;
}
return 0;
}
int tsl_value_equals(const TslValue *lhs, const TslValue *rhs) {
if(lhs->type == rhs->type) {
if(lhs->type == TSL_TYPE_STRING) {
return lhs->data.string->size == rhs->data.string->size
&& memcmp(lhs->data.string->data, rhs->data.string->data, lhs->data.string->size) == 0;
} else {
return *(uint64_t*)&lhs->data == *(uint64_t*)&rhs->data;
}
} else {
return 0;
}
}
|