File: stdhash.hh

package info (click to toggle)
maq 0.7.1-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,500 kB
  • sloc: cpp: 5,025; ansic: 3,333; sh: 3,303; perl: 2,547; makefile: 31
file content (683 lines) | stat: -rw-r--r-- 23,425 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/*
   stdhash -- standalone hash library

   Copyright (c) 2006, Heng Li <lh3lh3@gmail.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
   Standalone hash library is a free C++ template library for hash tables.
   It implements open-address hashing with the "double hashing" technique,
   which makes this library different from most of other implementations,
   such as STL and TR1 hash based classes, which are all based on chained
   hashing. However, fewer implementations do not mean open-address hashing
   is less efficient. As a matter of fact, this hash library is at least as
   efficient, in terms of both time and space, as STL and TR1 libraries, and
   in some cases outperforms others.
 */

/*
  2007-07-18, 1.9.2: fixed a bug in hash_*_char when an element is erased.
 */

#ifndef LH3_STDHASH_H_
#define LH3_STDHASH_H_

#include <string.h>
#include <stdlib.h>

#define LH3_STDHASH_VERSION "1.9.2"

typedef unsigned int bit32_t;
typedef unsigned long long bit64_t;
typedef unsigned short bit16_t;
// even on 64-bit systems, hashint_t should be bit32_t
typedef bit32_t hashint_t;

/*
 * Hash functions
 */
// Do a web search "g_str_hash X31_HASH" for more information.
/** hash function for strings (char*) */
inline bit32_t __lh3_X31_hash_string(const char *s)
{
	bit32_t h = 0;
	for ( ; *s; s++)
		h = (h << 5) - h + *s;
	return h;
}
/** Jenkins' hash function for 32-bit integers. Not used in this library. */
inline bit32_t __lh3_Jenkins_hash_int(bit32_t key)
{
	key += (key << 12);
	key ^= (key >> 22);
	key += (key << 4);
	key ^= (key >> 9);
	key += (key << 10);
	key ^= (key >> 2);
	key += (key << 7);
	key ^= (key >> 12);
	return key;
}
/** Jenkins' hash function for 64-bit integers. Used when LH3_HASH_INT macro is set. */
inline bit64_t __lh3_Jenkins_hash_64(bit64_t key)
{
	key += ~(key << 32);
	key ^= (key >> 22);
	key += ~(key << 13);
	key ^= (key >> 8);
	key += (key << 3);
	key ^= (key >> 15);
	key += ~(key << 27);
	key ^= (key >> 31);
	return key;
}
/** Wang's hash function for 32-bit inegers. Used when LH3_HASH_INT macro is set. */
inline bit32_t __lh3_Wang_hash_int(bit32_t key)
{
	key += ~(key << 15);
	key ^=  (key >> 10);
	key +=  (key << 3);
	key ^=  (key >> 6);
	key += ~(key << 11);
	key ^=  (key >> 16);
	return key;
}
/** hash function for 16-bit integers */
inline bit32_t __lh3_hash_fun(bit16_t key)
{
#ifndef LH3_HASH_INT
	return __lh3_Wang_hash_int(bit32_t(key));
#else
	return bit32_t(key);
#endif
}
/** hash function for 32-bit integers */
inline bit32_t __lh3_hash_fun(bit32_t key)
{
#ifndef LH3_HASH_INT
	return __lh3_Wang_hash_int(key);
#else
	return key;
#endif
}
/** hash function for 64-bit integers */
inline bit32_t __lh3_hash_fun(bit64_t key)
{
#ifdef LH3_HASH_INT
	return bit32_t(__lh3_Jenkins_hash_64(key));
#else
	return bit32_t(key>>16) ^ bit32_t(key);
#endif
}
/** hash function for strings (char*) */
inline bit32_t __lh3_hash_fun(const char *key)
{
	return __lh3_X31_hash_string(key);
}

/*
 * Equality operator "=="
 */
/** "==" for 16-bit integers */
inline bool __lh3_key_equal(bit16_t a, bit16_t b)
{
	return a == b;
}
/** "==" for 32-bit integers */
inline bool __lh3_key_equal(bit32_t a, bit32_t b)
{
	return a == b;
}
/** "==" for 64-bit integers */
inline bool __lh3_key_equal(bit64_t a, bit64_t b)
{
	return a == b;
}
/** "==" for strings (char*) */
inline bool __lh3_key_equal(const char *a, const char *b)
{
	return (a && b && strcmp(a, b) == 0);
}

/*
 * Table for primes
 */
const int __lh3_HASH_PRIME_SIZE = 32;
static const bit32_t __lh3_prime_list[__lh3_HASH_PRIME_SIZE] =
{
  0ul,          3ul,          11ul,         23ul,         53ul,
  97ul,         193ul,        389ul,        769ul,        1543ul,
  3079ul,       6151ul,       12289ul,      24593ul,      49157ul,
  98317ul,      196613ul,     393241ul,     786433ul,     1572869ul,
  3145739ul,    6291469ul,    12582917ul,   25165843ul,   50331653ul,
  100663319ul,  201326611ul,  402653189ul,  805306457ul,  1610612741ul,
  3221225473ul, 4294967291ul
};

/** Threshold for rehashing */
const double __lh3_HASH_UPPER = 0.77;

/*
 * Constants and macros for retrieve/set flags "isempty" and "isdel".
 */
typedef bit32_t __lh3_flag_t;
const int __lh3_FLAG_SHIFT = 4;
const int __lh3_FLAG_MASK = 0xful;
const __lh3_flag_t __lh3_FLAG_DEFAULT = 0xaaaaaaaaul;

#define __lh3_isempty(flag, i) ((flag[i>>__lh3_FLAG_SHIFT]>>((i&__lh3_FLAG_MASK)<<1))&2)
#define __lh3_isdel(flag, i) ((flag[i>>__lh3_FLAG_SHIFT]>>((i&__lh3_FLAG_MASK)<<1))&1)
#define __lh3_isboth(flag, i) ((flag[i>>__lh3_FLAG_SHIFT]>>((i&__lh3_FLAG_MASK)<<1))&3)
#define __lh3_set_isdel_false(flag, i) (flag[i>>__lh3_FLAG_SHIFT]&=~(1ul<<((i&__lh3_FLAG_MASK)<<1)))
#define __lh3_set_isempty_false(flag, i) (flag[i>>__lh3_FLAG_SHIFT]&=~(2ul<<((i&__lh3_FLAG_MASK)<<1)))
#define __lh3_set_isboth_false(flag, i) (flag[i>>__lh3_FLAG_SHIFT]&=~(3ul<<((i&__lh3_FLAG_MASK)<<1)))
#define __lh3_set_isdel_true(flag, i) (flag[i>>__lh3_FLAG_SHIFT]|=1ul<<((i&__lh3_FLAG_MASK)<<1))

/*
 * Auxiliary functions for search/insert/erase.
 */
template <class keytype_t>
inline hashint_t __lh3_hash_search_aux(const keytype_t &key, hashint_t m, const keytype_t *keys, const __lh3_flag_t *flag)
{
	if (!m) return 0;
	hashint_t inc, k, i;
	k = __lh3_hash_fun(key);
	i = k % m;
	inc = 1 + k % (m - 1);
	hashint_t last = i;
	while (!__lh3_isempty(flag, i) && !__lh3_key_equal(keys[i], key)) {
		if (i + inc >= m) i = i + inc - m; // inc < m, and so never write this line as: "i += inc - m;"
		else i += inc;
		if (i == last) return m; // fail to find
	}
	return i;
}
template <class keytype_t>
inline hashint_t __lh3_hash_insert_aux(const keytype_t &key, hashint_t m, const keytype_t *keys, const __lh3_flag_t *flag)
{
	hashint_t inc, k, i, site;
	site = m;
	k = __lh3_hash_fun(key);
	i = k % m;
	inc = 1 + k % (m - 1);
	
	hashint_t last = i;
	while (!__lh3_isempty(flag, i) && !__lh3_key_equal(keys[i], key)) {
		if (__lh3_isdel(flag, i)) site = i;
		if (i + inc >= m) i = i + inc - m;
		else i += inc;
		if (i == last) return site;
	}
	if (__lh3_isempty(flag, i) && site != m) return site;
	else return i;
}
template <class keytype_t>
inline hashint_t __lh3_hash_erase_aux(const keytype_t &key, hashint_t m, const keytype_t *keys, __lh3_flag_t *flag)
{
	if (!m) return 0;
	hashint_t i;
	i = __lh3_hash_search_aux(key, m, keys, flag);
	if (i != m && !__lh3_isempty(flag, i)) {
		if (__lh3_isdel(flag, i)) return m; // has been deleted
		__lh3_set_isdel_true(flag, i); // set "isdel" flag as "true"
		return i;
	} else return m;
}

/** "iterator" class for "hash_set_char" and "hash_set_misc" */
template <class keytype_t>
class __lh3_hash_base_iterator
{
protected:
	hashint_t i;
	keytype_t *keys;
	__lh3_flag_t *flags;
public:
	__lh3_hash_base_iterator() {} // No initialization. This is unsafe, but reasonable use will not cause any problems.
	__lh3_hash_base_iterator(hashint_t _i, keytype_t *_keys, __lh3_flag_t *_flags) {
		i = _i; keys = _keys; flags = _flags;
	}
	inline const keytype_t &operator & () { return keys[i]; } // Keys should never be changed by an iterator.
	inline const keytype_t &key() { return keys[i]; } // an alias of the operator "&"
	inline bool operator != (const __lh3_hash_base_iterator &iter) { return i != iter.i; }
	inline bool operator == (const __lh3_hash_base_iterator &iter) { return i == iter.i; }
	inline bool operator < (const __lh3_hash_base_iterator &iter) { return i < iter.i; }
	inline bool operator > (const __lh3_hash_base_iterator &iter) { return i > iter.i; }
	inline void operator ++ () { ++i; }
	inline void operator ++ (int) { ++i; }
	inline void operator -- () { --i; }
	inline void operator -- (int) { --i; }
	inline bool isfilled() { return !__lh3_isboth(flags, i); }
	inline bool operator + () { return isfilled(); } // an alias of "isfilled()"
};

/** "iterator" class for "hash_map_char" and "hash_map_misc" */
template <class keytype_t, class valtype_t>
class __lh3_hash_val_iterator : public __lh3_hash_base_iterator<keytype_t>
{
protected:
	valtype_t *vals;
public:
	__lh3_hash_val_iterator() {}
	__lh3_hash_val_iterator(hashint_t _i, keytype_t *_keys, __lh3_flag_t *_flags, valtype_t *_vals) {
		this->i = _i; this->keys = _keys; this->flags = _flags; vals = _vals;
	}
	inline valtype_t &operator * () { return vals[this->i]; } // Values can be changed here.
	inline const valtype_t &value() { return vals[this->i]; } // the following two functions are alternatives to the operator "*".
	inline void value(const valtype_t &v) { vals[this->i] = v; }
};

/** Base class of all hash classes */
template <class keytype_t>
class __lh3_hash_base_class
{
protected:
	hashint_t n_capacity; /**< maximum size of the hash table */
	hashint_t n_size; /**< number of elements in hash table */
	hashint_t n_occupied; /**< number of cells that have not been flaged as "isempty" (n_capacity >= n_occupied >= n_size) */
	hashint_t upper_bound; /**< The upper bound. When n_occupied exceeds this, rehashing will be performed. */
	__lh3_flag_t *flags; /**< flag array which stores the status "isempty" or "isdel" of each hash cell. */
	keytype_t *keys; /**< array that stores hash keys */
	// return 0 for unchanged, 1 for empty, 2 for deleted
	inline int direct_insert_aux(const keytype_t &key, hashint_t m, keytype_t *K, __lh3_flag_t *F, hashint_t *i) {
		*i = __lh3_hash_insert_aux(key, m, K, F);
		if (__lh3_isempty(F, *i)) {
			K[*i] = key;
			__lh3_set_isboth_false(F, *i);
			return 1;
		} else if (__lh3_isdel(F, *i)) {
			K[*i] = key;
			__lh3_set_isboth_false(F, *i);
			return 2;
		} else return 0;
	}
	inline bool resize_aux1(hashint_t *new_capacity, __lh3_flag_t **new_flags) {
		hashint_t t;
		t = __lh3_HASH_PRIME_SIZE - 1;
		while (__lh3_prime_list[t] > *new_capacity) --t;
		*new_capacity = __lh3_prime_list[t+1];
		if (n_size >= hashint_t(*new_capacity * __lh3_HASH_UPPER + 0.5)) return false; // do not rehash
		keys = (keytype_t*)realloc(keys, *new_capacity * sizeof(keytype_t));
		if (keys == 0) return false; // insufficient memory?
		*new_flags = (__lh3_flag_t*)malloc(*new_capacity * sizeof(__lh3_flag_t));
		if (*new_flags == 0) { // insufficient memory?
			::free(*new_flags); return false;
		}
		for (t = 0; t < ((*new_capacity>>__lh3_FLAG_SHIFT) + 1); ++t)
			(*new_flags)[t] = __lh3_FLAG_DEFAULT;
		return true;
	}
	inline void resize_aux2(hashint_t new_capacity, __lh3_flag_t *new_flags) {
		::free(flags);
		flags = new_flags;
		n_capacity = new_capacity;
		n_occupied = n_size;
		upper_bound = hashint_t(n_capacity * __lh3_HASH_UPPER + 0.5);
	}
	/** Test whether rehashing is needed and perform rehashing if this is the fact. */
	inline void rehash() {
		if (n_occupied >= upper_bound) {
			if (n_capacity > (n_size<<1)) resize(n_capacity - 1); // do not enlarge
			else resize(n_capacity + 1); // enlarge the capacity
		}
	}
public:
	typedef __lh3_hash_base_iterator<keytype_t> iterator;
	__lh3_hash_base_class(void) {
		keys = 0; flags = 0;
		n_capacity = n_size = n_occupied = upper_bound = 0;;
	}
	~__lh3_hash_base_class(void) { ::free(keys); ::free(flags); }
	/** resize the hash table and perform rehashing */
	inline bool resize(hashint_t new_capacity) {
		__lh3_flag_t *new_flags;
		if (!resize_aux1(&new_capacity, &new_flags)) return false;
		for (hashint_t j = 0; j != n_capacity; ++j) {
			if (__lh3_isboth(flags, j) == 0) {
				keytype_t key = keys[j]; // take out the key
				__lh3_set_isdel_true(flags, j); // mark "deleted"
				while (1) {
					hashint_t inc, k, i;
					k = __lh3_hash_fun(key);
					i = k % new_capacity; // calculate the new position
					inc = 1 + k % (new_capacity - 1);
					while (!__lh3_isempty(new_flags, i)) {
						if (i + inc >= new_capacity) i = i + inc - new_capacity;
						else i += inc;
					}
					__lh3_set_isempty_false(new_flags, i);
					if (i < this->n_capacity && __lh3_isboth(flags, i) == 0) { // something is here
						{ keytype_t tmp = keys[i]; keys[i] = key; key = tmp; } // take it out
						__lh3_set_isdel_true(flags, i);
					} else { // put key and quit the loop
						keys[i] = key;
						break;
					}
				}
			}
		}
		resize_aux2(new_capacity, new_flags);
		return true;
	}
	/** get n_size */
	inline hashint_t size(void) const { return n_size; };
	/** get n_capacity */
	inline hashint_t capacity(void) const { return n_capacity; };
	/** the first iterator */
	inline iterator begin() { return iterator(0, keys, flags); }
	/** the last iterator */
	inline iterator end() { return iterator(n_capacity, keys, flags); }
	/** clear the hash table, but do not free the memory */
	inline void clear(void)	{
		if (flags) {
			for (hashint_t t = 0; t < ((n_capacity>>__lh3_FLAG_SHIFT) + 1); ++t)
				flags[t] = __lh3_FLAG_DEFAULT;
		}
		n_size = 0;
	}
	/** clear the hash table and free the memory */
	inline void free() {
		::free(keys); ::free(flags);
		keys = 0; flags = 0;
		n_capacity = n_size = n_occupied = upper_bound = 0;;
	}
};

/** hash_set_misc class */
template <class keytype_t>
class hash_set_misc : public __lh3_hash_base_class<keytype_t>
{
public:
	hash_set_misc(void) {};
	~hash_set_misc(void) {};
	/** search a key */
	inline bool find(const keytype_t &key) const {
		hashint_t i = __lh3_hash_search_aux(key, this->n_capacity, this->keys, this->flags);
		return (i != this->n_capacity && __lh3_isboth(this->flags, i) == 0)? true : false;
	}
	/** insert a key */
	inline bool insert(const keytype_t &key) {
		__lh3_hash_base_class<keytype_t>::rehash();
		hashint_t i;
		int ret = this->direct_insert_aux(key, this->n_capacity, this->keys, this->flags, &i);
		if (ret == 0) return true;
		if (ret == 1) { ++(this->n_size); ++(this->n_occupied); }
		else ++(this->n_size); // then ret == 2
		return false;
	}
	/** delete a key */
	inline bool erase(const keytype_t &key) {
		hashint_t i = __lh3_hash_erase_aux(key, this->n_capacity, this->keys, this->flags);
		if (i != this->n_capacity) {
			--(this->n_size);
			return true;
		} else return false;
	}
};

/** hash_map_misc class */
template <class keytype_t, class valtype_t>
class hash_map_misc : public hash_set_misc<keytype_t>
{
	valtype_t *vals;
	/** a copy of __lh3_hash_base_class<keytype_t>::rehash() */
	inline void rehash() {
		if (this->n_occupied >= this->upper_bound) {
			if (this->n_capacity > (this->n_size<<1)) resize(this->n_capacity - 1);
			else resize(this->n_capacity + 1);
		}
	}
public:
	hash_map_misc(void) { vals = 0; };
	~hash_map_misc(void) { ::free(vals); };
	typedef __lh3_hash_val_iterator<keytype_t, valtype_t> iterator;
	/** analogy of __lh3_hash_base_class<keytype_t>::resize(hashint_t) */
	inline bool resize(hashint_t new_capacity) {
		__lh3_flag_t *new_flags;
		if (!__lh3_hash_base_class<keytype_t>::resize_aux1(&new_capacity, &new_flags)) return false;
		vals = (valtype_t*)realloc(vals, sizeof(valtype_t) * new_capacity);
		if (vals == 0) { // insufficient enough memory?
			::free(new_flags);
			return false;
		}
		for (hashint_t j = 0; j != this->n_capacity; ++j) {
			if (__lh3_isboth(this->flags, j) == 0) {
				keytype_t key = this->keys[j]; // take out the key
				valtype_t val = vals[j];
				__lh3_set_isdel_true(this->flags, j); // mark "deleted"
				while (1) {
					hashint_t inc, k, i;
					k = __lh3_hash_fun(key);
					i = k % new_capacity; // calculate the new position
					inc = 1 + k % (new_capacity - 1);
					while (!__lh3_isempty(new_flags, i)) {
						if (i + inc >= new_capacity) i = i + inc - new_capacity;
						else i += inc;
					}
					__lh3_set_isempty_false(new_flags, i);
					if (i < this->n_capacity && __lh3_isboth(this->flags, i) == 0) { // something is here
						{ keytype_t tmp = this->keys[i]; this->keys[i] = key; key = tmp; } // take it out
						{ valtype_t tmp = vals[i]; vals[i] = val; val = tmp; } // take it out
						__lh3_set_isdel_true(this->flags, i);
					} else { // clear
						this->keys[i] = key;
						vals[i] = val;
						break;
					}
				}
			}
		}
		__lh3_hash_base_class<keytype_t>::resize_aux2(new_capacity, new_flags);
		return true;
	}
	inline bool find(const keytype_t &key, valtype_t *q) const {
		hashint_t i = __lh3_hash_search_aux(key, this->n_capacity, this->keys, this->flags);
		if (i != this->n_capacity && __lh3_isboth(this->flags, i) == 0) {
			*q = vals[i];
			return true;
		} else return false;
	}
	inline bool insert(const keytype_t &key, const valtype_t &val) {
		rehash();
		hashint_t i;
		int ret = this->direct_insert_aux(key, this->n_capacity, this->keys, this->flags, &i);
		vals[i] = val;
		if (ret == 0) return true;
		if (ret == 1) { ++(this->n_size); ++(this->n_occupied); }
		else ++(this->n_size); // then ret == 2
		return false;
	}
	inline bool insert(const keytype_t &key, valtype_t **q) {
		rehash();
		hashint_t i;
		int ret = this->direct_insert_aux(key, this->n_capacity, this->keys, this->flags, &i);
		*q = vals + i;
		if (ret == 0) return true;
		if (ret == 1) { ++(this->n_size); ++(this->n_occupied); }
		else ++(this->n_size); // then ret == 2
		return false;
	}
	inline bool erase(const keytype_t &key) { return hash_set_misc<keytype_t>::erase(key); }
	inline bool erase(const keytype_t &key, valtype_t **q) {
		hashint_t i = __lh3_hash_erase_aux(key, this->n_capacity, this->keys, this->flags);
		if (i != this->n_capacity) {
			--(this->n_size);
			*q = vals + i;
			return true;
		} else return false;
	}
	inline iterator begin() { return iterator(0, this->keys, this->flags, vals); }
	inline iterator end() { return iterator(this->n_capacity, this->keys, this->flags, vals); }
	inline void free() {
		hash_set_misc<keytype_t>::free(); ::free(vals); vals = 0;
	}
};

typedef char *__lh3_char_t;

/** hash_set_char class */
class hash_set_char : public __lh3_hash_base_class<char*>
{
protected:
	inline int insert_aux(const char *key, hashint_t m, char **K, __lh3_flag_t *F, hashint_t *i) {
		*i = __lh3_hash_insert_aux((const __lh3_char_t)key, m, K, F);
		if (__lh3_isempty(F, *i)) {
			K[*i] = strdup(key);
			__lh3_set_isboth_false(F, *i);
			return 1;
		} else if (__lh3_isdel(F, *i)) {
			K[*i] = strdup(key);
			__lh3_set_isboth_false(F, *i);
			return 2;
		} else return 0;
	}
public:
	hash_set_char(void) {};
	~hash_set_char(void) { clear(); };
	inline bool find(const char *key) const {
		hashint_t i = __lh3_hash_search_aux((const __lh3_char_t)key, this->n_capacity, this->keys, this->flags);
		return (i != this->n_capacity && __lh3_isboth(this->flags, i) == 0)? true : false;
	}
	inline bool insert(const char *key) {
		rehash();
		hashint_t i;
		int ret = insert_aux(key, this->n_capacity, this->keys, this->flags, &i);
		if (ret == 0) return true;
		if (ret == 1) { ++(this->n_size); ++(this->n_occupied); }
		else ++(this->n_size); // then ret == 2
		return false;
	}
	inline bool erase(const char *key) {
		hashint_t i = __lh3_hash_erase_aux((const __lh3_char_t)key, this->n_capacity, this->keys, this->flags);
		if (i != this->n_capacity) {
			::free(this->keys[i]); this->keys[i] = 0;
			--(this->n_size);
			return true;
		} else return false;
	}
	inline void clear(void) {
		for (hashint_t i = 0; i != this->n_capacity; ++i)
			if (!__lh3_isboth(this->flags, i)) {
				::free(this->keys[i]); this->keys[i] = 0;
			}
		__lh3_hash_base_class<char*>::clear();	
	}
	inline void free() {
		clear();
		__lh3_hash_base_class<char*>::free();
	}
};

/** hash_map_char class */
template <class valtype_t>
class hash_map_char : public hash_set_char
{
	valtype_t *vals;
	inline void rehash() {
		if (this->n_occupied >= this->upper_bound) {
			if (this->n_capacity > (this->n_size<<1)) resize(this->n_capacity - 1);
			else resize(this->n_capacity + 1);
		}
	}
public:
	typedef __lh3_hash_val_iterator<char*, valtype_t> iterator;
	hash_map_char(void) { vals = 0; };
	~hash_map_char(void) { clear(); ::free(vals); };
	inline valtype_t &val(hashint_t i) { return vals[i]; }
	inline void val(hashint_t i, const valtype_t &v) { return vals[i] = v; }
	inline bool resize(hashint_t new_capacity) {
		__lh3_flag_t *new_flags;
		if (!resize_aux1(&new_capacity, &new_flags)) return false;
		vals = (valtype_t*)realloc(vals, sizeof(valtype_t) * new_capacity);
		if (vals == 0) { // insufficient enough memory?
			::free(new_flags);
			return false;
		}
		for (hashint_t j = 0; j != this->n_capacity; ++j) {
			if (__lh3_isboth(this->flags, j) == 0) {
				char *key = this->keys[j]; // take out the key
				valtype_t val = vals[j];
				__lh3_set_isdel_true(this->flags, j); // mark "deleted"
				while (1) {
					hashint_t inc, k, i;
					k = __lh3_hash_fun(key);
					i = k % new_capacity; // calculate the new position
					inc = 1 + k % (new_capacity - 1);
					while (!__lh3_isempty(new_flags, i)) {
						if (i + inc >= new_capacity) i = i + inc - new_capacity;
						else i += inc;
					}
					__lh3_set_isempty_false(new_flags, i);
					if (i < this->n_capacity && __lh3_isboth(this->flags, i) == 0) { // something is here
						{ char* tmp = this->keys[i]; this->keys[i] = key; key = tmp; } // take it out
						{ valtype_t tmp = vals[i]; vals[i] = val; val = tmp; } // take it out
						__lh3_set_isdel_true(this->flags, i);
					} else { // clear
						this->keys[i] = key;
						vals[i] = val;
						break;
					}
				}
			}
		}
		resize_aux2(new_capacity, new_flags);
		return true;
	}
	inline bool find(const char *key, valtype_t *q) const {
		hashint_t i = __lh3_hash_search_aux((const __lh3_char_t)key, this->n_capacity, this->keys, this->flags);
		if (i != this->n_capacity && __lh3_isboth(this->flags, i) == 0) {
			*q = vals[i];
			return true;
		} else return false;
	}
	inline bool insert(const char *key, const valtype_t &val) {
		rehash();
		hashint_t i;
		int ret = insert_aux(key, this->n_capacity, this->keys, this->flags, &i);
		vals[i] = val;
		if (ret == 0) return true;
		if (ret == 1) { ++(this->n_size); ++(this->n_occupied); }
		else ++(this->n_size); // then ret == 2
		return false;
	}
	inline bool insert(const char *key, valtype_t **q) {
		rehash();
		hashint_t i;
		int ret = insert_aux(key, this->n_capacity, this->keys, this->flags, &i);
		*q = vals + i;
		if (ret == 0) return true;
		if (ret == 1) { ++(this->n_size); ++(this->n_occupied); }
		else ++(this->n_size); // then ret == 2
		return false;
	}
	inline bool erase(const char *key) { return hash_set_char::erase(key); }
	inline bool erase(const char *key, valtype_t **q) {
		hashint_t i = __lh3_hash_erase_aux((const __lh3_char_t)key, this->n_capacity, this->keys, this->flags);
		if (i != this->n_capacity) {
			::free(this->keys[i]); this->keys[i] = 0;
			--(this->n_size);
			*q = vals + i;
			return true;
		} else return false;
	}
	inline iterator begin() { return iterator(0, this->keys, this->flags, vals); }
	inline iterator end() { return iterator(this->n_capacity, this->keys, this->flags, vals); }
	inline void free() {
		hash_set_char::free(); ::free(vals); vals = 0;
	}
};

#endif // LH3_STDHASH_H_