Othello
 All Classes Files Functions Variables Macros
othelloindex.h
Go to the documentation of this file.
1 #pragma once
2 #include "othello.h"
17 template <class keyType>
18 class OthelloIndex : public Othello<keyType> {
19  vector<uint32_t> offset;
20  uint32_t sum(uint32_t h) {
21  uint32_t p = (h & 0x1F);
22  return offset[h>>5] + __builtin_popcount(Othello<keyType>::fillcount[h>>5] & ((1<<p)-1));
23  }
24 public:
31  OthelloIndex(keyType *_keys, uint32_t keycount) :
32  Othello<keyType>(1,_keys, keycount)
33  {
34  offset.resize(Othello<keyType>::fillcount.size());
35  offset[0] = 0;
36  for (int i = 1 ; i < offset.size(); i++) {
37  offset[i] = offset[i-1] + __builtin_popcount(Othello<keyType>::fillcount[i-1]);
38  }
39 
40  }
41  OthelloIndex(unsigned char *v):
42  Othello<keyType>(v) {
44  offset.resize(Othello<keyType>::fillcount.size());
45  }
51  uint32_t query(const keyType &k) {
52  uint32_t ha,hb;
53  uint64_t v = Othello<keyType>::query(k,ha,hb);
54  uint32_t h = v?hb:ha;
55  return sum(h);
56  }
57 
62  void loadDataFromBinaryFile(FILE *pF) {
63  fread(&(Othello<keyType>::mem[0]),sizeof(Othello<keyType>::mem[0]), Othello<keyType>::mem.size(), pF);
65  fread(&(offset[0]),sizeof(offset[0]),offset.size(),pF);
66  }
70  void writeDataToBinaryFile(FILE *pF) {
71  fwrite(&(Othello<keyType>::mem[0]),sizeof(Othello<keyType>::mem[0]), Othello<keyType>::mem.size(), pF);
73  fwrite(&(offset[0]),sizeof(offset[0]),offset.size(),pF);
74  }
75 };
76 
77 
78 
OthelloIndex is a data structure that stores a minimum perfect hash function. For n keys...
Definition: othelloindex.h:18
void loadDataFromBinaryFile(FILE *pF)
load the array from file.
Definition: othelloindex.h:62
Describes the data structure l-Othello. It classifies keys of keyType into 2^L classes.
Definition: othello.h:25
uint32_t query(const keyType &k)
return the index of key k, in range [0..n-1]
Definition: othelloindex.h:51
void writeDataToBinaryFile(FILE *pF)
write array to binary file.
Definition: othelloindex.h:70
valueType query(const keyType &k, uint32_t &ha, uint32_t &hb)
compute the hash value of key and return query value.
Definition: othello.h:344
OthelloIndex(keyType *_keys, uint32_t keycount)
Construct OthelloIndex
Definition: othelloindex.h:31