blob: 7da2c106a6406d6f120cb5b12fc59b7cc365adf9 [file] [log] [blame]
James Kuszmaulb13e13f2023-11-22 20:44:04 -08001From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
James Kuszmaulcf324122023-01-14 14:07:17 -08002From: PJ Reiniger <pj.reiniger@gmail.com>
3Date: Tue, 3 May 2022 22:16:10 -0400
James Kuszmaulb13e13f2023-11-22 20:44:04 -08004Subject: [PATCH 12/31] Extra collections features
James Kuszmaulcf324122023-01-14 14:07:17 -08005
6---
7 llvm/include/llvm/ADT/StringMap.h | 103 +++++++++++++++++++++++++++++-
8 llvm/lib/Support/raw_ostream.cpp | 8 +++
9 2 files changed, 110 insertions(+), 1 deletion(-)
10
11diff --git a/llvm/include/llvm/ADT/StringMap.h b/llvm/include/llvm/ADT/StringMap.h
James Kuszmaulb13e13f2023-11-22 20:44:04 -080012index 34dfbf83c681f4e81a9dadd9382ddca6ef8d6c1d..c133e84f9b2e3a225cdac782c011fadbf07adab2 100644
James Kuszmaulcf324122023-01-14 14:07:17 -080013--- a/llvm/include/llvm/ADT/StringMap.h
14+++ b/llvm/include/llvm/ADT/StringMap.h
15@@ -42,7 +42,7 @@ protected:
16
17 protected:
18 explicit StringMapImpl(unsigned itemSize) : ItemSize(itemSize) {}
19- StringMapImpl(StringMapImpl &&RHS)
20+ StringMapImpl(StringMapImpl &&RHS) noexcept
21 : TheTable(RHS.TheTable), NumBuckets(RHS.NumBuckets),
22 NumItems(RHS.NumItems), NumTombstones(RHS.NumTombstones),
23 ItemSize(RHS.ItemSize) {
James Kuszmaulb13e13f2023-11-22 20:44:04 -080024@@ -432,11 +432,27 @@ public:
James Kuszmaulcf324122023-01-14 14:07:17 -080025 return Tmp;
26 }
27
28+ DerivedTy &operator--() { // Predecrement
29+ --Ptr;
30+ ReversePastEmptyBuckets();
31+ return static_cast<DerivedTy &>(*this);
32+ }
33+
34+ DerivedTy operator--(int) { // Post-decrement
35+ DerivedTy Tmp(Ptr);
36+ --*this;
37+ return Tmp;
38+ }
39+
40 private:
41 void AdvancePastEmptyBuckets() {
42 while (*Ptr == nullptr || *Ptr == StringMapImpl::getTombstoneVal())
43 ++Ptr;
44 }
45+ void ReversePastEmptyBuckets() {
46+ while (*Ptr == nullptr || *Ptr == StringMapImpl::getTombstoneVal())
47+ --Ptr;
48+ }
49 };
50
51 template <typename ValueTy>
James Kuszmaulb13e13f2023-11-22 20:44:04 -080052@@ -495,6 +511,91 @@ public:
James Kuszmaulcf324122023-01-14 14:07:17 -080053 std::string_view operator*() const { return this->wrapped()->getKey(); }
54 };
55
56+template <typename ValueTy>
57+bool operator==(const StringMap<ValueTy>& lhs, const StringMap<ValueTy>& rhs) {
58+ // same instance?
59+ if (&lhs == &rhs) return true;
60+
61+ // first check that sizes are identical
62+ if (lhs.size() != rhs.size()) return false;
63+
64+ // copy into vectors and sort by key
65+ SmallVector<StringMapConstIterator<ValueTy>, 16> lhs_items;
66+ lhs_items.reserve(lhs.size());
67+ for (auto i = lhs.begin(), end = lhs.end(); i != end; ++i)
68+ lhs_items.push_back(i);
69+ std::sort(lhs_items.begin(), lhs_items.end(),
70+ [](const StringMapConstIterator<ValueTy>& a,
71+ const StringMapConstIterator<ValueTy>& b) {
72+ return a->getKey() < b->getKey();
73+ });
74+
75+ SmallVector<StringMapConstIterator<ValueTy>, 16> rhs_items;
76+ rhs_items.reserve(rhs.size());
77+ for (auto i = rhs.begin(), end = rhs.end(); i != end; ++i)
78+ rhs_items.push_back(i);
79+ std::sort(rhs_items.begin(), rhs_items.end(),
80+ [](const StringMapConstIterator<ValueTy>& a,
81+ const StringMapConstIterator<ValueTy>& b) {
82+ return a->getKey() < b->getKey();
83+ });
84+
85+ // compare vector keys and values
86+ for (auto a = lhs_items.begin(), b = rhs_items.begin(),
87+ aend = lhs_items.end(), bend = rhs_items.end();
88+ a != aend && b != bend; ++a, ++b) {
89+ if ((*a)->first() != (*b)->first() || (*a)->second != (*b)->second)
90+ return false;
91+ }
92+ return true;
93+}
94+
95+template <typename ValueTy>
96+inline bool operator!=(const StringMap<ValueTy>& lhs,
97+ const StringMap<ValueTy>& rhs) {
98+ return !(lhs == rhs);
99+}
100+
101+template <typename ValueTy>
102+bool operator<(const StringMap<ValueTy>& lhs, const StringMap<ValueTy>& rhs) {
103+ // same instance?
104+ if (&lhs == &rhs) return false;
105+
106+ // copy into vectors and sort by key
107+ SmallVector<std::string_view, 16> lhs_keys;
108+ lhs_keys.reserve(lhs.size());
109+ for (auto i = lhs.begin(), end = lhs.end(); i != end; ++i)
110+ lhs_keys.push_back(i->getKey());
111+ std::sort(lhs_keys.begin(), lhs_keys.end());
112+
113+ SmallVector<std::string_view, 16> rhs_keys;
114+ rhs_keys.reserve(rhs.size());
115+ for (auto i = rhs.begin(), end = rhs.end(); i != end; ++i)
116+ rhs_keys.push_back(i->getKey());
117+ std::sort(rhs_keys.begin(), rhs_keys.end());
118+
119+ // use std::vector comparison
120+ return lhs_keys < rhs_keys;
121+}
122+
123+template <typename ValueTy>
124+inline bool operator<=(const StringMap<ValueTy>& lhs,
125+ const StringMap<ValueTy>& rhs) {
126+ return !(rhs < lhs);
127+}
128+
129+template <typename ValueTy>
130+inline bool operator>(const StringMap<ValueTy>& lhs,
131+ const StringMap<ValueTy>& rhs) {
132+ return !(lhs <= rhs);
133+}
134+
135+template <typename ValueTy>
136+inline bool operator>=(const StringMap<ValueTy>& lhs,
137+ const StringMap<ValueTy>& rhs) {
138+ return !(lhs < rhs);
139+}
140+
141 } // end namespace llvm
142
143 #endif // LLVM_ADT_STRINGMAP_H
144diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp
James Kuszmaulb13e13f2023-11-22 20:44:04 -0800145index dcdfdfd7a8e3fcc4019538a4fc9e158aeda0a8b8..b2a726633b48b179abfd24a5de110a2301e0f877 100644
James Kuszmaulcf324122023-01-14 14:07:17 -0800146--- a/llvm/lib/Support/raw_ostream.cpp
147+++ b/llvm/lib/Support/raw_ostream.cpp
148@@ -76,6 +76,14 @@ constexpr raw_ostream::Colors raw_ostream::WHITE;
149 constexpr raw_ostream::Colors raw_ostream::SAVEDCOLOR;
150 constexpr raw_ostream::Colors raw_ostream::RESET;
151
152+namespace {
153+// Find the length of an array.
154+template <class T, std::size_t N>
155+constexpr inline size_t array_lengthof(T (&)[N]) {
156+ return N;
157+}
158+} // namespace
159+
160 raw_ostream::~raw_ostream() {
161 // raw_ostream's subclasses should take care to flush the buffer
162 // in their destructors.