-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom_states.h
370 lines (317 loc) · 8.99 KB
/
atom_states.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#ifndef ATOM_STATES_H
#define ATOM_STATES_H
#include "problems.h"
#include <list>
#include <deque>
#include <map>
extern "C" {
#include "md4.h"
};
class Application;
class Atom;
class AtomSet;
class State;
class ValueMap;
class hash_t;
class stateHash_t;
template<class T>
class hashEntry_t;
/*******************************************************************************
*
* state
*
******************************************************************************/
/*
The state_t class represents a state as a bit vector. Each bit encodes the
value of a state variable. In turn, each state variable is a literal over a
ground atom. Thus, the value of a state variable bit is 1/true if the
corresponding ground atom holds in a state, and 0 otherwise.
*/
class state_t
{
// The bit vector representing the state
unsigned *data_;
// The number of atoms in the problem, i.e. the number of bits in a state
static size_t size_;
// The maximum number of bytes a state's internal data occupies on the heap
static size_t max_internal_size_in_bytes_;
/*
The hash holding pointers to all states encountered so far. These pointers
serve as unique state identifiers throughout the code base, so that different
parts of he code don't need to keep their own state copies
*/
static stateHash_t *state_hash_;
public:
explicit state_t()
{
notify( this, "state_t::state_t()" );
data_ = (unsigned*)calloc( size_, sizeof(unsigned) );
}
state_t( const State& state );
state_t( const state_t& state )
{
notify( this, "state_t::state_t(state_t&)" );
data_ = (unsigned*)malloc( size_ * sizeof(unsigned) );
memcpy( data_, state.data_, size_ * sizeof(unsigned) );
}
state_t( const atomList_t &alist );
state_t( const problem_t& problem );
~state_t() { free( data_ ); }
/*
Initializes the state hash.
*/
static void initialize( const problem_t &problem );
/*
Initializes the state hash.
*/
static void finalize( void );
/*
Prints the state hash stats.
*/
static void statistics( std::ostream &os );
/*
Retrieves the state ID (i.e., the pointer to the "main" copy of the state)
for the given state.
*/
static const state_t* get_state( const state_t &state );
/*
Gets the state ID (a state pointer) along with the state's hash value.
*/
static std::pair<const state_t*, unsigned> get_state_and_hash( const state_t &state );
/*
Deletes the given state from the state hash.
*/
static void destroy_state( const state_t &state );
static size_t size( void ) { return( size_ ); }
static size_t size_in_bytes( void ) { return max_internal_size_in_bytes_; }
/*
Deprecated in this version.
*/
void make_digest( void ) { }
bool make_check( void ) const { return( true ); }
/*
Returns the bit vector representing the state.
*/
const unsigned* data( void ) const { return( data_ ); }
/*
Computes the state's hash value.
*/
inline unsigned hash_value( void ) const
{
#if 1
unsigned *ptr, result;
unsigned char digest[16];
MD4_CTX context;
// compute MD4 digests
MD4Init( &context );
MD4Update( &context, (unsigned char*)data_, size_ * sizeof(unsigned) );
MD4Final( digest, &context );
// compact digest into unsigned (assumes sizeof(unsigned) = 4)
ptr = (unsigned*)digest;
result = (ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3]);
return( result );
#else
unsigned value = 0;
for( size_t i = 0; i < size_; ++i )
value = value ^ data_[i];
return( value );
#endif
}
unsigned digest( void ) const { return( hash_value() ); }
void operator=( const state_t &state )
{
memcpy( data_, state.data_, size_ * sizeof(unsigned) );
}
bool operator==( const state_t &state ) const
{
return( (size_ == state.size_) &&
!memcmp( data_, state.data_, size_ * sizeof(unsigned) ) );
}
/*
Checks whether the state variable corresponding to the given atom has
value 1 in this state. This amounts to figuring out the bit corresponding
to this atom and seeing whether it is set to 1.
*/
bool holds( ushort_t atom ) const
{
#if 0
register size_t i = atom % 32;
register unsigned data = data_[atom>>5];
for( size_t j = 0; j < i; data = (data>>1), ++j );
return( data % 2 );
#else
register size_t i = atom >> 5;
register size_t j = atom % 32;
return( data_[i] & (0x1<<j) );
#endif
}
bool holds( const Atom &atom ) const
{
return( holds( problem_t::atom_hash_get( atom ) ) );
}
/*
Sets the bit of the state variable correspoding to the given groud atom
to 1.
*/
bool add( ushort_t atom )
{
register size_t i = atom >> 5;
register size_t j = atom % 32;
bool rv = !(data_[i] & (0x1<<j));
data_[i] = data_[i] | (0x1<<j);
return( rv );
}
bool add( const Atom &atom ) { return( add( problem_t::atom_hash_get( atom ) ) ); }
/*
Sets the bit of the state variable correspoding to the given groud atom
to 0.
*/
bool clear( ushort_t atom )
{
register size_t i = atom >> 5;
register size_t j = atom % 32;
bool rv = (data_[i] & (0x1<<j));
data_[i] = data_[i] & ~(0x1<<j);
return( rv );
}
bool clear( const Atom &atom ) { return( clear( problem_t::atom_hash_get( atom ) ) ); }
/*
Prints the IDs of atoms that hold in the given state.
*/
void print( std::ostream &os ) const;
/*
Prints the names of atoms that hold in the given state.
*/
void full_print( std::ostream &os, const problem_t *problem ) const;
public: // iterators
/*
Iterator over the set of atoms that are set to "true" in a given state.
*/
class const_predicate_iterator
{
const state_t *state_;
size_t idx_;
protected:
const_predicate_iterator( const state_t &st, size_t idx = 0 ) : state_(&st), idx_(idx) { }
void first( void )
{
for( idx_ = 0; (idx_ < state_->size()) && !state_->data()[idx_]; ++idx_ );
if( idx_ < state_->size() )
{
register unsigned data = state_->data()[idx_];
for( idx_ = (idx_<<5); data % 2 == 0; data = (data>>1), ++idx_ );
}
else
{
idx_ = idx_ << 5;
}
}
size_t increase( void ) const
{
register unsigned data = 0;
register size_t i = idx_ >> 5;
register size_t j = idx_ % 32;
if( j == 31 )
{
j = 0;
for( ++i; (i < state_->size()) && !state_->data()[i]; ++i );
if( i < state_->size() ) data = state_->data()[i];
}
else
{
data = (state_->data()[i]) >> (j+1);
if( data != 0 )
{
++j;
}
else
{
j = 0;
for( ++i; (i < state_->size()) && !state_->data()[i]; ++i );
if( i < state_->size() ) data = state_->data()[i];
}
}
if( i < state_->size() )
{
i = (i << 5) + j;
for( ; data % 2 == 0; data = (data>>1), ++i );
}
else
{
i = i << 5;
}
return( i );
}
public:
const_predicate_iterator() : state_(0), idx_(0) { }
ushort_t operator*( void ) const { return( idx_ ); }
const_predicate_iterator operator++( void ) // pre increment
{
idx_ = increase();
return( *this );
}
const_predicate_iterator operator++( int ) // post increment
{
const_predicate_iterator it( *this ); // use default copy const.
idx_ = increase();
return( it );
}
bool operator==( const const_predicate_iterator &it ) const
{
return( (state_ == it.state_) && (idx_ == it.idx_) );
}
bool operator!=( const const_predicate_iterator &it ) const
{
return( (state_ != it.state_) || (idx_ != it.idx_) );
}
friend class state_t;
};
const const_predicate_iterator predicate_begin( void ) const
{
const_predicate_iterator it( *this );
it.first();
return( it );
}
const const_predicate_iterator predicate_end( void ) const
{
return( const_predicate_iterator( *this, (size()<<5) ) );
}
};
inline std::ostream&
operator<<( std::ostream &os, const state_t &state )
{
state.print( os );
return( os );
}
/*******************************************************************************
*
* state/probability list
*
******************************************************************************/
/*
A list intended to hold state-probability pairs, useful in several places
in the code.
*/
class stateProbList_t : public std::list<std::pair<state_t*,double> >
{
public:
stateProbList_t()
{
notify( this, "stateProbList_t::stateProbList_t()" );
}
stateProbList_t( state_t *state, double p )
{
notify( this, "stateProbList_t::stateProbList_t(state_t*,double)" );
push_back( std::make_pair(state,p) );
}
stateProbList_t( const stateProbList_t &state_list )
{
notify( this, "stateProbList_t::stateProbList_t(stateProbList_t&)" );
for( stateProbList_t::const_iterator si = state_list.begin(); si != state_list.end(); ++si )
{
state_t *state = new state_t( *(*si).first );
push_back( std::make_pair( state, (*si).second ) );
}
}
};
#endif // ATOM_STATES_H