diff --git a/src/main/native/include/llvm/StringMap.h b/src/main/native/include/llvm/StringMap.h index 522c08aca0..615bdb497f 100644 --- a/src/main/native/include/llvm/StringMap.h +++ b/src/main/native/include/llvm/StringMap.h @@ -14,8 +14,10 @@ #ifndef LLVM_ADT_STRINGMAP_H #define LLVM_ADT_STRINGMAP_H +#include "llvm/SmallVector.h" #include "llvm/StringRef.h" #include "llvm/PointerLikeTypeTraits.h" +#include #include #include #include @@ -471,6 +473,91 @@ public: } }; +template +bool operator==(const StringMap& lhs, const StringMap& rhs) { + // same instance? + if (&lhs == &rhs) return true; + + // first check that sizes are identical + if (lhs.size() != rhs.size()) return false; + + // copy into vectors and sort by key + SmallVector, 16> lhs_items; + lhs_items.reserve(lhs.size()); + for (auto i = lhs.begin(), end = lhs.end(); i != end; ++i) + lhs_items.push_back(i); + std::sort(lhs_items.begin(), lhs_items.end(), + [](const StringMapConstIterator& a, + const StringMapConstIterator& b) { + return a->getKey() < b->getKey(); + }); + + SmallVector, 16> rhs_items; + rhs_items.reserve(rhs.size()); + for (auto i = rhs.begin(), end = rhs.end(); i != end; ++i) + rhs_items.push_back(i); + std::sort(rhs_items.begin(), rhs_items.end(), + [](const StringMapConstIterator& a, + const StringMapConstIterator& b) { + return a->getKey() < b->getKey(); + }); + + // compare vector keys and values + for (auto a = lhs_items.begin(), b = rhs_items.begin(), + aend = lhs_items.end(), bend = rhs_items.end(); + a != aend && b != bend; ++a, ++b) { + if ((*a)->first() != (*b)->first() || (*a)->second != (*b)->second) + return false; + } + return true; +} + +template +inline bool operator!=(const StringMap& lhs, + const StringMap& rhs) { + return !(lhs == rhs); +} + +template +bool operator<(const StringMap& lhs, const StringMap& rhs) { + // same instance? + if (&lhs == &rhs) return false; + + // copy into vectors and sort by key + SmallVector lhs_keys; + lhs_keys.reserve(lhs.size()); + for (auto i = lhs.begin(), end = lhs.end(); i != end; ++i) + lhs_keys.push_back(i->getKey()); + std::sort(lhs_keys.begin(), lhs_keys.end()); + + SmallVector rhs_keys; + rhs_keys.reserve(rhs.size()); + for (auto i = rhs.begin(), end = rhs.end(); i != end; ++i) + rhs_keys.push_back(i->getKey()); + std::sort(rhs_keys.begin(), rhs_keys.end()); + + // use std::vector comparison + return lhs_keys < rhs_keys; +} + +template +inline bool operator<=(const StringMap& lhs, + const StringMap& rhs) { + return !(rhs < lhs); +} + +template +inline bool operator>(const StringMap& lhs, + const StringMap& rhs) { + return !(lhs <= rhs); +} + +template +inline bool operator>=(const StringMap& lhs, + const StringMap& rhs) { + return !(lhs < rhs); +} + } // namespace llvm #endif