|
| 1 | +#ifndef SUFFIX_H_ |
| 2 | +#define SUFFIX_H_ |
| 3 | + |
| 4 | +#include "bitvector.hpp" |
| 5 | + |
| 6 | +#include <assert.h> |
| 7 | + |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +#include "config.hpp" |
| 11 | +#include "hash.hpp" |
| 12 | + |
| 13 | +namespace surf { |
| 14 | + |
| 15 | +// Max suffix_len_ = 64 bits |
| 16 | +// For kReal suffixes, if the stored key is not long enough to provide |
| 17 | +// suffix_len_ suffix bits, its suffix field is cleared (i.e., all 0's) |
| 18 | +// to indicate that there is no suffix info associated with the key. |
| 19 | +class BitvectorSuffix : public Bitvector { |
| 20 | +public: |
| 21 | + BitvectorSuffix() : type_(kNone), suffix_len_(0) {}; |
| 22 | + |
| 23 | + BitvectorSuffix(const SuffixType type, const position_t suffix_len, |
| 24 | + const std::vector<std::vector<word_t> >& bitvector_per_level, |
| 25 | + const std::vector<position_t>& num_bits_per_level, |
| 26 | + const level_t start_level = 0, |
| 27 | + level_t end_level = 0/* non-inclusive */) |
| 28 | + : Bitvector(bitvector_per_level, num_bits_per_level, start_level, end_level) { |
| 29 | + assert(suffix_len <= kWordSize); |
| 30 | + type_ = type; |
| 31 | + suffix_len_ = suffix_len; |
| 32 | + } |
| 33 | + |
| 34 | + static word_t constructSuffix(const std::string& key, const level_t level, const position_t len); |
| 35 | + |
| 36 | + SuffixType getType() const { |
| 37 | + return type_; |
| 38 | + } |
| 39 | + |
| 40 | + position_t getSuffixLen() const { |
| 41 | + return suffix_len_; |
| 42 | + } |
| 43 | + |
| 44 | + position_t size() const { |
| 45 | + position_t bitvector_mem = (num_bits_ / kWordSize) * (kWordSize / 8); |
| 46 | + if (num_bits_ % kWordSize == 0) |
| 47 | + bitvector_mem += (kWordSize / 8); |
| 48 | + return (sizeof(BitvectorSuffix) + bitvector_mem); |
| 49 | + } |
| 50 | + |
| 51 | + word_t read(const position_t idx) const; |
| 52 | + bool checkEquality(const position_t idx, const std::string& key, const level_t level) const; |
| 53 | + |
| 54 | + // Compare stored suffix to querying suffix. |
| 55 | + // kReal suffix type only. |
| 56 | + int compare(const position_t idx, const std::string& key, const level_t level) const; |
| 57 | + |
| 58 | +private: |
| 59 | + static inline word_t constructHashSuffix(const std::string& key, const position_t len); |
| 60 | + static inline word_t constructRealSuffix(const std::string& key, const level_t level, const position_t len); |
| 61 | + |
| 62 | +private: |
| 63 | + SuffixType type_; |
| 64 | + position_t suffix_len_; // in bits |
| 65 | +}; |
| 66 | + |
| 67 | +static word_t BitvectorSuffix::constructSuffix(const std::string& key, const level_t level, const position_t len) { |
| 68 | + switch (type_) { |
| 69 | + case kHash: |
| 70 | + return constructHashSuffix(key, len); |
| 71 | + case kReal: |
| 72 | + return constructRealSuffix(key, level, len); |
| 73 | + default: |
| 74 | + return 0; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +word_t BitvectorSuffix::read(const position_t idx) const { |
| 79 | + assert(idx * suffix_len_ < num_bits_); |
| 80 | + position_t bit_pos = idx * suffix_len_; |
| 81 | + position_t word_id = bit_pos / kWordSize; |
| 82 | + position_t offset = bit_pos & (kWordSize - 1); |
| 83 | + return (bits_[word_id] << offset) >> (kWordSize - suffix_len_); |
| 84 | +} |
| 85 | + |
| 86 | +bool BitvectorSuffix::checkEquality(const position_t idx, const std::string& key, const level_t level) const { |
| 87 | + if (type_ == kNone) return true; |
| 88 | + assert(idx * suffix_len_ < num_bits_); |
| 89 | + word_t stored_suffix = read(idx); |
| 90 | + // if no suffix info for the stored key |
| 91 | + if (type_ == kReal && stored_suffix == 0) return true; |
| 92 | + // if the querying key is shorter than the stored key |
| 93 | + if (type_ == kReal && ((key.length() - level) * 8) < suffix_len_) return false; |
| 94 | + word_t querying_suffix = constructSuffix(key, level, suffix_len_); |
| 95 | + if (stored_suffix == querying_suffix) return true; |
| 96 | + return false; |
| 97 | +} |
| 98 | + |
| 99 | +int BitvectorSuffix::compare(const position_t pos, const std::string& key, const level_t level) const { |
| 100 | + assert(type_ == kReal); |
| 101 | + assert(idx * suffix_len_ < num_bits_); |
| 102 | + word_t stored_suffix = read(idx); |
| 103 | + if (stored_suffix == 0) return -1; |
| 104 | + word_t querying_suffix = constructSuffixFromKey(key, level); |
| 105 | + if (stored_suffix < querying_suffix) return -1; |
| 106 | + else if (stored_suffix == querying_suffix) return 0; |
| 107 | + else return 1; |
| 108 | +} |
| 109 | + |
| 110 | +static inline word_t BitvectorSuffix::constructHashSuffix(const std::string& key, const position_t len) { |
| 111 | + word_t suffix = suffixHash(key); |
| 112 | + clearMSBits(suffix, len); |
| 113 | + return suffix; |
| 114 | +} |
| 115 | + |
| 116 | +static inline word_t BitvectorSuffix::constructRealSuffix(const std::string& key, const level_t level, const position_t len) { |
| 117 | + word_t suffix = 0; |
| 118 | + level_t key_len = (level_t)key.length(); |
| 119 | + position_t num_complete_bytes = suffix_len_ / 8; |
| 120 | + if (num_complete_bytes > 0) { |
| 121 | + if (level < key_len) |
| 122 | + suffix += (word_t)key[level]; |
| 123 | + for (position_t i = 1; i < num_complete_bytes; i++) { |
| 124 | + suffix <<= 8; |
| 125 | + if ((level + i) < key_len) |
| 126 | + suffix += (word_t)key[level + i]; |
| 127 | + } |
| 128 | + } |
| 129 | + position_t offset = suffix_len_ % 8; |
| 130 | + if (offset > 0) { |
| 131 | + suffix << offset; |
| 132 | + word_t remaining_bits = 0; |
| 133 | + if ((level + num_complete_bytes) < key_len) |
| 134 | + remaining_bits = (word_t)key[level + num_complete_bytes]; |
| 135 | + remaining_bits >>= (8 - offset); |
| 136 | + suffix += remaining_bits; |
| 137 | + } |
| 138 | + return suffix; |
| 139 | +} |
| 140 | + |
| 141 | +} // namespace surf |
| 142 | + |
| 143 | +#endif // SUFFIXVECTOR_H_ |
0 commit comments