summaryrefslogtreecommitdiff
path: root/src/context/cdvector.h
blob: 2622ccdd2f008fe214d3a2d7983035881bab80e0 (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
/*********************                                                        */
/*! \file cdvector.h
 ** \verbatim
 ** Top contributors (to current version):
 **   Tim King, Morgan Deters
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2018 by the authors listed in the file AUTHORS
 ** in the top-level source directory) and their institutional affiliations.
 ** All rights reserved.  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 <vector>

#include "base/cvc4_assert.h"
#include "context/context.h"
#include "context/cdlist.h"

namespace CVC4 {
namespace context {


/**
 * Generic context-dependent dynamic vector.
 * This is quite different than CDList<T>.
 * It provides the ability to destructively update the i'th item.
 *
 * The back of the vector may only be popped if there have been no updates to it
 * at this context level.
 */
template <class T>
class CDVector {
private:
  static const int ImpossibleLevel = -1;

  struct UpdatableElement {
  public:
    T d_data;
    int d_contextLevelOfLastUpdate;

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

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

  typedef std::vector< UpdatableElement > CurrentVector;
  CurrentVector d_current;



  class HistoryListCleanUp{
  private:
    CurrentVector& d_current;
  public:
    HistoryListCleanUp(CurrentVector& current)
      : d_current(current)
    {}

    void operator()(HistoryElement* back){
      d_current[back->d_index] = back->d_cached;
    }
  };/* class CDVector<T>::HistoryListCleanUp */

  typedef CDList< HistoryElement,  HistoryListCleanUp > HistoryVector;
  HistoryVector d_history;

  Context* d_context;

  // no copy or assignment
  CDVector(const CDVector&) CVC4_UNDEFINED;
  CDVector& operator=(const CDVector&) CVC4_UNDEFINED;

public:
  CDVector(Context* c) :
    d_current(),
    d_history(c, true, HistoryListCleanUp(d_current)),
    d_context(c)
  {}

  size_t 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(data));
  }

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

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

  void set(size_t 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(d_current[index], index));

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

  bool hasUpdates(size_t index) const {
    Assert(index < size(), "index out of bounds in CDVector::hasUpdates()");
    return d_current[index].d_contextLevelOfLastUpdate == ImpossibleLevel;
  }

  void pop_back() {
    Assert(!empty(), "pop_back() on an empty CDVector");
    Assert(!hasUpdates(size() - 1), "popping an element with updates.");
    d_current.pop_back();
  }

};/* 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