77 lines
2.0 KiB
C++
77 lines
2.0 KiB
C++
/*
|
||
* © Copyright 2014 – Urheberrechtshinweis
|
||
* Alle Rechte vorbehalten / All Rights Reserved
|
||
*
|
||
* Programmcode ist urheberrechtlich geschuetzt.
|
||
* Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner.
|
||
* Keine Verwendung ohne explizite Genehmigung.
|
||
* (vgl. § 106 ff UrhG / § 97 UrhG)
|
||
*/
|
||
|
||
#ifndef GRIDNEIGHBORITERATOR_H
|
||
#define GRIDNEIGHBORITERATOR_H
|
||
|
||
|
||
/** allows iterating over all neighbors of one node */
|
||
class NeighborIter : std::iterator<std::input_iterator_tag, int> {
|
||
|
||
private:
|
||
|
||
/** the grid the src-node belongs to */
|
||
Grid<T>* grid;
|
||
|
||
/** index of the source-node within its grid */
|
||
int srcNodeIdx;
|
||
|
||
/** index of the current neighbor [0:10] */
|
||
int nIdx;
|
||
|
||
public:
|
||
|
||
/** ctor */
|
||
NeighborIter(const Grid<T>& grid, const int srcNodeIdx, const int nIdx) :
|
||
grid((Grid<T>*)&grid), srcNodeIdx(srcNodeIdx), nIdx(nIdx) {;}
|
||
|
||
/** next neighbor */
|
||
NeighborIter& operator++() {++nIdx; return *this;}
|
||
|
||
/** next neighbor */
|
||
NeighborIter operator++(int) {NeighborIter tmp(*this); operator++(); return tmp;}
|
||
|
||
/** compare with other iterator */
|
||
bool operator==(const NeighborIter& rhs) {return srcNodeIdx == rhs.srcNodeIdx && nIdx == rhs.nIdx;}
|
||
|
||
/** compare with other iterator */
|
||
bool operator!=(const NeighborIter& rhs) {return srcNodeIdx != rhs.srcNodeIdx || nIdx != rhs.nIdx;}
|
||
|
||
/** get the neighbor the iterator currently points to */
|
||
T& operator*() {return (T&) grid->getNeighbor(srcNodeIdx, nIdx);}
|
||
|
||
};
|
||
|
||
/** allows for-each iteration over all neighbors of one node */
|
||
class NeighborForEach {
|
||
private:
|
||
|
||
/** the grid the src-node belongs to */
|
||
const Grid<T>& grid;
|
||
|
||
/** index of the source-node within its grid */
|
||
const int srcNodeIdx;
|
||
|
||
public:
|
||
|
||
/** ctor */
|
||
NeighborForEach(const Grid<T>& grid, const int srcNodeIdx) :
|
||
grid(grid), srcNodeIdx(srcNodeIdx) {;}
|
||
|
||
/** starting point */
|
||
NeighborIter begin() {return NeighborIter(grid, srcNodeIdx, 0);}
|
||
|
||
/** end point */
|
||
NeighborIter end() {return NeighborIter(grid, srcNodeIdx, grid[srcNodeIdx]._numNeighbors);}
|
||
|
||
};
|
||
|
||
#endif // GRIDNEIGHBORITERATOR_H
|