aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/bboxset.hh
blob: a8e2ab0ad5d8f9a77c1a1c8cc3a589dacb6be50f (plain)
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
#ifndef BBOXSET_HH
#define BBOXSET_HH

#include <cassert>
#include <iostream>
#include <list>
#include <set>
#include <vector>

#include "bbox.hh"
#include "defs.hh"
#include "vect.hh"

using namespace std;



// Choose the implementation of bboxset by #defining exactly one of
// these
#undef BBOXSET_SET              // outdated
#undef BBOXSET_LIST             // well tested
#define BBOXSET_VECTOR          // brand new



// Forward declaration
template<typename T, int D> class bboxset;

// template<typename T,int D>
// bboxset<T,D> operator+ (const bbox<T,D>& b1, const bbox<T,D>& b2);
// template<typename T,int D>
// bboxset<T,D> operator+ (const bbox<T,D>& b, const bboxset<T,D>& s);

// template<typename T,int D>
// bboxset<T,D> operator- (const bbox<T,D>& b1, const bbox<T,D>& b2);
// template<typename T,int D>
// bboxset<T,D> operator- (const bbox<T,D>& b, const bboxset<T,D>& s);

// Input
template<typename T,int D>
istream& operator>> (istream& is, bboxset<T,D>& s);

// Output
template<typename T,int D>
ostream& operator<< (ostream& os, const bboxset<T,D>& s);



// Bounding box set class
template<typename T, int D>
class bboxset {
  
  // Cost annotations depend on the number of bset elements n, and
  // assume that normalization is skipped.
  
  struct skip_normalize_t {
    bboxset<T,D>& s;
    bool const saved_skip_normalize;
    skip_normalize_t(bboxset<T,D>& s_)
      : s(s_),
        saved_skip_normalize(s.skip_normalize)
    {
      s.skip_normalize = true;
    }
    ~skip_normalize_t()
    {
      s.skip_normalize = saved_skip_normalize;
      s.normalize();
    }
  };
  
  // Types
  typedef bbox<T,D> box;
#ifdef BBOXSET_SET
  //S typedef set<box> bset;
#endif
#ifdef BBOXSET_LIST
  typedef list<box> bset;
#endif
#ifdef BBOXSET_VECTOR
  typedef vector<box> bset;
#endif
  
  // Fields
  bset bs;
  // Invariant:
  // All bboxes have the same stride.
  // No bbox is empty.
  // The bboxes don't overlap.
  
  bool skip_normalize;
  
public:
  
  // Constructors
  bboxset ();                   // cost: O(1)
  bboxset (const box& b);       // cost: O(1)
  bboxset (const bboxset& s);   // cost: O(n)
  
  bboxset (const list<box>& lb);
  bboxset (const vector<box>& lb);
  bboxset (const vector<list<box> >& vlb);
  template<typename U>
  bboxset (const vector<U>& vb, const bbox<T,D> U::* const v);
  template<typename U>
  bboxset (const vector<U>& vb, const bboxset U::* const v);
  
  static bboxset poison ();
  
  // Invariant
  bool invariant () const CCTK_MEMBER_ATTRIBUTE_PURE;
  
private:
  // Normalisation
  void normalize ();
  
public:
  // Accessors
  bool empty () const { return bs.empty(); } // cost: O(1)
  // T size () const;
  typedef typename box::size_type size_type;
  size_type size () const CCTK_MEMBER_ATTRIBUTE_PURE; // cost: O(n)
  int setsize () const { return bs.size(); } // cost: O(1)
  
  // Find out whether the bbox contains the point x
  bool contains (const vect<T,D>& x)
    const CCTK_MEMBER_ATTRIBUTE_PURE; // cost: O(n)
  
  // Find out whether this bboxset intersects the bbox b
  bool intersects (const box& b) const CCTK_MEMBER_ATTRIBUTE_PURE; // cost: O(n)
  
  // Add (bboxes that don't overlap)
  bboxset& operator+= (const box& b);     // cost: O(1)
  bboxset& operator+= (const bboxset& s); // cost: O(n)
  bboxset& add_transfer (bboxset& s);     // cost: O(1)
  bboxset operator+ (const box& b)
    const CCTK_MEMBER_ATTRIBUTE_PURE; // cost: O(n)
  bboxset operator+ (const bboxset& s)
    const CCTK_MEMBER_ATTRIBUTE_PURE; // cost: O(n)
  
  // Union
  bboxset& operator|= (const box& b);     // cost: O(n)
  bboxset& operator|= (const bboxset& s); // cost: O(n^2)
  bboxset operator| (const box& b)
    const CCTK_MEMBER_ATTRIBUTE_PURE; // cost: O(n)
  bboxset operator| (const bboxset& s)
    const CCTK_MEMBER_ATTRIBUTE_PURE; // cost: O(n^2)
  
  // Intersection
  bboxset operator& (const box& b)
    const CCTK_MEMBER_ATTRIBUTE_PURE; // cost: O(n)
  bboxset operator& (const bboxset& s)
    const CCTK_MEMBER_ATTRIBUTE_PURE;     // cost: O(n)
  bboxset& operator&= (const box& b);     // cost: O(n)
  bboxset& operator&= (const bboxset& s); // cost: O(n)
  
  // Difference
  // friend bboxset operator- <T,D>(const box& b1, const box& b2);
  static bboxset minus (const box& b1, const box& b2)
    CCTK_ATTRIBUTE_PURE;        // cost: O(1)
  bboxset operator- (const box& b)
    const CCTK_MEMBER_ATTRIBUTE_PURE; // cost: O(n)
  // cost: O(n)
  bboxset& operator-= (const box& b)
  {
    *this = *this - b;
    assert (invariant());
    return *this;
  }
  bboxset& operator-= (const bboxset& s);     // cost: O(n^2)
  bboxset operator- (const bboxset& s)
    const CCTK_MEMBER_ATTRIBUTE_PURE; // cost: O(n^2)
  // friend bboxset operator- <T,D>(const box& b, const bboxset& s);
  static bboxset minus (const box& b, const bboxset& s)
    CCTK_ATTRIBUTE_PURE;        // cost: O(n^2)
  
  /** Find a bbox containing the whole set.  */
  box container () const CCTK_MEMBER_ATTRIBUTE_PURE; // cost: O(n)
  /** Find the pseudo-inverse.  */
  bboxset pseudo_inverse (const int n) const CCTK_MEMBER_ATTRIBUTE_PURE;
  
  /** Expand (enlarge) the bboxset by multiples of the stride.  */
  bboxset expand (const vect<T,D>& lo, const vect<T,D>& hi)
    const CCTK_MEMBER_ATTRIBUTE_PURE;
  bboxset expand (const vect<vect<T,D>,2>& lohi) const
  { return expand (lohi[0], lohi[1]); }
  
  /** Shift the bboxset by multiples of the stride.  */
  bboxset shift (const vect<T,D>& v) const
  { return expand (-v, v); }
  
  /** Expand (enlarge) the bboxset by multiples of a fraction of the
      stride.  */
  // cost: O(n^2) in general, but only O(n) for shifting
  bboxset expand (const vect<T,D>& lo, const vect<T,D>& hi,
                  const vect<T,D>& denom) const CCTK_MEMBER_ATTRIBUTE_PURE;
  // cost: O(n^2) in general, but only O(n) for shifting
  bboxset expand (const vect<vect<T,D>,2>& lohi, const vect<T,D>& denom) const
  { return expand (lohi[0], lohi[1], denom); }
  
  /** Shift the bboxset by multiples of a fraction of the stride.  */
// cost: O(n)
  bboxset shift (const vect<T,D>& v, const vect<T,D>& denom) const
  { return expand (-v, v, denom); }
  
  /** Find the smallest b-compatible box around this bbox.
      ("compatible" means having the same stride.)  */
  bboxset expanded_for (const box& b) const CCTK_MEMBER_ATTRIBUTE_PURE;
  
  // TODO: this is incorrect
#if 1
  /** Find the largest b-compatible box inside this bbox.  */
  bboxset contracted_for (const box& b) const CCTK_MEMBER_ATTRIBUTE_PURE;
#endif
  
  // Equality
  bool operator== (const bboxset& s) const CCTK_MEMBER_ATTRIBUTE_PURE;
  bool operator!= (const bboxset& s) const CCTK_MEMBER_ATTRIBUTE_PURE;
  bool operator< (const bboxset& s) const CCTK_MEMBER_ATTRIBUTE_PURE;
  bool operator<= (const bboxset& s) const CCTK_MEMBER_ATTRIBUTE_PURE;
  bool operator> (const bboxset& s) const CCTK_MEMBER_ATTRIBUTE_PURE;
  bool operator>= (const bboxset& s) const CCTK_MEMBER_ATTRIBUTE_PURE;
  
  // Iterators
  typedef typename bset::const_iterator const_iterator;
  typedef typename bset::iterator       iterator;
  
  const_iterator begin () const { return bs.begin(); }
  const_iterator end () const   { return bs.end(); }
//   iterator begin () const { return bs.begin(); }
//   iterator end () const   { return bs.end(); }
  
  // Memory usage
  size_t memory () const { return memoryof (bs); }
  
  // Input
  istream& input (istream& is);
  
  // Output
  ostream& output (ostream& os) const;
};



template<typename T,int D>
inline bboxset<T,D> operator+ (const bbox<T,D>& b1, const bbox<T,D>& b2) {
  return bboxset<T,D>(b1) + bboxset<T,D>(b2);
}

template<typename T,int D>
inline bboxset<T,D> operator+ (const bbox<T,D>& b, const bboxset<T,D>& s) {
  return bboxset<T,D>(b) + s;
}

// cost: O(1)
template<typename T,int D>
inline bboxset<T,D> operator- (const bbox<T,D>& b1, const bbox<T,D>& b2) {
  return bboxset<T,D>::minus(b1,b2);
}

// cost: O(n^2)
template<typename T,int D>
inline bboxset<T,D> operator- (const bbox<T,D>& b, const bboxset<T,D>& s) {
  return bboxset<T,D>::minus(b,s);
}



template<typename T,int D>
inline bboxset<T,D> operator| (const bbox<T,D>& b, const bboxset<T,D>& s) {
  return s | b;
}

template<typename T,int D>
inline bboxset<T,D> operator& (const bbox<T,D>& b, const bboxset<T,D>& s) {
  return s & b;
}



template<typename T,int D>
inline bool operator== (const bbox<T,D>& b, const bboxset<T,D>& s)
{
  return bboxset<T,D>(b) == s;
}

template<typename T,int D>
inline bool operator!= (const bbox<T,D>& b, const bboxset<T,D>& s)
{
  return bboxset<T,D>(b) != s;
}

template<typename T,int D>
inline bool operator< (const bbox<T,D>& b, const bboxset<T,D>& s)
{
  return bboxset<T,D>(b) < s;
}

template<typename T,int D>
inline bool operator<= (const bbox<T,D>& b, const bboxset<T,D>& s)
{
  return bboxset<T,D>(b) <= s;
}

template<typename T,int D>
inline bool operator> (const bbox<T,D>& b, const bboxset<T,D>& s)
{
  return bboxset<T,D>(b) > s;
}

template<typename T,int D>
inline bool operator>= (const bbox<T,D>& b, const bboxset<T,D>& s)
{
  return bboxset<T,D>(b) >= s;
}



template<typename T,int D>
inline bool operator== (const bboxset<T,D>& s, const bbox<T,D>& b)
{
  return s == bboxset<T,D>(b);
}

template<typename T,int D>
inline bool operator!= (const bboxset<T,D>& s, const bbox<T,D>& b)
{
  return s != bboxset<T,D>(b);
}

template<typename T,int D>
inline bool operator< (const bboxset<T,D>& s, const bbox<T,D>& b)
{
  return s < bboxset<T,D>(b);
}

template<typename T,int D>
inline bool operator<= (const bboxset<T,D>& s, const bbox<T,D>& b)
{
  return s <= bboxset<T,D>(b);
}

template<typename T,int D>
inline bool operator> (const bboxset<T,D>& s, const bbox<T,D>& b)
{
  return s > bboxset<T,D>(b);
}

template<typename T,int D>
inline bool operator>= (const bboxset<T,D>& s, const bbox<T,D>& b)
{
  return s >= bboxset<T,D>(b);
}



// Memory usage
template<typename T, int D>
inline size_t memoryof (bboxset<T,D> const & s)
{ return s.memory(); }



// Input
template<typename T,int D>
inline istream& operator>> (istream& is, bboxset<T,D>& s) {
  return s.input(is);
}



// Output
template<typename T,int D>
inline ostream& operator<< (ostream& os, const bboxset<T,D>& s) {
  return s.output(os);
}



#endif // BBOXSET_HH