dune-multidomaingrid 2.12-git
Loading...
Searching...
No Matches
arraybasedset.hh
Go to the documentation of this file.
1#ifndef DUNE_MULTIDOMAINGRID_ARRAYBASEDSET_HH
2#define DUNE_MULTIDOMAINGRID_ARRAYBASEDSET_HH
3
4#include <algorithm>
5#include <array>
6#include <cassert>
7#include <cstddef>
8#include <cstdint>
9#include <limits>
10#include <type_traits>
11
13
14namespace Dune {
15
16namespace mdgrid {
17
18template<typename SI, std::size_t capacity>
19class ArrayBasedSet;
20
21
22template<typename SI, std::size_t capacity>
25
26
27template<typename SI, std::size_t capacity>
30
31
32template<typename SI, std::size_t capacity>
34
37
40
41public:
43
44 static const std::size_t maxSize = capacity;
49
51
53 {
55
56 static bool fixedSize(int dim, int codim)
57 {
58 return false;
59 }
60
62 {
63 return sds.size();
64 }
65
66 template<typename MessageBufferImp>
68 {
69 for(Iterator it = sds.begin(); it != sds.end(); ++it)
70 buf.write(*it);
71 }
72
73 template<typename MessageBufferImp>
75 {
77 h._size = n;
78 ArrayIterator end = h._set.begin() + n;
79 for (ArrayIterator it = h._set.begin(); it != end; ++it)
80 buf.read(*it);
81 sds.addAll(h);
82 }
83
84 };
85
86 Iterator begin() const {
87 return _set.begin();
88 }
89
90 Iterator end() const {
91 return _set.begin() + _size;
92 }
93
94 bool contains(SubDomainIndex domain) const {
95 return std::binary_search(_set.begin(),_set.begin() + _size,domain);
96 }
97
98 template<typename Set>
99 bool containsAll(const Set& set) const {
100 return setContains(*this,set);
101 }
102
104 {
106 subtrahend.begin(),subtrahend.end(),
107 _set.begin());
108 _size = res - _set.begin();
109 }
110
111 bool simple() const {
112 return _size == 1;
113 }
114
115 bool empty() const {
116 return _size == 0;
117 }
118
119 SetState state() const {
120 switch (_size) {
121 case 0:
122 return emptySet;
123 case 1:
124 return simpleSet;
125 default:
126 return multipleSet;
127 }
128 }
129
131 return _size;
132 }
133
134 void clear() {
135 _size = 0;
136 }
137
138 void add(SubDomainIndex domain) {
139 if (!std::binary_search(_set.begin(),_set.begin()+_size,domain)) {
140 assert(_size < maxSize);
141 _set[_size++] = domain;
142 std::sort(_set.begin(),_set.begin()+_size);
143 }
144 }
145
146 void remove(SubDomainIndex domain) {
147 ArrayIterator it = std::lower_bound(_set.begin(),_set.begin()+_size,domain);
148 assert(*it == domain);
149 *it = emptyTag;
150 std::sort(_set.begin(),_set.end() + (_size--));
151 }
152
153 void set(SubDomainIndex domain) {
154 _size = 1;
155 _set[0] = domain;
156 }
157
158 template<typename Set>
159 void addAll(const Set& set) {
160 setAdd(*this,set);
161 }
162
163 int domainOffset(SubDomainIndex domain) const {
164 Iterator it = std::lower_bound(_set.begin(),_set.begin()+_size,domain);
165 assert(*it == domain);
166 return it - _set.begin();
167 }
168
170 _size(0)
171 {}
172
173 bool operator==(const ArrayBasedSet& r) const {
174 return _size == r._size && std::equal(_set.begin(),_set.begin()+_size,r._set.begin());
175 }
176
177 bool operator!=(const ArrayBasedSet& r) const {
178 return !operator==(r);
179 }
180
181private:
182 std::size_t _size;
184
185};
186
187
188template<typename SubDomainIndex, std::size_t capacity>
191 return std::includes(a._set.begin(),a._set.begin() + a._size,b._set.begin(),b._set.begin() + b._size);
192}
193
194template<typename SubDomainIndex, std::size_t capacity>
198 typename std::array<SubDomainIndex,2*capacity>::iterator it = std::set_union(a._set.begin(), a._set.begin() + a._size,
199 b._set.begin(), b._set.begin() + b._size,
200 tmp.begin());
201 a._size = it - tmp.begin();
202 assert(a._size <= capacity);
203 std::copy(tmp.begin(),++it,a._set.begin());
204}
205
206} // namespace mdgrid
207
208} // namespace Dune
209
210#endif // DUNE_MULTIDOMAINGRID_ARRAYBASEDSET_HH
bool setContains(const ArrayBasedSet< SI, capacity > &a, const ArrayBasedSet< SI, capacity > &b)
void setAdd(ArrayBasedSet< SI, capacity > &a, const ArrayBasedSet< SI, capacity > &b)
size_type dim() const
Definition arraybasedset.hh:33
Iterator begin() const
Definition arraybasedset.hh:86
void clear()
Definition arraybasedset.hh:134
Iterator end() const
Definition arraybasedset.hh:90
friend void setAdd(ArrayBasedSet< SI, capacity > &a, const ArrayBasedSet< SI, capacity > &b)
bool simple() const
Definition arraybasedset.hh:111
std::size_t size() const
Definition arraybasedset.hh:130
bool operator!=(const ArrayBasedSet &r) const
Definition arraybasedset.hh:177
friend bool setContains(const ArrayBasedSet< SI, capacity > &a, const ArrayBasedSet< SI, capacity > &b)
void set(SubDomainIndex domain)
Definition arraybasedset.hh:153
bool contains(SubDomainIndex domain) const
Definition arraybasedset.hh:94
std::array< SubDomainIndex, maxSize >::const_iterator Iterator
Definition arraybasedset.hh:47
bool operator==(const ArrayBasedSet &r) const
Definition arraybasedset.hh:173
bool empty() const
Definition arraybasedset.hh:115
void remove(SubDomainIndex domain)
Definition arraybasedset.hh:146
SetState state() const
Definition arraybasedset.hh:119
void difference(const ArrayBasedSet &minuend, const ArrayBasedSet &subtrahend)
Definition arraybasedset.hh:103
int domainOffset(SubDomainIndex domain) const
Definition arraybasedset.hh:163
bool containsAll(const Set &set) const
Definition arraybasedset.hh:99
SI SubDomainIndex
Definition arraybasedset.hh:42
SetState
Definition arraybasedset.hh:50
@ multipleSet
Definition arraybasedset.hh:50
@ emptySet
Definition arraybasedset.hh:50
@ simpleSet
Definition arraybasedset.hh:50
std::array< SubDomainIndex, maxSize >::iterator ArrayIterator
Definition arraybasedset.hh:46
static const std::size_t maxSize
Definition arraybasedset.hh:44
ArrayBasedSet< SubDomainIndex, capacity > This
Definition arraybasedset.hh:48
static const SubDomainIndex emptyTag
Definition arraybasedset.hh:45
ArrayBasedSet()
Definition arraybasedset.hh:169
void addAll(const Set &set)
Definition arraybasedset.hh:159
void add(SubDomainIndex domain)
Definition arraybasedset.hh:138
Definition arraybasedset.hh:53
static void scatter(MessageBufferImp &buf, ArrayBasedSet &sds, std::size_t n)
Definition arraybasedset.hh:74
static std::size_t size(const ArrayBasedSet &sds)
Definition arraybasedset.hh:61
SubDomainIndex DataType
Definition arraybasedset.hh:54
static void gather(MessageBufferImp &buf, const ArrayBasedSet &sds)
Definition arraybasedset.hh:67
static bool fixedSize(int dim, int codim)
Definition arraybasedset.hh:56
T begin(T... args)
T binary_search(T... args)
T copy(T... args)
T end(T... args)
T equal(T... args)
T includes(T... args)
T lower_bound(T... args)
T max(T... args)
T set_difference(T... args)
T set_union(T... args)
T sort(T... args)