PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/core/hash.h

https://github.com/nardo/torque_sockets
C Header | 103 lines | 84 code | 18 blank | 1 comment | 7 complexity | 6835ec88cab99e627699bce6d7bb54fb MD5 | raw file
  1. static uint32 next_larger_hash_prime(uint32 number)
  2. {
  3. assert(number < 4294967291ul);
  4. static uint32 primes[] = { 5, 11, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593, 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469, 12582917, 25165843, 50331653, 100663319, 201326611, 402653189, 805306457, 1610612741, 3221225473ul, 4294967291ul, };
  5. uint32 *walk = primes;
  6. while(number >= *walk)
  7. walk++;
  8. return *walk;
  9. };
  10. class looping_counter
  11. {
  12. public:
  13. looping_counter(uint32 initial_value, uint32 count) { assert(count > 0); _index = ((initial_value < count) ? 0 : (initial_value % count)); _count = count; }
  14. looping_counter &operator=(uint32 value) { _index = value % _count; return *this; }
  15. looping_counter &operator++() { _index++; if(_index >= _count) _index = 0; return *this; }
  16. operator uint32() { return _index; }
  17. private:
  18. uint32 _index;
  19. uint32 _count;
  20. };
  21. template <typename type> inline uint32 hash(const type &x)
  22. {
  23. return x.hash();
  24. }
  25. template <typename type> inline uint32 hash(const type *x)
  26. {
  27. return x->hash();
  28. }
  29. uint32 hash_buffer(const void *buffer, uint32 len)
  30. {
  31. uint8 *buf = (uint8 *) buffer;
  32. uint32 result = 0;
  33. while(len--)
  34. result = ((result << 8) | (result >> 24)) ^ uint32(*buf++);
  35. return result;
  36. }
  37. template <typename signature> uint32 hash_method(signature the_method)
  38. {
  39. return hash_buffer((void *) &the_method, sizeof(the_method));
  40. }
  41. #define numeric_hash_overload(type) template<> inline uint32 hash<type>(const type &x) { return uint32(x); }
  42. typedef void *void_ptr;
  43. template<> inline uint32 hash<void_ptr>(const void_ptr &x) {
  44. return uint32(x);
  45. }
  46. numeric_hash_overload(int)
  47. numeric_hash_overload(uint32)
  48. template<> inline uint32 hash(const char *x) { return uint32(x); }
  49. //template<class type> uint32 hash(const type *x) { return uint32(x); }
  50. template<class table> class hash_table_tester
  51. {
  52. public:
  53. static void dump_table(table &the_table)
  54. {
  55. typename table::pointer p;
  56. printf("table has %d elements:\n", the_table.size());
  57. for(p = the_table.first(); p; ++p)
  58. printf("table element[%d]: %d -> %s\n", p.index(), *(p.key()), *(p.value()));
  59. }
  60. static void run(bool verbose = false)
  61. {
  62. static const char *values[] = { "one: foo", "two: bar", "three: foobar", "four: cool companies", "five: apple", "six: garagegames", "seven: google", "eight: tesla", "nine: peace", "ten: in the world", "eleven: is worth working towards", "twelve: if it really involves work", "" };
  63. static int keys[] = { 300201, 12002, 58003, 9203, 278904, 87305, 880006, 123007, 3267008, 50409, 230010, 30659011, 2301012 };
  64. table the_table;
  65. for(int i = 0; values[i][0]; i++)
  66. {
  67. printf(".inserting \"%d\", \"%s\"\n", keys[i], values[i]);
  68. the_table.insert(keys[i], values[i]);
  69. if(verbose)
  70. dump_table(the_table);
  71. }
  72. dump_table(the_table);
  73. printf(".removing elements two...four\n");
  74. for(int i = 1; i < 4; i++)
  75. {
  76. typename table::pointer p = the_table.find(keys[i]);
  77. printf("key %d %s\n", keys[i], bool(p) ? "found." : "NOT FOUND - ERROR!");
  78. if(p)
  79. p.remove();
  80. if(verbose)
  81. dump_table(the_table);
  82. }
  83. dump_table(the_table);
  84. }
  85. };