std::vector<uint16_t> NeighborsOfCell(int cellX, int cellY)
{
std::vector<uint16_t> neighbors;
if (cellX > 0 && cellY > 0) [[likely]] {
const auto &cell = m_cells[(cellX - 1) + NUM_CELLS * (cellY - 1)];
neighbors.insert(neighbors.end(), cell.begin(), cell.end());
}
if (cellX > 0) [[likely]] {
const auto &cell = m_cells[(cellX - 1) + NUM_CELLS * cellY];
neighbors.insert(neighbors.end(), cell.begin(), cell.end());
}
if (cellY > 0) [[likely]] {
const auto &cell = m_cells[cellX + NUM_CELLS * (cellY - 1)];
neighbors.insert(neighbors.end(), cell.begin(), cell.end());
}
if (cellX > 0 && cellY < NUM_CELLS - 1) [[likely]] {
const auto &cell = m_cells[(cellX - 1) + NUM_CELLS * (cellY + 1)];
neighbors.insert(neighbors.end(), cell.begin(), cell.end());
}
return neighbors;
}
This is only a performance issue in debug builds. If I were to use an iterator instead is there a way to effectively "concatenate" the iterators begin and end to cover the same neighbors?
I think that I want to return a std::vector<uint16_t>::iterator?