summaryrefslogtreecommitdiff
path: root/src/ht.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/ht.h')
-rw-r--r--src/ht.h72
1 files changed, 56 insertions, 16 deletions
diff --git a/src/ht.h b/src/ht.h
index e5d16f7..617fd94 100644
--- a/src/ht.h
+++ b/src/ht.h
@@ -2,7 +2,7 @@
#define HT_H
#include "util.h"
-enum { HT_INIT_CAP = 257 };
+enum { HT_INIT_CAP = 256 };
/* TODO: Modify the probing algorithm. Imitate the algorithm used by
python's hash tables. The source code of the python dict object
@@ -21,51 +21,91 @@ enum { HT_INIT_CAP = 257 };
know if two keys are "equal". */
/* type for a hash function */
-typedef NUM (*hash_func_t) (void *key);
+typedef NUM (*hash_func_t) (CCR_MOD(void *)key);
/* comparison function type */
-typedef BOOL (*compare_func_t) (void *keya, void *keyb);
+typedef BOOL (*compare_func_t)
+(CCR_MOD(void *)keya, CCR_MOD(void *)keyb);
struct ht_s {
- NUM *keys;
+ void **keys;
void **values;
NUM *indices;
UNUM capability;
/* REVIEW: What is the purpose of SIZE? */
/* ANSWER: For looping over keys / values. */
int size;
- /* hash_func_t hf;
- * compare_func_t cf; */
+ hash_func_t hf;
+ compare_func_t cf;
};
typedef struct ht_s ht;
/* On error return NULL */
-ht *new_ht(UNUM size);
-
+ht *new_ht(UNUM size, int nargs, ...);
+
+/* This enum empowers the user the ability to control how to free the
+ hash table. The destroy function always frees keys, values, and
+ indices pointers.
+
+ If the option contains the word "SELF", it means to also free the
+ hash table pointer itself. Otherwise, it contains NO_SELF, and it
+ means not to free the hash table pointer.
+
+ If it contains the word "KEY" (respectively "VALUE"), then it means
+ to also free the pointers stored in the keys (respectively values)
+ array. How to free the pointers? If the option contains the word
+ "FIRST", then it means to free the first pointer stored in the
+ array corresponding to the word before "FIRST". And it frees each
+ and every of the other word's pointers. To free both keys and
+ values pointers in the same manner, i.e. free both of the first
+ pointers or free each and every pointers, use the option containing
+ "EVERY" or "EVERY_FIRST" respectively. */
enum HT_DESTROY_FLAG_e {
DESTROY_NONE_NO_SELF,
DESTROY_NONE_SELF,
- DESTROY_EVERY_NO_SELF,
+ DESTROY_KEY_SELF,
+ DESTROY_KEY_FIRST_SELF,
+ DESTROY_KEY_NO_SELF,
+ DESTROY_KEY_FIRST_NO_SELF,
+ DESTROY_VALUE_SELF,
+ DESTROY_VALUE_FIRST_SELF,
+ DESTROY_VALUE_NO_SELF,
+ DESTROY_VALUE_FIRST_NO_SELF,
DESTROY_EVERY_SELF,
- DESTROY_FIRST_NO_SELF,
- DESTROY_FIRST_SELF
+ DESTROY_EVERY_FIRST_SELF,
+ DESTROY_EVERY_NO_SELF,
+ DESTROY_EVERY_FIRST_NO_SELF,
+ DESTROY_KEY_FIRST_VALUE_SELF,
+ DESTROY_KEY_FIRST_VALUE_NO_SELF,
+ DESTROY_KEY_VALUE_FIRST_SELF,
+ DESTROY_KEY_VALUE_FIRST_NO_SELF
};
typedef enum HT_DESTROY_FLAG_e HT_DESTROY_FLAG;
+
void destroy_ht(ht * restrict htp, HT_DESTROY_FLAG flag);
-BOOL ht_insert(ht *htp, NUM key, void *value);
+BOOL ht_insert(ht * const restrict htp, void *key, void *value);
P_ATTR NUM ht_size(CCR_MOD(ht *) htp);
P_ATTR void **ht_values(CCR_MOD(ht *) htp);
-P_ATTR NUM *ht_keys(CCR_MOD(ht *) htp);
+P_ATTR void **ht_keys(CCR_MOD(ht *) htp);
+
+enum HT_DELETE_FLAG_e {
+ DELETE_NONE,
+ DELETE_KEY,
+ DELETE_VALUE,
+ DELETE_EVERY
+};
+
+typedef enum HT_DELETE_FLAG_e HT_DELETE_FLAG;
-/* If FLAG is non-zero, also free the value pointer. */
-BOOL ht_delete(ht * const restrict htp, NUM key, int flag);
+BOOL ht_delete(ht * const restrict htp, void *key,
+ HT_DELETE_FLAG flag);
-P_ATTR void *ht_find(CCR_MOD(ht *) htp, NUM key);
+P_ATTR void *ht_find(CCR_MOD(ht *) htp, void *key);
#endif