Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: third_party/WebKit/Source/wtf/FlatMap.h

Issue 2396533004: Introduce a FlatMap and FlatSet into WTF (Closed)
Patch Set: Fix compile Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef WTF_FlatMap_h
6 #define WTF_FlatMap_h
7
8 #include "base/macros.h"
9 #include "wtf/FlatTable.h"
10
11 namespace WTF {
12
13 template <typename Key, typename Value>
14 struct FlatMapDelegate {
15 using key_type = Key;
16 using value_type = std::pair<Key, Value>;
17
18 static inline const Key& ToKey(const value_type& v) { return v.first; }
19 };
20
21 // A FlatMap is an STL-like associative container backed by a ring buffer. This
22 // can offer superior performance and a lower memory overhead when the map has
23 // a low number of Elements or it doesn't change much.
Sami 2016/10/07 09:49:11 It'd be useful to quantify what "low" means.
24 template <typename Key, typename Value>
25 class FlatMap : public FlatTable<FlatMapDelegate<Key, Value>> {
26 public:
27 FlatMap() {}
28
29 using BaseClass = FlatTable<FlatMapDelegate<Key, Value>>;
30 using key_type = typename BaseClass::key_type;
31 using value_type = typename BaseClass::value_type;
32 using iterator = typename BaseClass::iterator;
33 using const_iterator = typename BaseClass::const_iterator;
34 using reverse_iterator = typename BaseClass::reverse_iterator;
35 using const_reverse_iterator = typename BaseClass::const_reverse_iterator;
36
37 using BaseClass::empty;
38 using BaseClass::clear;
39 using BaseClass::insert;
40 using BaseClass::insertUnique;
41 using BaseClass::size;
42
43 iterator begin() { return BaseClass::begin(); }
44 iterator end() { return BaseClass::end(); }
45 const_iterator begin() const { return BaseClass::begin(); }
46 const_iterator end() const { return BaseClass::end(); }
47 reverse_iterator rbegin() { return BaseClass::rbegin(); }
48 const_reverse_iterator rbegin() const { return BaseClass::rbegin(); }
49
50 // Erases an entry corresponding to |key|, if any, from the map.
51 // Invalidates any iterators. O(log n + n / 2)
52 size_t erase(const Key& key) { return BaseClass::erase(key); }
53
54 // Erases |it| from the map, invalidating any iterators. O(n / 2)
55 void erase(const iterator& it) { return BaseClass::erase(it); }
56 void erase(const reverse_iterator& it) { return BaseClass::erase(it); }
57
58 // O(log n)
59 iterator find(const Key& key) { return BaseClass::find(key); }
60
61 // O(log n)
62 const_iterator find(const Key& key) const { return BaseClass::find(key); }
63
64 // O(log n + n / 2)
65 void ChangeKey(const iterator& it, const Key& new_key) {
66 DCHECK(iteratorBelongsToThisTable(it));
67 size_t index;
68 if (BinarySearch(new_key, &index)) {
69 // We are overwriting an existing entry.
70 Element(index).second = std::move(it->second);
71 erase(it);
72 return;
73 }
74
75 ChangeKeyUnique(it, new_key);
76 }
77
78 // O(n / 2)
79 void ChangeKeyUnique(const iterator& it, const Key& new_key) {
80 DCHECK(iteratorBelongsToThisTable(it));
81 size_t i = iteratorPosition(it);
82 if (Element(i).first < new_key) {
83 Value tempValue = std::move(Element(i).second);
84 for (; (i + 1) != back_ && Element(i + 1).first < new_key; i++) {
85 Element(i) = std::move(Element(i + 1));
86 }
87 Element(i).first = new_key;
88 Element(i).second = std::move(tempValue);
89 } else if (new_key < Element(i).first) {
90 Value tempValue = std::move(Element(i).second);
91 for (; i != front_ && new_key < Element(i - 1).first; i--) {
92 Element(i) = std::move(Element(i - 1));
93 }
94 Element(i).first = new_key;
95 Element(i).second = std::move(tempValue);
96 }
97 }
98
99 private:
100 using BaseClass::Element;
101 using BaseClass::iteratorBelongsToThisTable;
102 using BaseClass::BinarySearch;
103 using BaseClass::iteratorPosition;
104 using BaseClass::front_;
105 using BaseClass::back_;
106
107 DISALLOW_COPY_AND_ASSIGN(FlatMap);
108 };
109
110 } // namespace WTF
111
112 #endif // WTF_FlatMap_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698