KrisLibrary  1.0.0
IndexSet.h
1 #ifndef INDEX_SET_H
2 #define INDEX_SET_H
3 
4 #include <vector>
5 #include <KrisLibrary/errors.h>
6 
10 class IndexSet
11 {
12  public:
13  IndexSet(int imax=0);
14  IndexSet(int imin,int imax);
15  IndexSet(const std::vector<int>& indices);
16  operator std::vector<int> () const;
17  int operator [] (int i) const;
18  inline bool IsRange() const { return imin<=imax; }
19  size_t Size() const;
20  int Find(int index) const;
21  int MaxValue() const;
22  template <class T> void GetElements(const T& x,T& xind) const;
23  template <class T> void SetElements(const T& xind,T& x) const;
24 
25  int imin,imax;
26  std::vector<int> indices;
27 };
28 
29 template <class T>
30 void IndexSet::GetElements(const T& x,T& xind) const
31 {
32  xind.resize(Size());
33  if(IsRange())
34  std::copy(x.begin()+imin,x.begin()+imax,xind.begin());
35  else {
36  for(size_t i=0;i<indices.size();i++)
37  xind[i] = x[indices[i]];
38  }
39 }
40 
41 template <class T>
42 void IndexSet::SetElements(const T& xind,T& x) const
43 {
44  Assert((size_t)xind.size()==Size());
45  if(IsRange()) {
46  Assert(imin >= 0);
47  Assert(imax < (int)x.size());
48  std::copy(xind.begin(),xind.end(),x.begin()+imin);
49  }
50  else {
51  for(size_t i=0;i<indices.size();i++) {
52  Assert(indices[i] >= 0 && indices[i] < (int)x.size());
53  x[indices[i]] = xind[i];
54  }
55  }
56 }
57 
58 #endif
void copy(const T &a, T *out, int n)
Definition: arrayutils.h:34
A generic set of indices, either listed explicitly or in a range. If imax < imin, this indicates that...
Definition: IndexSet.h:10