llvm.org GIT mirror llvm / 6bbf4ff
Add an insert() method to MapVector. Adds the first MapVector unit test. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@173505 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 6 years ago
2 changed file(s) with 54 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1818
1919 #include "llvm/ADT/ArrayRef.h"
2020 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/STLExtras.h"
2122 #include
2223
2324 namespace llvm {
8384 return Pos == Map.end()? ValueT() : Vector[Pos->second].second;
8485 }
8586
87 std::pair insert(const std::pair &KV) {
88 std::pair Pair = std::make_pair(KV.first, 0);
89 std::pair Result = Map.insert(Pair);
90 unsigned &I = Result.first->second;
91 if (Result.second) {
92 Vector.push_back(std::make_pair(KV.first, KV.second));
93 I = Vector.size() - 1;
94 return std::make_pair(llvm::prior(end()), true);
95 }
96 return std::make_pair(begin() + I, false);
97 }
98
8699 unsigned count(const KeyT &Key) const {
87100 typename MapType::const_iterator Pos = Map.find(Key);
88101 return Pos == Map.end()? 0 : 1;
0 //===- unittest/ADT/MapVectorTest.cpp - MapVector unit tests ----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "gtest/gtest.h"
10 #include "llvm/ADT/MapVector.h"
11 #include
12
13 using namespace llvm;
14
15 TEST(MapVectorTest, insert) {
16 MapVector MV;
17 std::pair::iterator, bool> R;
18
19 R = MV.insert(std::make_pair(1, 2));
20 ASSERT_EQ(R.first, MV.begin());
21 EXPECT_EQ(R.first->first, 1);
22 EXPECT_EQ(R.first->second, 2);
23 EXPECT_TRUE(R.second);
24
25 R = MV.insert(std::make_pair(1, 3));
26 ASSERT_EQ(R.first, MV.begin());
27 EXPECT_EQ(R.first->first, 1);
28 EXPECT_EQ(R.first->second, 2);
29 EXPECT_FALSE(R.second);
30
31 R = MV.insert(std::make_pair(4, 5));
32 ASSERT_NE(R.first, MV.end());
33 EXPECT_EQ(R.first->first, 4);
34 EXPECT_EQ(R.first->second, 5);
35 EXPECT_TRUE(R.second);
36
37 EXPECT_EQ(MV.size(), 2u);
38 EXPECT_EQ(MV[1], 2);
39 EXPECT_EQ(MV[4], 5);
40 }