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
/*
* This file is part of the source code of the software program
* Vampire. It is protected by applicable
* copyright laws.
*
* This source code is distributed under the licence found here
* https://vprover.github.io/license.html
* and in the source directory
*/
/**
* @file ArrayMap.hpp
* Defines a class ArrayMap, a map from a bounded range of unsigned keys
* with constant time reset via timestamps.
*
* @since 30/12/2007 Manchester
*/
#ifndef __ArrayMap__
#define __ArrayMap__
#include "Forwards.hpp"
#include "DArray.hpp"
namespace Lib {
/**
* An entry of the @b ArrayMap object
*/
template<typename T>
struct ArrayMapEntry
{
ArrayMapEntry() : _timestamp(0) {}
T _obj;
unsigned _timestamp;
};
/**
* A map from @b size_t type to elements @b T
*
* The @b ensure function has to be called with an upper bound of
* key values.
*/
template<typename T>
class ArrayMap
: DArray<ArrayMapEntry<T> >
{
typedef ArrayMapEntry<T> Entry;
public:
USE_ALLOCATOR(ArrayMap<T>);
/**
* Create the map object
*
* The @b ensure function has to be called to determine what keys
* can be stored in the map
*/
ArrayMap() : DArray<Entry>(8), _timestamp(1) {}
/**
* Create the map object that can contain keys less than @b size
*/
ArrayMap(size_t size) : DArray<Entry>(size), _timestamp(1) {}
/**
* Make the map empty
*/
void reset()
{
_timestamp++;
if(_timestamp==0) {
Entry* nptr=DArray<Entry>::_array;
Entry* afterLast=DArray<Entry>::_array +DArray<Entry>::_capacity;
while(nptr!=afterLast) {
(nptr++)->_timestamp=0;
}
_timestamp=1;
}
}
using DArray<Entry>::keepRecycled;
/**
* Ensure that keys less than @b size can be stored in the map
*
* Call to this function makes the content of the map undefined,
* so @b reset should be called after each call to this function.
*/
inline
void ensure(size_t size)
{
DArray<Entry>::ensure(size);
}
/**
* Ensure that keys less than @b size can be stored in the map.
* The current content of the map remains unchanged.
*/
inline
void expand(size_t size)
{
DArray<Entry>::expand(size);
}
/**
* Insert the object @b obj into the map under the key @b index.
* There must not have been any object stored under the key
* @b index before.
*/
inline
void insert(size_t index, T obj=T())
{
Entry& e=(*this)[index];
ASS_NEQ(e._timestamp,_timestamp);
e._obj=obj;
e._timestamp=_timestamp;
}
/**
* Assign the key @b index the object @b obj
*
* The difference from the @b insert function is that with this
* function the key could have been assigned an object before.
*/
inline
void set(size_t index, T obj=T())
{
Entry& e=(*this)[index];
e._obj=obj;
e._timestamp=_timestamp;
}
/**
* Return the object assigned to key @b index. The key @b index must
* have an object assigned.
*/
inline
T get(size_t index)
{
ASS_EQ((*this)[index]._timestamp,_timestamp);
return (*this)[index]._obj;
}
/**
* Return the object assigned to key @b index or @b def if there isn't any.
*/
inline
T get(size_t index, T def)
{
if((*this)[index]._timestamp!=_timestamp) {
return def;
}
return (*this)[index]._obj;
}
/**
* Return @b true if key @b index has an object assigned
*
* Even for this function the value of @b index must be
* lower that the bound set by the @b ensure function.
*/
inline
bool find(size_t index)
{
return (*this)[index]._timestamp==_timestamp;
}
/**
* Return @b true if key @b index has an object assigned and assign
* its value into @c val. Otherwise return false and leave @c val
* unmodified.
*
* Even for this function the value of @b index must be
* lower that the bound set by the @b ensure function.
*/
inline
bool find(size_t index, T& val)
{
if((*this)[index]._timestamp==_timestamp) {
val = (*this)[index]._obj;
return true;
}
return false;
}
/**
* Remove the value assigned to the @b index key. The key
* @b index must have been assigned a value before.
*/
inline
void remove(size_t index)
{
ASS((*this)[index]._timestamp==_timestamp);
(*this)[index]._timestamp=0;
}
/**
* Assign into @b pObj the pointer that stores the value associated
* with key @b index. If the key was not previously assigned a value,
* initialize it to @b init and return true; otherwise return false.
*/
inline
bool getValuePtr(size_t index, T*& pObj, T init)
{
Entry& e=(*this)[index];
pObj=&e._obj;
if(e._timestamp!=_timestamp) {
e._timestamp=_timestamp;
e._obj=init;
return true;
}
return false;
}
class KeyIterator
{
public:
DECL_ELEMENT_TYPE(unsigned);
KeyIterator(const ArrayMap& parent) : _parent(parent), _idx(0) {}
bool hasNext()
{
while(_idx < static_cast<const DArray<Entry>&>(_parent).size()) {
if(_parent[_idx]._timestamp == _parent._timestamp) {
return true;
}
++_idx;
}
return false;
}
size_t next()
{
ASS_L(_idx,_parent.size());
ASS_EQ(_parent[_idx]._timestamp,_parent._timestamp);
return _idx++;
}
private:
const ArrayMap& _parent;
size_t _idx;
};
private:
/**
* Timestamp value that is stored in map entries that correspond
* to existing key-value pairs
*
* This is to allow reset of the map in constant time by increasing
* the value of this field.
*/
unsigned _timestamp;
};
class ArraySet : public ArrayMap<EmptyStruct>
{
public:
ArraySet() {}
ArraySet(size_t size) : ArrayMap<EmptyStruct>(size) {}
template<class It>
void insertFromIterator(It it)
{
while(it.hasNext()) {
insert(it.next());
}
}
typedef KeyIterator Iterator;
};
}
#endif