1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef LIBTEXTCLASSIFIER_UTILS_CONTAINER_BIT_VECTOR_H_ 18 #define LIBTEXTCLASSIFIER_UTILS_CONTAINER_BIT_VECTOR_H_ 19 20 #include <set> 21 #include <vector> 22 23 #include "utils/base/integral_types.h" 24 #include "utils/container/bit-vector_generated.h" 25 26 namespace libtextclassifier3 { 27 28 // A read-only bit vector. It does not own the data and it is like a view on 29 // the given data. There are two internal representations, sparse and dense. 30 // The dense one stores every bits. The sparse stores only the indices of 31 // elements that are 1. 32 class BitVector { 33 public: 34 explicit BitVector(const BitVectorData* bit_vector_data); 35 36 // Gets a particular bit. If the underlying data does not contain the 37 // value of the asked bit, false is returned. 38 const bool operator[](int index) const { return Get(index); } 39 40 // Creates a BitVectorDataT using the dense representation. 41 static std::unique_ptr<BitVectorDataT> CreateDenseBitVectorData( 42 const std::vector<bool>& data); 43 44 // Creates a BitVectorDataT using the sparse representation. 45 static std::unique_ptr<BitVectorDataT> CreateSparseBitVectorData( 46 const std::vector<int32>& indices); 47 48 private: 49 const BitVectorData* bit_vector_data_; 50 51 bool Get(int index) const; 52 bool GetFromSparseData(int index) const; 53 bool GetFromDenseData(int index) const; 54 }; 55 56 } // namespace libtextclassifier3 57 #endif // LIBTEXTCLASSIFIER_UTILS_CONTAINER_BIT_VECTOR_H_ 58