This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
Indoor/grid/GridNeighborIterator.h
2018-10-25 11:50:12 +02:00

77 lines
2.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* © 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