summaryrefslogtreecommitdiff
path: root/src/theory/arith/arithvar_set.h
blob: 68937acc463d79590f3a0f25760b2741b317f9ee (plain)
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
/*********************                                                        */
/*! \file arithvar_set.h
 ** \verbatim
 ** Original author: taking
 ** Major contributors: none
 ** Minor contributors (to current version): none
 ** This file is part of the CVC4 prototype.
 ** Copyright (c) 2009, 2010, 2011  The Analysis of Computer Systems Group (ACSys)
 ** Courant Institute of Mathematical Sciences
 ** New York University
 ** See the file COPYING in the top-level source directory for licensing
 ** information.\endverbatim
 **
 ** \brief [[ Add one-line brief description here ]]
 **
 ** [[ Add lengthier description here ]]
 ** \todo document this file
 **/

#include "cvc4_private.h"

#ifndef __CVC4__THEORY__ARITH__ARTIHVAR_SET_H
#define __CVC4__THEORY__ARITH__ARTIHVAR_SET_H

#include <vector>
#include "theory/arith/arith_utilities.h"

namespace CVC4 {
namespace theory {
namespace arith {

/**
 * This is an abstraction of a set of ArithVars.
 * This class is designed to provide constant time insertion, deletion, and element_of
 * and fast iteration.
 *
 * ArithVarSets come in 2 varieties: ArithVarSet, and PermissiveBackArithVarSet.
 * The default ArithVarSet assumes that there is no knowledge assumed about ArithVars
 * that are greater than allocated(). Asking isMember() of such an ArithVar
 * is an assertion failure. The cost of doing this is that it takes O(M)
 * where M is the total number of ArithVars in memory.
 *
 * PermissiveBackArithVarSet means that all ArithVars are implicitly not in the set,
 * and any ArithVar past the end of d_posVector is not in the set.
 * A permissiveBack allows for less memory to be consumed on average.
 *
 */
template <bool permissiveBack>
class ArithVarSetImpl {
public:
  typedef std::vector<ArithVar> VarList;
private:
  //List of the ArithVars in the set.
  VarList d_list;

  //Each ArithVar in the set is mapped to its position in d_list.
  //Each ArithVar not in the set is mapped to ARITHVAR_SENTINEL
  std::vector<unsigned> d_posVector;

public:
  typedef VarList::const_iterator const_iterator;

  ArithVarSetImpl() :  d_list(), d_posVector() {}

  size_t size() const {
    return d_list.size();
  }
  bool empty() const {
    return d_list.empty();
  }

  size_t allocated() const {
    return d_posVector.size();
  }

  void purge() {
    for(VarList::const_iterator i=d_list.begin(), endIter=d_list.end();i!= endIter; ++i){
      d_posVector[*i] = ARITHVAR_SENTINEL;
    }
    d_list.clear();
    Assert(empty());
  }

  void increaseSize(ArithVar max){
    Assert(max >= allocated());
    d_posVector.resize(max+1, ARITHVAR_SENTINEL);
  }

  void increaseSize(){
    increaseSize(allocated());
  }

  bool isMember(ArithVar x) const{
    if(permissiveBack && x >=  allocated()){
      return false;
    }else{
      Assert(x <  allocated());
      return d_posVector[x] != ARITHVAR_SENTINEL;
    }
  }

  /** Invalidates iterators */
  void init(ArithVar x, bool val) {
    Assert(x >= allocated());
    increaseSize(x);
    if(val){
      add(x);
    }
  }

  /** Invalidates iterators */
  void add(ArithVar x){
    Assert(!isMember(x));
    if(permissiveBack && x >=  allocated()){
      increaseSize(x);
    }
    d_posVector[x] = size();
    d_list.push_back(x);
  }

  const_iterator begin() const{ return d_list.begin(); }
  const_iterator end() const{ return d_list.end(); }

  /**
   * Invalidates iterators.
   * Adds x to the set if it is not already in the set.
   */
  void softAdd(ArithVar x){
    if(!isMember(x)){
      add(x);
    }
  }

  const VarList& getList() const{
    return d_list;
  }

  /** Invalidates iterators */
  void remove(ArithVar x){
    Assert(isMember(x));
    swapToBack(x);
    Assert(d_list.back() == x);
    pop_back();
  }

  ArithVar pop_back() {
    Assert(!empty());
    ArithVar atBack = d_list.back();
    d_posVector[atBack] = ARITHVAR_SENTINEL;
    d_list.pop_back();
    return atBack;
  }

 private:

  /** This should be true of all x < allocated() after every operation. */
  bool wellFormed(ArithVar x){
    if(d_posVector[x] == ARITHVAR_SENTINEL){
      return true;
    }else{
      return d_list[d_posVector[x]] == x;
    }
  }

  /** Swaps a member x to the back of d_list. */
  void swapToBack(ArithVar x){
    Assert(isMember(x));

    unsigned currentPos = d_posVector[x];
    ArithVar atBack = d_list.back();

    d_list[currentPos] = atBack;
    d_posVector[atBack] = currentPos;

    d_list[size() - 1] = x;
    d_posVector[x] = size() - 1;
  }
};

typedef ArithVarSetImpl<false> ArithVarSet;
typedef ArithVarSetImpl<true> PermissiveBackArithVarSet;


/**
 * ArithVarMultiset
 */
class ArithVarMultiset {
public:
  typedef std::vector<ArithVar> VarList;
private:
  //List of the ArithVars in the multi set.
  VarList d_list;

  //Each ArithVar in the set is mapped to its position in d_list.
  //Each ArithVar not in the set is mapped to ARITHVAR_SENTINEL
  std::vector<unsigned> d_posVector;

  //Count of the number of elements in the array
  std::vector<unsigned> d_counts;

public:
  typedef VarList::const_iterator const_iterator;

  ArithVarMultiset() :  d_list(), d_posVector() {}

  size_t size() const {
    return d_list.size();
  }

  bool empty() const {
    return d_list.empty();
  }

  size_t allocated() const {
    Assert(d_posVector.size() == d_counts.size());
    return d_posVector.size();
  }

  void purge() {
    for(VarList::const_iterator i=d_list.begin(), endIter=d_list.end(); i!= endIter; ++i){
      d_posVector[*i] = ARITHVAR_SENTINEL;
      d_counts[*i] = 0;
    }
    d_list.clear();
    Assert(empty());
  }

  void increaseSize(ArithVar max){
    Assert(max >= allocated());
    d_posVector.resize(max+1, ARITHVAR_SENTINEL);
    d_counts.resize(max+1, 0);
  }

  void increaseSize(){
    increaseSize(allocated());
  }

  bool isMember(ArithVar x) const{
    if( x >=  allocated()){
      return false;
    }else{
      Assert(x <  allocated());
      return d_posVector[x] != ARITHVAR_SENTINEL;
    }
  }

  /** Invalidates iterators */
  /* void init(ArithVar x, bool val) { */
  /*   Assert(x >= allocated()); */
  /*   increaseSize(x); */
  /*   if(val){ */
  /*     add(x); */
  /*   } */
  /* } */

  /**
   * Invalidates iterators.
   */
  void addMultiset(ArithVar x){
    if( x >=  allocated()){
      increaseSize(x);
    }
    if(d_counts[x] == 0){
      d_posVector[x] = size();
      d_list.push_back(x);
    }
    d_counts[x]++;
  }

  unsigned count(ArithVar x){
    if( x >=  allocated()){
      return 0;
    }else{
      return d_counts[x];
    }
  }

  const_iterator begin() const{ return d_list.begin(); }
  const_iterator end() const{ return d_list.end(); }

  const VarList& getList() const{
    return d_list;
  }

  /** Invalidates iterators */
  void remove(ArithVar x){
    Assert(isMember(x));
    swapToBack(x);
    Assert(d_list.back() == x);
    pop_back();
  }

  ArithVar pop_back() {
    Assert(!empty());
    ArithVar atBack = d_list.back();
    d_posVector[atBack] = ARITHVAR_SENTINEL;
    d_counts[atBack] = 0;
    d_list.pop_back();
    return atBack;
  }

 private:

  /** This should be true of all x < allocated() after every operation. */
  bool wellFormed(ArithVar x){
    if(d_posVector[x] == ARITHVAR_SENTINEL){
      return true;
    }else{
      return d_list[d_posVector[x]] == x;
    }
  }

  /** Swaps a member x to the back of d_list. */
  void swapToBack(ArithVar x){
    Assert(isMember(x));

    unsigned currentPos = d_posVector[x];
    ArithVar atBack = d_list.back();

    d_list[currentPos] = atBack;
    d_posVector[atBack] = currentPos;

    d_list[size() - 1] = x;
    d_posVector[x] = size() - 1;
  }
};

}/* CVC4::theory::arith namespace */
}/* CVC4::theory namespace */
}/* CVC4 namespace */

#endif /* __CVC4__THEORY__ARITH__ARTIHVAR_SET_H */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback