<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 31, 2014 at 3:33 PM, Michael Gottesman <span dir="ltr"><<a href="mailto:mgottesman@apple.com" target="_blank">mgottesman@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: mgottesman<br>
Date: Wed Dec 31 17:33:21 2014<br>
New Revision: 225054<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=225054&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=225054&view=rev</a><br>
Log:<br>
Add a SmallMapVector class that is a MapVector with a Map of SmallDenseMap and a Vector of SmallVector.<br>
<br>
Modified:<br>
llvm/trunk/include/llvm/ADT/MapVector.h<br>
llvm/trunk/unittests/ADT/MapVectorTest.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/ADT/MapVector.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/MapVector.h?rev=225054&r1=225053&r2=225054&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/MapVector.h?rev=225054&r1=225053&r2=225054&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/ADT/MapVector.h (original)<br>
+++ llvm/trunk/include/llvm/ADT/MapVector.h Wed Dec 31 17:33:21 2014<br>
@@ -18,6 +18,7 @@<br>
#define LLVM_ADT_MAPVECTOR_H<br>
<br>
#include "llvm/ADT/DenseMap.h"<br>
+#include "llvm/ADT/SmallVector.h"<br>
#include <vector><br>
<br>
namespace llvm {<br>
@@ -181,6 +182,16 @@ void MapVector<KeyT, ValueT, MapType, Ve<br>
Vector.erase(O, Vector.end());<br>
}<br>
<br>
+/// \brief A MapVector that performs no allocations if smaller than a certain<br>
+/// size.<br>
+template <typename KeyT, typename ValueT, unsigned N><br>
+class SmallMapVector<br>
+ : public MapVector<KeyT, ValueT, SmallDenseMap<KeyT, unsigned, N>,<br>
+ SmallVector<std::pair<KeyT, ValueT>, N>> {<br></blockquote><div><br></div><div>Should this be an alias template instead? (or is that not supported on all our compilers? (I imagine it probably isn't supported on MSVC?))</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+public:<br>
+ SmallMapVector() {}<br></blockquote><div><br>This is just the default, isn't it? Perhaps you could omit it (I'd probably just make SmallMapVector a struct instead of a class - with no members and rely on the implicit public-ness of inheritance in structs)<br><br>- David<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+};<br>
+<br>
} // end namespace llvm<br>
<br>
#endif<br>
<br>
Modified: llvm/trunk/unittests/ADT/MapVectorTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/MapVectorTest.cpp?rev=225054&r1=225053&r2=225054&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/MapVectorTest.cpp?rev=225054&r1=225053&r2=225054&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/unittests/ADT/MapVectorTest.cpp (original)<br>
+++ llvm/trunk/unittests/ADT/MapVectorTest.cpp Wed Dec 31 17:33:21 2014<br>
@@ -122,3 +122,221 @@ TEST(MapVectorTest, iteration_test) {<br>
count--;<br>
}<br>
}<br>
+<br>
+TEST(SmallMapVectorSmallTest, insert_pop) {<br>
+ SmallMapVector<int, int, 32> MV;<br>
+ std::pair<SmallMapVector<int, int, 32>::iterator, bool> R;<br>
+<br>
+ R = MV.insert(std::make_pair(1, 2));<br>
+ ASSERT_EQ(R.first, MV.begin());<br>
+ EXPECT_EQ(R.first->first, 1);<br>
+ EXPECT_EQ(R.first->second, 2);<br>
+ EXPECT_TRUE(R.second);<br>
+<br>
+ R = MV.insert(std::make_pair(1, 3));<br>
+ ASSERT_EQ(R.first, MV.begin());<br>
+ EXPECT_EQ(R.first->first, 1);<br>
+ EXPECT_EQ(R.first->second, 2);<br>
+ EXPECT_FALSE(R.second);<br>
+<br>
+ R = MV.insert(std::make_pair(4, 5));<br>
+ ASSERT_NE(R.first, MV.end());<br>
+ EXPECT_EQ(R.first->first, 4);<br>
+ EXPECT_EQ(R.first->second, 5);<br>
+ EXPECT_TRUE(R.second);<br>
+<br>
+ EXPECT_EQ(MV.size(), 2u);<br>
+ EXPECT_EQ(MV[1], 2);<br>
+ EXPECT_EQ(MV[4], 5);<br>
+<br>
+ MV.pop_back();<br>
+ EXPECT_EQ(MV.size(), 1u);<br>
+ EXPECT_EQ(MV[1], 2);<br>
+<br>
+ R = MV.insert(std::make_pair(4, 7));<br>
+ ASSERT_NE(R.first, MV.end());<br>
+ EXPECT_EQ(R.first->first, 4);<br>
+ EXPECT_EQ(R.first->second, 7);<br>
+ EXPECT_TRUE(R.second);<br>
+<br>
+ EXPECT_EQ(MV.size(), 2u);<br>
+ EXPECT_EQ(MV[1], 2);<br>
+ EXPECT_EQ(MV[4], 7);<br>
+}<br>
+<br>
+TEST(SmallMapVectorSmallTest, erase) {<br>
+ SmallMapVector<int, int, 32> MV;<br>
+<br>
+ MV.insert(std::make_pair(1, 2));<br>
+ MV.insert(std::make_pair(3, 4));<br>
+ MV.insert(std::make_pair(5, 6));<br>
+ ASSERT_EQ(MV.size(), 3u);<br>
+<br>
+ MV.erase(MV.find(1));<br>
+ ASSERT_EQ(MV.size(), 2u);<br>
+ ASSERT_EQ(MV.find(1), MV.end());<br>
+ ASSERT_EQ(MV[3], 4);<br>
+ ASSERT_EQ(MV[5], 6);<br>
+<br>
+ ASSERT_EQ(MV.erase(3), 1u);<br>
+ ASSERT_EQ(MV.size(), 1u);<br>
+ ASSERT_EQ(MV.find(3), MV.end());<br>
+ ASSERT_EQ(MV[5], 6);<br>
+<br>
+ ASSERT_EQ(MV.erase(79), 0u);<br>
+ ASSERT_EQ(MV.size(), 1u);<br>
+}<br>
+<br>
+TEST(SmallMapVectorSmallTest, remove_if) {<br>
+ SmallMapVector<int, int, 32> MV;<br>
+<br>
+ MV.insert(std::make_pair(1, 11));<br>
+ MV.insert(std::make_pair(2, 12));<br>
+ MV.insert(std::make_pair(3, 13));<br>
+ MV.insert(std::make_pair(4, 14));<br>
+ MV.insert(std::make_pair(5, 15));<br>
+ MV.insert(std::make_pair(6, 16));<br>
+ ASSERT_EQ(MV.size(), 6u);<br>
+<br>
+ MV.remove_if([](const std::pair<int, int> &Val) { return Val.second % 2; });<br>
+ ASSERT_EQ(MV.size(), 3u);<br>
+ ASSERT_EQ(MV.find(1), MV.end());<br>
+ ASSERT_EQ(MV.find(3), MV.end());<br>
+ ASSERT_EQ(MV.find(5), MV.end());<br>
+ ASSERT_EQ(MV[2], 12);<br>
+ ASSERT_EQ(MV[4], 14);<br>
+ ASSERT_EQ(MV[6], 16);<br>
+}<br>
+<br>
+TEST(SmallMapVectorSmallTest, iteration_test) {<br>
+ SmallMapVector<int, int, 32> MV;<br>
+<br>
+ MV.insert(std::make_pair(1, 11));<br>
+ MV.insert(std::make_pair(2, 12));<br>
+ MV.insert(std::make_pair(3, 13));<br>
+ MV.insert(std::make_pair(4, 14));<br>
+ MV.insert(std::make_pair(5, 15));<br>
+ MV.insert(std::make_pair(6, 16));<br>
+ ASSERT_EQ(MV.size(), 6u);<br>
+<br>
+ int count = 1;<br>
+ for (auto P : make_range(MV.begin(), MV.end())) {<br>
+ ASSERT_EQ(P.first, count);<br>
+ count++;<br>
+ }<br>
+<br>
+ count = 6;<br>
+ for (auto P : make_range(MV.rbegin(), MV.rend())) {<br>
+ ASSERT_EQ(P.first, count);<br>
+ count--;<br>
+ }<br>
+}<br>
+<br>
+TEST(SmallMapVectorLargeTest, insert_pop) {<br>
+ SmallMapVector<int, int, 1> MV;<br>
+ std::pair<SmallMapVector<int, int, 1>::iterator, bool> R;<br>
+<br>
+ R = MV.insert(std::make_pair(1, 2));<br>
+ ASSERT_EQ(R.first, MV.begin());<br>
+ EXPECT_EQ(R.first->first, 1);<br>
+ EXPECT_EQ(R.first->second, 2);<br>
+ EXPECT_TRUE(R.second);<br>
+<br>
+ R = MV.insert(std::make_pair(1, 3));<br>
+ ASSERT_EQ(R.first, MV.begin());<br>
+ EXPECT_EQ(R.first->first, 1);<br>
+ EXPECT_EQ(R.first->second, 2);<br>
+ EXPECT_FALSE(R.second);<br>
+<br>
+ R = MV.insert(std::make_pair(4, 5));<br>
+ ASSERT_NE(R.first, MV.end());<br>
+ EXPECT_EQ(R.first->first, 4);<br>
+ EXPECT_EQ(R.first->second, 5);<br>
+ EXPECT_TRUE(R.second);<br>
+<br>
+ EXPECT_EQ(MV.size(), 2u);<br>
+ EXPECT_EQ(MV[1], 2);<br>
+ EXPECT_EQ(MV[4], 5);<br>
+<br>
+ MV.pop_back();<br>
+ EXPECT_EQ(MV.size(), 1u);<br>
+ EXPECT_EQ(MV[1], 2);<br>
+<br>
+ R = MV.insert(std::make_pair(4, 7));<br>
+ ASSERT_NE(R.first, MV.end());<br>
+ EXPECT_EQ(R.first->first, 4);<br>
+ EXPECT_EQ(R.first->second, 7);<br>
+ EXPECT_TRUE(R.second);<br>
+<br>
+ EXPECT_EQ(MV.size(), 2u);<br>
+ EXPECT_EQ(MV[1], 2);<br>
+ EXPECT_EQ(MV[4], 7);<br>
+}<br>
+<br>
+TEST(SmallMapVectorLargeTest, erase) {<br>
+ SmallMapVector<int, int, 1> MV;<br>
+<br>
+ MV.insert(std::make_pair(1, 2));<br>
+ MV.insert(std::make_pair(3, 4));<br>
+ MV.insert(std::make_pair(5, 6));<br>
+ ASSERT_EQ(MV.size(), 3u);<br>
+<br>
+ MV.erase(MV.find(1));<br>
+ ASSERT_EQ(MV.size(), 2u);<br>
+ ASSERT_EQ(MV.find(1), MV.end());<br>
+ ASSERT_EQ(MV[3], 4);<br>
+ ASSERT_EQ(MV[5], 6);<br>
+<br>
+ ASSERT_EQ(MV.erase(3), 1u);<br>
+ ASSERT_EQ(MV.size(), 1u);<br>
+ ASSERT_EQ(MV.find(3), MV.end());<br>
+ ASSERT_EQ(MV[5], 6);<br>
+<br>
+ ASSERT_EQ(MV.erase(79), 0u);<br>
+ ASSERT_EQ(MV.size(), 1u);<br>
+}<br>
+<br>
+TEST(SmallMapVectorLargeTest, remove_if) {<br>
+ SmallMapVector<int, int, 1> MV;<br>
+<br>
+ MV.insert(std::make_pair(1, 11));<br>
+ MV.insert(std::make_pair(2, 12));<br>
+ MV.insert(std::make_pair(3, 13));<br>
+ MV.insert(std::make_pair(4, 14));<br>
+ MV.insert(std::make_pair(5, 15));<br>
+ MV.insert(std::make_pair(6, 16));<br>
+ ASSERT_EQ(MV.size(), 6u);<br>
+<br>
+ MV.remove_if([](const std::pair<int, int> &Val) { return Val.second % 2; });<br>
+ ASSERT_EQ(MV.size(), 3u);<br>
+ ASSERT_EQ(MV.find(1), MV.end());<br>
+ ASSERT_EQ(MV.find(3), MV.end());<br>
+ ASSERT_EQ(MV.find(5), MV.end());<br>
+ ASSERT_EQ(MV[2], 12);<br>
+ ASSERT_EQ(MV[4], 14);<br>
+ ASSERT_EQ(MV[6], 16);<br>
+}<br>
+<br>
+TEST(SmallMapVectorLargeTest, iteration_test) {<br>
+ SmallMapVector<int, int, 1> MV;<br>
+<br>
+ MV.insert(std::make_pair(1, 11));<br>
+ MV.insert(std::make_pair(2, 12));<br>
+ MV.insert(std::make_pair(3, 13));<br>
+ MV.insert(std::make_pair(4, 14));<br>
+ MV.insert(std::make_pair(5, 15));<br>
+ MV.insert(std::make_pair(6, 16));<br>
+ ASSERT_EQ(MV.size(), 6u);<br>
+<br>
+ int count = 1;<br>
+ for (auto P : make_range(MV.begin(), MV.end())) {<br>
+ ASSERT_EQ(P.first, count);<br>
+ count++;<br>
+ }<br>
+<br>
+ count = 6;<br>
+ for (auto P : make_range(MV.rbegin(), MV.rend())) {<br>
+ ASSERT_EQ(P.first, count);<br>
+ count--;<br>
+ }<br>
+}<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>