#How would I refactor this to use iterator or otherwise not construct and return a container?

7 messages · Page 1 of 1 (latest)

hollow dome
#
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?

mortal cosmosBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

hollow dome
#

apparently there is a way to make it work with joining views or something but it runs even worse

#

!solved

mortal cosmosBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

static jacinth
#

A simple solution is to not return anything

#

Instead accept a function and call it for each neighbor