ABLATE Source Documentation  0.12.33
reverseRange.hpp
1 #ifndef ABLATELIBRARY_REVERSERANGE_HPP
2 #define ABLATELIBRARY_REVERSERANGE_HPP
3 #include <vector>
4 #include "petsc.h"
5 #include "range.hpp"
6 namespace ablate::domain {
7 
11 struct ReverseRange {
12  private:
13  PetscInt rangeStart = 0;
14  PetscInt pointStart = 0;
15  std::vector<PetscInt> indices;
16 
17  public:
18  explicit ReverseRange(const ablate::domain::Range& range) {
19  rangeStart = range.start;
20  if (range.points && (range.start != range.end)) {
21  // find the min/max point
22  PetscInt maxPoint = rangeStart;
23  for (PetscInt i = range.start; i < range.end; ++i) {
24  pointStart = PetscMin(pointStart, range.GetPoint(i));
25  maxPoint = PetscMax(maxPoint, range.GetPoint(i));
26  }
27 
28  // size to the maximum location, set default to -1
29  indices.resize(maxPoint - pointStart + 1, -1);
30 
31  // store the index at each point
32  for (PetscInt i = range.start; i < range.end; ++i) {
33  indices[range.GetPoint(i) - pointStart] = i;
34  }
35  }
36  }
37 
38  ReverseRange() : rangeStart(-1), pointStart(-1) {}
39 
45  inline PetscInt GetIndex(PetscInt point) const { return indices.empty() ? point : indices[point - pointStart]; }
46 
52  inline PetscInt GetAbsoluteIndex(PetscInt point) const { return GetIndex(point) - rangeStart; }
53 };
54 } // namespace ablate::domain
55 #endif // ABLATELIBRARY_RANGE_HPP
Definition: range.hpp:11
PetscInt GetPoint(PetscInt i) const
Definition: range.hpp:22
Definition: reverseRange.hpp:11
PetscInt GetAbsoluteIndex(PetscInt point) const
Definition: reverseRange.hpp:52
PetscInt GetIndex(PetscInt point) const
Definition: reverseRange.hpp:45