1 // Copyright (C) 2019 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef ICING_INDEX_TERM_ID_HIT_PAIR_H_ 16 #define ICING_INDEX_TERM_ID_HIT_PAIR_H_ 17 18 #include <cstdint> 19 #include <limits> 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include "icing/index/hit/hit.h" 25 #include "icing/util/bit-util.h" 26 27 namespace icing { 28 namespace lib { 29 30 class TermIdHitPair { 31 public: 32 // Layout bits: 24 termid + 32 hit value + 8 hit term frequency. 33 using Value = uint64_t; 34 35 static constexpr int kTermIdBits = 24; 36 static constexpr int kHitValueBits = sizeof(Hit::Value) * 8; 37 static constexpr int kHitTermFrequencyBits = sizeof(Hit::TermFrequency) * 8; 38 39 static const Value kInvalidValue; 40 value_(v)41 explicit TermIdHitPair(Value v = kInvalidValue) : value_(v) {} 42 TermIdHitPair(uint32_t term_id,const Hit & hit)43 TermIdHitPair(uint32_t term_id, const Hit& hit) { 44 static_assert(kTermIdBits + kHitValueBits + kHitTermFrequencyBits <= 45 sizeof(Value) * 8, 46 "TermIdHitPairTooBig"); 47 48 value_ = 0; 49 // Term id goes into the most significant bits because it takes 50 // precedent in sorts. 51 bit_util::BitfieldSet(term_id, kHitValueBits + kHitTermFrequencyBits, 52 kTermIdBits, &value_); 53 bit_util::BitfieldSet(hit.value(), kHitTermFrequencyBits, kHitValueBits, 54 &value_); 55 bit_util::BitfieldSet(hit.term_frequency(), 0, kHitTermFrequencyBits, 56 &value_); 57 } 58 term_id()59 uint32_t term_id() const { 60 return bit_util::BitfieldGet(value_, kHitValueBits + kHitTermFrequencyBits, 61 kTermIdBits); 62 } 63 hit()64 Hit hit() const { 65 return Hit( 66 bit_util::BitfieldGet(value_, kHitTermFrequencyBits, kHitValueBits), 67 bit_util::BitfieldGet(value_, 0, kHitTermFrequencyBits)); 68 } 69 value()70 Value value() const { return value_; } 71 72 bool operator==(const TermIdHitPair& rhs) const { 73 return value_ == rhs.value_; 74 } 75 76 private: 77 Value value_; 78 }; 79 80 } // namespace lib 81 } // namespace icing 82 83 #endif // ICING_INDEX_TERM_ID_HIT_PAIR_H_ 84