summaryrefslogtreecommitdiff
path: root/src/ht.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/ht.h')
-rw-r--r--src/ht.h59
1 files changed, 51 insertions, 8 deletions
diff --git a/src/ht.h b/src/ht.h
index 617fd94..4ac0c5d 100644
--- a/src/ht.h
+++ b/src/ht.h
@@ -2,17 +2,16 @@
#define HT_H
#include "util.h"
-enum { HT_INIT_CAP = 256 };
+enum { HT_INIT_CAP = 1 << 8 };
-/* TODO: Modify the probing algorithm. Imitate the algorithm used by
- python's hash tables. The source code of the python dict object
- can be found in the following URL:
+/* The algorithm here imitates the algorithm used by python's hash
+ tables. The source code of the python dict object can be found in
+ the following URL:
<https://hg.python.org/cpython/file/52f68c95e025/Objects/dictobject.c#l33> */
-/* TODO: Generalize the type of the keys, so that we can use anything
- as the key. The type of the keys should satisfy the following
- conditions.
+/* We can use anything as the key, except that the type of the keys
+ should satisfy the following conditions.
- Hash-able: We must know how to produce the hash value of a
key.
@@ -27,6 +26,7 @@ typedef NUM (*hash_func_t) (CCR_MOD(void *)key);
typedef BOOL (*compare_func_t)
(CCR_MOD(void *)keya, CCR_MOD(void *)keyb);
+/* FIXME: Hide this struct from the header file. */
struct ht_s {
void **keys;
void **values;
@@ -60,7 +60,7 @@ ht *new_ht(UNUM size, int nargs, ...);
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. */
+ "EVERY_FIRST" or "EVERY" respectively. */
enum HT_DESTROY_FLAG_e {
DESTROY_NONE_NO_SELF,
DESTROY_NONE_SELF,
@@ -108,4 +108,47 @@ BOOL ht_delete(ht * const restrict htp, void *key,
P_ATTR void *ht_find(CCR_MOD(ht *) htp, void *key);
+/* Pairing hash tables */
+
+struct pair2_s { NUM x; NUM y; };
+
+typedef struct pair2_s pair2;
+
+/* On error return NULL */
+ht *new_ht2(UNUM size);
+
+struct pair3_s { NUM x; NUM y; NUM z; };
+
+typedef struct pair3_s pair3;
+
+/* On error return NULL */
+ht *new_ht3(UNUM size);
+
+struct pair4_s { NUM x; NUM y; NUM z; NUM w; };
+
+typedef struct pair4_s pair4;
+
+ht *new_ht4(UNUM size);
+
+#define NEW_P2(P, X, Y, CLEAN) do { \
+ SAFE_MALLOC(pair2, P, 1, CLEAN); \
+ P->x = X; \
+ P->y = Y; \
+ } while (0)
+
+#define NEW_P3(P, X, Y, Z, CLEAN) do { \
+ SAFE_MALLOC(pair3, P, 1, CLEAN); \
+ P->x = X; \
+ P->y = Y; \
+ P->z = Z; \
+ } while (0)
+
+#define NEW_P4(P, X, Y, Z, W, CLEAN) do { \
+ SAFE_MALLOC(pair4, P, 1, CLEAN); \
+ P->x = X; \
+ P->y = Y; \
+ P->z = Z; \
+ P->w = W; \
+ } while (0)
+
#endif