diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/hash/buzhash.h | 4 | ||||
| -rw-r--r-- | src/hash/djb2.h | 2 | ||||
| -rw-r--r-- | src/hash/farmhash.h | 4 | ||||
| -rw-r--r-- | src/hash/fasthash.h | 4 | ||||
| -rw-r--r-- | src/hash/jenkins.h | 2 | ||||
| -rw-r--r-- | src/hash/loselose.h | 2 | ||||
| -rw-r--r-- | src/hash/md5.c | 10 | ||||
| -rw-r--r-- | src/hash/md5.h | 2 | ||||
| -rw-r--r-- | src/hash/metrohash.c | 4 | ||||
| -rw-r--r-- | src/hash/metrohash.h | 8 | ||||
| -rw-r--r-- | src/hash/murmur.h | 4 | ||||
| -rw-r--r-- | src/hash/pjw.h | 2 | ||||
| -rw-r--r-- | src/hash/sdbm.h | 2 | ||||
| -rw-r--r-- | src/hash/sha01.c | 16 | ||||
| -rw-r--r-- | src/hash/sha01.h | 2 | ||||
| -rw-r--r-- | src/hash/sha2-256.c | 34 | ||||
| -rw-r--r-- | src/hash/sha2-256.h | 16 | ||||
| -rw-r--r-- | src/hash/spookyhash.c | 18 | ||||
| -rw-r--r-- | src/hash/spookyhash.h | 9 | ||||
| -rw-r--r-- | src/types/map.c | 106 | ||||
| -rw-r--r-- | src/types/map.h | 3 | ||||
| -rw-r--r-- | src/util.h | 3 |
22 files changed, 165 insertions, 92 deletions
diff --git a/src/hash/buzhash.h b/src/hash/buzhash.h index 74020ea..abc7fea 100644 --- a/src/hash/buzhash.h +++ b/src/hash/buzhash.h @@ -1,4 +1,8 @@ #include "../lua.h" +#include <stdint.h> + +uint8_t i_buzhash8(uint8_t*, size_t); +uint16_t i_buzhash16(uint8_t*, size_t); int l_setbuzhash(lua_State*); int l_buzhash8(lua_State*); diff --git a/src/hash/djb2.h b/src/hash/djb2.h index 9c8036a..ab0d20f 100644 --- a/src/hash/djb2.h +++ b/src/hash/djb2.h @@ -1,3 +1,5 @@ #include "../lua.h" +#include <stdint.h> +uint32_t djb2(uint8_t*, size_t); int l_djb2(lua_State*); diff --git a/src/hash/farmhash.h b/src/hash/farmhash.h index 8d99a46..97c0813 100644 --- a/src/hash/farmhash.h +++ b/src/hash/farmhash.h @@ -1,4 +1,8 @@ #include "../lua.h" +#include <stdint.h> + +uint32_t farmhash32(uint8_t* in, size_t len); +uint64_t farmhash64(uint8_t* in, size_t len); int l_farmhash32(lua_State*); int l_farmhash64(lua_State*); diff --git a/src/hash/fasthash.h b/src/hash/fasthash.h index feff2fe..0a950e7 100644 --- a/src/hash/fasthash.h +++ b/src/hash/fasthash.h @@ -1,4 +1,8 @@ #include "../lua.h" +#include <stdint.h> + +uint64_t fasthash64(uint8_t* in, size_t len, uint64_t seed); +uint32_t fasthash32(uint8_t *buf, size_t len, uint32_t seed); int l_fasthash32(lua_State*); int l_fasthash64(lua_State*); diff --git a/src/hash/jenkins.h b/src/hash/jenkins.h index d5959d2..d295896 100644 --- a/src/hash/jenkins.h +++ b/src/hash/jenkins.h @@ -1,3 +1,5 @@ #include "../lua.h" +#include <stdint.h> +uint32_t jenkins_oaat(uint8_t* in, size_t len); int l_oaat(lua_State*); diff --git a/src/hash/loselose.h b/src/hash/loselose.h index dc7d200..434b8cb 100644 --- a/src/hash/loselose.h +++ b/src/hash/loselose.h @@ -1,3 +1,5 @@ #include "../lua.h" +#include <stdint.h> +uint64_t loselose(uint8_t* in, size_t len); int l_loselose(lua_State*); diff --git a/src/hash/md5.c b/src/hash/md5.c index 7671066..a909819 100644 --- a/src/hash/md5.c +++ b/src/hash/md5.c @@ -20,14 +20,12 @@ static const uint32_t s[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21}; -void i_md5(char* input, char out_stream[64]){ +void i_md5(char* input, size_t len, char out_stream[64]){ uint32_t a0 = 0x67452301; uint32_t b0 = 0xefcdab89; uint32_t c0 = 0x98badcfe; uint32_t d0 = 0x10325476; - int len = 0; - for(int i = 0; input[i] != '\0'; i++) len++; int tlen = ((((len + 8) /64) + 1) * 64) - 8; uint8_t* b = NULL; @@ -96,11 +94,11 @@ void i_md5(char* input, char out_stream[64]){ int l_md5(lua_State* L){ - - char* a = (char*)luaL_checklstring(L, 1, NULL); + size_t len = 0; + char* a = (char*)luaL_checklstring(L, 1, &len); char digest[64]; - i_md5(a, digest); + i_md5(a, len, digest); lua_pushstring(L, digest); return 1; diff --git a/src/hash/md5.h b/src/hash/md5.h index 70161bd..aeae0c0 100644 --- a/src/hash/md5.h +++ b/src/hash/md5.h @@ -7,6 +7,6 @@ * @param {char[64]} output stream * @return {void} */ -void i_md5(char*,char*); +void i_md5(char*,size_t,char*); int l_md5(lua_State*); diff --git a/src/hash/metrohash.c b/src/hash/metrohash.c index 4e59f81..7da502a 100644 --- a/src/hash/metrohash.c +++ b/src/hash/metrohash.c @@ -8,10 +8,6 @@ #define u16(a) (*(uint16_t*)a) #define u8(a) (*(uint8_t*)a) -enum metrohash_version { - v1, v2 -}; - uint64_t metrohash64(uint8_t* in, size_t len, uint32_t seed, enum metrohash_version v){ uint64_t k0, k1, k2, k3, inner_r, inner_r2; if(v == v1){ diff --git a/src/hash/metrohash.h b/src/hash/metrohash.h index 30dabb2..8c9ebea 100644 --- a/src/hash/metrohash.h +++ b/src/hash/metrohash.h @@ -1,4 +1,12 @@ #include "../lua.h" +#include <stdint.h> + +enum metrohash_version { + v1, v2 +}; + +uint64_t metrohash64(uint8_t* in, size_t len, uint32_t seed, enum metrohash_version v); +void metrohash128(uint8_t* in, size_t len, uint32_t seed, uint64_t *a, uint64_t *b, enum metrohash_version ver); int l_metrohash64_v1(lua_State*); int l_metrohash64_v2(lua_State*); diff --git a/src/hash/murmur.h b/src/hash/murmur.h index d7d7fc1..73a18a2 100644 --- a/src/hash/murmur.h +++ b/src/hash/murmur.h @@ -1,4 +1,8 @@ #include "../lua.h" +#include <stdint.h> + +uint32_t murmur1_32(uint8_t* in, size_t len, uint32_t seed); +uint32_t murmur2_32(uint8_t* in, size_t len, uint32_t seed); int l_murmur1_32(lua_State*); int l_murmur2_32(lua_State*); diff --git a/src/hash/pjw.h b/src/hash/pjw.h index 7a3c1de..f1ab910 100644 --- a/src/hash/pjw.h +++ b/src/hash/pjw.h @@ -1,3 +1,5 @@ #include "../lua.h" +#include <stdint.h> +uint32_t pjw(uint8_t* in, size_t len); int l_pjw(lua_State*); diff --git a/src/hash/sdbm.h b/src/hash/sdbm.h index 1420e48..797cc22 100644 --- a/src/hash/sdbm.h +++ b/src/hash/sdbm.h @@ -1,3 +1,5 @@ #include "../lua.h" +#include <stdint.h> +uint64_t sdbm(uint8_t* in, size_t len); int l_sdbm(lua_State*); diff --git a/src/hash/sha01.c b/src/hash/sha01.c index 07f8829..b8b70f9 100644 --- a/src/hash/sha01.c +++ b/src/hash/sha01.c @@ -3,7 +3,7 @@ #include <string.h> #include <stdint.h> -void i_sha01(uint8_t version, char* out_stream, const char* input){ +void i_sha01(uint8_t version, char* out_stream, int len, const char* input){ if(!out_stream||version > 2) return; uint32_t h0 = 0x67452301; uint32_t h1 = 0xEFCDAB89; @@ -11,8 +11,6 @@ void i_sha01(uint8_t version, char* out_stream, const char* input){ uint32_t h3 = 0x10325476; uint32_t h4 = 0xC3D2E1F0; - int len = 0; - for(int i = 0; input[i]!='\0'; i++) len++; int tlen = ((((len + 8) /64) + 1) * 64) - 8; uint8_t* by = NULL; @@ -87,12 +85,12 @@ void i_sha01(uint8_t version, char* out_stream, const char* input){ int l_sha1(lua_State* L){ - int len = 0; - char* a = (char*)luaL_checklstring(L, 1, NULL); + size_t len = 0; + char* a = (char*)luaL_checklstring(L, 1, &len); char digest[160]; - i_sha01(1, digest, a); + i_sha01(1, digest, len, a); lua_pushstring(L, digest); return 1; @@ -100,12 +98,12 @@ int l_sha1(lua_State* L){ int l_sha0(lua_State* L){ - int len = 0; - char* a = (char*)luaL_checklstring(L, 1, NULL); + size_t len = 0; + char* a = (char*)luaL_checklstring(L, 1, &len); char digest[160]; - i_sha01(0, digest, a); + i_sha01(0, digest, len, a); lua_pushstring(L, digest); return 1; diff --git a/src/hash/sha01.h b/src/hash/sha01.h index 7c37115..c574d68 100644 --- a/src/hash/sha01.h +++ b/src/hash/sha01.h @@ -9,7 +9,7 @@ * @param {const char*} input bytes * @return {void} */ -void i_sha01(uint8_t, char*, const char*); +void i_sha01(uint8_t, char*, int, const char*); int l_sha1(lua_State*); int l_sha0(lua_State*); diff --git a/src/hash/sha2-256.c b/src/hash/sha2-256.c index d0c03f9..32af13a 100644 --- a/src/hash/sha2-256.c +++ b/src/hash/sha2-256.c @@ -23,17 +23,7 @@ void endian_swap64(uint64_t *x){ } } -struct iv { - uint64_t h0, h1, h2, h3, h4, h5, h6, h7; -}; - -static const struct iv sha512_iv = {.h0 = 0x6a09e667f3bcc908, .h1 = 0xbb67ae8584caa73b, .h2 = 0x3c6ef372fe94f82b, .h3 = 0xa54ff53a5f1d36f1, - .h4 = 0x510e527fade682d1, .h5 = 0x9b05688c2b3e6c1f, .h6 = 0x1f83d9abfb41bd6b, .h7 = 0x5be0cd19137e2179}; - -static const struct iv sha384_iv = {.h0 = 0xcbbb9d5dc1059ed8, .h1 = 0x629a292a367cd507, .h2 = 0x9159015a3070dd17, .h3 = 0x152fecd8f70e5939, - .h4 = 0x67332667ffc00b31, .h5 = 0x8eb44a8768581511, .h6 = 0xdb0c2e0d64f98fa7, .h7 = 0x47b5481dbefa4fa4}; - -void sha512_gen(uint64_t* out_stream, uint8_t* input, struct iv sha_iv){ +void sha512_gen(uint64_t* out_stream, uint8_t* input, size_t len, struct iv sha_iv){ uint64_t h0 = sha_iv.h0, h1 = sha_iv.h1, h2 = sha_iv.h2, h3 = sha_iv.h3, h4 = sha_iv.h4, h5 = sha_iv.h5, h6 = sha_iv.h6, h7 = sha_iv.h7; const uint64_t k[80] = {0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, 0x3956c25bf348b538, @@ -53,8 +43,6 @@ void sha512_gen(uint64_t* out_stream, uint8_t* input, struct iv sha_iv){ 0x113f9804bef90dae, 0x1b710b35131c471b, 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817}; - size_t len = 0; - for(int i = 0; input[i]!='\0'; i++) len++; size_t blen = len*8; int ulen = 0; @@ -152,27 +140,27 @@ struct iv sha_iv_gen(int i){ uint64_t nh[8] = {0}; uint8_t in[12]; sprintf((char*)in, "SHA-512/%i",i); - sha512_gen(nh, in, oh); + sha512_gen(nh, in, strlen((char*)in), oh); return (struct iv){.h0 = nh[0], .h1 = nh[1], .h2 = nh[2], .h3 = nh[3], .h4 = nh[4], .h5 = nh[5], .h6 = nh[6], .h7 = nh[7]}; } -void sha2_512_t(uint8_t* out, uint8_t* in, int t){ +void sha2_512_t(uint8_t* out, uint8_t* in, size_t len, int t){ if(t%8!=0) return; uint64_t out_stream[8] = {0}; - sha512_gen(out_stream, in, sha_iv_gen(t)); + sha512_gen(out_stream, in, len, sha_iv_gen(t)); for(int i = 0; i != 8; i++) sprintf((char*)out, "%s%016llx", out, out_stream[i]); out[t/4] = '\0'; } -void sha2_512(uint8_t* out, uint8_t* in){ +void sha2_512(uint8_t* out, uint8_t* in, size_t len){ uint64_t out_stream[8] = {0}; - sha512_gen(out_stream, in, sha512_iv); + sha512_gen(out_stream, in, len, sha512_iv); for(int i = 0; i != 8; i++) sprintf((char*)out, "%s%016llx", out, out_stream[i]); } -void sha2_384(uint8_t* out, uint8_t* in){ +void sha2_384(uint8_t* out, uint8_t* in, size_t len){ uint64_t out_stream[8] = {0}; - sha512_gen(out_stream, in, sha384_iv); + sha512_gen(out_stream, in, len, sha384_iv); for(int i = 0; i != 6; i++) sprintf((char*)out, "%s%016llx", out, out_stream[i]); } @@ -182,7 +170,7 @@ int l_sha512(lua_State* L){ char digest[512] = {0}; - sha2_512((uint8_t*)digest, a); + sha2_512((uint8_t*)digest, a, len); lua_pushstring(L, digest); return 1; } @@ -193,7 +181,7 @@ int l_sha384(lua_State* L){ char digest[384] = {0}; - sha2_384((uint8_t*)digest, a); + sha2_384((uint8_t*)digest, a, len); lua_pushstring(L, digest); return 1; } @@ -205,7 +193,7 @@ int l_sha512_t(lua_State* L){ char digest[512] = {0}; - sha2_512_t((uint8_t*)digest, a, t); + sha2_512_t((uint8_t*)digest, a, t, len); lua_pushstring(L, digest); return 1; } diff --git a/src/hash/sha2-256.h b/src/hash/sha2-256.h index d794dec..a261819 100644 --- a/src/hash/sha2-256.h +++ b/src/hash/sha2-256.h @@ -1,4 +1,20 @@ #include "../lua.h" +#include <stdint.h> + +struct iv { + uint64_t h0, h1, h2, h3, h4, h5, h6, h7; +}; + +static const struct iv sha512_iv = {.h0 = 0x6a09e667f3bcc908, .h1 = 0xbb67ae8584caa73b, .h2 = 0x3c6ef372fe94f82b, .h3 = 0xa54ff53a5f1d36f1, + .h4 = 0x510e527fade682d1, .h5 = 0x9b05688c2b3e6c1f, .h6 = 0x1f83d9abfb41bd6b, .h7 = 0x5be0cd19137e2179}; + +static const struct iv sha384_iv = {.h0 = 0xcbbb9d5dc1059ed8, .h1 = 0x629a292a367cd507, .h2 = 0x9159015a3070dd17, .h3 = 0x152fecd8f70e5939, + .h4 = 0x67332667ffc00b31, .h5 = 0x8eb44a8768581511, .h6 = 0xdb0c2e0d64f98fa7, .h7 = 0x47b5481dbefa4fa4}; + +struct iv sha_iv_gen(int i); +void sha2_512_t(uint8_t* out, uint8_t* in, size_t len, int t); +void sha2_512(uint8_t* out, uint8_t* in, size_t len); +void sha2_384(uint8_t* out, uint8_t* in, size_t len); int l_sha512(lua_State*); int l_sha384(lua_State*); diff --git a/src/hash/spookyhash.c b/src/hash/spookyhash.c index 5281452..abc3ea0 100644 --- a/src/hash/spookyhash.c +++ b/src/hash/spookyhash.c @@ -8,10 +8,6 @@ static const uint64_t sc_const = 0xdeadbeefdeadbeefLL; static const size_t sc_blockSize = sc_numVars*8; static const size_t sc_bufSize = 2*sc_blockSize; -enum spooky_version { - v1, v2 -}; - uint64_t i_rot64(uint64_t x, int k){ return (x << k) | (x >> (64 - k)); } @@ -81,7 +77,7 @@ void spooky_short(uint8_t* in, size_t len, uint64_t* hash1, uint64_t* hash2, enu } } - d = (((uint64_t)len) << 56) + (d * (v == v2)); + d = (((uint64_t)len) << 56) + (d * (v == spv2)); switch(remainder){ case 15: d += ((uint64_t)u.p8[14]) << 48; @@ -243,7 +239,7 @@ int l_spookyhash128_v1(lua_State* L){ char digest[128] = {0}; //uint64_t b = 0; //uint64_t c = 0; - spookyhash128(a, 4, &b, &c, v1); + spookyhash128(a, 4, &b, &c, spv1); sprintf(digest, "%016lx%016lx", b, c); lua_pushstring(L, digest); @@ -263,7 +259,7 @@ int l_spookyhash128_v2(lua_State* L){ char digest[128] = {0}; //uint64_t b = 0; //uint64_t c = 0; - spookyhash128(a, 4, &b, &c, v2); + spookyhash128(a, 4, &b, &c, spv2); sprintf(digest, "%016lx%016lx", b, c); lua_pushstring(L, digest); @@ -279,7 +275,7 @@ int l_spookyhash64_v1(lua_State* L){ char digest[64] = {0}; - sprintf(digest, "%08lx", spookyhash64(a, len, seed, v1)); + sprintf(digest, "%08lx", spookyhash64(a, len, seed, spv1)); lua_pushstring(L, digest); return 1; } @@ -293,7 +289,7 @@ int l_spookyhash64_v2(lua_State* L){ char digest[64] = {0}; - sprintf(digest, "%08lx", spookyhash64(a, len, seed, v2)); + sprintf(digest, "%08lx", spookyhash64(a, len, seed, spv2)); lua_pushstring(L, digest); return 1; } @@ -307,7 +303,7 @@ int l_spookyhash32_v1(lua_State* L){ char digest[32] = {0}; - sprintf(digest, "%04x", spookyhash32(a, len, seed, v1)); + sprintf(digest, "%04x", spookyhash32(a, len, seed, spv1)); lua_pushstring(L, digest); return 1; } @@ -321,7 +317,7 @@ int l_spookyhash32_v2(lua_State* L){ char digest[32] = {0}; - sprintf(digest, "%04x", spookyhash32(a, len, seed, v2)); + sprintf(digest, "%04x", spookyhash32(a, len, seed, spv2)); lua_pushstring(L, digest); return 1; } diff --git a/src/hash/spookyhash.h b/src/hash/spookyhash.h index 5bcecf6..5ca6c8a 100644 --- a/src/hash/spookyhash.h +++ b/src/hash/spookyhash.h @@ -1,4 +1,13 @@ #include "../lua.h" +#include <stdint.h> + +enum spooky_version { + spv1, spv2 +}; + +void spookyhash128(uint8_t* in, size_t len, uint64_t* hash1, uint64_t* hash2, enum spooky_version v); +uint64_t spookyhash64(uint8_t *message, size_t length, uint64_t seed, enum spooky_version v); +uint32_t spookyhash32(uint8_t *message, size_t length, uint32_t seed, enum spooky_version v); int l_spookyhash128_v1(lua_State*); int l_spookyhash128_v2(lua_State*); diff --git a/src/types/map.c b/src/types/map.c index b5afc1c..cd46537 100644 --- a/src/types/map.c +++ b/src/types/map.c @@ -10,52 +10,74 @@ uint64_t hash(char* c, size_t len){ return fnv_1((uint8_t*)c, len, v_a); } -map_t* map_init(){ - map_t* awa = malloc(sizeof * awa); - awa->M = malloc(sizeof * awa->M * 4); +void map_dump(map_t* M){ + printf("---\n%i %i\n- **\n",M->mod, M->len); + for(int i = 0; i != M->mod; i++){ + if(M->M[i].used){ + printf("%i | %s : %p\n",i,M->M[i].key->c, M->M[i].value); + } + } +} + +map_t* map_initl(size_t len){ + map_t* awa = calloc(sizeof * awa, 1); + awa->M = calloc(sizeof * awa->M, len); + //for(int i = 0; i != len; i++) awa->M[i].used = 0; awa->len = 0; - awa->mod = 4; + awa->mod = len; return awa; } -void map_regraph(map_t* M){ - map_t* remade = map_init(); - for(int i = 0; i != M->len; i++){ +map_t* map_init(){ + return map_initl(4); +} + +void map_expand(map_t** _M){ + map_t* M = *_M; + map_t* remade = map_initl(M->mod * 4); + for(int i = 0; i != M->mod; i++){ //what happens if the map_set calls map_regraph??? idk - map_set(remade, M->M[i].key->c, M->M[i].value); + if(M->M[i].used) + map_set(&remade, M->M[i].key->c, M->M[i].value); } - M = remade; + + *_M = remade; } -void map_set(map_t* M, char* key, void* value){ +void map_set(map_t** _M, char* key, void* value){ + map_t* M = *_M; uint64_t h = hash(key, strlen(key)); - if(M->len >= M->mod){ + + if(M->len + 1 >= M->mod){ expand: - M->mod *= 4; - M->M = realloc(M->M, sizeof * M->M * M->mod); - //regraph it - map_regraph(M); + map_expand(&M); } uint64_t ind = h % M->mod; - - for(int count = 0; M->M[ind].key != NULL && M->M[ind].hash != h && strcmp(M->M[ind].key->c, key) != 0; count++){ + uint64_t oind = ind; + + //iterates until there is a free space + for(int count = 0; M->M[ind].used && M->M[ind].hash != h && strcmp(M->M[ind].key->c, key) != 0; count++){ ind++; if(ind >= M->mod) ind = 0; - if(count > 5) goto expand; + if(ind == oind || count > 10) goto expand; } M->M[ind].hash = h; M->M[ind].key = str_init(key); M->M[ind].value = value; + M->M[ind].used = 1; + M->len++; + + *_M = M; } int map_geti(map_t* M, char* key){ uint64_t h = hash(key, strlen(key)); uint64_t ind = h % M->mod; - //melem_t sel = M->M[]; for(uint64_t initial = ind; ind != initial - 1;){ if(M->M[ind].key == NULL) return -1; + //printf("%s\n",M->M[ind].key->c); if(M->M[ind].hash == h && strcmp(M->M[ind].key->c, key)==0) return ind; ind++; if(ind >= M->mod) ind = 0; @@ -65,35 +87,49 @@ int map_geti(map_t* M, char* key){ void* map_get(map_t* M, char* key){ int r = map_geti(M, key); - printf("%i\n",r); + //printf("%i\n",r); return r == -1? NULL : M->M[r].value; } -void map_remove(map_t* p, char* key, enum free_type free); -void map_clear(map_t*, enum free_type); -void map_lclear(map_t*); +void map_remove(map_t* p, char* key, enum free_type free){ + int ind = map_geti(p, key); + if(ind == -1) return; + p->M[ind].used = 0; + p->M[ind].hash = 0; + str_free(p->M[ind].key); + free_method(p->M[ind].value, free); +} -void map_dump(map_t* M){ +void map_lclear(map_t* M){ + free(M->M); + free(M); +} + +void map_clear(map_t* M, enum free_type free){ for(int i = 0; i != M->mod; i++){ - if(M->M[i].key != NULL){ - printf("%i | %s : %p\n",i,M->M[i].key->c, M->M[i].value); - } + if(M->M[i].used){ + str_free(M->M[i].key); + free_method(M->M[i].value, free); + } } + map_lclear(M); } + int main(){ int i = 5; int b = 24; int c = 9; map_t* m = map_init(); - for(int i = 65; i != 91; i++){ - //printf("%c\n",i); - int* ww = malloc(sizeof * ww * 55); - ww[0] = i; - map_set(m, ((char[]){i, 0}), ww); - } - map_dump(m); - printf("%i\n",*(int*)map_get(m, "B")); + map_set(&m, "wowa", &b); + printf("%i\n",*(int*)map_get(m, "wowa")); + map_set(&m, "aw", &i); + map_set(&m, "awc", &i); + map_set(&m, "awa", &i); + map_set(&m, "aww", &i); + printf("%i\n",*(int*)map_get(m, "wowa")); + + map_clear(m, NONE); return 0; }
\ No newline at end of file diff --git a/src/types/map.h b/src/types/map.h index cb8fed2..efa6d65 100644 --- a/src/types/map.h +++ b/src/types/map.h @@ -10,6 +10,7 @@ typedef struct { void* value; str* key; uint64_t hash; + int used; } melem_t; typedef struct { @@ -19,7 +20,7 @@ typedef struct { } map_t; map_t* map_init(); -void map_set(map_t*, char*, void*); +void map_set(map_t**, char*, void*); void* map_get(map_t* , char*); int map_geti(map_t* , char*); void map_remove(map_t* p, char* key, enum free_type free); @@ -23,7 +23,8 @@ #define color_reset "\e[0m" #define i_swap(A,B) double temp = A; A = B; B = temp; -#define lesser(A,B) (A>B?B:A) +#define lesser(A,B) ((A)>(B)?(B):(A)) +#define inter(V,I) (I * ceil((double)V / I)) int gen_parse(char*,int, parray_t**); #define p_fatal(M) _p_fatal(M, __LINE__, __FILE__, __func__ ); |
