summaryrefslogtreecommitdiff
path: root/src/ht.c
blob: 923eca78688f62a747e190ba97d6b61e374bc4dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <string.h>
#include <stdio.h>
#include "ht.h"

#define HT_REHASH_THRESHOLD 0.5

struct ht_s {
  NUM *keys;
  void **values;
  UNUM capability;
  /* REVIEW: What is the purpose of SIZE? */
  int size;
};

/* SIZE is a hint to the number of elements that might be put in the
   table.  In fact we will allocate more memory than this number, as a
   hash table works better if it is not fully used. */
ht *
new_ht(UNUM size)
{
  ht *htp = MYALLOC(ht, 1);

  if (htp == NULL) {
    fleprintf0("Fail to malloc\n");
    return NULL;
  }

  htp->capability = (size<<1)+(size>>1);
  htp->size = 0;
  htp->keys = MYALLOC(NUM, htp->capability);

  if (htp->keys == NULL) {
    fleprintf0("Fail to malloc\n");
    free(htp);
    return NULL;
  }

  htp->values = MYALLOC(void*, htp->capability);

  if (htp->values == NULL) {
    fleprintf0("Fail to malloc\n");
    free(htp->keys);
    free(htp);
    return NULL;
  }

  /* Initialize the keys to be -1 and the values to be NULL */
  memset(htp->keys, 0xff, sizeof (NUM) * htp->capability);
  memset(htp->values, 0, sizeof (NUM) * htp->capability);

  return htp;
}

void
destroy_ht(ht * restrict htp, int flag)
{
  switch (flag) {
  case 1:
    for (UNUM i = 0; i < htp->capability;)
      free(*(htp->values+i++));
    break;
  case 2:
    free(*(htp->values));
    break;
  default:
    break;
  }

  free(htp->values);

  free(htp->keys);

  free(htp);
}

static BOOL
ht_expand(ht *htp)
{
  NUM oldcap = htp->capability;
  htp->capability <<= 1;
  htp->capability++;

  NUM *newkeys = realloc(htp->keys, htp->capability);

  if (newkeys == NULL) {
    fleprintf0("Fail to realloc\n");
    return 1;
  }

  htp->keys = newkeys;

  memset(htp->keys+oldcap, 0xff,
         sizeof (NUM) * (htp->capability - oldcap));

  void **newvalues = realloc(htp->values, htp->capability);

  if (newvalues == NULL) {
    fleprintf0("Fail to realloc\n");
    return 1;
  }

  htp->values = newvalues;

  memset(htp->values+oldcap, 0,
         sizeof(NUM) * (htp->capability - oldcap));

  return 0;
}

BOOL
ht_insert(ht *htp, NUM key, void *value)
{
  if (htp->size+1 >= HT_REHASH_THRESHOLD * htp->capability)
    ht_expand(htp);

  NUM ikey = key % htp->capability;

  /* linear probing */
  for (NUM count = 0;
       count < htp->capability * HT_REHASH_THRESHOLD &&
         *(htp->keys+ikey) > 0 &&
         *(htp->keys+ikey) != key;
       count++)
    ikey = (ikey + 1) % htp->capability;

  if (*(htp->keys+ikey) < 0) {
    /* not inserted before */
    *(htp->keys+ikey) = key;
    *(htp->values+ikey) = value;
    (htp->size)++;
  } else if (*(htp->keys+ikey) == key) {
    *(htp->values+ikey) = value;
  } else {
    fleprintf("Fail to probe an appropriate slot for %ld, "
              "the result ikey = %ld\n",
              key, ikey);
    return 1;
  }

  /* fleprintf("ikey = %ld, size = %d, capability = %llu\n",
   *           ikey, htp->size, htp->capability); */

  return 0;
}

BOOL
ht_delete(ht * const restrict htp, NUM key, int flag)
{
  NUM ikey = key % htp->capability;

  /* linear probing */
  for (NUM count = 0;
       count < htp->capability * HT_REHASH_THRESHOLD &&
         *(htp->keys+ikey) > 0 &&
         *(htp->keys+ikey) != key;
       count++)
    ikey = (ikey + 1) % htp->capability;

  if (*(htp->keys+ikey) < 0) {
    /* not inserted before */
    return 0;
  } else if (*(htp->keys+ikey) == key) {
    (htp->size)--;
    *(htp->keys+ikey) = -1;
    if (flag) {
      free(*(htp->values+ikey));
      *(htp->values+ikey) = NULL;
    }
    return 0;
  } else {
    fleprintf("Fail to probe an appropriate slot for %ld, "
              "the result ikey = %ld\n",
              key, ikey);
    return 1;
  }

  return 0;
}

void *
ht_find(ht * const restrict htp, NUM key)
{
  NUM ikey = key % htp->capability;

  /* linear probing */
  for (NUM count = 0;
       count < htp->capability * HT_REHASH_THRESHOLD &&
         *(htp->keys+ikey) > 0 &&
         *(htp->keys+ikey) != key;
       count++)
    ikey = (ikey + 1) % htp->capability;

  if (*(htp->keys+ikey) == key)
    return *(htp->values+ikey);
  else if (*(htp->keys+ikey) > 0)
    fleprintf("Fail to probe an appropriate slot for %ld, "
              "with the result ikey = %ld\n",
              key, ikey);

  return NULL;
}