@@ -31,120 +31,113 @@ template <typename KeyT, typename ValueT,
31
31
typename MapT = llvm::DenseMap<KeyT, size_t >,
32
32
typename VectorT = std::vector<Optional<std::pair<KeyT, ValueT>>>>
33
33
class BlotMapVector {
34
- // / Map keys to indices in Vector.
35
- MapT Map;
34
+ // / Map keys to indices in Vector.
35
+ MapT Map;
36
36
37
- // / Keys and values.
38
- VectorT Vector;
37
+ // / Keys and values.
38
+ VectorT Vector;
39
39
40
- public:
41
- using iterator = typename VectorT::iterator;
42
- using const_iterator = typename VectorT::const_iterator;
43
- using key_type = KeyT;
44
- using mapped_type = ValueT;
45
-
46
- iterator begin () { return Vector.begin (); }
47
- iterator end () { return Vector.end (); }
48
- const_iterator begin () const { return Vector.begin (); }
49
- const_iterator end () const { return Vector.end (); }
50
-
51
- iterator_range<iterator> getItems () {
52
- return swift::make_range (begin (), end ());
53
- }
54
- iterator_range<const_iterator> getItems () const {
55
- return swift::make_range (begin (), end ());
56
- }
57
-
58
- ValueT &operator [](const KeyT &Arg) {
59
- auto Pair = Map.insert (std::make_pair (Arg, size_t (0 )));
60
- if (Pair.second ) {
61
- size_t Num = Vector.size ();
62
- Pair.first ->second = Num;
63
- Vector.push_back ({std::make_pair (Arg, ValueT ())});
64
- return (*Vector[Num]).second ;
65
- }
66
- return Vector[Pair.first ->second ].getValue ().second ;
67
- }
68
-
69
- std::pair<iterator, bool >
70
- insert (const std::pair<KeyT, ValueT> &InsertPair) {
71
- auto Pair = Map.insert (std::make_pair (InsertPair.first , size_t (0 )));
72
- if (Pair.second ) {
73
- size_t Num = Vector.size ();
74
- Pair.first ->second = Num;
75
- Vector.push_back (InsertPair);
76
- return std::make_pair (Vector.begin () + Num, true );
77
- }
78
- return std::make_pair (Vector.begin () + Pair.first ->second , false );
79
- }
80
-
81
- iterator find (const KeyT &Key) {
82
- typename MapT::iterator It = Map.find (Key);
83
- if (It == Map.end ()) return Vector.end ();
84
- auto Iter = Vector.begin () + It->second ;
85
- if (!Iter->hasValue ())
86
- return Vector.end ();
87
- return Iter;
88
- }
89
-
90
- const_iterator find (const KeyT &Key) const {
91
- return const_cast <BlotMapVector &>(*this ).find (Key);
92
- }
93
-
94
- // / Eliminate the element at `Key`. Instead of removing the element from the
95
- // / vector, just zero out the key in the vector. This leaves iterators
96
- // / intact, but clients must be prepared for zeroed-out keys when iterating.
97
- // /
98
- // / Return true if the element was erased.
99
- bool erase (const KeyT &Key) { return blot (Key); }
100
-
101
- // / Eliminate the element at the given iterator. Instead of removing the
102
- // / element from the vector, just zero out the key in the vector. This
103
- // / leaves iterators intact, but clients must be prepared for zeroed-out
104
- // / keys when iterating.
105
- void erase (iterator I) { erase ((*I)->first ); }
106
-
107
- // / Eliminate the element at `Key`. Instead of removing the element from the
108
- // / vector, it just zeros out the key in the vector. This leaves iterators
109
- // / intact, but clients must be prepared for zeroed-out keys when iterating.
110
- // /
111
- // / Return true if the element was found and erased.
112
- bool blot (const KeyT &Key) {
113
- typename MapT::iterator It = Map.find (Key);
114
- if (It == Map.end ())
115
- return false ;
116
- Vector[It->second ] = None;
117
- Map.erase (It);
118
- return true ;
119
- }
120
-
121
- void clear () {
122
- Map.clear ();
123
- Vector.clear ();
40
+ public:
41
+ using iterator = typename VectorT::iterator;
42
+ using const_iterator = typename VectorT::const_iterator;
43
+ using key_type = KeyT;
44
+ using mapped_type = ValueT;
45
+
46
+ iterator begin () { return Vector.begin (); }
47
+ iterator end () { return Vector.end (); }
48
+ const_iterator begin () const { return Vector.begin (); }
49
+ const_iterator end () const { return Vector.end (); }
50
+
51
+ iterator_range<iterator> getItems () {
52
+ return swift::make_range (begin (), end ());
53
+ }
54
+ iterator_range<const_iterator> getItems () const {
55
+ return swift::make_range (begin (), end ());
56
+ }
57
+
58
+ ValueT &operator [](const KeyT &Arg) {
59
+ auto Pair = Map.insert (std::make_pair (Arg, size_t (0 )));
60
+ if (Pair.second ) {
61
+ size_t Num = Vector.size ();
62
+ Pair.first ->second = Num;
63
+ Vector.push_back ({std::make_pair (Arg, ValueT ())});
64
+ return (*Vector[Num]).second ;
124
65
}
125
-
126
- unsigned size () const { return Map.size (); }
127
-
128
- ValueT lookup (const KeyT &Val) const {
129
- auto Iter = Map.find (Val);
130
- if (Iter == Map.end ())
131
- return ValueT ();
132
- auto &P = Vector[Iter->second ];
133
- if (!P.hasValue ())
134
- return ValueT ();
135
- return P->second ;
66
+ return Vector[Pair.first ->second ].getValue ().second ;
67
+ }
68
+
69
+ std::pair<iterator, bool > insert (const std::pair<KeyT, ValueT> &InsertPair) {
70
+ auto Pair = Map.insert (std::make_pair (InsertPair.first , size_t (0 )));
71
+ if (Pair.second ) {
72
+ size_t Num = Vector.size ();
73
+ Pair.first ->second = Num;
74
+ Vector.push_back (InsertPair);
75
+ return std::make_pair (Vector.begin () + Num, true );
136
76
}
77
+ return std::make_pair (Vector.begin () + Pair.first ->second , false );
78
+ }
79
+
80
+ iterator find (const KeyT &Key) {
81
+ typename MapT::iterator It = Map.find (Key);
82
+ if (It == Map.end ())
83
+ return Vector.end ();
84
+ auto Iter = Vector.begin () + It->second ;
85
+ if (!Iter->hasValue ())
86
+ return Vector.end ();
87
+ return Iter;
88
+ }
89
+
90
+ const_iterator find (const KeyT &Key) const {
91
+ return const_cast <BlotMapVector &>(*this ).find (Key);
92
+ }
93
+
94
+ // / Eliminate the element at `Key`. Instead of removing the element from the
95
+ // / vector, just zero out the key in the vector. This leaves iterators
96
+ // / intact, but clients must be prepared for zeroed-out keys when iterating.
97
+ // /
98
+ // / Return true if the element was found and erased.
99
+ bool erase (const KeyT &Key) {
100
+ typename MapT::iterator It = Map.find (Key);
101
+ if (It == Map.end ())
102
+ return false ;
103
+ Vector[It->second ] = None;
104
+ Map.erase (It);
105
+ return true ;
106
+ }
107
+
108
+ // / Eliminate the element at the given iterator. Instead of removing the
109
+ // / element from the vector, just zero out the key in the vector. This
110
+ // / leaves iterators intact, but clients must be prepared for zeroed-out
111
+ // / keys when iterating.
112
+ void erase (iterator I) { erase ((*I)->first ); }
113
+
114
+ void clear () {
115
+ Map.clear ();
116
+ Vector.clear ();
117
+ }
118
+
119
+ unsigned size () const { return Map.size (); }
120
+
121
+ ValueT lookup (const KeyT &Val) const {
122
+ auto Iter = Map.find (Val);
123
+ if (Iter == Map.end ())
124
+ return ValueT ();
125
+ auto &P = Vector[Iter->second ];
126
+ if (!P.hasValue ())
127
+ return ValueT ();
128
+ return P->second ;
129
+ }
130
+
131
+ size_t count (const KeyT &Val) const { return Map.count (Val); }
132
+
133
+ bool empty () const { return Map.empty (); }
134
+ };
137
135
138
- size_t count (const KeyT &Val) const { return Map.count (Val); }
139
-
140
- bool empty () const { return Map.empty (); }
141
- };
142
-
143
- template <typename KeyT, typename ValueT, unsigned N,
144
- typename MapT = llvm::SmallDenseMap<KeyT, size_t , N>,
145
- typename VectorT =
146
- llvm::SmallVector<Optional<std::pair<KeyT, ValueT>>, N>>
147
- class SmallBlotMapVector : public BlotMapVector <KeyT, ValueT, MapT, VectorT> {
136
+ template <typename KeyT, typename ValueT, unsigned N,
137
+ typename MapT = llvm::SmallDenseMap<KeyT, size_t , N>,
138
+ typename VectorT =
139
+ llvm::SmallVector<Optional<std::pair<KeyT, ValueT>>, N>>
140
+ class SmallBlotMapVector : public BlotMapVector <KeyT, ValueT, MapT, VectorT> {
148
141
public:
149
142
SmallBlotMapVector () {}
150
143
};
0 commit comments