Mercurial > repos > dawe > srf2fastq
annotate srf2fastq/io_lib-1.12.2/io_lib/hash_table.c @ 0:d901c9f41a6a default tip
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
author | dawe |
---|---|
date | Tue, 07 Jun 2011 17:48:05 -0400 |
parents | |
children |
rev | line source |
---|---|
0
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1 #ifdef HAVE_CONFIG_H |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
2 #include "io_lib_config.h" |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
3 #endif |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
4 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
5 #include <stdio.h> |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
6 #include <string.h> |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
7 #include <stdlib.h> |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
8 #include "io_lib/os.h" |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
9 #include "io_lib/hash_table.h" |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
10 #include "io_lib/jenkins_lookup3.h" |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
11 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
12 /* ========================================================================= |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
13 * TCL's hash function. Basically hash*9 + char. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
14 * ========================================================================= |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
15 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
16 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
17 uint32_t HashTcl(uint8_t *data, int len) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
18 uint32_t hash = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
19 int i; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
20 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
21 for (i = 0; i < len; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
22 hash += (hash<<3) + data[i]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
23 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
24 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
25 return hash; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
26 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
27 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
28 /* ========================================================================= |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
29 * Paul Hsieh's hash function |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
30 * http://www.azillionmonkeys.com/qed/hash.html |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
31 * ========================================================================= |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
32 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
33 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
34 #undef get16bits |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
35 #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
36 || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
37 #define get16bits(d) (*((const uint16_t *) (d))) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
38 #endif |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
39 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
40 #if !defined (get16bits) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
41 #define get16bits(d) ((((const uint8_t *)(d))[1] << 8UL)\ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
42 +((const uint8_t *)(d))[0]) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
43 #endif |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
44 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
45 uint32_t HashHsieh(uint8_t *data, int len) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
46 uint32_t hash = 0, tmp; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
47 int rem; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
48 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
49 if (len <= 0 || data == NULL) return 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
50 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
51 rem = len & 3; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
52 len >>= 2; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
53 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
54 /* Main loop */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
55 for (;len > 0; len--) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
56 hash += get16bits (data); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
57 tmp = (get16bits (data+2) << 11) ^ hash; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
58 hash = (hash << 16) ^ tmp; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
59 data += 2*sizeof (uint16_t); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
60 hash += hash >> 11; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
61 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
62 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
63 /* Handle end cases */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
64 switch (rem) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
65 case 3: hash += get16bits (data); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
66 hash ^= hash << 16; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
67 hash ^= data[sizeof (uint16_t)] << 18; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
68 hash += hash >> 11; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
69 break; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
70 case 2: hash += get16bits (data); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
71 hash ^= hash << 11; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
72 hash += hash >> 17; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
73 break; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
74 case 1: hash += *data; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
75 hash ^= hash << 10; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
76 hash += hash >> 1; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
77 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
78 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
79 /* Force "avalanching" of final 127 bits */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
80 hash ^= hash << 3; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
81 hash += hash >> 5; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
82 hash ^= hash << 2; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
83 hash += hash >> 15; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
84 hash ^= hash << 10; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
85 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
86 return hash; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
87 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
88 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
89 /* ========================================================================= |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
90 * Bob Jenkins' hash function |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
91 * http://burtleburtle.net/bob/hash/doobs.html |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
92 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
93 * See jenkins_lookup3.c for a new version of this that has good hash |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
94 * characteristics for a full 64-bit hash value. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
95 * ========================================================================= |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
96 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
97 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
98 #define hashsize(n) ((uint32_t)1<<(n)) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
99 #define hashmask(n) (hashsize(n)-1) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
100 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
101 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
102 -------------------------------------------------------------------- |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
103 mix -- mix 3 32-bit values reversibly. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
104 For every delta with one or two bits set, and the deltas of all three |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
105 high bits or all three low bits, whether the original value of a,b,c |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
106 is almost all zero or is uniformly distributed, |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
107 * If mix() is run forward or backward, at least 32 bits in a,b,c |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
108 have at least 1/4 probability of changing. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
109 * If mix() is run forward, every bit of c will change between 1/3 and |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
110 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
111 mix() was built out of 36 single-cycle latency instructions in a |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
112 structure that could supported 2x parallelism, like so: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
113 a -= b; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
114 a -= c; x = (c>>13); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
115 b -= c; a ^= x; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
116 b -= a; x = (a<<8); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
117 c -= a; b ^= x; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
118 c -= b; x = (b>>13); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
119 ... |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
120 Unfortunately, superscalar Pentiums and Sparcs can't take advantage |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
121 of that parallelism. They've also turned some of those single-cycle |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
122 latency instructions into multi-cycle latency instructions. Still, |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
123 this is the fastest good hash I could find. There were about 2^^68 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
124 to choose from. I only looked at a billion or so. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
125 -------------------------------------------------------------------- |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
126 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
127 #define mix(a,b,c) \ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
128 { \ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
129 a -= b; a -= c; a ^= (c>>13); \ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
130 b -= c; b -= a; b ^= (a<<8); \ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
131 c -= a; c -= b; c ^= (b>>13); \ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
132 a -= b; a -= c; a ^= (c>>12); \ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
133 b -= c; b -= a; b ^= (a<<16); \ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
134 c -= a; c -= b; c ^= (b>>5); \ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
135 a -= b; a -= c; a ^= (c>>3); \ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
136 b -= c; b -= a; b ^= (a<<10); \ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
137 c -= a; c -= b; c ^= (b>>15); \ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
138 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
139 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
140 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
141 -------------------------------------------------------------------- |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
142 hash() -- hash a variable-length key into a 32-bit value |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
143 k : the key (the unaligned variable-length array of bytes) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
144 len : the length of the key, counting by bytes |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
145 initval : can be any 4-byte value |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
146 Returns a 32-bit value. Every bit of the key affects every bit of |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
147 the return value. Every 1-bit and 2-bit delta achieves avalanche. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
148 About 6*len+35 instructions. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
149 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
150 The best hash table sizes are powers of 2. There is no need to do |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
151 mod a prime (mod is sooo slow!). If you need less than 32 bits, |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
152 use a bitmask. For example, if you need only 10 bits, do |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
153 h = (h & hashmask(10)); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
154 In which case, the hash table should have hashsize(10) elements. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
155 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
156 If you are hashing n strings (uint8_t **)k, do it like this: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
157 for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
158 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
159 By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
160 code any way you wish, private, educational, or commercial. It's free. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
161 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
162 See http://burtleburtle.net/bob/hash/evahash.html |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
163 Use for hash table lookup, or anything where one collision in 2^^32 is |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
164 acceptable. Do NOT use for cryptographic purposes. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
165 -------------------------------------------------------------------- |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
166 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
167 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
168 uint32_t HashJenkins(uint8_t *k, int length /*, uint32_t initval */) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
169 { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
170 register uint32_t a,b,c,len; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
171 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
172 /* Set up the internal state */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
173 len = length; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
174 a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
175 c = 0; /* initval; */ /* the previous hash value */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
176 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
177 /*---------------------------------------- handle most of the key */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
178 while (len >= 12) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
179 { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
180 a += (k[0] +((uint32_t)k[1]<<8) +((uint32_t)k[2]<<16) +((uint32_t)k[3]<<24)); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
181 b += (k[4] +((uint32_t)k[5]<<8) +((uint32_t)k[6]<<16) +((uint32_t)k[7]<<24)); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
182 c += (k[8] +((uint32_t)k[9]<<8) +((uint32_t)k[10]<<16)+((uint32_t)k[11]<<24)); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
183 mix(a,b,c); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
184 k += 12; len -= 12; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
185 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
186 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
187 /*------------------------------------- handle the last 11 bytes */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
188 c += length; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
189 switch(len) /* all the case statements fall through */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
190 { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
191 case 11: c+=((uint32_t)k[10]<<24); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
192 case 10: c+=((uint32_t)k[9]<<16); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
193 case 9 : c+=((uint32_t)k[8]<<8); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
194 /* the first byte of c is reserved for the length */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
195 case 8 : b+=((uint32_t)k[7]<<24); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
196 case 7 : b+=((uint32_t)k[6]<<16); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
197 case 6 : b+=((uint32_t)k[5]<<8); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
198 case 5 : b+=k[4]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
199 case 4 : a+=((uint32_t)k[3]<<24); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
200 case 3 : a+=((uint32_t)k[2]<<16); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
201 case 2 : a+=((uint32_t)k[1]<<8); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
202 case 1 : a+=k[0]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
203 /* case 0: nothing left to add */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
204 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
205 mix(a,b,c); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
206 /*-------------------------------------------- report the result */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
207 return c; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
208 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
209 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
210 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
211 * An interface to the above hash functions. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
212 * Returns: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
213 * A 32-bit hash key, suitable for masking down to smaller bit sizes |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
214 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
215 uint32_t hash(int func, uint8_t *key, int key_len) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
216 switch (func) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
217 case HASH_FUNC_HSIEH: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
218 return HashHsieh(key, key_len); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
219 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
220 case HASH_FUNC_TCL: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
221 return HashTcl(key, key_len); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
222 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
223 case HASH_FUNC_JENKINS: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
224 return HashJenkins(key, key_len); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
225 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
226 case HASH_FUNC_JENKINS3: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
227 { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
228 uint32_t pc = 0, pb = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
229 HashJenkins3(key, key_len, &pc, &pb); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
230 return pc; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
231 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
232 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
233 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
234 return 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
235 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
236 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
237 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
238 * As per hash() above but returns a 64-bit key. For 32-bit hash functions |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
239 * this is simply a duplication of the 32-bit value. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
240 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
241 uint64_t hash64(int func, uint8_t *key, int key_len) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
242 uint32_t pc = 0, pb = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
243 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
244 switch (func) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
245 case HASH_FUNC_HSIEH: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
246 pb = pc = HashHsieh(key, key_len); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
247 break; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
248 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
249 case HASH_FUNC_TCL: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
250 pb = pc = HashTcl(key, key_len); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
251 break; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
252 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
253 case HASH_FUNC_JENKINS: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
254 pb = pc = HashJenkins(key, key_len); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
255 break; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
256 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
257 case HASH_FUNC_JENKINS3: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
258 HashJenkins3(key, key_len, &pc, &pb); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
259 break; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
260 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
261 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
262 return pc + (((uint64_t)pb)<<32); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
263 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
264 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
265 /* ========================================================================= |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
266 * Hash Table handling code |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
267 * ========================================================================= |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
268 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
269 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
270 /* Multiplicative factors indicating when to grow or shrink the hash table */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
271 #define HASH_TABLE_RESIZE 3 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
272 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
273 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
274 * Creates a HashItem for use with HashTable h. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
275 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
276 * Returns: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
277 * A pointer to new HashItem on success |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
278 * NULL on failure. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
279 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
280 static HashItem *HashItemCreate(HashTable *h) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
281 HashItem *hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
282 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
283 hi = (h->options & HASH_POOL_ITEMS |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
284 ? pool_alloc(h->hi_pool) : malloc(sizeof(*hi))); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
285 if (NULL == hi) return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
286 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
287 hi->data.p = NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
288 hi->data.i = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
289 hi->next = NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
290 hi->key = NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
291 hi->key_len = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
292 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
293 h->nused++; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
294 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
295 return hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
296 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
297 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
298 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
299 * Deallocates a HashItem created via HashItemCreate. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
300 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
301 * This function will not remove the item from the HashTable so be sure to |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
302 * call HashTableDel() first if appropriate. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
303 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
304 static void HashItemDestroy(HashTable *h, HashItem *hi, int deallocate_data) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
305 if (!hi) return; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
306 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
307 if (!(h->options & HASH_NONVOLATILE_KEYS) || (h->options & HASH_OWN_KEYS)) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
308 if (hi->key) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
309 free(hi->key); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
310 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
311 if (deallocate_data && hi->data.p) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
312 free(hi->data.p); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
313 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
314 if (h->options & HASH_POOL_ITEMS) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
315 pool_free(h->hi_pool, hi); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
316 } else { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
317 free(hi); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
318 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
319 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
320 h->nused--; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
321 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
322 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
323 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
324 * Creates a new HashTable object. Size will be rounded up to the next |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
325 * power of 2. It is a starting point and hash tables may be grown or shrunk |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
326 * as needed (if HASH_DYNAMIC_SIZE is used). |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
327 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
328 * If HASH_POOL_ITEMS is used, HashItems will be allocated in blocks to reduce |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
329 * malloc overhead in the case where a large number of items is required. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
330 * HashItems allocated this way will be put on a free list when destroyed; the |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
331 * memory will only be reclaimed when the entire hash table is destroyed. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
332 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
333 * Options are as defined in the header file (see HASH_* macros). |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
334 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
335 * Returns: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
336 * A pointer to a HashTable on success |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
337 * NULL on failure |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
338 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
339 HashTable *HashTableCreate(int size, int options) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
340 HashTable *h; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
341 int i, bits; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
342 uint32_t mask; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
343 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
344 if (!(h = (HashTable *)malloc(sizeof(*h)))) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
345 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
346 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
347 if (options & HASH_POOL_ITEMS) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
348 h->hi_pool = pool_create(sizeof(HashItem)); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
349 if (NULL == h->hi_pool) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
350 free(h); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
351 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
352 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
353 } else { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
354 h->hi_pool = NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
355 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
356 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
357 if (size < 4) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
358 size = 4; /* an inconsequential minimum size */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
359 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
360 /* Round the requested size to the next power of 2 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
361 bits = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
362 size--; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
363 while (size) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
364 size /= 2; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
365 bits++; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
366 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
367 size = 1<<bits; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
368 mask = size-1; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
369 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
370 h->nbuckets = size; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
371 h->mask = mask; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
372 h->options = options; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
373 h->nused = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
374 h->bucket = (HashItem **)malloc(sizeof(*h->bucket) * size); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
375 if (NULL == h->bucket) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
376 HashTableDestroy(h, 0); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
377 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
378 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
379 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
380 for (i = 0; i < size; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
381 h->bucket[i] = NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
382 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
383 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
384 return h; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
385 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
386 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
387 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
388 * Deallocates a HashTable object (created by HashTableCreate). |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
389 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
390 * The deallocate_data parameter is a boolean to indicate whether the |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
391 * data attached to the hash table should also be free()d. DO NOT USE |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
392 * this if the HashData attached was not a pointer allocated using |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
393 * malloc(). |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
394 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
395 void HashTableDestroy(HashTable *h, int deallocate_data) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
396 int i; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
397 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
398 if (!h) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
399 return; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
400 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
401 if (h->bucket) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
402 for (i = 0; i < h->nbuckets; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
403 HashItem *hi = h->bucket[i], *next = NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
404 for (hi = h->bucket[i]; hi; hi = next) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
405 next = hi->next; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
406 HashItemDestroy(h, hi, deallocate_data); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
407 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
408 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
409 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
410 free(h->bucket); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
411 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
412 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
413 if (h->hi_pool) pool_destroy(h->hi_pool); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
414 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
415 free(h); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
416 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
417 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
418 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
419 * Resizes a HashTable to have 'newsize' buckets. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
420 * This is called automatically when adding or removing items so that the |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
421 * hash table keeps at a sensible scale. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
422 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
423 * FIXME: Halving the size of the hash table is simply a matter of coaelescing |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
424 * every other bucket. Instead we currently rehash (which is slower). |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
425 * Doubling the size of the hash table currently requires rehashing, but this |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
426 * too could be optimised by storing the full 32-bit hash of the key along |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
427 * with the key itself. This then means that it's just a matter of seeing what |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
428 * the next significant bit is. It's a memory vs speed tradeoff though and |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
429 * re-hashing is pretty quick. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
430 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
431 * Returns 0 for success |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
432 * -1 for failure |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
433 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
434 int HashTableResize(HashTable *h, int newsize) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
435 HashTable *h2; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
436 int i; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
437 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
438 /* fprintf(stderr, "Resizing to %d\n", newsize); */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
439 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
440 /* Create a new hash table and rehash everything into it */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
441 h2 = HashTableCreate(newsize, h->options); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
442 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
443 for (i = 0; i < h->nbuckets; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
444 HashItem *hi, *next; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
445 for (hi = h->bucket[i]; hi; hi = next) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
446 uint64_t hv = hash64(h2->options & HASH_FUNC_MASK, |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
447 (uint8_t *)hi->key, hi->key_len) & h2->mask; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
448 next = hi->next; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
449 hi->next = h2->bucket[hv]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
450 h2->bucket[hv] = hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
451 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
452 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
453 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
454 /* Swap the links over & free */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
455 free(h->bucket); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
456 h->bucket = h2->bucket; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
457 h->nbuckets = h2->nbuckets; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
458 h->mask = h2->mask; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
459 free(h2); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
460 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
461 return 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
462 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
463 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
464 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
465 * Adds a HashData item to HashTable h with a specific key. Key can be binary |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
466 * data, but if key_len is passed as zero then strlen() will be used to |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
467 * determine the key length. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
468 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
469 * The "new" pointer may be passed as NULL. When not NULL it is filled out |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
470 * as a boolean to indicate whether the key is already in this hash table. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
471 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
472 * The HASH_ALLOW_DUP_KEYS option (specified when using HashTableCreate) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
473 * will allow duplicate keys to be stored, and hence *new is also zero. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
474 * By default duplicate keys are disallowed. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
475 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
476 * Keys are considered to be volatile memory (ie temporary storage) and so the |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
477 * hash table takes separate copies of them. To avoid this use the |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
478 * HASH_NONVOLATILE_KEYS option. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
479 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
480 * If the HASH_OWN_KEYS option was specified when creating the table then |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
481 * keys will be considered to be owned by the hash table. In this case |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
482 * the key will be freed when the table is destroyed regardless of |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
483 * whether the HASH_NONVOLATILE_KEYS option was used to allocate its |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
484 * own private copy. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
485 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
486 * Returns: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
487 * The HashItem created (or matching if a duplicate) on success |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
488 * NULL on failure |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
489 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
490 HashItem *HashTableAdd(HashTable *h, char *key, int key_len, HashData data, |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
491 int *new) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
492 uint64_t hv; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
493 HashItem *hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
494 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
495 if (!key_len) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
496 key_len = strlen(key); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
497 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
498 hv = hash64(h->options & HASH_FUNC_MASK, (uint8_t *)key, key_len) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
499 & h->mask; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
500 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
501 /* Already exists? */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
502 if (!(h->options & HASH_ALLOW_DUP_KEYS)) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
503 for (hi = h->bucket[hv]; hi; hi = hi->next) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
504 if (key_len == hi->key_len && |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
505 memcmp(key, hi->key, key_len) == 0) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
506 if (new) *new = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
507 return hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
508 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
509 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
510 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
511 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
512 /* No, so create a new one and link it in */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
513 if (NULL == (hi = HashItemCreate(h))) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
514 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
515 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
516 if (h->options & HASH_NONVOLATILE_KEYS) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
517 hi->key = key; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
518 else { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
519 hi->key = (char *)malloc(key_len+1); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
520 memcpy(hi->key, key, key_len); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
521 hi->key[key_len] = 0; /* null terminate incase others print keys */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
522 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
523 hi->key_len = key_len; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
524 hi->data = data; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
525 hi->next = h->bucket[hv]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
526 h->bucket[hv] = hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
527 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
528 if ((h->options & HASH_DYNAMIC_SIZE) && |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
529 h->nused > HASH_TABLE_RESIZE * h->nbuckets) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
530 HashTableResize(h, h->nbuckets*4); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
531 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
532 if (new) *new = 1; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
533 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
534 return hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
535 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
536 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
537 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
538 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
539 * Removes a specified HashItem from the HashTable. (To perform this it needs |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
540 * to rehash based on the hash key as hash_item only has a next pointer and |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
541 * not a previous pointer.) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
542 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
543 * The HashItem itself is also destroyed (by an internal call to |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
544 * HashItemDestroy). The deallocate_data parameter controls whether the data |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
545 * associated with the HashItem should also be free()d. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
546 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
547 * See also the HashTableRemove() function to remove by key instead of |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
548 * HashItem. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
549 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
550 * Returns 0 on success |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
551 * -1 on failure (eg HashItem not in the HashTable); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
552 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
553 int HashTableDel(HashTable *h, HashItem *hi, int deallocate_data) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
554 uint64_t hv; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
555 HashItem *next, *last; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
556 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
557 hv = hash64(h->options & HASH_FUNC_MASK, |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
558 (uint8_t *)hi->key, hi->key_len) & h->mask; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
559 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
560 for (last = NULL, next = h->bucket[hv]; next; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
561 last = next, next = next->next) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
562 if (next == hi) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
563 /* Link last to next->next */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
564 if (last) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
565 last->next = next->next; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
566 else |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
567 h->bucket[hv] = next->next; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
568 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
569 HashItemDestroy(h, hi, deallocate_data); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
570 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
571 return 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
572 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
573 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
574 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
575 return -1; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
576 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
577 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
578 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
579 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
580 * Searches the HashTable for the data registered with 'key' and removes |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
581 * these items from the HashTable. In essence this is a combination of |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
582 * HashTableSearch and HashTableDel functions. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
583 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
584 * If HASH_ALLOW_DUP_KEYS is used this will remove all items matching 'key', |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
585 * otherwise just a single item will be removed. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
586 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
587 * If 'deallocate_data' is true the data associated with the HashItem will |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
588 * be free()d. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
589 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
590 * Returns |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
591 * 0 on success (at least one item found) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
592 * -1 on failure (no items found). |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
593 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
594 int HashTableRemove(HashTable *h, char *key, int key_len, |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
595 int deallocate_data) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
596 uint64_t hv; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
597 HashItem *last, *next, *hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
598 int retval = -1; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
599 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
600 if (!key_len) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
601 key_len = strlen(key); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
602 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
603 hv = hash64(h->options & HASH_FUNC_MASK, (uint8_t *)key, key_len) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
604 & h->mask; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
605 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
606 last = NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
607 next = h->bucket[hv]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
608 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
609 while (next) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
610 hi = next; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
611 if (key_len == hi->key_len && |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
612 memcmp(key, hi->key, key_len) == 0) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
613 /* An item to remove, adjust links and destroy */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
614 if (last) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
615 last->next = hi->next; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
616 else |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
617 h->bucket[hv] = hi->next; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
618 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
619 next = hi->next; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
620 HashItemDestroy(h, hi, deallocate_data); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
621 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
622 retval = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
623 if (!(h->options & HASH_ALLOW_DUP_KEYS)) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
624 break; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
625 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
626 } else { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
627 /* We only update last when it's something we haven't destroyed */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
628 last = hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
629 next = hi->next; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
630 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
631 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
632 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
633 return retval; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
634 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
635 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
636 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
637 * Searches the HashTable for the data registered with 'key'. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
638 * If HASH_ALLOW_DUP_KEYS is used this will just be the first one found. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
639 * You will then need to use HashTableNext to iterate through the matches. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
640 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
641 * Returns |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
642 * HashItem if found |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
643 * NULL if not found |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
644 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
645 HashItem *HashTableSearch(HashTable *h, char *key, int key_len) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
646 uint64_t hv; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
647 HashItem *hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
648 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
649 if (!key_len) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
650 key_len = strlen(key); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
651 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
652 hv = hash64(h->options & HASH_FUNC_MASK, (uint8_t *)key, key_len) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
653 & h->mask; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
654 for (hi = h->bucket[hv]; hi; hi = hi->next) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
655 if (key_len == hi->key_len && |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
656 memcmp(key, hi->key, key_len) == 0) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
657 return hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
658 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
659 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
660 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
661 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
662 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
663 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
664 * Find the next HashItem (starting from 'hi') to also match this key. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
665 * This is only valid when the HASH_ALLOW_DUP_KEYS is in use. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
666 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
667 * Returns |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
668 * HashItem if found |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
669 * NULL if not found |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
670 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
671 HashItem *HashTableNext(HashItem *hi, char *key, int key_len) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
672 if (!hi) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
673 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
674 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
675 for (hi = hi->next; hi; hi = hi->next) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
676 if (key_len == hi->key_len && |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
677 memcmp(key, hi->key, key_len) == 0) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
678 return hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
679 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
680 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
681 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
682 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
683 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
684 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
685 * Dumps a textual represenation of the hash table to stdout. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
686 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
687 void HashTableDump(HashTable *h, FILE *fp) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
688 int i; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
689 for (i = 0; i < h->nbuckets; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
690 HashItem *hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
691 for (hi = h->bucket[i]; hi; hi = hi->next) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
692 fprintf(fp, "%.*s\n", hi->key_len, hi->key); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
693 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
694 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
695 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
696 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
697 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
698 * Produces some simple statistics on the hash table population. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
699 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
700 void HashTableStats(HashTable *h, FILE *fp) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
701 int i; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
702 double avg = (double)h->nused / h->nbuckets; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
703 double var = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
704 int maxlen = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
705 int filled = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
706 int clen[51]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
707 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
708 for (i = 0; i <= 50; i++) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
709 clen[i] = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
710 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
711 for (i = 0; i < h->nbuckets; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
712 int len = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
713 HashItem *hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
714 for (hi = h->bucket[i]; hi; hi = hi->next) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
715 len++; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
716 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
717 if (len > 0) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
718 filled++; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
719 if (len > maxlen) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
720 maxlen = len; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
721 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
722 clen[len <= 50 ? len : 50]++; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
723 var += (len-avg) * (len-avg); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
724 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
725 var /= h->nbuckets; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
726 /* sd = sqrt(var); */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
727 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
728 fprintf(fp, "Nbuckets = %d\n", h->nbuckets); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
729 fprintf(fp, "Nused = %d\n", h->nused); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
730 fprintf(fp, "Avg chain = %f\n", avg); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
731 fprintf(fp, "Chain var.= %f\n", var); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
732 fprintf(fp, "%%age full = %f\n", (100.0*filled)/h->nbuckets); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
733 fprintf(fp, "max len = %d\n", maxlen); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
734 for (i = 0; i <= maxlen; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
735 fprintf(fp, "Chain %2d = %d\n", i, clen[i]); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
736 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
737 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
738 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
739 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
740 * -------------------------------------------------------------------- |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
741 * Below we have a specialisation of the HashTable code where the data |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
742 * attached to the hash table is a position,size pair. This allows for the |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
743 * hash table to encode positions and sizes of items within a file archive. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
744 * -------------------------------------------------------------------- |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
745 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
746 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
747 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
748 * Writes the HashTable structures to 'fp'. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
749 * This is a specialisation of the HashTable where the HashData is a |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
750 * position,size tuple. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
751 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
752 * This consists of the following format: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
753 * Header: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
754 * ".hsh" (magic numebr) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
755 * x4 (1-bytes of version code, eg "1.00") |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
756 * x1 (HASH_FUNC_? function used) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
757 * x1 (number of file headers: FH. These count from 1 to FH inclusive) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
758 * x1 (number of file footers: FF. These count from 1 to FF inclusive) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
759 * x1 (reserved - zero for now) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
760 * x4 (4-bytes big-endian; number of hash buckets) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
761 * x8 (offset to add to item positions. eg size of this index) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
762 * x4 (4-bytes big-endian; number of bytes in hash file, inc. header) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
763 * Archive name: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
764 * x1 (length, zero => no name) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
765 * ? (archive filename) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
766 * File headers (FH copies of): |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
767 * x8 (position) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
768 * x4 (size) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
769 * File footers (FH copies of): |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
770 * x8 (position) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
771 * x4 (size) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
772 * Buckets (multiples of) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
773 * x4 (4-byte offset of linked list pos, rel. to the start of the hdr) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
774 * Items (per bucket chain, not written if Bucket[?]==0) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
775 * x1 (key length, zero => end of chain) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
776 * ? (key) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
777 * x0.5 (File header to use. zero => none) top 4 bits |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
778 * x0.5 (File footer to use. zero => none) bottom 4 bits |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
779 * x8 (position) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
780 * x4 (size) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
781 * ... arbitrary gap (but likely none) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
782 * Index footer: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
783 * ".hsh" (magic number) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
784 * x8 (offset to Hash Header. +ve = absolute, -ve = relative to end) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
785 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
786 * It is designed such that on-disk querying of the hash table can be done |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
787 * purely by forward seeks. (This is generally faster due to pre-fetching of |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
788 * the subsequent blocks by many disk controllers.) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
789 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
790 * Returns: the number of bytes written on success |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
791 * -1 for error |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
792 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
793 uint64_t HashFileSave(HashFile *hf, FILE *fp, int64_t offset) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
794 int i; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
795 HashItem *hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
796 uint32_t *bucket_pos; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
797 uint64_t hfsize = 0, be_hfsize; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
798 HashTable *h = hf->h; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
799 HashFileFooter foot; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
800 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
801 /* Compute the coordinates of the hash items */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
802 hfsize = HHSIZE; /* header */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
803 hfsize += 1 + (hf->archive |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
804 ? strlen(hf->archive) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
805 : 0); /* filename */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
806 hfsize += h->nbuckets * 4; /* buckets */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
807 for (i = 0; i < hf->nheaders; i++) /* headers */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
808 hfsize += 12; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
809 for (i = 0; i < hf->nfooters; i++) /* footers */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
810 hfsize += 12; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
811 bucket_pos = (uint32_t *)calloc(h->nbuckets, sizeof(uint32_t)); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
812 for (i = 0; i < h->nbuckets; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
813 bucket_pos[i] = hfsize; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
814 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
815 if (!(hi = h->bucket[i])) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
816 continue; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
817 for (; hi; hi = hi->next) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
818 hfsize += 1 + 1 + hi->key_len + 8 + 4; /* keys, pos, size */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
819 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
820 hfsize++; /* list-end marker */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
821 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
822 hfsize += sizeof(foot); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
823 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
824 /* Write the header: */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
825 memcpy(hf->hh.magic, HASHFILE_MAGIC, 4); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
826 memcpy(hf->hh.vers, HASHFILE_VERSION, 4); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
827 hf->hh.hfunc = h->options & HASH_FUNC_MASK; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
828 hf->hh.nheaders = hf->nheaders; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
829 hf->hh.nfooters = hf->nfooters; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
830 hf->hh.nbuckets = be_int4(h->nbuckets); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
831 hf->hh.offset = offset == HASHFILE_PREPEND |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
832 ? be_int8(hfsize) /* archive will be append to this file */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
833 : be_int8(offset); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
834 hf->hh.size = be_int4(hfsize); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
835 fwrite(&hf->hh, HHSIZE, 1, fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
836 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
837 /* Write the archive filename, if known */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
838 if (hf->archive && *hf->archive) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
839 fputc(strlen(hf->archive), fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
840 fputs(hf->archive, fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
841 } else { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
842 fputc(0, fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
843 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
844 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
845 /* Write out the headers and footers */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
846 for (i = 0; i < hf->nheaders; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
847 HashFileSection hs; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
848 hs.pos = be_int8(hf->headers[i].pos); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
849 fwrite(&hs.pos, 8, 1, fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
850 hs.size = be_int4(hf->headers[i].size); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
851 fwrite(&hs.size, 4, 1, fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
852 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
853 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
854 for (i = 0; i < hf->nfooters; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
855 HashFileSection hs; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
856 hs.pos = be_int8(hf->footers[i].pos); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
857 fwrite(&hs.pos, 8, 1, fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
858 hs.size = be_int4(hf->footers[i].size); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
859 fwrite(&hs.size, 4, 1, fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
860 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
861 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
862 /* Write out hash buckets */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
863 for (i = 0; i < h->nbuckets; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
864 uint32_t zero = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
865 uint32_t be32; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
866 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
867 if (!(hi = h->bucket[i])) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
868 fwrite(&zero, 4, 1, fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
869 continue; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
870 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
871 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
872 be32 = be_int4(bucket_pos[i]); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
873 fwrite(&be32, 4, 1, fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
874 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
875 free(bucket_pos); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
876 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
877 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
878 * Write the hash_item linked lists. The first item is the |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
879 * hash key length. We append a zero to the end of the list so we |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
880 * can check this key length to determine the end of this hash |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
881 * item list. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
882 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
883 for (i = 0; i < h->nbuckets; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
884 if (!(hi = h->bucket[i])) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
885 continue; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
886 for (; hi; hi = hi->next) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
887 uint64_t be64; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
888 uint32_t be32; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
889 HashFileItem *hfi = (HashFileItem *)hi->data.p; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
890 unsigned char headfoot = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
891 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
892 fprintf(fp, "%c%.*s", hi->key_len, |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
893 hi->key_len, hi->key); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
894 headfoot = (((hfi->header) & 0xf) << 4) | ((hfi->footer) & 0xf); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
895 fwrite(&headfoot, 1, 1, fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
896 be64 = be_int8(hfi->pos); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
897 fwrite(&be64, 8, 1, fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
898 be32 = be_int4(hfi->size); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
899 fwrite(&be32, 4, 1, fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
900 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
901 fputc(0, fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
902 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
903 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
904 /* Finally write the footer referencing back to the header start */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
905 memcpy(foot.magic, HASHFILE_MAGIC, 4); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
906 be_hfsize = be_int8(-hfsize); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
907 memcpy(foot.offset, &be_hfsize, 8); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
908 fwrite(&foot, sizeof(foot), 1, fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
909 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
910 return hfsize; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
911 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
912 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
913 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
914 * Reads an entire HashTable from fp. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
915 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
916 * Returns: |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
917 * A filled out HashTable pointer on success |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
918 * NULL on failure |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
919 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
920 HashFile *HashFileLoad_old(FILE *fp) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
921 int i; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
922 HashTable *h; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
923 HashItem *hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
924 HashFile *hf; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
925 uint32_t *bucket_pos; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
926 unsigned char *htable; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
927 int htable_pos; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
928 int fnamelen; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
929 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
930 if (NULL == (hf = (HashFile *)calloc(1, sizeof(HashFile)))) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
931 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
932 if (NULL == (htable = (unsigned char *)malloc(HHSIZE))) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
933 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
934 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
935 /* Read and create the hash table header */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
936 if (HHSIZE != fread(htable, 1, HHSIZE, fp)) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
937 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
938 memcpy(&hf->hh, htable, HHSIZE); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
939 hf->hh.nbuckets = be_int4(hf->hh.nbuckets); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
940 hf->hh.offset = be_int8(hf->hh.offset); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
941 hf->hh.size = be_int4(hf->hh.size); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
942 hf->h = h = HashTableCreate(hf->hh.nbuckets, hf->hh.hfunc); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
943 bucket_pos = (uint32_t *)calloc(h->nbuckets, sizeof(uint32_t)); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
944 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
945 /* Load the archive filename */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
946 fnamelen = fgetc(fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
947 if (fnamelen) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
948 hf->archive = (char *)malloc(fnamelen+1); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
949 fread(hf->archive, 1, fnamelen, fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
950 hf->archive[fnamelen] = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
951 if (hf->archive[0] == 0) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
952 free(hf->archive); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
953 hf->archive = NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
954 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
955 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
956 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
957 /* Load the rest of the hash table to memory */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
958 htable_pos = HHSIZE + fnamelen + 1; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
959 if (NULL == (htable = (unsigned char *)realloc(htable, hf->hh.size))) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
960 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
961 if (hf->hh.size-htable_pos != |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
962 fread(&htable[htable_pos], 1, hf->hh.size-htable_pos, fp)) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
963 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
964 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
965 /* Read the header / footer items */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
966 for (i = 0; i < hf->hh.nheaders; i++) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
967 htable_pos += 8; /* skip them for now */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
968 for (i = 0; i < hf->hh.nfooters; i++) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
969 htable_pos += 8; /* skip them for now */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
970 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
971 /* Identify the "bucket pos". Detemines which buckets have data */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
972 for (i = 0; i < h->nbuckets; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
973 memcpy(&bucket_pos[i], &htable[htable_pos], 4); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
974 bucket_pos[i] = be_int4(bucket_pos[i]); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
975 htable_pos += 4; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
976 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
977 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
978 /* Read the hash table items */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
979 for (i = 0; i < h->nbuckets; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
980 if (!bucket_pos[i]) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
981 continue; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
982 for (;;) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
983 int c; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
984 unsigned char uc; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
985 char key[256]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
986 uint64_t pos; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
987 uint32_t size; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
988 HashFileItem *hfi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
989 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
990 c = htable[htable_pos++]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
991 if (c == EOF || !c) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
992 break; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
993 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
994 /* key */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
995 memcpy(key, &htable[htable_pos], c); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
996 htable_pos += c; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
997 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
998 /* header/footer */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
999 uc = htable[htable_pos++]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1000 hfi = (HashFileItem *)malloc(sizeof(*hfi)); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1001 hfi->header = (uc >> 4) & 0xf; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1002 hfi->footer = uc & 0xf; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1003 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1004 /* pos */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1005 memcpy(&pos, &htable[htable_pos], 8); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1006 htable_pos += 8; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1007 hfi->pos = be_int8(pos) + hf->hh.offset; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1008 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1009 /* size */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1010 memcpy(&size, &htable[htable_pos], 4); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1011 htable_pos += 4; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1012 hfi->size = be_int4(size); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1013 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1014 /* Add to the hash table */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1015 hi = HashItemCreate(h); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1016 hi->next = h->bucket[i]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1017 h->bucket[i] = hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1018 hi->key_len = c; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1019 hi->key = (char *)malloc(c+1); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1020 memcpy(hi->key, key, c); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1021 hi->key[c] = 0; /* For debugging convenience only */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1022 hi->data.p = hfi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1023 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1024 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1025 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1026 fprintf(stderr, "done\n"); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1027 fflush(stderr); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1028 free(bucket_pos); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1029 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1030 return hf; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1031 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1032 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1033 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1034 * Opens a stored hash table file. It also internally keeps an open file to |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1035 * hash and the archive files. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1036 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1037 * Returns the HashFile pointer on success |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1038 * NULL on failure |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1039 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1040 HashFile *HashFileFopen(FILE *fp) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1041 HashFile *hf = HashFileCreate(0, 0); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1042 int archive_len; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1043 int i; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1044 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1045 /* Set the stdio buffer to be small to avoid massive I/O wastage */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1046 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1047 /* Read the header */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1048 hf->hfp = fp; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1049 hf->afp = fp; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1050 hf->hf_start = ftello(hf->hfp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1051 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1052 if (HHSIZE != fread(&hf->hh, 1, HHSIZE, hf->hfp)) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1053 HashFileDestroy(hf); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1054 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1055 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1056 if (memcmp(HASHFILE_MAGIC, &hf->hh, 4) != 0) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1057 HashFileFooter foot; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1058 int64_t offset; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1059 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1060 /* Invalid magic number, try other end of file! */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1061 fseeko(hf->hfp, -(off_t)sizeof(HashFileFooter), SEEK_END); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1062 if (sizeof(foot) != fread(&foot, 1, sizeof(foot), hf->hfp)) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1063 HashFileDestroy(hf); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1064 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1065 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1066 if (memcmp(HASHFILE_MAGIC, &foot.magic, 4) != 0) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1067 HashFileDestroy(hf); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1068 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1069 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1070 memcpy(&offset, foot.offset, 8); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1071 offset = be_int8(offset); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1072 fseeko(hf->hfp, offset, SEEK_CUR); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1073 hf->hf_start = ftello(hf->hfp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1074 if (HHSIZE != fread(&hf->hh, 1, HHSIZE, hf->hfp)) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1075 HashFileDestroy(hf); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1076 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1077 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1078 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1079 if (memcmp(hf->hh.vers, HASHFILE_VERSION, 4) != 0) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1080 /* incorrect version */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1081 HashFileDestroy(hf); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1082 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1083 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1084 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1085 hf->hh.nbuckets = be_int4(hf->hh.nbuckets); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1086 hf->hh.offset = be_int8(hf->hh.offset); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1087 hf->hh.size = be_int4(hf->hh.size); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1088 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1089 /* Load the main archive filename */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1090 if ((archive_len = fgetc(hf->hfp))) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1091 hf->archive = (char *)malloc(archive_len+1); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1092 fread(hf->archive, 1, archive_len, hf->hfp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1093 hf->archive[archive_len] = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1094 if (hf->archive[0] == 0) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1095 free(hf->archive); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1096 hf->archive = NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1097 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1098 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1099 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1100 hf->header_size = HHSIZE + 1 + archive_len + |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1101 12 * (hf->hh.nheaders + hf->hh.nfooters); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1102 hf->nheaders = hf->hh.nheaders; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1103 hf->nfooters = hf->hh.nfooters; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1104 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1105 /* Load the header and footer locations */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1106 hf->headers = hf->nheaders |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1107 ? (HashFileSection *)malloc(hf->nheaders * sizeof(HashFileSection)) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1108 : NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1109 for (i = 0; i < hf->nheaders; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1110 fread(&hf->headers[i].pos, 8, 1, hf->hfp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1111 fread(&hf->headers[i].size, 4, 1, hf->hfp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1112 hf->headers[i].pos = be_int8(hf->headers[i].pos) + hf->hh.offset; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1113 hf->headers[i].size = be_int4(hf->headers[i].size); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1114 hf->headers[i].cached_data = NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1115 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1116 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1117 hf->footers = hf->nfooters |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1118 ? (HashFileSection *)malloc(hf->nfooters * sizeof(HashFileSection)) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1119 : NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1120 for (i = 0; i < hf->nfooters; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1121 fread(&hf->footers[i].pos, 8, 1, hf->hfp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1122 fread(&hf->footers[i].size, 4, 1, hf->hfp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1123 hf->footers[i].pos = be_int8(hf->footers[i].pos) + hf->hh.offset; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1124 hf->footers[i].size = be_int4(hf->footers[i].size); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1125 hf->footers[i].cached_data = NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1126 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1127 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1128 return hf; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1129 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1130 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1131 HashFile *HashFileOpen(char *fname) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1132 FILE *fp; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1133 HashFile *hf; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1134 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1135 /* Open the hash and read the header */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1136 if (NULL == (fp = fopen(fname, "rb"))) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1137 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1138 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1139 if (!(hf = HashFileFopen(fp))) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1140 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1141 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1142 /* Open the main archive too */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1143 if (hf->archive) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1144 if (NULL == (hf->afp = fopen(hf->archive, "rb"))) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1145 /* Possibly done via a relative pathname (optimal infact) */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1146 char *cp; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1147 char aname[1024]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1148 if (NULL == (cp = strrchr(fname, '/'))) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1149 HashFileDestroy(hf); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1150 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1151 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1152 sprintf(aname, "%.*s%s", (int)(cp-fname+1), fname, hf->archive); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1153 if (NULL == (hf->afp = fopen(aname, "rb"))) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1154 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1155 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1156 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1157 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1158 } else { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1159 hf->afp = hf->hfp; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1160 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1161 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1162 return hf; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1163 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1164 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1165 HashFile *HashFileLoad(FILE *fp) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1166 HashFile *hf; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1167 char *htable; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1168 off_t htable_pos; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1169 int i; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1170 HashItem *hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1171 HashTable *h; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1172 uint32_t *bucket_pos; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1173 uint32_t hsize; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1174 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1175 /* Open the hash table */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1176 fseeko(fp, 0, SEEK_SET); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1177 if (NULL == (hf = HashFileFopen(fp))) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1178 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1179 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1180 HashTableDestroy(hf->h, 1); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1181 h = hf->h = HashTableCreate(hf->hh.nbuckets, hf->hh.hfunc); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1182 bucket_pos = (uint32_t *)calloc(h->nbuckets, sizeof(uint32_t)); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1183 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1184 /* Also load in the entire thing to memory */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1185 htable = (char *)malloc(hf->hh.size); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1186 fseeko(fp, hf->hf_start, SEEK_SET); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1187 hsize = fread(htable, 1, hf->hh.size, fp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1188 if (hf->hh.size != hsize) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1189 free(htable); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1190 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1191 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1192 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1193 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1194 * HashFileOpen has already decoded the headers up to and including the |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1195 * individual file header/footer sections, but not the buckets and item |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1196 * lists, so we start from there. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1197 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1198 htable_pos = hf->header_size; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1199 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1200 /* Identify the "bucket pos". Detemines which buckets have data */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1201 for (i = 0; i < h->nbuckets; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1202 memcpy(&bucket_pos[i], &htable[htable_pos], 4); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1203 bucket_pos[i] = be_int4(bucket_pos[i]); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1204 htable_pos += 4; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1205 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1206 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1207 /* Read the hash table items */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1208 for (i = 0; i < h->nbuckets; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1209 if (!bucket_pos[i]) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1210 continue; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1211 for (;;) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1212 int c; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1213 unsigned char uc; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1214 char key[256]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1215 uint64_t pos; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1216 uint32_t size; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1217 HashFileItem *hfi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1218 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1219 c = htable[htable_pos++]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1220 if (c == EOF || !c) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1221 break; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1222 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1223 /* key */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1224 memcpy(key, &htable[htable_pos], c); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1225 htable_pos += c; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1226 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1227 /* header/footer */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1228 uc = htable[htable_pos++]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1229 hfi = (HashFileItem *)malloc(sizeof(*hfi)); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1230 hfi->header = (uc >> 4) & 0xf; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1231 hfi->footer = uc & 0xf; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1232 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1233 /* pos */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1234 memcpy(&pos, &htable[htable_pos], 8); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1235 htable_pos += 8; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1236 hfi->pos = be_int8(pos) + hf->hh.offset; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1237 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1238 /* size */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1239 memcpy(&size, &htable[htable_pos], 4); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1240 htable_pos += 4; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1241 hfi->size = be_int4(size); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1242 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1243 /* Add to the hash table */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1244 hi = HashItemCreate(h); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1245 hi->next = h->bucket[i]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1246 h->bucket[i] = hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1247 hi->key_len = c; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1248 hi->key = (char *)malloc(c+1); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1249 memcpy(hi->key, key, c); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1250 hi->key[c] = 0; /* For debugging convenience only */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1251 hi->data.p = hfi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1252 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1253 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1254 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1255 fflush(stderr); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1256 free(bucket_pos); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1257 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1258 return hf; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1259 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1260 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1261 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1262 * Searches the named HashFile for a specific key. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1263 * When found it returns the position and size of the object in pos and size. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1264 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1265 * Returns |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1266 * 0 on success (pos & size updated) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1267 * -1 on failure |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1268 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1269 int HashFileQuery(HashFile *hf, uint8_t *key, int key_len, |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1270 HashFileItem *item) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1271 uint64_t hval; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1272 uint32_t pos; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1273 int klen; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1274 int cur_offset = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1275 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1276 /* Hash 'key' to compute the bucket number */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1277 hval = hash64(hf->hh.hfunc, key, key_len) & (hf->hh.nbuckets-1); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1278 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1279 /* Read the bucket to find the first linked list item location */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1280 if (-1 == fseeko(hf->hfp, hf->hf_start + 4*hval + hf->header_size,SEEK_SET)) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1281 return -1; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1282 if (4 != fread(&pos, 1, 4, hf->hfp)) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1283 return -1; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1284 pos = be_int4(pos); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1285 cur_offset = 4*hval + 4 + hf->header_size; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1286 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1287 if (0 == pos) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1288 /* No bucket pos => key not present */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1289 return -1; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1290 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1291 /* Jump to the HashItems list and look through for key */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1292 if (-1 == fseeko(hf->hfp, pos - cur_offset, SEEK_CUR)) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1293 return -1; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1294 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1295 for (klen = fgetc(hf->hfp); klen; klen = fgetc(hf->hfp)) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1296 char k[256]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1297 unsigned char headfoot; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1298 uint64_t pos; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1299 uint32_t size; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1300 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1301 fread(k, klen, 1, hf->hfp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1302 fread(&headfoot, 1, 1, hf->hfp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1303 item->header = (headfoot >> 4) & 0xf; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1304 item->footer = headfoot & 0xf; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1305 fread(&pos, 8, 1, hf->hfp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1306 pos = be_int8(pos) + hf->hh.offset; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1307 fread(&size, 4, 1, hf->hfp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1308 size = be_int4(size); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1309 if (klen == key_len && 0 == memcmp(key, k, key_len)) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1310 item->pos = pos; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1311 item->size = size; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1312 return 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1313 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1314 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1315 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1316 return -1; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1317 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1318 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1319 HashFile *HashFileCreate(int size, int options) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1320 HashFile *hf; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1321 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1322 if (NULL == (hf = (HashFile *)calloc(1, sizeof(*hf)))) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1323 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1324 if (NULL == (hf->h = HashTableCreate(size, options))) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1325 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1326 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1327 return hf; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1328 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1329 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1330 void HashFileDestroy(HashFile *hf) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1331 if (!hf) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1332 return; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1333 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1334 if (hf->h) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1335 HashTableDestroy(hf->h, 1); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1336 if (hf->archive) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1337 free(hf->archive); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1338 if (hf->headers) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1339 int i; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1340 for (i = 0; i < hf->nheaders; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1341 if (hf->headers[i].cached_data) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1342 free(hf->headers[i].cached_data); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1343 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1344 free(hf->headers); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1345 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1346 if (hf->footers) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1347 int i; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1348 for (i = 0; i < hf->nfooters; i++) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1349 if (hf->footers[i].cached_data) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1350 free(hf->footers[i].cached_data); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1351 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1352 free(hf->footers); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1353 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1354 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1355 if (hf->afp) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1356 fclose(hf->afp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1357 if (hf->hfp && hf->hfp != hf->afp) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1358 fclose(hf->hfp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1359 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1360 free(hf); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1361 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1362 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1363 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1364 * Extracts the contents for a file out of the HashFile. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1365 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1366 char *HashFileExtract(HashFile *hf, char *fname, size_t *len) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1367 HashFileItem hfi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1368 size_t sz, pos; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1369 char *data; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1370 HashFileSection *head = NULL, *foot = NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1371 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1372 /* Find out if and where the item is in the archive */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1373 if (-1 == HashFileQuery(hf, (uint8_t *)fname, strlen(fname), &hfi)) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1374 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1375 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1376 /* Work out the size including header/footer and allocate */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1377 sz = hfi.size; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1378 if (hfi.header) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1379 head = &hf->headers[hfi.header-1]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1380 sz += head->size; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1381 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1382 if (hfi.footer) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1383 foot = &hf->footers[hfi.footer-1]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1384 sz += foot->size; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1385 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1386 *len = sz; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1387 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1388 if (NULL == (data = (char *)malloc(sz+1))) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1389 return NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1390 data[sz] = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1391 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1392 /* Header */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1393 pos = 0; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1394 if (head) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1395 fseeko(hf->afp, head->pos, SEEK_SET); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1396 fread(&data[pos], head->size, 1, hf->afp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1397 pos += head->size; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1398 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1399 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1400 /* Main file */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1401 fseeko(hf->afp, hfi.pos, SEEK_SET); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1402 fread(&data[pos], hfi.size, 1, hf->afp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1403 pos += hfi.size; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1404 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1405 /* Footer */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1406 if (foot) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1407 fseeko(hf->afp, foot->pos, SEEK_SET); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1408 fread(&data[pos], foot->size, 1, hf->afp); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1409 pos += foot->size; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1410 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1411 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1412 return data; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1413 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1414 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1415 /* |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1416 * Iterates through members of a hash table returning items sequentially. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1417 * |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1418 * Returns the next HashItem on success |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1419 * NULL on failure. |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1420 */ |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1421 HashItem *HashTableIterNext(HashTable *h, HashIter *iter) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1422 do { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1423 if (iter->hi == NULL) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1424 if (++iter->bnum >= h->nbuckets) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1425 break; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1426 iter->hi = h->bucket[iter->bnum]; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1427 } else { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1428 iter->hi = iter->hi->next; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1429 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1430 } while (!iter->hi); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1431 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1432 return iter->hi; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1433 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1434 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1435 void HashTableIterReset(HashIter *iter) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1436 if (iter) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1437 iter->bnum = -1; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1438 iter->hi = NULL; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1439 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1440 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1441 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1442 HashIter *HashTableIterCreate(void) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1443 HashIter *iter = (HashIter *)malloc(sizeof(*iter)); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1444 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1445 HashTableIterReset(iter); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1446 return iter; |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1447 } |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1448 |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1449 void HashTableIterDestroy(HashIter *iter) { |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1450 if (iter) |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1451 free(iter); |
d901c9f41a6a
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
dawe
parents:
diff
changeset
|
1452 } |