forked from venkatarun95/genericCC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhiskertree.cc
268 lines (224 loc) · 5.09 KB
/
whiskertree.cc
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <cassert>
#include <cmath>
#include <algorithm>
#include <numeric>
#include "whiskertree.hh"
using namespace std;
WhiskerTree::WhiskerTree()
: _domain( Memory(), MAX_MEMORY() ),
_children(),
_leaf( 1, Whisker( _domain ) )
{
}
WhiskerTree::WhiskerTree( const Whisker & whisker, const bool bisect )
: _domain( whisker.domain() ),
_children(),
_leaf()
{
if ( !bisect ) {
_leaf.push_back( whisker );
} else {
for ( auto &x : whisker.bisect() ) {
_children.push_back( WhiskerTree( x, false ) );
}
}
}
void WhiskerTree::reset_counts( void )
{
if ( is_leaf() ) {
_leaf.front().reset_count();
} else {
for ( auto &x : _children ) {
x.reset_counts();
}
}
}
const Whisker & WhiskerTree::use_whisker( const Memory & _memory, const bool track ) const
{
const Whisker * ret( whisker( _memory ) );
if ( !ret ) {
fprintf( stderr, "ERROR: No whisker found for %s\n", _memory.str().c_str() );
exit( 1 );
}
assert( ret );
ret->use();
if ( track ) {
ret->domain().track( _memory );
}
return *ret;
}
const Whisker * WhiskerTree::whisker( const Memory & _memory ) const
{
if ( !_domain.contains( _memory ) ) {
return nullptr;
}
if ( is_leaf() ) {
return &_leaf[ 0 ];
}
/* need to descend */
for ( auto &x : _children ) {
auto ret( x.whisker( _memory ) );
if ( ret ) {
return ret;
}
}
assert( false );
return nullptr;
}
const Whisker * WhiskerTree::most_used( const unsigned int max_generation ) const
{
if ( is_leaf() ) {
if ( (_leaf.front().generation() <= max_generation)
&& (_leaf.front().count() > 0) ) {
return &_leaf[ 0 ];
}
return nullptr;
}
/* recurse */
unsigned int count_max = 0;
const Whisker * ret( nullptr );
for ( auto &x : _children ) {
const Whisker * candidate( x.most_used( max_generation ) );
if ( candidate
&& (candidate->generation() <= max_generation)
&& (candidate->count() >= count_max) ) {
ret = candidate;
count_max = candidate->count();
}
}
return ret;
}
void WhiskerTree::reset_generation( void )
{
if ( is_leaf() ) {
assert( _leaf.size() == 1 );
assert( _children.empty() );
_leaf.front().demote( 0 );
} else {
for ( auto &x : _children ) {
x.reset_generation();
}
}
}
void WhiskerTree::promote( const unsigned int generation )
{
if ( is_leaf() ) {
assert( _leaf.size() == 1 );
assert( _children.empty() );
_leaf.front().promote( generation );
} else {
for ( auto &x : _children ) {
x.promote( generation );
}
}
}
bool WhiskerTree::replace( const Whisker & w )
{
if ( !_domain.contains( w.domain().range_median() ) ) {
return false;
}
if ( is_leaf() ) {
assert( w.domain() == _leaf.front().domain() );
_leaf.front() = w;
return true;
}
for ( auto &x : _children ) {
if ( x.replace( w ) ) {
return true;
}
}
assert( false );
return false;
}
bool WhiskerTree::replace( const Whisker & src, const WhiskerTree & dst )
{
if ( !_domain.contains( src.domain().range_median() ) ) {
return false;
}
if ( is_leaf() ) {
assert( src.domain() == _leaf.front().domain() );
/* convert from leaf to interior node */
*this = dst;
return true;
}
for ( auto &x : _children ) {
if ( x.replace( src, dst ) ) {
return true;
}
}
assert( false );
return false;
}
unsigned int WhiskerTree::total_whisker_queries( void ) const
{
if ( is_leaf() ) {
assert( _children.empty() );
return _leaf.front().domain().count();
}
return accumulate( _children.begin(),
_children.end(),
0,
[]( const unsigned int sum,
const WhiskerTree & x )
{ return sum + x.total_whisker_queries(); } );
}
string WhiskerTree::str() const
{
return str( total_whisker_queries() );
}
string WhiskerTree::str( const unsigned int total ) const
{
if ( is_leaf() ) {
assert( _children.empty() );
string tmp = string( "[" ) + _leaf.front().str( total ) + "]";
return tmp;
}
string ret;
for ( const auto &x : _children ) {
ret += x.str( total );
}
return ret;
}
unsigned int WhiskerTree::num_children( void ) const
{
if ( is_leaf() ) {
assert( _leaf.size() == 1 );
return 1;
}
return _children.size();
}
bool WhiskerTree::is_leaf( void ) const
{
return !_leaf.empty();
}
RemyBuffers::WhiskerTree WhiskerTree::DNA( void ) const
{
RemyBuffers::WhiskerTree ret;
/* set domain */
ret.mutable_domain()->CopyFrom( _domain.DNA() );
/* set children */
if ( is_leaf() ) {
ret.mutable_leaf()->CopyFrom( _leaf.front().DNA() );
} else {
for ( auto &x : _children ) {
RemyBuffers::WhiskerTree *child = ret.add_children();
*child = x.DNA();
}
}
return ret;
}
WhiskerTree::WhiskerTree( const RemyBuffers::WhiskerTree & dna )
: _domain( dna.domain() ),
_children(),
_leaf()
{
if ( dna.has_leaf() ) {
assert( dna.children_size() == 0 );
_leaf.emplace_back( dna.leaf() );
} else {
assert( dna.children_size() > 0 );
for ( const auto &x : dna.children() ) {
_children.emplace_back( x );
}
}
}