STL Memory Versioning
vs_stack.h
Go to the documentation of this file.
1 #ifndef _VS_STACK_H
2 #define _VS_STACK_H
3 
4 #include <functional>
5 #include <stack>
6 #include <initializer_list>
7 #include <iterator>
8 
9 #include "versioned.h"
10 #include "revision.h"
11 #include "strategy.h"
12 
13 namespace vs
14 {
15 
16  template<typename _Key>
17  class vs_stack_strategy;
18 
25  template<typename _Key, typename _Strategy = vs_stack_strategy<_Key>>
26  class vs_stack
27  {
28 
29  static_assert(vs::IsMergeStrategy<_Strategy, std::stack<_Key>>,
30  "Provided invalid strategy class in template");
31 
32  public:
33  /* public typedefs */
34 
36  typedef std::stack<_Key>::size_type size_type;
37 
38  private:
39 
40  _Versioned _v_s;
41 
42  public:
43 
44  /* ------------------ Constructors ----------------------*/
48  explicit
50  : _v_s(std::stack<_Key>()) { }
51 
60  vs_stack(std::initializer_list<_Key> __l)
61  : _v_s(std::stack<_Key>())
62  {
63  _v_s.Set(_v_s.Get(),
64  [&](std::stack<_Key>& _stack){
65  for (auto& i: __l){
66  _stack.push(i);
67  }
68  return true;
69  });
70  }
71 
77  vs_stack(const vs_stack& __vs_stack)
78  : _v_s(__vs_stack._v_s.Get()) { }
79 
80  //* @brief %stack move constructor
81 
82  //* @brief Builds a %stack from a range.
83 
84  /* ------------------ Accessors ----------------------*/
85 
92  const _Key&
93  top() const
94  { return _v_s.Get().top(); }
95 
99  size_type
100  size() const noexcept
101  { return _v_s.Get().size(); }
102 
103  /* ------------------ Operators ----------------------*/
104 
109  void
110  push(const _Key& __x)
111  {
112  _v_s.Set(_v_s.Get(), [&](std::stack<_Key>& _stack){ _stack.push(__x); return true; });
113  }
114 
118  void
119  pop()
120  {
121  if (_v_s.Get().size() > 0)
122  _v_s.Set(_v_s.Get(), [](std::stack<_Key>& _stack){ _stack.pop(); return true; });
123  }
124  // = (copy)
125  // = {}
126  };
127 
135  template<typename _Key>
137  {
138  public:
139 
140  void
141  merge(std::stack<_Key>& dst, std::stack<_Key>& src)
142  {
143  while (src.size() > 0)
144  {
145  dst.push(src.top());
146  src.pop();
147  }
148  }
149 
150  void
151  merge_same_element(std::stack<_Key>& dst, _Key& dstk, _Key& srck)
152  {
153  dst.push(srck);
154  }
155 
156  };
157 
158  template<typename _Key>
159  std::ostream& operator << (std::ostream& os, vs_stack<_Key> const& value) {
160  vs_stack<_Key> temp = value;
161  std::stack<_Key> reversedStack;
162 
163  while (temp.size() != 0) {
164  reversedStack.push(temp.top());
165  temp.pop();
166  }
167 
168  std::ostringstream o;
169  o << "{ ";
170  while (reversedStack.size() != 0) {
171  o << reversedStack.top();
172  reversedStack.pop();
173  if (reversedStack.size() != 0) {
174  o << ", ";
175  }
176  }
177  o << " }";
178 
179  os << o.str();
180  return os;
181  }
182 }
183 
184 #endif
Wrapper to make any class Versioned.
Definition: versioned.h:53
const T & Get() const
Get the current value of the object in the current Revision.
Definition: versioned.h:186
bool Set(const T &v, const std::function< bool(T &)> &updater=nullptr)
Set new value of the object.
Definition: versioned.h:205
simpliest determenistic merge strategy.
Definition: vs_stack.h:137
void merge_same_element(std::stack< _Key > &dst, _Key &dstk, _Key &srck)
Definition: vs_stack.h:151
void merge(std::stack< _Key > &dst, std::stack< _Key > &src)
Definition: vs_stack.h:141
A versioned mimic of a stl::stack, suitable for multithread.
Definition: vs_stack.h:27
vs_stack(std::initializer_list< _Key > __l)
Builds a vs_stack from an initializer_list.
Definition: vs_stack.h:60
void pop()
Remove first element of stack.
Definition: vs_stack.h:119
const _Key & top() const
access top element
Definition: vs_stack.h:93
vs_stack(const vs_stack &__vs_stack)
vs_stack copy constructor
Definition: vs_stack.h:77
vs_stack()
Creates a vs_stack with no elements.
Definition: vs_stack.h:49
std::stack< _Key >::size_type size_type
Definition: vs_stack.h:36
void push(const _Key &__x)
Attempts to push an element into the stack.
Definition: vs_stack.h:110
Versioned< std::stack< _Key >, _Strategy > _Versioned
Definition: vs_stack.h:30
size_type size() const noexcept
size of underlying stack
Definition: vs_stack.h:100
Definition: strategy.h:6
concept IsMergeStrategy
Concept for compile-time type checking passed user strategies.
Definition: strategy.h:12
std::ostream & operator<<(std::ostream &os, vs_stack< _Key > const &value)
Definition: vs_stack.h:159
Implementation of the class Revision.
Implementation of the Versioned classes and interface.