aboutsummaryrefslogtreecommitdiff
path: root/src/types
diff options
context:
space:
mode:
authorame <[email protected]>2024-03-13 16:03:10 -0500
committerame <[email protected]>2024-03-13 16:03:10 -0500
commiteb6608e16d253ded9ee3979f236b68d4988c1da5 (patch)
tree6e7f5619f468e5b4e34378def3e9f2960d909b38 /src/types
parent2a3575134b386b60bafbf59d6475cf45ded7d4e3 (diff)
blake2{b,s} and maybe other stuff
Diffstat (limited to 'src/types')
-rw-r--r--src/types/map.c268
-rw-r--r--src/types/map.h58
-rw-r--r--src/types/str.c132
3 files changed, 229 insertions, 229 deletions
diff --git a/src/types/map.c b/src/types/map.c
index cd46537..a15762a 100644
--- a/src/types/map.c
+++ b/src/types/map.c
@@ -1,135 +1,135 @@
-#include "map.h"
-#include "../hash/fnv.h"
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-
-#define mod_inc 4
-
-uint64_t hash(char* c, size_t len){
- return fnv_1((uint8_t*)c, len, v_a);
-}
-
-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 = len;
- return awa;
-}
-
-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
- if(M->M[i].used)
- map_set(&remade, M->M[i].key->c, M->M[i].value);
- }
-
- *_M = remade;
-}
-
-void map_set(map_t** _M, char* key, void* value){
- map_t* M = *_M;
- uint64_t h = hash(key, strlen(key));
-
- if(M->len + 1 >= M->mod){
- expand:
- map_expand(&M);
- }
- uint64_t ind = h % M->mod;
- 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(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;
-
- 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;
- }
- return -1;
-}
-
-void* map_get(map_t* M, char* key){
- int r = map_geti(M, key);
- //printf("%i\n",r);
- return r == -1? NULL : M->M[r].value;
-}
-
-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_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].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();
-
- 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;
+#include "map.h"
+#include "../hash/fnv.h"
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#define mod_inc 4
+
+uint64_t hash(char* c, size_t len){
+ return fnv_1((uint8_t*)c, len, v_a);
+}
+
+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 = len;
+ return awa;
+}
+
+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
+ if(M->M[i].used)
+ map_set(&remade, M->M[i].key->c, M->M[i].value);
+ }
+
+ *_M = remade;
+}
+
+void map_set(map_t** _M, char* key, void* value){
+ map_t* M = *_M;
+ uint64_t h = hash(key, strlen(key));
+
+ if(M->len + 1 >= M->mod){
+ expand:
+ map_expand(&M);
+ }
+ uint64_t ind = h % M->mod;
+ 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(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;
+
+ 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;
+ }
+ return -1;
+}
+
+void* map_get(map_t* M, char* key){
+ int r = map_geti(M, key);
+ //printf("%i\n",r);
+ return r == -1? NULL : M->M[r].value;
+}
+
+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_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].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();
+
+ 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 efa6d65..2603f47 100644
--- a/src/types/map.h
+++ b/src/types/map.h
@@ -1,30 +1,30 @@
-
-#ifndef _MAP_H
-#define _MAP_H
-
-#include <stdint.h>
-#include "str.h"
-#include "parray.h"
-
-typedef struct {
- void* value;
- str* key;
- uint64_t hash;
- int used;
-} melem_t;
-
-typedef struct {
- melem_t* M;
- int len;
- int mod;
-} map_t;
-
-map_t* map_init();
-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);
-void map_clear(map_t*, enum free_type);
-void map_lclear(map_t*);
-
+
+#ifndef _MAP_H
+#define _MAP_H
+
+#include <stdint.h>
+#include "str.h"
+#include "parray.h"
+
+typedef struct {
+ void* value;
+ str* key;
+ uint64_t hash;
+ int used;
+} melem_t;
+
+typedef struct {
+ melem_t* M;
+ int len;
+ int mod;
+} map_t;
+
+map_t* map_init();
+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);
+void map_clear(map_t*, enum free_type);
+void map_lclear(map_t*);
+
#endif //_MAP_H \ No newline at end of file
diff --git a/src/types/str.c b/src/types/str.c
index 929057e..364a02f 100644
--- a/src/types/str.c
+++ b/src/types/str.c
@@ -1,66 +1,66 @@
-#include "str.h"
-#include "../lua.h"
-#include "../util.h"
-
-#define alloc_buffer 64
-
-str* str_initl(char* init, size_t len){
- if(init == NULL){
- char cc = '\0';
- init = &cc;
- }
-
- str* s = malloc(sizeof * s);
- s->_bytes = len + 1 + alloc_buffer;
- s->c = malloc(s->_bytes);
- if(s->c == NULL) p_fatal("failed to allocate string\n");
- s->len = len ;
-
- memcpy(s->c, init, len + 1);
- return s;
-}
-
-str* str_init(char* init){
- return str_initl(init, strlen(init));
-}
-
-void str_free(str* s){
- free(s->c);
- return free(s);
-}
-
-void str_push(str* s, char* insert){
- s->len += strlen(insert);
- if(s->len + 1 >= s->_bytes)
- s->c = realloc(s->c, s->_bytes = s->len + 1 + alloc_buffer);
- strcat(s->c, insert);
-}
-
-void str_pushl(str* s, char* insert, size_t l){
- if(s->len + l >= s->_bytes)
- s->c = realloc(s->c, s->_bytes = s->len + l + alloc_buffer);
- //strcat(s->c, insert);
- for(int i = 0; i != l; i++){
- s->c[i + s->len] = insert[i];
- }
- s->len += l;
- s->c[s->len] = '\0';
-}
-
-void str_clear(str* s){
- memset(s->c, 0, s->len);
-
- s->len = 0;
-}
-
-void str_popf(str* s, int len){
- memmove(s->c, s->c + len, s->len -= len);
- s->c[s->len] = 0;
-}
-
-void str_popb(str* s, int len){
- s->len -= len;
- s->c[s->len] = 0;
-}
-
-
+#include "str.h"
+#include "../lua.h"
+#include "../util.h"
+
+#define alloc_buffer 64
+
+str* str_initl(char* init, size_t len){
+ if(init == NULL){
+ char cc = '\0';
+ init = &cc;
+ }
+
+ str* s = malloc(sizeof * s);
+ s->_bytes = len + 1 + alloc_buffer;
+ s->c = malloc(s->_bytes);
+ if(s->c == NULL) p_fatal("failed to allocate string\n");
+ s->len = len ;
+
+ memcpy(s->c, init, len + 1);
+ return s;
+}
+
+str* str_init(char* init){
+ return str_initl(init, strlen(init));
+}
+
+void str_free(str* s){
+ free(s->c);
+ return free(s);
+}
+
+void str_push(str* s, char* insert){
+ s->len += strlen(insert);
+ if(s->len + 1 >= s->_bytes)
+ s->c = realloc(s->c, s->_bytes = s->len + 1 + alloc_buffer);
+ strcat(s->c, insert);
+}
+
+void str_pushl(str* s, char* insert, size_t l){
+ if(s->len + l >= s->_bytes)
+ s->c = realloc(s->c, s->_bytes = s->len + l + alloc_buffer);
+ //strcat(s->c, insert);
+ for(int i = 0; i != l; i++){
+ s->c[i + s->len] = insert[i];
+ }
+ s->len += l;
+ s->c[s->len] = '\0';
+}
+
+void str_clear(str* s){
+ memset(s->c, 0, s->len);
+
+ s->len = 0;
+}
+
+void str_popf(str* s, int len){
+ memmove(s->c, s->c + len, s->len -= len);
+ s->c[s->len] = 0;
+}
+
+void str_popb(str* s, int len){
+ s->len -= len;
+ s->c[s->len] = 0;
+}
+
+