| Rev | Line | |
|---|
| [30] | 1 | #ifndef _libgeodecomp_misc_neighborhoodadapter_h_ |
|---|
| 2 | #define _libgeodecomp_misc_neighborhoodadapter_h_ |
|---|
| 3 | |
|---|
| 4 | #include <libgeodecomp/misc/coord.h> |
|---|
| 5 | #include <libgeodecomp/misc/coordbox.h> |
|---|
| 6 | #include <libgeodecomp/misc/neighborhoodadapter.h> |
|---|
| 7 | |
|---|
| 8 | namespace LibGeoDecomp { |
|---|
| 9 | |
|---|
| 10 | /** |
|---|
| 11 | * This class is most useful for interfacing meshless codes with |
|---|
| 12 | * LibGeoDecomp. It can retrieve cells matching a certain id from the |
|---|
| 13 | * ContainerCells in the current neighborhood. |
|---|
| 14 | */ |
|---|
| 15 | template<class NEIGHBORHOOD, typename KEY, typename CARGO, int DIM> |
|---|
| 16 | class NeighborhoodAdapter |
|---|
| 17 | { |
|---|
| 18 | public: |
|---|
| 19 | typedef KEY Key; |
|---|
| 20 | typedef CARGO Cargo; |
|---|
| 21 | |
|---|
| 22 | NeighborhoodAdapter(NEIGHBORHOOD *_neighbors) : |
|---|
| 23 | neighbors(_neighbors) |
|---|
| 24 | {} |
|---|
| 25 | |
|---|
| 26 | const Cargo& operator[](const Key& id) |
|---|
| 27 | { |
|---|
| [33] | 28 | const Cargo *res = (*neighbors)[Coord<DIM>()][id]; |
|---|
| [30] | 29 | |
|---|
| 30 | if (res) |
|---|
| 31 | return *res; |
|---|
| 32 | |
|---|
| [188] | 33 | CoordBox<DIM> surroundingBox(Coord<DIM>::diagonal(-1), Coord<DIM>::diagonal(3)); |
|---|
| [30] | 34 | CoordBoxSequence<DIM> s = surroundingBox.sequence(); |
|---|
| 35 | while (s.hasNext()) { |
|---|
| 36 | Coord<DIM> c = s.next(); |
|---|
| 37 | if (c != Coord<DIM>()) { |
|---|
| 38 | res = (*neighbors)[c][id]; |
|---|
| 39 | if (res) |
|---|
| 40 | return *res; |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | throw std::logic_error("id not found"); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | inline const Cargo& operator[](const Key& id) const |
|---|
| 48 | { |
|---|
| 49 | return (const_cast<NeighborhoodAdapter&>(*this))[id]; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | private: |
|---|
| 53 | NEIGHBORHOOD *neighbors; |
|---|
| 54 | }; |
|---|
| 55 | |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | #endif |
|---|