Permalink
Browse files

README記述

  • Loading branch information...
1 parent ca76332 commit 0376c99b3a793b4e807df7f2eeb579d2438d6e6d @sile committed Jan 21, 2012
Showing with 140 additions and 178 deletions.
  1. +8 −0 README
  2. +11 −48 include/hamt/hash.hh
  3. +121 −130 include/hamt/map.hh
View
8 README
@@ -0,0 +1,8 @@
+[概要]
+- HAMT(Hash Array Mapped Trie)実装
+ -- HAMT:
+  --- Bagwell, P. (2001) Ideal Hash Trees. Technical Report, 2001
+  --- http://lampwww.epfl.ch/papers/idealhashtrees.pdf
+- ハッシュマップの一種
+
+=> 単純なハッシュテーブル(ex: https://github.com/sile/cc-dict)に対して性能的な優位性が見られなかったの、ある程度(性能評価が可能なレベル)動くところまでしか作っていない
View
@@ -2,69 +2,32 @@
#define HAMT_HASH_HH
#include <string>
+#include <cassert>
namespace hamt {
- const unsigned GOLDEN_RATIO_PRIME=(2^31) + (2^29) - (2^25) + (2^22) - (2^19) - (2^16) + 1;
+ const unsigned GOLDEN_RATIO_PRIME=0x9e370001; //(2^31) + (2^29) - (2^25) + (2^22) - (2^19) - (2^16) + 1;
template<class Key>
class hash_functor {
public:
hash_functor() {}
- unsigned operator()(const Key& key) const {
- return hash(key);
- }
-
unsigned operator()(const Key& key, unsigned n) const {
+ assert(n == 0); // XXX: 複数のハッシュ関数には未対応
return hash(key, n);
}
};
- unsigned hash(int key) {
- return key * GOLDEN_RATIO_PRIME;
- }
-
- unsigned hash(unsigned key) {
- return key * GOLDEN_RATIO_PRIME;
- }
-
- unsigned hash(long key) {
- // XXX: if sizeof(long) > sizeof(unsigned), the calculation will lose high bits information of key.
- return key * GOLDEN_RATIO_PRIME;
- }
-
- unsigned hash(const char* key) {
- return hash(reinterpret_cast<long>(key));
- }
-
- unsigned hash(const std::string& key) {
- unsigned h = GOLDEN_RATIO_PRIME;
- for(const char* c=key.c_str(); *c != 0; c++)
- h = (h*33) + *c;
- return h;
- }
-
- unsigned hash(int key, unsigned n) {
- return key * GOLDEN_RATIO_PRIME * n;
- }
-
- unsigned hash(unsigned key, unsigned n) {
- return key * GOLDEN_RATIO_PRIME * n;
- }
-
- unsigned hash(long key, unsigned n) {
- // XXX: if sizeof(long) > sizeof(unsigned), the calculation will lose high bits information of key.
- return key * GOLDEN_RATIO_PRIME * n;
- }
-
- unsigned hash(const char* key, unsigned n) {
- return hash(reinterpret_cast<long>(key));
- }
+ unsigned hash(unsigned key, unsigned n) { return key * GOLDEN_RATIO_PRIME; }
+ unsigned hash(int key, unsigned n) { return hash(static_cast<unsigned>(key), n); }
+ unsigned hash(long key, unsigned n) { return hash(static_cast<unsigned>(key), n ); }
+ unsigned hash(const char* key, unsigned n) { return hash(reinterpret_cast<long>(key), n); }
unsigned hash(const std::string& key, unsigned n) {
- unsigned h = GOLDEN_RATIO_PRIME * 33 + n;
- for(const char* c=key.c_str(); *c != 0; c++)
- h = (h*33) + *c;
+ unsigned h = GOLDEN_RATIO_PRIME;
+ for(unsigned i=0; i < n; i++) // XXX: 適当
+ for(const char* c=key.c_str(); *c != 0; c++)
+ h = (h*33) + *c;
return h;
}
}
Oops, something went wrong.

0 comments on commit 0376c99

Please sign in to comment.