-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
stacked_vector.h
180 lines (148 loc) · 4.57 KB
/
stacked_vector.h
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
/*++
Copyright (c) 2017 Microsoft Corporation
Module Name:
<name>
Abstract:
<abstract>
Author:
Lev Nachmanson (levnach)
Revision History:
--*/
#pragma once
#include "util/vector.h"
namespace lp {
template < typename B> class stacked_vector {
struct log_entry {
unsigned m_i; unsigned m_ts; B b;
log_entry(unsigned i, unsigned t, B const& b): m_i(i), m_ts(t), b(b) {}
log_entry():m_i(UINT_MAX), m_ts(0) {}
};
svector<unsigned> m_stack_of_vector_sizes;
svector<unsigned> m_stack_of_change_sizes;
vector<log_entry> m_log;
vector<B> m_vector;
svector<unsigned> m_last_update;
public:
class ref {
stacked_vector<B> & m_vec;
unsigned m_i;
public:
ref(stacked_vector<B> &m, unsigned key): m_vec(m), m_i(key) {
lp_assert(key < m.size());
}
ref & operator=(const B & b) {
m_vec.emplace_replace(m_i, b);
return *this;
}
ref & operator=(const ref & b) {
m_vec.emplace_replace(m_i, b.m_vec.m_vector[b.m_i]);
return *this;
}
operator const B&() const {
return m_vec.m_vector[m_i];
}
bool operator==(B const& other) const {
return m_vec.m_vector[m_i] == other;
}
bool operator!=(B const& other) const {
return m_vec.m_vector[m_i] != other;
}
B& operator+=(B const &delta) {
// not tracking the change here!
return m_vec.m_vector[m_i] += delta;
}
B& operator-=(B const &delta) {
// not tracking the change here!
return m_vec.m_vector[m_i] -= delta;
}
};
class ref_const {
const stacked_vector<B> & m_vec;
unsigned m_i;
public:
ref_const(const stacked_vector<B> &m, unsigned key) :m_vec(m), m_i(key) {
lp_assert(key < m.size());
}
operator const B&() const {
return m_vec.m_vector[m_i];
}
};
private:
unsigned now() const { return m_stack_of_change_sizes.size(); }
void emplace_replace(unsigned i,const B & b) {
unsigned n = now();
if (m_last_update[i] == n) {
m_vector[i] = b;
}
else if (m_vector[i] != b) {
m_log.push_back(log_entry(i, m_last_update[i], m_vector[i]));
m_vector[i] = b;
m_last_update[i] = n;
}
}
public:
stacked_vector() { }
ref operator[] (unsigned a) {
return ref(*this, a);
}
ref_const operator[] (unsigned a) const {
return ref_const(*this, a);
}
/*
const B & operator[](unsigned a) const {
lp_assert(a < m_vector.size());
return m_vector[a];
}
*/
unsigned size() const {
return m_vector.size();
}
void push() {
m_stack_of_change_sizes.push_back(m_log.size());
m_stack_of_vector_sizes.push_back(m_vector.size());
}
void pop() {
pop(1);
}
template <typename T>
void pop_tail(svector<T> & v, unsigned k) {
lp_assert(v.size() >= k);
v.resize(v.size() - k);
}
template <typename T>
void resize(vector<T> & v, unsigned new_size) {
v.resize(new_size);
}
void pop(unsigned k) {
lp_assert(m_stack_of_vector_sizes.size() >= k);
lp_assert(k > 0);
m_vector.resize(m_stack_of_vector_sizes[m_stack_of_vector_sizes.size() - k]);
m_last_update.resize(m_stack_of_vector_sizes[m_stack_of_vector_sizes.size() - k]);
pop_tail(m_stack_of_vector_sizes, k);
unsigned first_change = m_stack_of_change_sizes[m_stack_of_change_sizes.size() - k];
pop_tail(m_stack_of_change_sizes, k);
for (unsigned j = m_log.size(); j-- > first_change; ) {
const auto & p = m_log[j];
unsigned jc = p.m_i;
if (jc < m_vector.size()) {
m_vector[jc] = p.b; // restore the old value
m_last_update[jc] = p.m_ts;
}
}
resize(m_log, first_change);
}
void push_back(const B & b) {
m_vector.push_back(b);
m_last_update.push_back(now());
}
void increase_size_by_one() {
m_vector.resize(m_vector.size() + 1);
m_last_update.push_back(now());
}
unsigned peek_size(unsigned k) const {
lp_assert(k > 0 && k <= m_stack_of_vector_sizes.size());
return m_stack_of_vector_sizes[m_stack_of_vector_sizes.size() - k];
}
const vector<B>& operator()() const { return m_vector; }
};
}