summaryrefslogtreecommitdiff
path: root/src/context/cdvector.h
blob: 0076d509fcfbc9861ce2d765f4fdad88deb79815 (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
/*********************                                                        */
/*! \file cdvector.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  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__CONTEXT__CDVECTOR_H
#define __CVC4__CONTEXT__CDVECTOR_H

#include "context/context.h"
#include "context/cdo.h"
#include "util/Assert.h"
#include "util/dynamic_array.h"

namespace CVC4 {
namespace context {

template <class T>
struct UpdatableElement {
public:
  T d_data;
  int d_contextLevelOfLastUpdate;

  UpdatableElement(const T& data) :
    d_data(data),
    d_contextLevelOfLastUpdate(0) {
  }
};/* struct UpdatableElement<T> */

template <class T>
struct HistoryElement {
public:
  UpdatableElement<T> d_cached;
  unsigned d_index;
  HistoryElement(const UpdatableElement<T>& cache, unsigned index) :
    d_cached(cache), d_index(index) {
  }
};/* struct HistoryElement<T> */


/**
 * Generic context-dependent dynamic vector.
 * It provides the ability to destructively update the i'th item.
 * Note that the size of the vector never decreases.
 *
 * This is quite different than CDList<T>.
 */
template <class T>
class CDVector {
private:
  typedef DynamicArray< UpdatableElement<T> > CurrentVector;
  typedef DynamicArray< HistoryElement<T> > HistoryVector;

  Context* d_context;

  DynamicArray< UpdatableElement<T> > d_current;
  DynamicArray< HistoryElement<T> > d_history;

  CDO<unsigned> d_historySize;

private:
  void restoreConsistency() {
    Assert(d_history.size() >= d_historySize.get());
    while(d_history.size() > d_historySize.get()) {
      const HistoryElement<T>& back = d_history.back();
      d_current[back.d_index] = back.d_cached;
      d_history.pop_back();
    }
  }

  bool isConsistent() const {
    return d_history.size() == d_historySize.get();
  }

  void makeConsistent() {
    if(!isConsistent()) {
      restoreConsistency();
    }
    Assert(isConsistent());
  }

public:
  CDVector(Context* c, bool callDestructor = false) :
    d_context(c),
    d_current(callDestructor),
    d_history(callDestructor),
    d_historySize(c, 0) {
  }

  unsigned size() const {
    return d_current.size();
  }

  /**
   * Return true iff there are no valid objects in the list.
   */
  bool empty() const {
    return d_current.empty();
  }

  /**
   * Add an item to the end of the list.
   * Assigns its state at level 0 to be equal to data.
   */
  void push_back(const T& data) {
    d_current.push_back(UpdatableElement<T>(data));
  }

  /**
   * Access to the ith item in the list.
   */
  const T& operator[](unsigned i) {
    return get(i);
  }

  const T& get(unsigned i) {
    Assert(i < size(), "index out of bounds in CDVector::get()");
    makeConsistent();
    return d_current[i].d_data;
  }

  void set(unsigned index, const T& data) {
    Assert(index < size(), "index out of bounds in CDVector::set()");
    makeConsistent();

    if(d_current[index].d_contextLevelOfLastUpdate == d_context->getLevel()) {
      d_current[index].d_data = data;
    }else{
      d_history.push_back(HistoryElement<T>(d_current[index], index));
      d_historySize.set(d_historySize.get() + 1);

      d_current[index].d_data = data;
      d_current[index].d_contextLevelOfLastUpdate = d_context->getLevel();
    }
  }

};/* class CDVector<T> */

}/* CVC4::context namespace */
}/* CVC4 namespace */

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