diff options
Diffstat (limited to 'src/BlockTypePalette.cpp')
-rw-r--r-- | src/BlockTypePalette.cpp | 75 |
1 files changed, 55 insertions, 20 deletions
diff --git a/src/BlockTypePalette.cpp b/src/BlockTypePalette.cpp index fabf5698e..f0f54b7c1 100644 --- a/src/BlockTypePalette.cpp +++ b/src/BlockTypePalette.cpp @@ -4,9 +4,9 @@ -BlockTypePalette::BlockTypePalette() +BlockTypePalette::BlockTypePalette(): + mMaxIndex(0) { - // Nothing needed yet } @@ -22,8 +22,10 @@ UInt32 BlockTypePalette::index(const AString & aBlockTypeName, const BlockState } // Not found, append: - mPalette.push_back(std::make_pair(aBlockTypeName, aBlockState)); - return static_cast<UInt32>(mPalette.size() - 1); + auto index = mMaxIndex++; + mBlockToNumber[aBlockTypeName][aBlockState] = index; + mNumberToBlock[index] = {aBlockTypeName, aBlockState}; + return index; } @@ -32,16 +34,17 @@ UInt32 BlockTypePalette::index(const AString & aBlockTypeName, const BlockState std::pair<UInt32, bool> BlockTypePalette::maybeIndex(const AString & aBlockTypeName, const BlockState & aBlockState) const { - auto count = mPalette.size(); - for (size_t idx = 0; idx < count; ++idx) + auto itr1 = mBlockToNumber.find(aBlockTypeName); + if (itr1 == mBlockToNumber.end()) { - const auto & entry = mPalette[idx]; - if ((entry.first == aBlockTypeName) && (entry.second == aBlockState)) - { - return std::make_pair(static_cast<UInt32>(idx), true); - } + return {0, false}; + } + auto itr2 = itr1->second.find(aBlockState); + if (itr2 == itr1->second.end()) + { + return {0, false}; } - return std::make_pair(0, false); + return {itr2->second, true}; } @@ -50,7 +53,7 @@ std::pair<UInt32, bool> BlockTypePalette::maybeIndex(const AString & aBlockTypeN UInt32 BlockTypePalette::count() const { - return static_cast<UInt32>(mPalette.size()); + return static_cast<UInt32>(mNumberToBlock.size()); } @@ -59,22 +62,54 @@ UInt32 BlockTypePalette::count() const const std::pair<AString, BlockState> & BlockTypePalette::entry(UInt32 aIndex) const { - ASSERT(aIndex < mPalette.size()); - return mPalette[aIndex]; + auto itr = mNumberToBlock.find(aIndex); + if (itr == mNumberToBlock.end()) + { + throw NoSuchIndexException(aIndex); + } + return itr->second; } -std::map<UInt32, UInt32> BlockTypePalette::createTransformMap(const BlockTypePalette & aOther) +std::map<UInt32, UInt32> BlockTypePalette::createTransformMapAddMissing(const BlockTypePalette & aFrom) { std::map<UInt32, UInt32> res; - auto numIndices = aOther.count(); - for (UInt32 idx = 0; idx < numIndices; ++idx) + for (const auto & fromEntry: aFrom.mNumberToBlock) { - const auto & e = aOther.mPalette[idx]; - res[idx] = index(e.first, e.second); + auto fromIndex = fromEntry.first; + const auto & blockTypeName = fromEntry.second.first; + const auto & blockState = fromEntry.second.second; + res[fromIndex] = index(blockTypeName, blockState); + } + return res; +} + + + + + +std::map<UInt32, UInt32> BlockTypePalette::createTransformMapWithFallback(const BlockTypePalette & aFrom, UInt32 aFallbackIndex) const +{ + std::map<UInt32, UInt32> res; + for (const auto & fromEntry: aFrom.mNumberToBlock) + { + auto fromIndex = fromEntry.first; + const auto & blockTypeName = fromEntry.second.first; + const auto & blockState = fromEntry.second.second; + auto thisIndex = maybeIndex(blockTypeName, blockState); + if (thisIndex.second) + { + // The entry was found in this + res[fromIndex] = thisIndex.first; + } + else + { + // The entry was NOT found in this, replace with fallback: + res[fromIndex] = aFallbackIndex; + } } return res; } |