1#ifndef OSMIUM_INDEX_ID_SET_HPP
2#define OSMIUM_INDEX_ID_SET_HPP
70 virtual ~
IdSet() noexcept = default;
75 virtual
void set(T
id) = 0;
80 virtual
bool get(T
id) const noexcept = 0;
106 default_chunk_bits = 22U
111 template <
typename T, std::
size_t chunk_bits = detail::default_chunk_bits>
117 template <
typename T, std::
size_t chunk_bits>
128 while (m_value != m_last && !m_set->
get(m_value)) {
129 const T cid = id_set::chunk_id(m_value);
130 assert(cid < m_set->m_data.size());
131 if (!m_set->
m_data[cid]) {
132 m_value = (cid + 1) << (chunk_bits + 3);
134 const auto slot = m_set->
m_data[cid][id_set::offset(m_value)];
160 if (m_value != m_last) {
174 return m_set == rhs.m_set && m_value == rhs.m_value;
178 return !(*
this == rhs);
181 T operator*() const noexcept {
182 assert(m_value < m_last);
195 template <
typename T, std::
size_t chunk_bits>
202 chunk_size = 1U << chunk_bits
205 std::vector<std::unique_ptr<unsigned char[]>>
m_data;
209 return id >> (chunk_bits + 3U);
212 static std::size_t
offset(T
id)
noexcept {
213 return (
id >> 3U) & ((1U << chunk_bits) - 1U);
217 return 1U << (
id & 0x7U);
221 return static_cast<T
>(m_data.size()) * chunk_size * 8;
225 const auto cid = chunk_id(
id);
226 if (cid >= m_data.size()) {
227 m_data.resize(cid + 1);
230 auto& chunk = m_data[cid];
232 chunk.reset(
new unsigned char[chunk_size]);
233 ::memset(chunk.get(), 0, chunk_size);
236 return chunk[offset(
id)];
245 swap(first.m_data, second.m_data);
246 swap(first.m_size, second.m_size);
253 m_size(other.m_size) {
254 m_data.reserve(other.
m_data.size());
255 for (
const auto& ptr : other.
m_data) {
257 m_data.emplace_back(
new unsigned char[chunk_size]);
258 ::memcpy(m_data.back().get(), ptr.get(), chunk_size);
260 m_data.emplace_back();
284 bool check_and_set(T
id) {
285 auto& element = get_element(
id);
287 if ((element & bitmask(
id)) == 0) {
288 element |= bitmask(
id);
302 (void)check_and_set(
id);
311 auto& element = get_element(
id);
313 if ((element & bitmask(
id)) != 0) {
314 element &= ~bitmask(
id);
324 bool get(T
id)
const noexcept final {
325 if (chunk_id(
id) >= m_data.size()) {
328 const auto* r = m_data[chunk_id(
id)].get();
332 return (r[offset(
id)] & bitmask(
id)) != 0;
358 return m_data.size() * chunk_size;
362 return {
this, 0, last()};
366 return {
this, last(), last()};
375 template <
typename T>
396 if (m_data.empty() || m_data.back() !=
id) {
397 m_data.push_back(
id);
406 bool get(T
id)
const noexcept final {
407 const auto it = std::find(m_data.cbegin(), m_data.cend(),
id);
408 return it != m_data.cend();
422 return std::binary_search(m_data.cbegin(), m_data.cend(),
id);
429 return m_data.empty();
445 std::sort(m_data.begin(), m_data.end());
446 const auto last = std::unique(m_data.begin(), m_data.end());
447 m_data.erase(last, m_data.end());
457 std::size_t
size() const noexcept {
458 return m_data.size();
462 return m_data.capacity() *
sizeof(T);
472 std::vector<T> new_data;
473 new_data.reserve(m_data.size() + other.
m_data.size());
474 std::set_union(m_data.cbegin(), m_data.cend(),
476 std::back_inserter(new_data));
478 swap(new_data, m_data);
485 return m_data.cbegin();
489 return m_data.cend();
493 return m_data.cbegin();
497 return m_data.cend();
Definition: id_set.hpp:118
value_type & reference
Definition: id_set.hpp:150
T m_last
Definition: id_set.hpp:125
void next() noexcept
Definition: id_set.hpp:127
IdSetDenseIterator operator++(int) noexcept
Definition: id_set.hpp:167
IdSetDenseIterator & operator++() noexcept
Definition: id_set.hpp:159
T m_value
Definition: id_set.hpp:124
IdSetDenseIterator(const id_set *set, T value, T last) noexcept
Definition: id_set.hpp:152
const id_set * m_set
Definition: id_set.hpp:123
bool operator!=(const IdSetDenseIterator &rhs) const noexcept
Definition: id_set.hpp:177
bool operator==(const IdSetDenseIterator &rhs) const noexcept
Definition: id_set.hpp:173
T value_type
Definition: id_set.hpp:148
std::forward_iterator_tag iterator_category
Definition: id_set.hpp:147
value_type * pointer
Definition: id_set.hpp:149
Definition: id_set.hpp:196
IdSetDense & operator=(IdSetDense other)
Definition: id_set.hpp:265
const_iterator begin() const
Definition: id_set.hpp:361
IdSetDense(const IdSetDense &other)
Definition: id_set.hpp:251
void set(T id) final
Definition: id_set.hpp:301
std::vector< std::unique_ptr< unsigned char[]> > m_data
Definition: id_set.hpp:205
IdSetDense(IdSetDense &&) noexcept=default
static std::size_t chunk_id(T id) noexcept
Definition: id_set.hpp:208
T last() const noexcept
Definition: id_set.hpp:220
friend void swap(IdSetDense &first, IdSetDense &second) noexcept
Definition: id_set.hpp:243
T size() const noexcept
Definition: id_set.hpp:345
void clear() final
Definition: id_set.hpp:352
const_iterator end() const
Definition: id_set.hpp:365
static std::size_t offset(T id) noexcept
Definition: id_set.hpp:212
static unsigned int bitmask(T id) noexcept
Definition: id_set.hpp:216
unsigned char & get_element(T id)
Definition: id_set.hpp:224
void unset(T id)
Definition: id_set.hpp:310
std::size_t used_memory() const noexcept final
Definition: id_set.hpp:357
bool get(T id) const noexcept final
Definition: id_set.hpp:324
bool empty() const noexcept final
Definition: id_set.hpp:338
Definition: id_set.hpp:376
std::size_t used_memory() const noexcept final
Definition: id_set.hpp:461
typename std::vector< T >::const_iterator const_iterator
Iterator type. There is no non-const iterator.
Definition: id_set.hpp:482
void merge_sorted(const IdSetSmall< T > &other)
Definition: id_set.hpp:471
const_iterator cend() const noexcept
Definition: id_set.hpp:496
const_iterator begin() const noexcept
Definition: id_set.hpp:484
IdSetSmall(const IdSetSmall &)=default
const_iterator end() const noexcept
Definition: id_set.hpp:488
const_iterator cbegin() const noexcept
Definition: id_set.hpp:492
std::vector< T > m_data
Definition: id_set.hpp:378
bool get_binary_search(T id) const noexcept
Definition: id_set.hpp:421
bool empty() const noexcept final
Definition: id_set.hpp:428
bool get(T id) const noexcept final
Definition: id_set.hpp:406
void clear() final
Definition: id_set.hpp:435
IdSetSmall & operator=(const IdSetSmall &)=default
void sort_unique()
Definition: id_set.hpp:444
IdSetSmall(IdSetSmall &&) noexcept=default
std::size_t size() const noexcept
Definition: id_set.hpp:457
Definition: id_set.hpp:58
virtual bool empty() const =0
virtual std::size_t used_memory() const noexcept=0
IdSet(const IdSet &)=default
IdSet & operator=(const IdSet &)=default
virtual bool get(T id) const noexcept=0
IdSet(IdSet &&) noexcept=default
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: location.hpp:555