blob: 437abe7c36e04e6d2bf75cbc25beed4a3997d0f1 (
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
|
#include "search/cbv.hpp"
#include "std/limits.hpp"
#include "std/vector.hpp"
using namespace my;
namespace search
{
namespace
{
uint64_t const kModulo = 18446744073709551557LLU;
} // namespace
CBV::CBV(unique_ptr<coding::CompressedBitVector> p) : m_p(move(p)) {}
CBV::CBV(CBV && cbv) : m_p(move(cbv.m_p)), m_isFull(cbv.m_isFull) { cbv.m_isFull = false; }
CBV & CBV::operator=(unique_ptr<coding::CompressedBitVector> p)
{
m_p = move(p);
m_isFull = false;
return *this;
}
CBV & CBV::operator=(CBV && rhs)
{
if (this == &rhs)
return *this;
m_p = move(rhs.m_p);
m_isFull = rhs.m_isFull;
rhs.m_isFull = false;
return *this;
}
void CBV::SetFull()
{
m_p.Reset();
m_isFull = true;
}
void CBV::Reset()
{
m_p.Reset();
m_isFull = false;
}
bool CBV::HasBit(uint64_t id) const
{
if (IsFull())
return true;
if (IsEmpty())
return false;
return m_p->GetBit(id);
}
uint64_t CBV::PopCount() const
{
ASSERT(!IsFull(), ());
if (IsEmpty())
return 0;
return m_p->PopCount();
}
CBV CBV::Union(CBV const & rhs) const
{
if (IsFull() || rhs.IsEmpty())
return *this;
if (IsEmpty() || rhs.IsFull())
return rhs;
return CBV(coding::CompressedBitVector::Union(*m_p, *rhs.m_p));
}
CBV CBV::Intersect(CBV const & rhs) const
{
if (IsFull() || rhs.IsEmpty())
return rhs;
if (IsEmpty() || rhs.IsFull())
return *this;
return CBV(coding::CompressedBitVector::Intersect(*m_p, *rhs.m_p));
}
CBV CBV::Take(uint64_t n) const
{
if (IsEmpty())
return *this;
if (IsFull())
{
vector<uint64_t> groups((n + 63) / 64, numeric_limits<uint64_t>::max());
uint64_t const r = n % 64;
if (r != 0)
{
ASSERT(!groups.empty(), ());
groups.back() = (static_cast<uint64_t>(1) << r) - 1;
}
return CBV(coding::DenseCBV::BuildFromBitGroups(move(groups)));
}
return CBV(m_p->LeaveFirstSetNBits(n));
}
uint64_t CBV::Hash() const
{
if (IsEmpty())
return 0;
if (IsFull())
return kModulo;
return coding::CompressedBitVectorHasher::Hash(*m_p) % kModulo;
}
} // namespace search
|