KrisLibrary  1.0.0
RangeSet2D.h
1 #ifndef RANGE_SET_2D_H
2 #define RANGE_SET_2D_H
3 
4 #include "IntTuple.h"
5 #include <KrisLibrary/structs/array2d.h>
6 #include <set>
7 using namespace std;
8 
20 {
21  public:
22  typedef set<IntPair>::iterator iterator;
23  typedef set<IntPair>::const_iterator const_iterator;
24 
25  RangeSet2D();
26  void clear();
27  inline bool empty() { return items.empty(); }
28  void setRange(const IntPair& indexmin,const IntPair& indexmax);
29  void expandRange(const IntPair& index);
30  void insert(const IntPair& index);
31  template <class It>
32  void insert(It first,It last);
33  void erase(iterator it);
34  void erase(const IntPair& item);
35  template <class It>
36  void erase(It first,It last);
37  inline iterator begin() { return items.begin(); }
38  inline const_iterator begin() const { return items.begin(); }
39  inline iterator end() { return items.end(); }
40  inline const_iterator end() const { return items.end(); }
41  size_t size() const { return items.size(); }
42  int count(const IntPair& item);
43  iterator find(const IntPair& item);
44  const_iterator find(const IntPair& item) const;
45  inline bool isRangeEmpty() const { return imax.a < imin.a; }
46  inline const IntPair& minimum() const { return imin; }
47  inline const IntPair& maximum() const { return imax; }
48  inline bool inRange(const IntPair& item) const { return item.a >= imin.a && item.a <= imax.a && item.b >= imin.b && item.b <= imax.b; }
49  inline bool cacheGet(const IntPair& item) const { return contains(item.a-imin.a,item.b-imin.b); }
50  inline void cacheSet(const IntPair& item,bool value) { contains(item.a-imin.a,item.b-imin.b)=value; }
51 
52  void BuildCache();
53  void ClearCache();
54  inline bool IsCacheBuilt() const { return hasContainmentCache; }
55 
56  private:
57  set<IntPair> items;
58  IntPair imin,imax;
59  bool hasContainmentCache;
60  Array2D<bool> contains;
61 };
62 
63 template <class It>
64 void RangeSet2D::insert(It first,It last)
65 {
66  for(It i=first;i!=last;i++)
67  insert(*i);
68 }
69 
70 template <class It>
71 void RangeSet2D::erase(It first,It last)
72 {
73  for(It i=first;i!=last;i++)
74  erase(*i);
75 }
76 
77 #endif
A lightweight integer 2-tuple class.
Definition: IntPair.h:9
Definition: rayprimitives.h:132
A set of 2d indices within a range. Operates in two modes, set or bit matrix mode. In bit-matrix mode, allows O(1) membership testing (but the range is fixed). Set mode is just like a regular set.
Definition: RangeSet2D.h:19