I am rewriting this block from C++ to Swift.
Here's the original C++ script by Lukhnos Liu:
inline void
Span::removeNodeOfLengthGreaterThan(size_t length)
{
if (length > m_maximumLength) {
return;
}
size_t max = 0;
std::set<size_t> removeSet;
for (std::map<size_t, Node>::iterator i = m_lengthNodeMap.begin(),
e = m_lengthNodeMap.end();
i != e; ++i) {
if ((*i).first > length) {
removeSet.insert((*i).first);
} else {
if ((*i).first > max) {
max = (*i).first;
}
}
}
for (std::set<size_t>::iterator i = removeSet.begin(), e = removeSet.end();
i != e; ++i) {
m_lengthNodeMap.erase(*i);
}
m_maximumLength = max;
}
Here's my Swift recraft:
mutating func removeNodeOfLengthGreaterThan(_ length: Int) {
if length > mutMaximumLength { return }
var max = 0
var removalList: Set<Int> = []
for map in mutLengthNodeMap {
if map.0 > length {
removalList.insert(map.0)
} else if map.0 > max {
max = map.0
}
}
for (i, key) in removalList.enumerated() {
mutLengthNodeMap.removeValue(forKey: key)
}
mutMaximumLength = max
}
However, it seems that the mutLengthNodeMap.removeValue is removing the wrong thing.
Since mutLengthNodeMap is an ordered dictionary in Swift (an std::map in Cpp), what should I do to remove an entry from this ordered dictionary at given index?
P.S.: For reference, the repo is here:
Failure from zapping overlapped spans. · Issue #6 · ShikiSuen/Megrez (github.com)
Lukhnos Liu's Cpp files:
McBopomofo/Source/Engine/Gramambular at master · openvanilla/McBopomofo (github.com)