-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.h
159 lines (140 loc) · 4.12 KB
/
utils.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
#ifndef UTILS_H
#define UTILS_H
#include "global.h"
#include <stdlib.h>
#include <assert.h>
#include <iostream>
class state_t;
/*******************************************************************************
*
* atom list
*
******************************************************************************/
class atomList_t
{
size_t size_;
ushort_t *data_, *data_ptr_;
public:
atomList_t() : size_(0), data_(0), data_ptr_(0)
{
notify( this, "atomList_t::atomList_t()" );
}
atomList_t( const atomList_t &alist );
atomList_t( const ushort_t *array, size_t sz );
~atomList_t() { if( data_ ) free( data_ ); }
unsigned hash_value( void ) const;
bool equal( const ushort_t *array, size_t sz ) const;
void intersect( const atomList_t &alist );
bool empty_intersection( const atomList_t &alist ) const;
size_t intersection_size( const atomList_t &alist ) const;
size_t size( void ) const { return( data_ptr_ - data_ ); }
ushort_t atom( size_t i ) const { return( data_[i] ); }
bool find( ushort_t atm ) const;
void insert( ushort_t atm );
void insert( const atomList_t &alist );
void remove( ushort_t atm );
void remove( const atomList_t &alist );
bool holds( ushort_t atm, bool nprec = false ) const
{
if( nprec )
return( find( atm ) );
else
return( atm%2 ? !find(atm) : find(atm) );
}
bool holds( const state_t &state, bool nprec = false ) const;
bool holds( const atomList_t &alist, bool nprec = false ) const;
bool contradiction( void ) const
{
for( size_t i = 0; i+1 < size(); ++i )
if( (data_[i] % 2 == 0) && (data_[i]+1 == data_[i+1]) ) return( true );
return( false );
}
void clear( void ) { data_ptr_ = data_; }
void print( std::ostream &os ) const;
bool operator==( const atomList_t &alist ) const;
atomList_t& operator=( const atomList_t &effect );
};
inline std::ostream&
operator<<( std::ostream &os, const atomList_t& alist )
{
alist.print( os );
return( os );
}
inline bool
atomList_t::find( ushort_t atm ) const
{
for( size_t i = 0; i < size(); ++i )
if( atom( i ) == atm ) return( true );
return( false );
}
inline void
atomList_t::insert( ushort_t atm )
{
size_t i;
for( i = 0; (i < size()) && (atom( i ) < atm); ++i );
if( (i == size()) || (atom( i ) > atm) )
{
if( i == size() )
{
if( !data_ || (data_ptr_ == &data_[size_]) )
{
size_ = (!data_ ? 1 : size_ << 1);
ushort_t *ndata_ = (ushort_t*)realloc( data_, size_ * sizeof(ushort_t) );
data_ptr_ = (!data_ ? ndata_ : &ndata_[size()]);
data_ = ndata_;
}
*data_ptr_++ = atm;
}
else
{
assert( data_ != NULL );
if( data_ptr_ == &data_[size_] )
{
size_ = size_ << 1;
ushort_t *ndata_ = (ushort_t*)realloc( data_, size_ * sizeof(ushort_t) );
data_ptr_ = (!data_ ? ndata_ : &ndata_[size()]);
data_ = ndata_;
}
for( int j = (int)size(); j > (int)i; --j )
data_[j] = data_[j-1];
data_[i] = atm;
++data_ptr_;
}
}
}
inline void
atomList_t::remove( ushort_t atm )
{
size_t i;
for( i = 0; (i < size()) && (atom( i ) != atm); ++i );
if( i < size() )
{
for( size_t j = i; j < size(); ++j )
data_[j] = data_[j+1];
--data_ptr_;
}
}
/*******************************************************************************
*
* atom list list
*
******************************************************************************/
class atomListList_t
{
size_t size_;
atomList_t **data_, **data_ptr_;
public:
atomListList_t() : size_(0), data_(0), data_ptr_(0) { }
~atomListList_t() { if( data_ ) free( data_ ); }
size_t size( void ) const { return( data_ptr_ - data_ ); }
atomList_t& atom_list( size_t i ) { return( *data_[i] ); }
const atomList_t& atom_list( size_t i ) const { return( *data_[i] ); }
bool find( const atomList_t &alist ) const;
void insert( atomList_t *alist );
bool holds( const state_t &state, bool nprec = false ) const;
void clear( void ) { data_ptr_ = data_; }
void print( std::ostream &os ) const;
bool operator==( const atomListList_t &alist ) const;
atomListList_t& operator=( const atomListList_t &alist );
};
#endif // UTILS_H