-
Notifications
You must be signed in to change notification settings - Fork 4
/
all_ones_sequence.hpp
95 lines (80 loc) · 2.56 KB
/
all_ones_sequence.hpp
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
#pragma once
#include "global_parameters.hpp"
#include "util.hpp"
namespace ds2i {
struct all_ones_sequence {
inline static uint64_t
bitsize(global_parameters const& /* params */, uint64_t universe, uint64_t n)
{
return (universe == n || n == 1) ? 0 : uint64_t(-1);
}
template <typename Iterator>
static void write(succinct::bit_vector_builder&,
Iterator begin,
uint64_t universe, uint64_t n,
global_parameters const&)
{
assert(universe == n || n == 1); (void)universe; (void)n;
assert(*std::next(begin, n - 1) == universe - 1); (void)begin;
}
class enumerator {
public:
typedef std::pair<uint64_t, uint64_t> value_type; // (position, value)
enumerator(succinct::bit_vector const&, uint64_t,
uint64_t universe, uint64_t n,
global_parameters const&)
: m_n(n)
, m_universe(universe)
, m_position(size())
{
assert(universe == n || n == 1); (void)n;
}
value_type move(uint64_t position)
{
assert(position <= size());
m_position = position;
return value();
}
value_type next_geq(uint64_t lower_bound)
{
assert(lower_bound <= m_universe);
if (m_n == 1) {
m_position = 0;
} else {
m_position = lower_bound;
}
return value();
}
value_type next()
{
m_position += 1;
return value();
}
uint64_t size() const
{
return m_n;
}
uint64_t prev_value() const
{
if (m_position == 0) {
return 0;
}
if (m_n == 1) {
return m_universe - 1;
}
return m_position - 1;
}
private:
value_type value() const {
if (m_n == 1) {
return value_type(m_position, m_universe - 1);
} else {
return value_type(m_position, m_position);
}
}
uint64_t m_n;
uint64_t m_universe;
uint64_t m_position;
};
};
}