diff options
| author | notyourav <65437533+notyourav@users.noreply.github.com> | 2020-11-16 15:49:02 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-16 15:49:02 -0800 |
| commit | d4f80d7cf97641742dd56cabc0b1d503cfdf7b0d (patch) | |
| tree | 05b09142b74869f74779a00deff410019b8dcaac /src/KingSystem/Utils/Byaml/ByamlHashIter.cpp | |
| parent | 8205744abcae5784138629038976f2449269a81f (diff) | |
| parent | b0498a15bf6f1782f1429ffbd0b03c187f61d7c9 (diff) | |
Merge branch 'master' into placement
Diffstat (limited to 'src/KingSystem/Utils/Byaml/ByamlHashIter.cpp')
| -rw-r--r-- | src/KingSystem/Utils/Byaml/ByamlHashIter.cpp | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/KingSystem/Utils/Byaml/ByamlHashIter.cpp b/src/KingSystem/Utils/Byaml/ByamlHashIter.cpp new file mode 100644 index 00000000..edce10ee --- /dev/null +++ b/src/KingSystem/Utils/Byaml/ByamlHashIter.cpp @@ -0,0 +1,110 @@ +#include "KingSystem/Utils/Byaml/ByamlHashIter.h" +#include "KingSystem/Utils/Byaml/Byaml.h" +#include "KingSystem/Utils/Byaml/ByamlData.h" +#include "KingSystem/Utils/Byaml/ByamlLocal.h" + +namespace al { + +ByamlHashIter::ByamlHashIter(const u8* data) { + mData = data; +} + +s32 ByamlHashIter::getSize() const { + if (!mData) { + return 0; + } + + return ByamlLocalUtil::getContainerSize(mData); +} + +const ByamlHashPair* ByamlHashIter::getPairTable() const { + if (!mData) { + return nullptr; + } + + return reinterpret_cast<const ByamlHashPair*>(&mData[mTableOffset]); +} + +bool ByamlHashIter::getDataByIndex(ByamlData* data, s32 index) const { + if (!mData) { + return false; + } + + if (ByamlLocalUtil::getContainerSize(mData) == 0) { + return false; + } + + const ByamlHashPair* pair_table = getPairTable(); + const ByamlHashPair* pair = &pair_table[index]; + if (!pair) // This seems wrong, this can never be null? + { + return false; + } + + data->set(pair); + + return true; +} + +bool ByamlHashIter::getDataByKey(ByamlData* data, s32 key_index) const { + if (getSize() == 0) { + return false; + } + + const ByamlHashPair* pair = findPair(key_index); + if (!pair) { + return false; + } + + data->set(pair); + return true; +} + +const ByamlHashPair* ByamlHashIter::findPair(s32 key_index) const { + const ByamlHashPair* pair_table = getPairTable(); + if (!pair_table) { + return nullptr; + } + + if (ByamlLocalUtil::getContainerSize(mData) == 0) { + return nullptr; + } + + // Binary Search + s32 start = 0; + s32 end = getSize(); + s32 index; + const ByamlHashPair* pair; + while (true) { + if (start >= end) { + return nullptr; + } + + index = (start + end) / 2; + pair = &pair_table[index]; + s32 result = key_index - pair->getKey(); + if (result == 0) + break; + if (result > 0) + start = index + 1; + else if (result < 0) + end = index; + } + + return pair; +} + +const ByamlHashPair* ByamlHashIter::getPairByIndex(s32 index) const { + if (index < 0) { + return nullptr; + } + + if (getSize() <= index) { + return nullptr; + } + + const ByamlHashPair* pair_table = getPairTable(); + return pair_table + index; +} + +} // namespace al |
