Welcome to mirror list, hosted at ThFree Co, Russian Federation.

small_set.hpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c99fade4e6875556d2706317c25315f2fc5e8081 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#pragma once

#include "base/assert.hpp"
#include "base/bits.hpp"
#include "base/macros.hpp"

#include <cstdint>
#include <iterator>
#include <sstream>
#include <string>

namespace base
{
// A set of nonnegative integers less than |UpperBound|.
//
// Requires UpperBound + O(1) bits of memory.  All operations except
// Clear() and iteration are O(1).  Clear() and iteration require
// O(UpperBound) steps.
//
// *NOTE* This class *IS NOT* thread safe.
template <uint64_t UpperBound>
class SmallSet
{
public:
  static uint64_t constexpr kNumBlocks = (UpperBound + 63) / 64;

  class Iterator
  {
  public:
    using difference_type = uint64_t;
    using value_type = uint64_t;
    using pointer = void;
    using reference = void;
    using iterator_category = std::forward_iterator_tag;

    Iterator(uint64_t const * blocks, uint64_t current_block_index)
      : m_blocks(blocks), m_current_block_index(current_block_index), m_current_block(0)
    {
      ASSERT_LESS_OR_EQUAL(current_block_index, kNumBlocks, ());
      if (current_block_index < kNumBlocks)
        m_current_block = m_blocks[current_block_index];
      SkipZeroes();
    }

    bool operator==(Iterator const & rhs) const
    {
      return m_blocks == rhs.m_blocks && m_current_block_index == rhs.m_current_block_index &&
             m_current_block == rhs.m_current_block;
    }

    bool operator!=(Iterator const & rhs) const { return !(*this == rhs); }

    uint64_t operator*() const
    {
      ASSERT_NOT_EQUAL(m_current_block, 0, ());
      auto const bit = m_current_block & -m_current_block;
      return bits::FloorLog(bit) + m_current_block_index * 64;
    }

    Iterator const & operator++()
    {
      ASSERT(m_current_block_index < kNumBlocks, ());
      ASSERT_NOT_EQUAL(m_current_block, 0, ());
      m_current_block = m_current_block & (m_current_block - 1);
      SkipZeroes();
      return *this;
    }

  private:
    void SkipZeroes()
    {
      ASSERT_LESS_OR_EQUAL(m_current_block_index, kNumBlocks, ());

      if (m_current_block != 0 || m_current_block_index == kNumBlocks)
        return;

      do
        ++m_current_block_index;
      while (m_current_block_index < kNumBlocks && m_blocks[m_current_block_index] == 0);
      if (m_current_block_index < kNumBlocks)
        m_current_block = m_blocks[m_current_block_index];
      else
        m_current_block = 0;
    }

    uint64_t const * m_blocks;
    uint64_t m_current_block_index;
    uint64_t m_current_block;
  };

#define DEFINE_BLOCK_OFFSET(value)   \
  uint64_t const block = value / 64; \
  uint64_t const offset = value % 64

  // This invalidates all iterators except end().
  void Insert(uint64_t value)
  {
    ASSERT_LESS(value, UpperBound, ());

    DEFINE_BLOCK_OFFSET(value);
    auto const bit = kOne << offset;
    m_size += (m_blocks[block] & bit) == 0;
    m_blocks[block] |= bit;
  }

  // This invalidates all iterators except end().
  void Remove(uint64_t value)
  {
    ASSERT_LESS(value, UpperBound, ());

    DEFINE_BLOCK_OFFSET(value);
    auto const bit = kOne << offset;
    m_size -= (m_blocks[block] & bit) != 0;
    m_blocks[block] &= ~bit;
  }

  bool Contains(uint64_t value) const
  {
    ASSERT_LESS(value, UpperBound, ());

    DEFINE_BLOCK_OFFSET(value);
    return m_blocks[block] & (kOne << offset);
  }

#undef DEFINE_BLOCK_OFFSET

  uint64_t Size() const { return m_size; }

  // This invalidates all iterators except end().
  void Clear()
  {
    std::fill(std::begin(m_blocks), std::end(m_blocks), static_cast<uint64_t>(0));
    m_size = 0;
  }

  Iterator begin() const { return Iterator(m_blocks, 0); }
  Iterator cbegin() const { return Iterator(m_blocks, 0); }

  Iterator end() const { return Iterator(m_blocks, kNumBlocks); }
  Iterator cend() const { return Iterator(m_blocks, kNumBlocks); }

private:
  static uint64_t constexpr kOne = 1;

  uint64_t m_blocks[kNumBlocks] = {};
  uint64_t m_size = 0;
};

// static
template <uint64_t UpperBound>
uint64_t constexpr SmallSet<UpperBound>::kNumBlocks;

// static
template <uint64_t UpperBound>
uint64_t constexpr SmallSet<UpperBound>::kOne;

template <uint64_t UpperBound>
std::string DebugPrint(SmallSet<UpperBound> const & set)
{
  std::ostringstream os;
  os << "SmallSet<" << UpperBound << "> [" << set.Size() << ": ";
  for (auto const & v : set)
    os << v << " ";
  os << "]";
  return os.str();
}

// This is a delegate for SmallSet<>, that checks the validity of
// argument in Insert(), Remove() and Contains() methods and does
// nothing when the argument is not valid.
template <uint64_t UpperBound>
class SafeSmallSet
{
public:
  using Set = SmallSet<UpperBound>;
  using Iterator = typename Set::Iterator;

  void Insert(uint64_t value)
  {
    if (IsValid(value))
      m_set.Insert(value);
  }

  void Remove(uint64_t value)
  {
    if (IsValid(value))
      m_set.Remove(value);
  }

  bool Contains(uint64_t value) const { return IsValid(value) && m_set.Contains(value); }

  uint64_t Size() const { return m_set.Size(); }

  void Clear() { m_set.Clear(); }

  Iterator begin() const { return m_set.begin(); }
  Iterator cbegin() const { return m_set.cbegin(); }

  Iterator end() const { return m_set.end(); }
  Iterator cend() const { return m_set.cend(); }

private:
  bool IsValid(uint64_t value) const { return value < UpperBound; }

  Set m_set;
};

template <uint64_t UpperBound>
std::string DebugPrint(SafeSmallSet<UpperBound> const & set)
{
  std::ostringstream os;
  os << "SafeSmallSet<" << UpperBound << "> [" << set.Size() << ": ";
  for (auto const & v : set)
    os << v << " ";
  os << "]";
  return os.str();
}
}  // namespace base