STL Memory Versioning
vs_set.h
Go to the documentation of this file.
1 #ifndef _VS_SET_H
2 #define _VS_SET_H
3 
4 #include <functional>
5 #include <set>
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, typename _Comp>
17  class vs_set_strategy;
18 
26  template<typename _Key, typename _Comp = std::less<_Key>,
27  typename _Strategy = vs_set_strategy<_Key, _Comp>>
28  class vs_set
29  {
30 
31  static_assert(vs::IsMergeStrategy<_Strategy, std::set<_Key, _Comp>>,
32  "Provided invalid strategy class in template");
33 
34  public:
35  /* public typedefs */
36 
38  typedef std::set<_Key, _Comp>::iterator iterator;
39  typedef std::set<_Key, _Comp>::size_type size_type;
40 
41  private:
42 
43  _Versioned _v_s;
44 
45  public:
46 
47  /* ------------------ Constructors ----------------------*/
52  explicit
53  vs_set(const _Comp& __comp = _Comp())
54  : _v_s(std::set<_Key, _Comp>(__comp)) { }
55 
64  vs_set(std::initializer_list<_Key> __l,
65  const _Comp& __comp = _Comp())
66  : _v_s(std::set<_Key, _Comp>(__l, __comp)) { }
67 
73  vs_set(const vs_set& __vs_set)
74  : _v_s(__vs_set._v_s.Get()) { }
75 
76  //* @brief %Set move constructor
77 
78  //* @brief Builds a %set from a range.
79 
80 
81  /* ------------------ Accessors ----------------------*/
82 
90  iterator
91  begin() const noexcept
92  { return _v_s.Get().begin(); }
93 
101  iterator
102  end() const noexcept
103  { return _v_s.Get().end(); }
104 
105 
109  size_type
110  size() const noexcept
111  { return _v_s.Get().size(); }
112 
116  bool
117  contains(const _Key& __x)
118  {
119  auto& _set = _v_s.Get();
120  return _set.find(__x) != _set.end();
121  }
122 
126  iterator
127  find(const _Key& __x) const
128  { return _v_s.Get().find(__x); }
129 
130  /* ------------------ Operators ----------------------*/
131 
132  /* XXX: add pair<iteraror as in stl> */
133  /* XXX: move insert */
134 
143  bool
144  insert(const _Key& __x)
145  {
146  return _v_s.Set(_v_s.Get(), [&](std::set<_Key, _Comp>& _set){return _set.insert(__x).second;});
147  }
148  // = (copy)
149  // = {}
150  };
151 
162  template<typename _Key, typename _Comp>
164  {
165  public:
166 
167  void
168  merge(std::set<_Key, _Comp>& dst, std::set<_Key, _Comp>& src)
169  {
170  for (auto& i: src)
171  {
172  auto found = dst.find(i);
173  /* XXX: dirty const_cast, but it is not used as const anyway */
174  if (found != dst.end())
175  merge_same_element(dst, const_cast<_Key&>(*found), const_cast<_Key&>(i));
176  else
177  dst.insert(i);
178  }
179  }
180 
181  void
182  merge_same_element(std::set<_Key, _Comp>& dst, _Key& dstk, _Key& srck)
183  {
184  /* do nothing, as insert would handle it */
185  }
186 
187  };
188 }
189 
190 #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_set.h:164
void merge_same_element(std::set< _Key, _Comp > &dst, _Key &dstk, _Key &srck)
Definition: vs_set.h:182
void merge(std::set< _Key, _Comp > &dst, std::set< _Key, _Comp > &src)
Definition: vs_set.h:168
A versioned mimic of a stl::set, suitable for multithread.
Definition: vs_set.h:29
size_type size() const noexcept
size of underlying set
Definition: vs_set.h:110
iterator end() const noexcept
end constant iterator
Definition: vs_set.h:102
bool insert(const _Key &__x)
Attempts to insert an element into the set.
Definition: vs_set.h:144
vs_set(const _Comp &__comp=_Comp())
Creates a vs_set with no elements.
Definition: vs_set.h:53
std::set< _Key, _Comp >::iterator iterator
Definition: vs_set.h:38
vs_set(std::initializer_list< _Key > __l, const _Comp &__comp=_Comp())
Builds a vs_set from an initializer_list.
Definition: vs_set.h:64
iterator begin() const noexcept
begin constant iterator
Definition: vs_set.h:91
Versioned< std::set< _Key, _Comp >, _Strategy > _Versioned
Definition: vs_set.h:32
std::set< _Key, _Comp >::size_type size_type
Definition: vs_set.h:39
iterator find(const _Key &__x) const
find element in set
Definition: vs_set.h:127
bool contains(const _Key &__x)
check if element is contained in set
Definition: vs_set.h:117
vs_set(const vs_set &__vs_set)
vs_set copy constructor
Definition: vs_set.h:73
Definition: strategy.h:6
concept IsMergeStrategy
Concept for compile-time type checking passed user strategies.
Definition: strategy.h:12
Implementation of the class Revision.
Implementation of the Versioned classes and interface.