BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
auto_set.h
Go to the documentation of this file.
1#ifndef BPMNOS_Execution_auto_set_H
2#define BPMNOS_Execution_auto_set_H
3
4#include <set>
5#include <tuple>
6#include <memory>
7#include "expired.h"
8
9namespace BPMNOS::Execution {
10
11/**
12 * @brief Set of tuples ordered in increasing order of the first component with automatic removal of tuples containing an expired weak_ptr.
13 */
14template <typename V, typename... U>
15class auto_set {
16public:
17 struct iterator {
18 typename std::set< std::tuple< V, U... > >::iterator current;
20
21 iterator(typename std::set< std::tuple< V, U... > >::iterator iter, auto_set<V,U...>* cont)
22 : current(iter), container(cont) {
23 skipExpired();
24 }
25
27 ++current;
28 skipExpired();
29 return *this;
30 }
31
32 std::tuple< V, U... > operator*() const {
33 return *current;
34 }
35
36 bool operator!=(const iterator& other) const {
37 return current != other.current;
38 }
39
40 private:
41 void skipExpired() {
42 // erase all tuples in which any of the initial weak_ptrs are expired
43 while ( current != container->tuples.end() && expired<1>(*current) ) {
44 current = container->tuples.erase(current);
45 }
46 }
47 };
48
50 return iterator(tuples.begin(), this);
51 }
52
54 return iterator(tuples.end(), this);
55 }
56
57 iterator cbegin() const {
58 return iterator(tuples.begin(), const_cast<auto_set<V,U...>*>(this));
59 }
60
61 iterator cend() const {
62 return iterator(tuples.end(), const_cast<auto_set<V,U...>*>(this));
63 }
64
65 iterator begin() const {
66 return iterator(tuples.begin(), const_cast<auto_set<V,U...>*>(this));
67 }
68
69 iterator end() const {
70 return iterator(tuples.end(), const_cast<auto_set<V,U...>*>(this));
71 }
72
73 void emplace(V value, U... data) {
74 tuples.emplace(value, data...);
75 }
76
78 return iterator(tuples.erase(it.current), this);
79 }
80
81 template <typename T>
82 void remove(T* pointer) {
83 auto it = begin();
84 while (it != end()) {
85 auto& element = std::get< std::weak_ptr<T> >(*it.current);
86 if ( auto shared = element.lock() ) {
87 if ( shared.get() == pointer ) {
88 tuples.erase(it.current);
89 return;
90 }
91 }
92 ++it;
93 }
94 }
95
96 bool empty() const {
97 for ( [[maybe_unused]] auto _ : *this ) {
98 return false;
99 }
100 return true;
101 }
102
103 void clear() {
104 tuples.clear();
105 }
106private:
107 struct comparator {
108 bool operator()(const std::tuple<V, U... >& lhs, const std::tuple<V, U... >& rhs) const {
109 // Compare based on the value of the first component
110 if ( std::get<0>(lhs) != std::get<0>(rhs) ) {
111 return std::get<0>(lhs) < std::get<0>(rhs);
112 }
113 // Compare pointers, if both have same value
114 return (&lhs < &rhs);
115 }
116 };
117 mutable std::set< std::tuple< V, U... >, comparator > tuples;
118};
119
120} // namespace BPMNOS::Execution
121#endif // BPMNOS_Execution_auto_set_H
122
Set of tuples ordered in increasing order of the first component with automatic removal of tuples con...
Definition auto_set.h:15
iterator end() const
Definition auto_set.h:69
iterator erase(iterator it)
Definition auto_set.h:77
iterator cbegin() const
Definition auto_set.h:57
void emplace(V value, U... data)
Definition auto_set.h:73
iterator cend() const
Definition auto_set.h:61
iterator begin() const
Definition auto_set.h:65
void remove(T *pointer)
Definition auto_set.h:82
bool expired(const std::tuple< U... > &t)
Determines whether any of the initial weak_ptr elements in a tuple is expired.
Definition expired.h:19
std::tuple< V, U... > operator*() const
Definition auto_set.h:32
auto_set< V, U... > * container
Definition auto_set.h:19
iterator(typename std::set< std::tuple< V, U... > >::iterator iter, auto_set< V, U... > *cont)
Definition auto_set.h:21
bool operator!=(const iterator &other) const
Definition auto_set.h:36
std::set< std::tuple< V, U... > >::iterator current
Definition auto_set.h:18