| 1 |
|
| #ifndef TXS_HASH_H_ |
| 2 |
|
| #define TXS_HASH_H_ |
| 3 |
|
| /* |
| 4 |
|
| Hash buckets will be in the following format: |
| 5 |
|
| [int next_offset; char *data; ], .... |
| 6 |
|
| */ |
| 7 |
|
| |
| 8 |
|
| #define txs_hash2slot(hash, n_buckets) \ |
| 9 |
|
| hash % (n_buckets-1) |
| 10 |
|
| |
| 11 |
|
| struct TXS_HashTable { |
| 12 |
|
| /*How many buckets*/ |
| 13 |
|
| int n_buckets; |
| 14 |
|
| char **buckets; |
| 15 |
|
| |
| 16 |
|
| int slot_min; |
| 17 |
|
| int slot_max; |
| 18 |
|
| }; |
| 19 |
|
| |
| 20 |
|
| typedef unsigned int txs_ht_off_t; |
| 21 |
|
| |
| 22 |
113 |
| static inline uint32_t mkhash(const char *str, int len) |
| 23 |
|
| { |
| 24 |
113 |
| register uint32_t hash = 5381; |
| 25 |
|
| |
| 26 |
143 |
▶ | for(len; len > 8; len -= 8, str += 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(len; len > 8; len -= 8, str += 8) { |
30 |
113 |
143 |
|
| 27 |
30 |
| hash ^= *(uint32_t*)(str); |
| 28 |
30 |
| hash ^= *(uint32_t*)(str+4); |
| 29 |
|
| } |
| 30 |
|
| |
| 31 |
158 |
▶ | for(len; len > 4; len -= 4, str += 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(len; len > 4; len -= 4, str += 4) { |
45 |
113 |
158 |
|
| 32 |
45 |
| hash ^= *(uint32_t*)(str); |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
164 |
▶ | for(len; len > 2; len -=2, str += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(len; len > 2; len -=2, str += 2) { |
51 |
113 |
164 |
|
| 36 |
51 |
| hash = hash * (33 + str[0]); |
| 37 |
51 |
| hash = hash * (33 + str[1]); |
| 38 |
|
| } |
| 39 |
|
| |
| 40 |
281 |
▶ | for(len; len; len--, str++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(len; len; len--, str++) { |
168 |
113 |
281 |
|
| 41 |
168 |
| hash = hash * (33 + *str); |
| 42 |
|
| } |
| 43 |
113 |
| return hash; |
| 44 |
|
| } |
| 45 |
|
| |
| 46 |
|
| #define QUAL_COUNT 8 |
| 47 |
|
| |
| 48 |
|
| static inline struct TXS_HashTable* |
| 49 |
6 |
| THX_txs_ht_build(pTHX_ HV *perl_hv) |
| 50 |
|
| #define txs_ht_build(hv) THX_txs_ht_build(aTHX_ hv) |
| 51 |
|
| { |
| 52 |
6 |
| uint32_t n_buckets = 1; |
| 53 |
19 |
▶ | while(n_buckets < HvKEYS(perl_hv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| while(n_buckets < HvKEYS(perl_hv)) { |
0 |
19 |
19 |
| while(n_buckets < HvKEYS(perl_hv)) { |
13 |
6 |
19 |
|
| 54 |
13 |
| n_buckets <<= 1; |
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
|
| int retlen; |
| 58 |
|
| uint32_t hash; |
| 59 |
|
| HE *entry; |
| 60 |
|
| char *str; |
| 61 |
|
| int i; |
| 62 |
6 |
| uint32_t slot = 0; |
| 63 |
|
| |
| 64 |
|
| char **buckets; |
| 65 |
|
| char *slotp; |
| 66 |
|
| int *bucket_sizes; |
| 67 |
|
| |
| 68 |
|
| struct TXS_HashTable *ret; |
| 69 |
6 |
| Newx(ret, 1, struct TXS_HashTable); |
| 70 |
|
| |
| 71 |
6 |
| ret->slot_min = n_buckets; |
| 72 |
6 |
| ret->slot_max = 0; |
| 73 |
|
| |
| 74 |
6 |
| Newxz(buckets, n_buckets, char*); |
| 75 |
6 |
| Newxz(bucket_sizes, n_buckets, int); |
| 76 |
|
| |
| 77 |
|
| |
| 78 |
6 |
| hv_iterinit(perl_hv); |
| 79 |
27 |
▶ | while(entry = hv_iternext(perl_hv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(entry = hv_iternext(perl_hv)) { |
21 |
6 |
27 |
|
| 80 |
|
| |
| 81 |
21 |
| str = hv_iterkey(entry, &retlen); |
| 82 |
|
| |
| 83 |
21 |
| slot = txs_hash2slot(mkhash(str, retlen), n_buckets); |
| 84 |
21 |
▶ | if(ret->slot_min > slot) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ret->slot_min > slot) { |
10 |
11 |
21 |
|
| 85 |
10 |
| ret->slot_min = slot; |
| 86 |
|
| } |
| 87 |
21 |
▶ | if(ret->slot_max < slot) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ret->slot_max < slot) { |
8 |
13 |
21 |
|
| 88 |
8 |
| ret->slot_max = slot; |
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
21 |
| bucket_sizes[slot] += (sizeof(txs_ht_off_t)); |
| 92 |
21 |
| bucket_sizes[slot] += retlen; |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
|
| /*Now, allocate the buckets*/ |
| 96 |
34 |
▶ | for(i = 0; i < n_buckets; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < n_buckets; i++) { |
28 |
6 |
34 |
|
| 97 |
28 |
▶ | if(!bucket_sizes[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(!bucket_sizes[i]) { |
15 |
13 |
28 |
|
| 98 |
15 |
| continue; |
| 99 |
|
| } |
| 100 |
13 |
| Newxz(buckets[i], bucket_sizes[i]+sizeof(int), char); |
| 101 |
|
| } |
| 102 |
|
| |
| 103 |
|
| /*Now, fill the buckets, again*/ |
| 104 |
6 |
| hv_iterinit(perl_hv); |
| 105 |
27 |
▶ | while (entry = hv_iternext(perl_hv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (entry = hv_iternext(perl_hv)) { |
21 |
6 |
27 |
|
| 106 |
21 |
| str = hv_iterkey(entry, &retlen); |
| 107 |
|
| |
| 108 |
21 |
| slot = txs_hash2slot(mkhash(str, retlen), n_buckets); |
| 109 |
|
| |
| 110 |
21 |
| slotp = buckets[slot]; |
| 111 |
31 |
▶ | while(*slotp) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(*slotp) { |
10 |
21 |
31 |
|
| 112 |
10 |
| slotp += *(txs_ht_off_t*)(slotp) + sizeof(txs_ht_off_t); |
| 113 |
|
| } |
| 114 |
21 |
| *(txs_ht_off_t*)(slotp) = retlen; |
| 115 |
21 |
| slotp += sizeof(txs_ht_off_t); |
| 116 |
21 |
| memcpy(slotp, str, retlen); |
| 117 |
|
| } |
| 118 |
|
| |
| 119 |
6 |
| Safefree(bucket_sizes); |
| 120 |
|
| |
| 121 |
|
| |
| 122 |
6 |
| ret->n_buckets = n_buckets; |
| 123 |
6 |
| ret->buckets = buckets; |
| 124 |
|
| |
| 125 |
6 |
| return ret; |
| 126 |
|
| } |
| 127 |
|
| |
| 128 |
|
| static inline int |
| 129 |
71 |
| txs_ht_check(const struct TXS_HashTable *ht, const char *str, int len) |
| 130 |
|
| { |
| 131 |
|
| register char *slotp; |
| 132 |
71 |
| register uint32_t slot = txs_hash2slot(mkhash(str, len), ht->n_buckets); |
| 133 |
|
| |
| 134 |
71 |
▶ | if(ht->buckets[slot] == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ht->buckets[slot] == NULL) { |
18 |
53 |
71 |
|
| 135 |
18 |
| return 0; |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
109 |
▶ | for(slotp = ht->buckets[slot]; *(txs_ht_off_t*)slotp; |
Branch: 1 branch
| Branch | True | False | Total |
| for(slotp = ht->buckets[slot]; *(txs_ht_off_t*)slotp; |
88 |
21 |
109 |
|
| 139 |
56 |
| slotp += (sizeof(txs_ht_off_t)) + *(txs_ht_off_t*)slotp) { |
| 140 |
|
| |
| 141 |
88 |
▶ | if(*(txs_ht_off_t*)(slotp) != len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(*(txs_ht_off_t*)(slotp) != len) { |
39 |
49 |
88 |
|
| 142 |
39 |
| continue; |
| 143 |
|
| } |
| 144 |
|
| |
| 145 |
49 |
▶ | if(memcmp(slotp+sizeof(txs_ht_off_t), str, len) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(memcmp(slotp+sizeof(txs_ht_off_t), str, len) == 0) { |
32 |
17 |
49 |
|
| 146 |
32 |
| return 1; |
| 147 |
|
| } |
| 148 |
|
| } |
| 149 |
21 |
| return 0; |
| 150 |
|
| } |
| 151 |
|
| |
| 152 |
|
| static inline void |
| 153 |
6 |
| txs_ht_free(struct TXS_HashTable *ht) |
| 154 |
|
| { |
| 155 |
|
| int i; |
| 156 |
34 |
▶ | for(i = 0; i < ht->n_buckets; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < ht->n_buckets; i++) { |
28 |
6 |
34 |
|
| 157 |
28 |
▶ | if(ht->buckets[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ht->buckets[i]) { |
13 |
15 |
28 |
|
| 158 |
13 |
| Safefree(ht->buckets[i]); |
| 159 |
|
| } |
| 160 |
|
| } |
| 161 |
6 |
| Safefree(ht->buckets); |
| 162 |
6 |
| Safefree(ht); |
| 163 |
6 |
| } |
| 164 |
|
| |
| 165 |
0 |
| txs_ht_dump_stats(struct TXS_HashTable *ht) |
| 166 |
|
| { |
| 167 |
|
| int i; |
| 168 |
0 |
| int tmp_count = 0; |
| 169 |
|
| char *slotp; |
| 170 |
0 |
| int bucket_stats[QUAL_COUNT] = { 0 }; |
| 171 |
|
| |
| 172 |
0 |
▶ | for(i = 0; i < ht->n_buckets; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < ht->n_buckets; i++) { |
0 |
0 |
0 |
|
| 173 |
0 |
▶ | if(!ht->buckets[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(!ht->buckets[i]) { |
0 |
0 |
0 |
|
| 174 |
0 |
| continue; |
| 175 |
|
| } |
| 176 |
|
| |
| 177 |
0 |
| for( |
| 178 |
0 |
| tmp_count = 0, slotp = ht->buckets[i]; |
| 179 |
0 |
▶ | *slotp; |
Branch: 1 branch
| Branch | True | False | Total |
| *slotp; |
0 |
0 |
0 |
|
| 180 |
0 |
| tmp_count++, slotp += sizeof(txs_ht_off_t) + *(txs_ht_off_t*)slotp |
| 181 |
|
| ); |
| 182 |
0 |
▶ | if(!tmp_count) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(!tmp_count) { |
0 |
0 |
0 |
|
| 183 |
0 |
| continue; /*Eh?*/ |
| 184 |
|
| } |
| 185 |
|
| |
| 186 |
0 |
▶ | if(tmp_count >= QUAL_COUNT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(tmp_count >= QUAL_COUNT) { |
0 |
0 |
0 |
|
| 187 |
0 |
| bucket_stats[QUAL_COUNT-1]++; |
| 188 |
|
| } else { |
| 189 |
0 |
| bucket_stats[tmp_count-1]++; |
| 190 |
|
| } |
| 191 |
|
| } |
| 192 |
|
| |
| 193 |
0 |
▶ | for(i = 0; i < QUAL_COUNT; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < QUAL_COUNT; i++) { |
0 |
0 |
0 |
|
| 194 |
0 |
| printf("[%d: %d] ", i+1, bucket_stats[i]); |
| 195 |
|
| } |
| 196 |
0 |
| printf("\n"); |
| 197 |
0 |
| } |
| 198 |
|
| |
| 199 |
|
| #endif /*TXS_HASH_H_*/ |