root/src/misc/neighborhoodadapter.h @ 188:e8da57ee83e2

Revision 188:e8da57ee83e2, 1.4 KB (checked in by Andreas Schaefer <gentryx@…>, 14 months ago)

refactored CoordDiagonal?, which is now a static member of Coord

Line 
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
8namespace 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 */
15template<class NEIGHBORHOOD, typename KEY, typename CARGO, int DIM>
16class NeighborhoodAdapter
17{
18public:
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    {
28        const Cargo *res = (*neighbors)[Coord<DIM>()][id];
29           
30        if (res)
31            return *res;
32
33        CoordBox<DIM> surroundingBox(Coord<DIM>::diagonal(-1), Coord<DIM>::diagonal(3));
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
52private:
53    NEIGHBORHOOD *neighbors;
54};
55
56}
57
58#endif
Note: See TracBrowser for help on using the browser.