aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--HashTable.c64
-rw-r--r--HashTable.h37
-rw-r--r--String.c30
-rw-r--r--String.h17
-rw-r--r--Types.h22
-rw-r--r--Vector.c70
-rw-r--r--Vector.h19
7 files changed, 171 insertions, 88 deletions
diff --git a/HashTable.c b/HashTable.c
index ed49ee7..0fed138 100644
--- a/HashTable.c
+++ b/HashTable.c
@@ -3,6 +3,8 @@
#include <stdio.h>
#include <string.h>
+/** Hashing function **/
+
// Fowler/Noll/Vo 32-bit hash implementation
// http://www.isthe.com/chongo/tech/comp/fnv/index.html
// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1_hash
@@ -20,6 +22,8 @@ static fnv_int fnv1_hash(const char* str) {
return hash;
}
+/** Metadata **/
+
struct HashTableMeta {
MemoryManager *mm;
usize size;
@@ -31,6 +35,8 @@ H_metadata(HashTable ht) {
return ht - sizeof(struct HashTableMeta);
}
+/** Helpers **/
+
static inline const char**
H_keys(HashTable ht) {
return ht;
@@ -64,6 +70,8 @@ H_find_hash(HashTable ht, const char *key, fnv_int *hash) {
return R_NotFound;
}
+/* Create */
+
HashTable
H_new(MemoryManager *mm, usize size) {
if (NULL == mm)
@@ -89,6 +97,8 @@ H_new(MemoryManager *mm, usize size) {
return start;
}
+/* Free */
+
Result
H_free(HashTable* ht) {
if (NULL == ht || NULL == *ht)
@@ -106,20 +116,6 @@ H_free(HashTable* ht) {
}
Result
-H_freevals(HashTable* ht) {
- if (NULL == ht || NULL == *ht)
- return R_NullArgument;
-
- struct HashTableMeta *htm = H_metadata(*ht);
-
- for (usize i = 0; i < htm->size; ++i) {
- htm->mm->free(*htm->mm, (void**)(H_vals(*ht) + i));
- }
-
- return R_Ok;
-}
-
-Result
H_freeat(HashTable ht, const char* key) {
if (NULL == ht || NULL == key)
return R_NullArgument;
@@ -136,20 +132,22 @@ H_freeat(HashTable ht, const char* key) {
return R_Ok;
}
-usize
-H_size(HashTable ht) {
- if (NULL == ht)
- return 0;
- return H_metadata(ht)->size;
-}
+Result
+H_freevals(HashTable* ht) {
+ if (NULL == ht || NULL == *ht)
+ return R_NullArgument;
-usize
-H_count(HashTable ht) {
- if (NULL == ht)
- return 0;
- return H_metadata(ht)->count;
+ struct HashTableMeta *htm = H_metadata(*ht);
+
+ for (usize i = 0; i < htm->size; ++i) {
+ htm->mm->free(*htm->mm, (void**)(H_vals(*ht) + i));
+ }
+
+ return R_Ok;
}
+/* Modify */
+
Result
H_insert(HashTable ht, const char *key, void *value) {
if (NULL == ht || NULL == key)
@@ -229,6 +227,22 @@ H_insertf(HashTable ht, const char *key, double value) {
return r;
}
+/* Get */
+
+usize
+H_size(HashTable ht) {
+ if (NULL == ht)
+ return 0;
+ return H_metadata(ht)->size;
+}
+
+usize
+H_count(HashTable ht) {
+ if (NULL == ht)
+ return 0;
+ return H_metadata(ht)->count;
+}
+
void*
H_get(HashTable ht, const char* key) {
if (NULL == ht || NULL == key)
diff --git a/HashTable.h b/HashTable.h
index d32f890..f20e951 100644
--- a/HashTable.h
+++ b/HashTable.h
@@ -6,19 +6,30 @@
typedef void* HashTable;
+/* Create */
+
HashTable H_new(MemoryManager *mm, usize size);
-Result H_free(HashTable* ht);
-Result H_freevals(HashTable* ht);
-Result H_freeat(HashTable ht, const char *key);
-usize H_size(HashTable ht);
-usize H_count(HashTable ht);
-Result H_insert(HashTable ht, const char *key, void *value);
-Result H_insertstr(HashTable ht, const char *key, const char *value);
-Result H_inserti(HashTable ht, const char *key, int value);
-Result H_insertf(HashTable ht, const char *key, double value);
-void* H_get(HashTable ht, const char* key);
-char* H_getstr(HashTable ht, const char* key);
-int* H_geti(HashTable ht, const char* key);
-double* H_getf(HashTable ht, const char* key);
+
+/* Free */
+
+Result H_free(HashTable* ht);
+Result H_freeat(HashTable ht, const char *key);
+Result H_freevals(HashTable* ht);
+
+/* Modify */
+
+Result H_insert(HashTable ht, const char *key, void *value);
+Result H_insertstr(HashTable ht, const char *key, const char *value);
+Result H_inserti(HashTable ht, const char *key, int value);
+Result H_insertf(HashTable ht, const char *key, double value);
+
+/* Get */
+
+usize H_size(HashTable ht);
+usize H_count(HashTable ht);
+void* H_get(HashTable ht, const char* key);
+char* H_getstr(HashTable ht, const char* key);
+int* H_geti(HashTable ht, const char* key);
+double* H_getf(HashTable ht, const char* key);
#endif /* _HASHTABLE */
diff --git a/String.c b/String.c
index ffcf687..fb003d8 100644
--- a/String.c
+++ b/String.c
@@ -3,6 +3,8 @@
#include <stdio.h>
#include <string.h>
+/** Metadata **/
+
struct StringMeta {
MemoryManager *mm;
usize length;
@@ -13,6 +15,8 @@ S_metadata(String s) {
return (struct StringMeta*)(s - sizeof(struct StringMeta));
}
+/** Helpers **/
+
static inline void*
S_start(MemoryManager *mm, usize length) {
if (NULL == mm)
@@ -34,6 +38,8 @@ S_start(MemoryManager *mm, usize length) {
return start + sizeof(sm);
}
+/* Create */
+
String
S_new(MemoryManager *mm, const char *str) {
if (NULL == str)
@@ -149,13 +155,20 @@ S_printf(MemoryManager *mm, const char *fmt, ...) {
return result;
}
-usize
-S_length(String s) {
+/* Free */
+
+Result
+S_free(String *s) {
if (NULL == s)
- return 0;
- return S_metadata(s)->length;
+ return R_NullArgument;
+
+ struct StringMeta *sm = S_metadata(*s);
+ *s = (char*)sm;
+ return sm->mm->free(*sm->mm, (void*)s);
}
+/* Modify */
+
static inline Result
S_resize(String *s, usize length) {
if (NULL == s || NULL == *s)
@@ -207,6 +220,15 @@ S_prepend(String *s, const char *str) {
return R_Ok;
}
+/* Get */
+
+usize
+S_length(String s) {
+ if (NULL == s)
+ return 0;
+ return S_metadata(s)->length;
+}
+
usize
S_find(String s, char c) {
if (NULL == s)
diff --git a/String.h b/String.h
index 16f1990..40d0e55 100644
--- a/String.h
+++ b/String.h
@@ -6,18 +6,29 @@
typedef char* String;
+/* Create */
+
String S_new(MemoryManager *mm, const char* str);
String S_newlen(MemoryManager *mm, usize length);
String S_dup(MemoryManager *mm, String s);
+
String S_vconcat(MemoryManager *mm, va_list args);
String S_concat(MemoryManager *mm, ...);
String S_vprintf(MemoryManager *mm, const char* fmt, va_list args);
String S_printf(MemoryManager *mm, const char* fmt, ...);
-usize S_length(String s);
+/* Free */
+
+Result S_free(String *s);
+
+/* Modify */
+
Result S_append(String *s, const char* str);
Result S_prepend(String *s, const char* str);
-usize S_find(String s, char c);
-Result S_free(String s);
+
+/* Get */
+
+usize S_length(String s);
+usize S_find(String s, char c);
#endif /* _STRING */
diff --git a/Types.h b/Types.h
index f12a52e..a9742fc 100644
--- a/Types.h
+++ b/Types.h
@@ -4,16 +4,25 @@
#include "stdint.h"
#include "stddef.h"
-typedef int_fast8_t i8;
-typedef int_fast16_t i16;
-typedef int_fast32_t i32;
-typedef int_fast64_t i64;
+/* Numeric */
+
+typedef int_fast8_t i8;
+typedef int_fast16_t i16;
+typedef int_fast32_t i32;
+typedef int_fast64_t i64;
typedef uint_fast8_t u8;
typedef uint_fast16_t u16;
typedef uint_fast32_t u32;
typedef uint_fast64_t u64;
typedef size_t usize;
+#define B(x) ((x) * 8)
+#define KiB(x) ((x) * 1024)
+#define MiB(x) ((x) * 1048576)
+#define GiB(x) ((x) * 1073741824)
+
+/* Result */
+
typedef enum Result {
/* Generic */
R_Ok = 0,
@@ -63,9 +72,4 @@ const static char* Result_TO_STR[R_PRIVATE+1] = {
#define RETURN_NOTOK(x) { Result r = x; if (R_Ok != r) return r; }
#define PRINT_NOTOK(x) { Result r = x; if (R_Ok != r) fprintf(stderr, "%s:%d %s() Error: %s (%d)\n", __FILE__, __LINE__, __func__, Result_TO_STR[r], r); }
-#define B(x) ((x) * 8)
-#define KiB(x) ((x) * 1024)
-#define MiB(x) ((x) * 1048576)
-#define GiB(x) ((x) * 1073741824)
-
#endif /* _TYPES */
diff --git a/Vector.c b/Vector.c
index 2236c64..d2906d8 100644
--- a/Vector.c
+++ b/Vector.c
@@ -2,6 +2,8 @@
#include "Types.h"
#include <string.h>
+/** Metadata **/
+
struct VectorMeta {
MemoryManager *mm;
usize element_size;
@@ -14,6 +16,8 @@ V_metadata(void *arr) {
return arr - sizeof(struct VectorMeta);
}
+/* Create */
+
Vector
V_new(MemoryManager *mm, usize element_size, usize length) {
if (NULL == mm)
@@ -37,33 +41,20 @@ V_new(MemoryManager *mm, usize element_size, usize length) {
return start + sizeof(vm);
}
-usize
-V_length(Vector arr) {
- if (NULL == arr)
- return 0;
- return V_metadata(arr)->length;
-}
+/* Free */
-usize
-V_count(Vector arr) {
- if (NULL == arr)
- return 0;
- return V_metadata(arr)->count;
-}
-
-void*
-V_top(Vector arr) {
- if (NULL == arr)
- return NULL;
-
- struct VectorMeta *vm = V_metadata(arr);
-
- if (0 == vm->count)
- return NULL;
+Result
+V_free(Vector *arr) {
+ if (NULL == arr || NULL == *arr)
+ return R_NullArgument;
- return arr + (vm->count - 1) * vm->element_size;
+ struct VectorMeta *vm = V_metadata(*arr);
+ *arr = vm;
+ return vm->mm->free(*vm->mm, arr);
}
+/* Modify */
+
Result
V_push(Vector *arr, void *pvalue) {
if (NULL == arr || NULL == *arr)
@@ -106,12 +97,31 @@ V_pop(Vector *arr) {
return R_Ok;
}
-Result
-V_free(Vector *arr) {
- if (NULL == arr || NULL == *arr)
- return R_NullArgument;
+/* Get */
- struct VectorMeta *vm = V_metadata(*arr);
- *arr = vm;
- return vm->mm->free(*vm->mm, arr);
+usize
+V_length(Vector arr) {
+ if (NULL == arr)
+ return 0;
+ return V_metadata(arr)->length;
+}
+
+usize
+V_count(Vector arr) {
+ if (NULL == arr)
+ return 0;
+ return V_metadata(arr)->count;
+}
+
+void*
+V_top(Vector arr) {
+ if (NULL == arr)
+ return NULL;
+
+ struct VectorMeta *vm = V_metadata(arr);
+
+ if (0 == vm->count)
+ return NULL;
+
+ return arr + (vm->count - 1) * vm->element_size;
}
diff --git a/Vector.h b/Vector.h
index ae82c23..6f12bff 100644
--- a/Vector.h
+++ b/Vector.h
@@ -6,13 +6,24 @@
typedef void* Vector;
+/* Create */
+
Vector V_new(MemoryManager *mm, usize element_size, usize length);
-usize V_length(Vector arr);
-usize V_count(Vector arr);
-void* V_top(Vector arr);
+
+/* Free */
+
+Result V_free(Vector *arr);
+
+/* Modify */
+
Result V_push(Vector *arr, void *pointer_to_value);
Result V_pushp(Vector* *arr, void *value);
Result V_pop(Vector *arr);
-Result V_free(Vector *arr);
+
+/* Get */
+
+usize V_length(Vector arr);
+usize V_count(Vector arr);
+void* V_top(Vector arr);
#endif /* _VECTOR */