]> git.sesse.net Git - plocate/blob - db.h
Make some padding in the header explicit.
[plocate] / db.h
1 #ifndef DB_H
2 #define DB_H 1
3
4 #include <stdint.h>
5
6 struct Header {
7         char magic[8];  // "\0plocate";
8         uint32_t version;  // 0.
9         uint32_t hashtable_size;
10         uint32_t extra_ht_slots;
11         uint32_t pad;   // Unused.
12         uint64_t hash_table_offset_bytes;
13         uint64_t filename_index_offset_bytes;
14 };
15
16 struct Trigram {
17         uint32_t trgm;
18         uint32_t num_docids;
19         uint64_t offset;
20
21         bool operator==(const Trigram &other) const
22         {
23                 return trgm == other.trgm;
24         }
25         bool operator<(const Trigram &other) const
26         {
27                 return trgm < other.trgm;
28         }
29 };
30
31 inline uint32_t hash_trigram(uint32_t trgm, uint32_t ht_size)
32 {
33         // CRC-like computation.
34         uint32_t crc = trgm;
35         for (int i = 0; i < 32; i++) {
36                 bool bit = crc & 0x80000000;
37                 crc <<= 1;
38                 if (bit) {
39                         crc ^= 0x1edc6f41;
40                 }
41         }
42         return crc % ht_size;
43 }
44
45 #endif  // !defined(DB_H)