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_DEFMOVE_PIMPL(
Set);
54 uint32_t
Rank0(uint32_t idx)
const NLIB_NOEXCEPT {
55 uint32_t rank1 = this->Rank1(idx);
56 return idx + 1 - rank1;
69 mutable SetPrivate* prv_;
78 #ifdef __cpp_rvalue_references 83 set_ = std::move(rhs.set_);
93 rhs.set_.assign(rhs.set_,
move_tag());
100 bool TurnOn(uint32_t idx) NLIB_NOEXCEPT {
return set_.TurnOn(idx); }
101 bool TurnOff(uint32_t idx) NLIB_NOEXCEPT {
return set_.TurnOff(idx); }
103 bool Has(uint32_t idx)
const NLIB_NOEXCEPT {
return set_.Has(idx); }
104 uint32_t
Rank1(uint32_t idx)
const NLIB_NOEXCEPT {
return set_.Rank1(idx); }
105 uint32_t
Rank0(uint32_t idx)
const NLIB_NOEXCEPT {
return set_.Rank0(idx); }
107 int32_t
Select0(uint32_t nth)
const NLIB_NOEXCEPT {
return set_.Select0(nth); }
110 const uint32_t*
GetBitVector() const NLIB_NOEXCEPT {
return set_.GetBitVector(); }
128 : bitvector_size_(0), lowest_idx_(0xFFFFFFFF), highest_idx_(0), num_on_bits_(0),
129 width_(0), blen_(0), b_(
nullptr) {}
131 #ifdef __cpp_rvalue_references 141 bool Has(uint64_t idx, uint32_t* rank)
const NLIB_NOEXCEPT {
143 uint32_t rank1 = this->Rank1(idx);
144 if (rank) *rank = rank1;
145 if (rank1 == 0)
return false;
146 int64_t select1 = this->Select1Ex(rank1 - 1);
147 NLIB_ASSERT(select1 >= 0);
149 return idx ==
static_cast<uint64_t
>(select1);
151 bool Has(uint64_t idx)
const NLIB_NOEXCEPT {
152 return this->Has(idx,
nullptr);
155 uint32_t
Rank0(uint64_t idx)
const NLIB_NOEXCEPT {
156 uint32_t rank1 = this->Rank1(idx);
157 return static_cast<uint32_t
>(idx + 1 - rank1);
163 return bitvector_size_;
170 uint32_t GetBits(uint64_t pos,
size_t num)
const NLIB_NOEXCEPT {
171 NLIB_ASSERT(num <= 32);
172 uint64_t idx64 = pos / detail::BLK_SIZE;
173 NLIB_ASSERT(idx64 <= SIZE_MAX);
174 size_t idx =
static_cast<size_t>(idx64);
175 size_t ofs =
static_cast<size_t>(pos % detail::BLK_SIZE);
176 if (ofs + num > 32) {
177 uint64_t b = b_[idx + 1];
180 return static_cast<uint32_t
>((b >> ofs) & ((1 << num) - 1));
182 return (b_[idx] >> ofs) & ((1 << num) - 1);
187 uint64_t bitvector_size_;
188 uint64_t lowest_idx_;
189 uint64_t highest_idx_;
190 uint32_t num_on_bits_;
199 static const size_t UNIT_SIZE = 64;
203 : header_(
nullptr), data_(
nullptr), header_size_(0), header_capacity_(0),
204 data_size_(0), data_capacity_(0), workidx_(0), work_() {}
208 #ifdef __cpp_rvalue_references 214 work_[workidx_++] = x;
215 if (workidx_ == UNIT_SIZE)
return this->Build();
219 size_t Size() const NLIB_NOEXCEPT {
220 return workidx_ + header_capacity_ * UNIT_SIZE / 2;
238 uint32_t header_size_;
239 uint32_t header_capacity_;
241 uint32_t data_capacity_;
243 uint32_t work_[UNIT_SIZE];
256 #ifdef __cpp_rvalue_references 257 #ifdef NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS 259 Map& operator=(
Map&& rhs) =
default;
263 set_ = std::move(rhs.set_);
264 value_ = std::move(rhs.value_);
273 value_.assign(rhs.value_,
move_tag());
277 bool Init(IdxType bv_size) NLIB_NOEXCEPT {
278 if (value_.Size() != 0)
return false;
279 return set_.Init(bv_size);
281 bool TurnOn(IdxType idx, uint32_t data) NLIB_NOEXCEPT {
282 if (!set_.TurnOn(idx))
return false;
283 value_.PushBack(data);
286 bool Build() NLIB_NOEXCEPT {
return set_.Build(); }
287 FindType
Find(IdxType idx) NLIB_NOEXCEPT {
289 return std::make_pair(
true, value_[set_.Rank1(idx) - 1]);
291 return std::make_pair(
false, 0);
293 const FindType
Find(IdxType idx)
const NLIB_NOEXCEPT {
295 return std::make_pair(
true, value_[set_.Rank1(idx) - 1]);
297 return std::make_pair(
false, 0);
300 size_t value_size = value_.MemSize();
301 size_t set_size = set_.MemSize();
302 return value_size + set_size;
304 const SetType&
GetKeys() const NLIB_NOEXCEPT {
return set_; }
305 const ArrayType&
GetValues() const NLIB_NOEXCEPT {
return value_; }
311 if (!value_.Export(w))
return false;
312 if (!set_.Export(w))
return false;
316 if (!value_.Import(r) || !set_.Import(r)) {
332 NLIB_DEFINE_STD_SWAP(::nlib_ns::succinct::Set)
333 NLIB_DEFINE_STD_SWAP(::nlib_ns::succinct::Sbv)
334 NLIB_DEFINE_STD_SWAP(::nlib_ns::succinct::SparseSet)
335 NLIB_DEFINE_STD_SWAP(::nlib_ns::succinct::CompressedArray)
336 NLIB_DEFINE_STD_SWAP(::nlib_ns::succinct::Map)
342 swap(prv_, rhs.prv_);
346 swap(set_, rhs.set_);
347 swap(prv_, rhs.prv_);
351 swap(set_, rhs.set_);
352 swap(value_, rhs.value_);
356 #if defined(_MSC_VER) && defined(nx_succinct_EXPORTS) 357 #undef NLIB_VIS_PUBLIC 358 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT 361 #endif // INCLUDE_NN_NLIB_SUCCINCT_SBV_H_ uint32_t Rank1(uint32_t idx) const noexcept
Performs a Rank operation.
~Sbv() noexcept
Destructor.
bool TurnOff(uint32_t idx) noexcept
Removes a 32-bit unsigned integer from the set.
constexpr 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.
bool Init(IdxType bv_size) noexcept
Initializes an object.
~CompressedArray() noexcept
Destructor.
bool Has(uint32_t idx) const noexcept
Tests to determine whether a particular value is contained in the set.
uint32_t Rank0(uint64_t idx) const noexcept
Performs a Rank operation.
constexpr Set() noexcept
Instantiates the object.
Map & assign(Map &rhs, move_tag) noexcept
Corresponds to a move assignment operator.
Sbv & assign(Sbv &rhs, move_tag) noexcept
Corresponds to a move assignment operator.
Sbv & operator=(Sbv &&rhs) noexcept
Move assignment operator. This function is useful when using C++11.
~SparseSet() noexcept
Destructor.
const SetType & GetKeys() const noexcept
Gets the key set.
bool Export(BinaryWriter *w) const noexcept
Writes the object to the file.
constexpr CompressedArray() noexcept
Instantiates the object.
Map(Map &rhs, move_tag) noexcept
Corresponds to a move constructor.
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...
Sbv(Sbv &rhs, move_tag) noexcept
Corresponds to a move constructor.
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.
constexpr SparseSet() noexcept
Instantiates the object.
~Set() noexcept
Destructor.
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.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
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.
Sbv(Sbv &&rhs) noexcept
Instantiates the object (move constructor). This function is useful when using C++11.
bool Build() noexcept
Builds an associative array.
bool Import(BinaryReader *r) noexcept
Reads the written object.
constexpr 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.