0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 17:10:48 +01:00
mongodb/util/hashtab.h

104 lines
2.0 KiB
C
Raw Normal View History

2007-10-20 01:35:48 +02:00
/* hashtab.h
Simple, fixed size hash table. Darn simple.
Uses a contiguous block of memory, so you can put it in a memory mapped file very easily.
*/
2007-11-10 22:46:30 +01:00
#pragma once
2007-10-20 01:35:48 +02:00
#include "../stdafx.h"
#include <map>
#pragma pack(push)
#pragma pack(1)
/* you should define:
int Key::hash() return > 0 always.
*/
template <
class Key,
class Type
>
class HashTable {
2007-11-02 03:34:44 +01:00
public:
2007-10-20 01:35:48 +02:00
const char *name;
struct Node {
int hash;
Key k;
Type value;
2007-11-02 03:34:44 +01:00
bool inUse() { return hash != 0; }
2007-10-20 01:35:48 +02:00
} *nodes;
int n;
int _find(const Key& k, bool& found) {
found = false;
int h = k.hash();
int i = h % n;
int start = i;
int chain = 0;
while( 1 ) {
2007-11-02 03:34:44 +01:00
if( !nodes[i].inUse() ) {
2007-10-20 01:35:48 +02:00
return i;
}
if( nodes[i].hash == h && nodes[i].k == k ) {
found = true;
return i;
}
chain++;
i = (i+1) % n;
if( i == start ) {
cout << "warning: hashtable is full " << name << endl;
return -1;
}
if( chain == 200 )
cout << "warning: hashtable long chain " << name << endl;
}
}
public:
/* buf must be all zeroes on initialization. */
HashTable(void *buf, int buflen, const char *_name) : name(_name) {
int m = sizeof(Node);
2007-11-03 02:30:40 +01:00
// cout << "hashtab init, buflen:" << buflen << " m:" << m << endl;
2007-10-20 01:35:48 +02:00
n = buflen / m;
if( (n & 1) == 0 )
n--;
nodes = (Node *) buf;
assert(nodes[n-1].hash == 0);
assert(nodes[0].hash == 0);
2007-11-27 19:40:08 +01:00
2008-01-27 20:51:47 +01:00
assert( sizeof(Node) == 628 );
//cout << "HashTable() " << _name << " sizeof(node):" << sizeof(Node) << " n:" << n << endl;
2007-10-20 01:35:48 +02:00
}
Type* get(const Key& k) {
bool found;
int i = _find(k, found);
if( found )
return &nodes[i].value;
return 0;
}
void put(const Key& k, const Type& value) {
bool found;
int i = _find(k, found);
if( i < 0 )
return;
if( !found ) {
nodes[i].k = k;
nodes[i].hash = k.hash();
}
else {
assert( nodes[i].hash == k.hash() );
}
nodes[i].value = value;
}
};
#pragma pack(pop)