00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #pragma once
00019
00020 #include "../pch.h"
00021 #include "../util/goodies.h"
00022
00023 namespace mongo {
00024
00025
00026
00027
00028
00029
00030 template <class K, class V, int MaxChain>
00031 class LRUishMap {
00032 public:
00033 LRUishMap(int _n) {
00034 n = nextPrime(_n);
00035 keys = new K[n];
00036 hashes = new int[n];
00037 for ( int i = 0; i < n; i++ ) hashes[i] = 0;
00038 }
00039 ~LRUishMap() {
00040 delete[] keys;
00041 delete[] hashes;
00042 }
00043
00044 int _find(const K& k, bool& found) {
00045 int h = k.hash();
00046 assert( h > 0 );
00047 int j = h % n;
00048 int first = j;
00049 for ( int i = 0; i < MaxChain; i++ ) {
00050 if ( hashes[j] == h ) {
00051 if ( keys[j] == k ) {
00052 found = true;
00053 return j;
00054 }
00055 }
00056 else if ( hashes[j] == 0 ) {
00057 found = false;
00058 return j;
00059 }
00060 }
00061 found = false;
00062 return first;
00063 }
00064
00065 V* find(const K& k) {
00066 bool found;
00067 int j = _find(k, found);
00068 return found ? &values[j] : 0;
00069 }
00070
00071 private:
00072 int n;
00073 K *keys;
00074 int *hashes;
00075 V *values;
00076 };
00077
00078 }