16 #ifndef INCLUDE_NN_NLIB_SUCCINCT_SBV_H_ 17 #define INCLUDE_NN_NLIB_SUCCINCT_SBV_H_ 23 #include "nn/nlib/Swap.h" 29 #if defined(_MSC_VER) && defined(nx_succinct_EXPORTS) 30 #undef NLIB_VIS_PUBLIC 31 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT 38 static const unsigned int BLK_SIZE = 32;
46 NLIB_MOVE_MEMBER_HELPER_1(
Set, prv_);
48 SetPrivate* tmp = rhs.prv_;
58 uint32_t
Rank0(uint32_t idx)
const NLIB_NOEXCEPT {
59 uint32_t rank1 = this->Rank1(idx);
60 return idx + 1 - rank1;
73 mutable SetPrivate* prv_;
82 NLIB_MOVE_MEMBER_HELPER_2(
Sbv, prv_, set_);
86 SbvPrivate* tmp = rhs.prv_;
91 bool TurnOn(uint32_t idx) NLIB_NOEXCEPT {
return set_.TurnOn(idx); }
92 bool TurnOff(uint32_t idx) NLIB_NOEXCEPT {
return set_.TurnOff(idx); }
94 bool Has(uint32_t idx)
const NLIB_NOEXCEPT {
return set_.Has(idx); }
95 uint32_t
Rank1(uint32_t idx)
const NLIB_NOEXCEPT {
return set_.Rank1(idx); }
96 uint32_t
Rank0(uint32_t idx)
const NLIB_NOEXCEPT {
return set_.Rank0(idx); }
98 int32_t
Select0(uint32_t nth)
const NLIB_NOEXCEPT {
return set_.Select0(nth); }
101 const uint32_t*
GetBitVector() const NLIB_NOEXCEPT {
return set_.GetBitVector(); }
119 lowest_idx_(0xFFFFFFFF),
126 #ifdef NLIB_CXX11_RVALUE_REFERENCES 133 bool Has(uint64_t idx, uint32_t* rank)
const NLIB_NOEXCEPT {
135 uint32_t rank1 = this->Rank1(idx);
136 if (rank) *rank = rank1;
137 if (rank1 == 0)
return false;
138 int64_t select1 = this->Select1Ex(rank1 - 1);
139 NLIB_ASSERT(select1 >= 0);
141 return idx ==
static_cast<uint64_t
>(select1);
143 bool Has(uint64_t idx)
const NLIB_NOEXCEPT {
144 return this->Has(idx, NULL);
147 uint32_t
Rank0(uint64_t idx)
const NLIB_NOEXCEPT {
148 uint32_t rank1 = this->Rank1(idx);
149 return static_cast<uint32_t
>(idx + 1 - rank1);
155 return bitvector_size_;
163 uint32_t GetBits(uint64_t pos,
size_t num)
const NLIB_NOEXCEPT {
164 NLIB_ASSERT(num <= 32);
165 uint64_t idx64 = pos / detail::BLK_SIZE;
166 NLIB_ASSERT(idx64 <= SIZE_MAX);
167 size_t idx =
static_cast<size_t>(idx64);
168 size_t ofs =
static_cast<size_t>(pos % detail::BLK_SIZE);
169 if (ofs + num > 32) {
170 uint64_t b = b_[idx + 1];
173 return static_cast<uint32_t
>((b >> ofs) & ((1 << num) - 1));
175 return (b_[idx] >> ofs) & ((1 << num) - 1);
180 uint64_t bitvector_size_;
181 uint64_t lowest_idx_;
182 uint64_t highest_idx_;
183 uint32_t num_on_bits_;
192 static const size_t UNIT_SIZE = 64;
196 : header_(NULL), data_(NULL), header_size_(0), header_capacity_(0),
197 data_size_(0), data_capacity_(0), workidx_(0) {}
201 #ifdef NLIB_CXX11_RVALUE_REFERENCES 208 work_[workidx_++] = x;
209 if (workidx_ == UNIT_SIZE)
return this->Build();
213 size_t Size() const NLIB_NOEXCEPT {
214 return workidx_ + header_capacity_ * UNIT_SIZE / 2;
232 uint32_t header_size_;
233 uint32_t header_capacity_;
235 uint32_t data_capacity_;
237 uint32_t work_[UNIT_SIZE];
250 NLIB_MOVE_MEMBER_HELPER_2(
Map, set_, value_);
253 value_.swap(rhs.value_);
256 if (value_.Size() != 0)
return false;
257 return set_.Init(bv_size);
260 if (!set_.TurnOn(idx))
return false;
261 value_.PushBack(data);
267 return std::make_pair(
true, value_[set_.Rank1(idx) - 1]);
269 return std::make_pair(
false, 0);
273 return std::make_pair(
true, value_[set_.Rank1(idx) - 1]);
275 return std::make_pair(
false, 0);
278 size_t value_size = value_.MemSize();
279 size_t set_size = set_.MemSize();
280 return value_size + set_size;
289 if (!value_.Export(w))
return false;
290 if (!set_.Export(w))
return false;
294 if (!value_.Import(r) || !set_.Import(r)) {
310 NLIB_DEFINE_STD_SWAP(::nlib_ns::succinct::Set)
311 NLIB_DEFINE_STD_SWAP(::nlib_ns::succinct::Sbv)
312 NLIB_DEFINE_STD_SWAP(::nlib_ns::succinct::SparseSet)
313 NLIB_DEFINE_STD_SWAP(::nlib_ns::succinct::CompressedArray)
314 NLIB_DEFINE_STD_SWAP(::nlib_ns::succinct::Map)
316 #if defined(_MSC_VER) && defined(nx_succinct_EXPORTS) 317 #undef NLIB_VIS_PUBLIC 318 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT 321 #endif // INCLUDE_NN_NLIB_SUCCINCT_SBV_H_ uint32_t Rank1(uint32_t idx) const noexcept
Performs a Rank operation.
bool TurnOff(uint32_t idx) noexcept
Removes a 32-bit unsigned integer from the set.
Sbv() noexcept
Instantiates the object.
Substitute definitions for the C++11 standard header type_traits. These substitute definitions are us...
uint32_t IdxType
typedef for the integer index type.
size_t MemSize() const noexcept
Returns the amount of memory explicitly allocated by the class.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
bool TurnOn(uint32_t idx) noexcept
Adds a 32-bit unsigned integer to the set.
Succinct data structure to hold a set of 32-bit unsigned integers for a Rank/Select operation...
Defines the class for reading binary files from streams.
int32_t Select0(uint32_t nth) const noexcept
Returns the position of the nth 0 bit. nth starts from 0.
FindType Find(IdxType idx) noexcept
Specifies a key and gets the value in the associative array.
bool Has(uint64_t idx, uint32_t *rank) const noexcept
Tests to determine whether a particular value is contained in the set.
void swap(Map &rhs) noexcept
Swaps the contents of an object.
bool Init(IdxType bv_size) noexcept
Initializes an object.
bool Has(uint32_t idx) const noexcept
Tests to determine whether a particular value is contained in the set.
void swap(Set &rhs) noexcept
Swaps the contents of an object.
uint32_t Rank0(uint64_t idx) const noexcept
Performs a Rank operation.
Set() noexcept
Instantiates the object.
void swap(Sbv &rhs) noexcept
Swaps the contents of an object.
const SetType & GetKeys() const noexcept
Gets the key set.
bool Export(BinaryWriter *w) const noexcept
Writes the object to the file.
CompressedArray() noexcept
Instantiates the object.
const ArrayType & GetValues() const noexcept
Gets the set of values.
uint32_t Rank0(uint32_t idx) const noexcept
Performs a Rank operation.
Succinct data structure to hold a set of 32-bit unsigned integers for a Rank/Select operation...
An empty structure indicating that an argument to a function needs to be moved.
const uint32_t * GetBitVector() const noexcept
Returns the pointer to the bit vector.
SparseSet() noexcept
Instantiates the object.
uint64_t GetBitVectorSize() const noexcept
Returns the size of the bit vector.
A compressed array of integers that allows appending additional data.
bool TurnOn(IdxType idx, uint32_t data) noexcept
Adds a key and value.
Succinct data structure to hold a nowhere dense set of 64-bit unsigned integers.
SetType::IdxType IdxType
Integer index.
Defines the class for writing binary files to streams.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
void Reset() noexcept
Returns the object to the state immediately after the constructor is called.
bool Has(uint64_t idx) const noexcept
Tests to determine whether a particular value is contained in the set.
A file that contains the configuration information for each development environment.
Defines the class for handing bit streams operated on by Rank and Select.
uint64_t IdxType
typedef for the integer index type.
bool Build() noexcept
Builds an associative array.
bool Import(BinaryReader *r) noexcept
Reads the written object.
Map() noexcept
Instantiates the object.
The class for writing binary to streams (to OutputStream).
A compact integer to integer read-only associative array.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
const FindType Find(IdxType idx) const noexcept
Specifies a key and gets the value in the associative array.
The class for reading binary from streams (from InputStream).
uint32_t IdxType
typedef for the integer index type.
uint32_t Rank0(uint32_t idx) const noexcept
Performs a Rank operation.
size_t Size() const noexcept
Gets the array length.
uint32_t GetBitVectorSize() const noexcept
Returns the size of the bit vector.
bool PushBack(uint32_t x) noexcept
Adds data. Compresses data after it becomes 64 elements long.
std::pair< bool, uint32_t > FindType
Pair of boolean value and integer. If the value is not found, the boolean value is false...
~Map() noexcept
Destructor.