From c4cccf904321d9ea77e62f183376754a6694472c Mon Sep 17 00:00:00 2001 From: David Williams Date: Sun, 12 Apr 2015 09:55:30 +0200 Subject: [PATCH] Replaced double for loop with cleaner do-while loop. --- include/PolyVox/PagedVolume.inl | 44 ++++++++------------------------- 1 file changed, 10 insertions(+), 34 deletions(-) diff --git a/include/PolyVox/PagedVolume.inl b/include/PolyVox/PagedVolume.inl index 9bcb3f10..ab6af57c 100644 --- a/include/PolyVox/PagedVolume.inl +++ b/include/PolyVox/PagedVolume.inl @@ -271,48 +271,24 @@ namespace PolyVox typename PagedVolume::Chunk* PagedVolume::getChunk(int32_t uChunkX, int32_t uChunkY, int32_t uChunkZ) const { // The chunk was not the same as last time, but we can now hope it is in the set of most recently used chunks. - /*Chunk* pChunk = nullptr; - auto itChunk = m_mapChunks.find(v3dChunkPos); - - // Check whether the chunk was found. - if ((itChunk) != m_mapChunks.end()) - { - // The chunk was found so we can use it. - pChunk = itChunk->second.get(); - POLYVOX_ASSERT(pChunk, "Recent chunk list shold never contain a null pointer."); - pChunk->m_uChunkLastAccessed = ++m_uTimestamper; - }*/ - Chunk* pChunk = nullptr; - const int32_t startIndex = (((uChunkX & 0xFF)) | ((uChunkY & 0x0F) << 4) | ((uChunkZ & 0x0F) << 8) << 2 ); - for (uint32_t ct = startIndex; ct < 16384; ct++) + const int32_t iStartIndex = (((uChunkX & 0xFF)) | ((uChunkY & 0x0F) << 4) | ((uChunkZ & 0x0F) << 8) << 2 ); + int32_t iIndex = iStartIndex; + do { - if (m_arrayChunks[ct]) + if (m_arrayChunks[iIndex]) { - Vector3DInt32& entryPos = m_arrayChunks[ct]->m_v3dChunkSpacePosition; + Vector3DInt32& entryPos = m_arrayChunks[iIndex]->m_v3dChunkSpacePosition; if (entryPos.getX() == uChunkX && entryPos.getY() == uChunkY && entryPos.getZ() == uChunkZ) { - pChunk = m_arrayChunks[ct].get(); + pChunk = m_arrayChunks[iIndex].get(); break; } } - } - if (pChunk == nullptr) - { - for (uint32_t ct = 0; ct < startIndex; ct++) - { - if (m_arrayChunks[ct]) - { - Vector3DInt32& entryPos = m_arrayChunks[ct]->m_v3dChunkSpacePosition; - if (entryPos.getX() == uChunkX && entryPos.getY() == uChunkY && entryPos.getZ() == uChunkZ) - { - pChunk = m_arrayChunks[ct].get(); - break; - } - } - } - } + iIndex++; + iIndex %= 16384; + } while (iIndex != iStartIndex); // Check whether the chunk was found. if (pChunk) @@ -331,7 +307,7 @@ namespace PolyVox pChunk->m_uChunkLastAccessed = ++m_uTimestamper; // Important, as we may soon delete the oldest chunk //m_mapChunks.insert(std::make_pair(v3dChunkPos, std::unique_ptr(pChunk))); - for (uint32_t ct = startIndex; ct < 16384; ct++) + for (uint32_t ct = iStartIndex; ct < 16384; ct++) { if (m_arrayChunks[ct] == nullptr) {