Skip to content

Commit

Permalink
Add iterator over all neighbors for cells.
Browse files Browse the repository at this point in the history
  • Loading branch information
iljah committed May 12, 2019
1 parent 63e356e commit f6cfb02
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
33 changes: 26 additions & 7 deletions dccrg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4330,6 +4330,7 @@ template <
if given error_cell.
*/
// TODO: make private?
// TODO: should same neighbor appear several times in returned list?
std::vector<
std::pair<
uint64_t,
Expand Down Expand Up @@ -7365,10 +7366,18 @@ template <
uint64_t id = error_cell;
Cell_Data* data = nullptr;
Iterator_Storage<Neighbors_Item>
//! iterator to cells considered as neighbor by this cell
/*!
iterator to cells considered as neighbor by this cell,
may (and probably does) overlap with neighbors_to
*/
neighbors_of,
//! iterator to cells that consider this one as neighbor
neighbors_to;
/*!
iterator to cells that consider this one as neighbor,
may (and probably does) overlap with neighbors_of
*/
neighbors_to,
//! iterates over both neighbors_of and neighbors_to
all_neighbors;

friend bool operator < (const Cells_Item& a, const Cells_Item& b)
{
Expand Down Expand Up @@ -11573,10 +11582,12 @@ template <
}

for (size_t i = 0; i < cells.size(); i++) {
cells[i].neighbors_of.begin_ =
cells[i].neighbors_of.end_ =
cells[i].neighbors_to.begin_ =
cells[i].neighbors_to.end_ = neighbors.cbegin();
cells[i].neighbors_of.begin_ =
cells[i].neighbors_of.end_ =
cells[i].neighbors_to.begin_ =
cells[i].neighbors_to.end_ =
cells[i].all_neighbors.begin_ =
cells[i].all_neighbors.end_ = neighbors.cbegin();
std::advance(
cells[i].neighbors_of.begin_,
nr_neighbors[i][0]
Expand All @@ -11593,6 +11604,14 @@ template <
cells[i].neighbors_to.end_,
nr_neighbors[i][0] + nr_neighbors[i][1] + nr_neighbors[i][2] + nr_neighbors[i][3]
);
std::advance(
cells[i].all_neighbors.begin_,
nr_neighbors[i][0]
);
std::advance(
cells[i].all_neighbors.end_,
nr_neighbors[i][0] + nr_neighbors[i][1] + nr_neighbors[i][2] + nr_neighbors[i][3]
);
}

// update iterators
Expand Down
2 changes: 1 addition & 1 deletion tests/iterators/project_makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ tests/iterators/test1.mtst: \
tests/iterators/test2.exe: \
tests/iterators/test2.cpp \
$(TESTS_ITERATORS_COMMON_DEPS)
$(TESTS_ITERATORS_COMPILE_COMMAND)
$(TESTS_ITERATORS_COMPILE_COMMAND) -std=c++14

tests/iterators/test2.tst: \
tests/iterators/test2.exe
Expand Down
15 changes: 15 additions & 0 deletions tests/iterators/test2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@ int main(int argc, char* argv[])
return EXIT_FAILURE;
}

// check all_neighbors
if (not std::equal(
cell.neighbors_of.begin(),
cell.neighbors_to.end(),
cell.all_neighbors.begin(),
[](const auto& first, const auto& second){
return first.id == second.id;
}
)) {
cerr << __FILE__ "(" << __LINE__ << "): "
<< "all_neighbors of cell " << cell.id
<< " doesn't equal neighbors_of + _to" << endl;
return EXIT_FAILURE;
}

// check offset
for (const auto& neighbor: cell.neighbors_of) {
const int ref_diff = (neighbor.id - cell.id) * grid.mapping.get_cell_length_in_indices(cell.id);
Expand Down

0 comments on commit f6cfb02

Please sign in to comment.