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

cbv_ptr.hpp « search - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3c9cd68fd8318bc76fd9ee173feef0a78baa6a1b (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
#pragma once

#include "coding/compressed_bit_vector.hpp"

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

#include "std/function.hpp"
#include "std/utility.hpp"

namespace search
{
/// CompressedBitVector pointer class that incapsulates
/// binary operators logic and takes ownership if needed.
class CBVPtr
{
  DISALLOW_COPY_AND_MOVE(CBVPtr);

  coding::CompressedBitVector const * m_ptr = nullptr;
  bool m_isOwner = false;
  bool m_isFull = false;  ///< True iff all bits are set to one.

  void Release();

public:
  CBVPtr() = default;
  CBVPtr(coding::CompressedBitVector const * p, bool isOwner);
  ~CBVPtr() { Release(); }

  inline void SetFull()
  {
    Release();
    m_isFull = true;
  }

  void Set(coding::CompressedBitVector const * p, bool isOwner = false);
  void Set(unique_ptr<coding::CompressedBitVector> p);

  inline coding::CompressedBitVector const * Get() const { return m_ptr; }

  coding::CompressedBitVector const & operator*() const { return *m_ptr; }
  coding::CompressedBitVector const * operator->() const { return m_ptr; }

  bool IsEmpty() const;

  void Union(coding::CompressedBitVector const * p);
  void Intersect(coding::CompressedBitVector const * p);

  template <class TFn>
  void ForEach(TFn && fn) const
  {
    ASSERT(!m_isFull, ());
    if (!IsEmpty())
      coding::CompressedBitVectorEnumerator::ForEach(*m_ptr, forward<TFn>(fn));
  }
};

}  // namespace search