OLD | NEW |
(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_FlatSet_h |
| 6 #define WTF_FlatSet_h |
| 7 |
| 8 #include "base/macros.h" |
| 9 #include "wtf/FlatTable.h" |
| 10 |
| 11 namespace WTF { |
| 12 |
| 13 template <typename Key> |
| 14 struct FlatSetDelegate { |
| 15 using key_type = Key; |
| 16 using value_type = Key; |
| 17 |
| 18 static inline const Key& ToKey(const value_type& v) { return v; } |
| 19 }; |
| 20 |
| 21 // A FlatSet is an STL-like associative container backed by a ring buffer. |
| 22 // Generally you should use WTF::HashSet unless you need an associative |
| 23 // container. FlatSet is usually faster than std::set for small sets (<=128) |
| 24 // (sometimes considerably so) and is always faster for immutable sets. |
| 25 template <typename Key> |
| 26 class FlatSet : public FlatTable<FlatSetDelegate<Key>> { |
| 27 public: |
| 28 FlatSet() {} |
| 29 |
| 30 using BaseClass = FlatTable<FlatSetDelegate<Key>>; |
| 31 using key_type = typename BaseClass::key_type; |
| 32 using value_type = typename BaseClass::value_type; |
| 33 using iterator = typename BaseClass::iterator; |
| 34 using const_iterator = typename BaseClass::const_iterator; |
| 35 using reverse_iterator = typename BaseClass::reverse_iterator; |
| 36 using const_reverse_iterator = typename BaseClass::const_reverse_iterator; |
| 37 |
| 38 using BaseClass::empty; |
| 39 using BaseClass::clear; |
| 40 using BaseClass::insert; |
| 41 using BaseClass::insertUnique; |
| 42 using BaseClass::size; |
| 43 |
| 44 iterator begin() { return BaseClass::begin(); } |
| 45 iterator end() { return BaseClass::end(); } |
| 46 const_iterator begin() const { return BaseClass::begin(); } |
| 47 const_iterator end() const { return BaseClass::end(); } |
| 48 reverse_iterator rbegin() { return BaseClass::rbegin(); } |
| 49 const_reverse_iterator rbegin() const { return BaseClass::rbegin(); } |
| 50 |
| 51 // Erases an entry corresponding to |key|, if any, from the map. |
| 52 // Invalidates any iterators. O(log n + n / 2) |
| 53 size_t erase(const Key& key) { return BaseClass::erase(key); } |
| 54 |
| 55 // Erases |it| from the map, invalidating any iterators. O(n / 2) |
| 56 void erase(const iterator& it) { return BaseClass::erase(it); } |
| 57 void erase(const reverse_iterator& it) { return BaseClass::erase(it); } |
| 58 |
| 59 // O(log n) |
| 60 iterator find(const Key& key) { return BaseClass::find(key); } |
| 61 |
| 62 // O(log n) |
| 63 const_iterator find(const Key& key) const { return BaseClass::find(key); } |
| 64 }; |
| 65 |
| 66 } // namespace WTF |
| 67 |
| 68 #endif // WTF_FlatSet_h |
OLD | NEW |