-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathone_dimensional_domain.hpp
383 lines (317 loc) · 9.49 KB
/
one_dimensional_domain.hpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <hpx/config.hpp>
#include <hpx/util/tuple.hpp>
namespace hpx
{
template<typename Index_Type>
struct oneDdomain
{
public:
typedef Index_Type index_type;
typedef std::size_t size_type;
typedef index_type element_type;
oneDdomain():
upper(0),
lower(1),
is_container_domain(false) {}
oneDdomain(size_type size, bool is_ = false)
{
if(size > 0)
{
lower = 0;
upper = size - 1;
}
else
{
lower = 1;upper = 0;
}
is_container_domain = is_;
}
oneDdomain(oneDdomain const& other)
{
lower = other.lower;
upper = other.upper;
is_container_domain = other.is_container_domain;
}
oneDdomain(oneDdomain && other)
{
lower = std::move(other.lower);
upper = std::move(other.upper);
is_container_domain = std::move(other.is_container_domain);
}
oneDdomain(index_type start, index_type end, bool is_ = false)
: lower(start), upper(end), is_container_domain(is_) {}
std::size_t size() const
{
if(lower > upper) return 0;
else
return (upper - lower + 1);
}
element_type first() const
{
return lower;
}
element_type last() const
{
return upper;
}
bool contains(index_type const& idx) const
{
if( idx >= lower && idx <= upper)
return true;
else
return false;
}
index_type advance(index_type idx, std::size_t n) const
{
return idx + n;
}
std::size_t distance(index_type & it1,index_type & it2) const
{
if(it1 >= it2) return it1 - it2;
else
return it2 - it1;
}
bool is_container_domain() const
{
return is_container_domain;
}
bool empty() const
{
return lower > upper;
}
template <typename T>
oneDdomain operator&(T const& other) const
{
T::index_type other_lower = other.first();
T::index_type other_upper = other.last();
if(empty())
return oneDdomain(other_lower, other_upper);
if (other.empty())
return oneDdomain(lower, upper);
if(other_lower > upper)
return oneDdomain(-1);
if(lower > other_upper)
return oneDdomain(-1);
if(other.contains(upper) == true && other.contains(lower) == true)
return oneDdomain(lower, upper);
if(other.contains(upper) == true && other.contains(lower) == false)
return oneDdomain(other_lower, upper);
if(other.contains(lower) == true && other.contains(upper) == false)
return oneDdomain(lower, other_upper);
}
template <typename T>
oneDdomain operator|(T const& other) const
{
T::index_type other_lower = other.first();
T::index_type other_upper = other.last();
index_type t_lower = index_type();
index_type t_upper = index_type();
if(other_lower >= lower)
t_lower = lower;
else
t_lower = other_lower;
if(other_upper >= upper)
t_upper = other_upper;
else
t_upper = upper;
oneDdomain(t_lower,t_upper, false);
}
void expand_by(std::size_t by)
{
static_assert(by >= 0," must be greater than zero");
if(empty())
{
lower = index_type();
upper = by;
}else
upper += by;
}
void contract_by(std::size_t by)
{
static_assert(by >=0 || !empty(), "must be greater than zero or trying to contract an empty domain");
upper -= by;
}
oneDdomain translate(std::size_t val)
{
index_type temp = upper + val;
oneDdomain(lower,temp,false);
}
bool operator == (oneDdomain const& other) const
{
if(upper == other.last() && lower == other.first())
return true;
else
return false;
}
bool operator != (oneDdomain const& other) const
{
if(upper != other.last() || lower != other.first())
return true;
else
return false;
}
std::size_t rank() const
{
return 1;
}
void swap(oneDdomain const& other)
{
if(size() == other.size())
{
index_type t_l, t_u;
t_l = other.first();
t_u = other.last();
other.set_lower(first());
other.set_upper(last());
set_lower(t_l);
set_upper(t_u);
}
else
HPX_ASSERT(false);
}
template<typename T>
friend std::ostream& operator<<(std::ostream& os, oneDdomain<T> const& to_p);
private:
index_type upper;
index_type lower;
bool is_container_domain;
};
template<typename T>
std::ostream& operator<<(std::ostream& print, oneDdomain<T> const& to_p)
{
if(to_p.empty())
print << "Empty";
else
{
print << "[ " << to_p.first() << ","
<< to_p.last() << " ]";
}
return print;
}
template<typename T,typename dom = oneDdomain<T>>
struct dom1D
{
public:
typedef dom domain_t;
typedef hpx::util::tuple<domain_t> domain_type;
typedef typename domain_t::index_type index_type;
typedef typename domain_t::size_type size_type;
typedef typename domain_t::element_type element_type;
private:
domain_type region;
// size_type region_size;
public:
dom1D()
{
region(domain_t());
// region_size = 0;
}
dom1D(domain_t dom)
{
region = domain_type(dom);
//region_size = dom.size();
}
dom1D(dom1D const& other)
{
region = other.region;
// region_size = other.region_size;
}
dom1D(dom1D && other)
{
region = std::move(other.region);
// region_size = std::move(other.region_size);
}
dom1D(index_type lower, index_type upper,bool is_container_domain = false)
: region(domain_t(lower, upper, is_container_domain)){}
//region_size((upper - lower)+1) {}
dom1D(std::size_t n)
: region(domain_t(n)) {}
//region_size(n) {}
std::size_t size() const
{
return get<0>(region).size();
}
element_type first() const
{
return element_type(get<0>(this->region).first());
}
element_type last() const
{
return element_type(get<0>(this->region).last());
}
bool contains(index_type idx) const
{
return get<0>(this->region).contains(idx);
}
index_type advance(index_type idx, std::size_t n) const
{
return get<0>(this->region).advance(idx,n);
}
std::size_t distance(index_type & it1,index_type & it2) const
{
return get<0>(this->region).distance(it1,it2);
}
bool is_container_domain()const
{
return get<0>(this->region).is_container_domain();
}
bool empty() const
{
return get<0>(this->region).empty();
}
dom1D operator&(dom1D const& other) const
{
return dom1D(get<0>.(this->region) & get<0>.(other.region));
}
/* template <typename ODom>
dom1D operator&(ODom const& other)
{
return intersect(get<0>(this->m_domain), other.template get_domain<0>());
}
*/
dom1D operator|(dom1D const& other) const
{
return dom1D(get<0>.(this->region) | get<0>.(other.region));
}
void expand_by(std::size_t by)
{
return get<0>.(this->region).expand_by(by);
}
void contract_by(std::size_t by)
{
return get<0>.(this->region).contract_by(by);
}
void translate(std::size_t by)
{
return get<0>.(this->region).translate(by);
}
bool operator == (dom1D const& other) const
{
return ( get<0>.(this->region) == get<0>.(other.region) );
}
bool operator != (dom1D const& other) const
{
return (get<0>.(this->region) != get<0>.(other.region));
}
std::size_t rank() const
{
return get<0>.(this->region).rank();
}
void swap(dom1D const& other)
{
return get<0>.(this->region).swap(other.region);
}
/* template<int N>
typename tuple_element<N,domain_type>::type get_domain() const
{ return get<N>(m_domain); }
*/
template<typename T>
friend std::ostream& operator<<(std::ostream& print, dom1D<T> const& to_p);
};
template<typename T>
std::ostream & operator<<(std::ostream &print, dom1D<T> const& to_p)
{
print << get<0>(d.m_domain);
return print;
}
}