OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 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 CONTENT_BROWSER_INDEXED_DB_LIST_SET_H_ | |
6 #define CONTENT_BROWSER_INDEXED_DB_LIST_SET_H_ | |
7 | |
8 #include <algorithm> | |
9 #include <iterator> | |
10 #include <list> | |
11 #include <set> | |
12 #include "base/logging.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 | |
15 // | |
16 // A container class that provides fast containment test (like a set) | |
17 // but maintains insertion order for iteration (like list). | |
18 // | |
19 // Member types of value (primitives and objects by value), raw pointers | |
20 // and scoped_refptr<> are supported. | |
21 // | |
22 template <typename T> class list_set { | |
23 public: | |
24 list_set() {} | |
25 list_set(const list_set<T>& other) : list_(other.list_), set_(other.set_) {} | |
26 list_set& operator=(const list_set<T>& other) { | |
27 list_ = other.list_; | |
28 set_ = other.set_; | |
29 return *this; | |
30 } | |
31 | |
32 void insert(const T& elem) { | |
33 if (set_.find(elem) != set_.end()) | |
34 return; | |
35 set_.insert(elem); | |
36 list_.push_back(elem); | |
37 } | |
38 | |
39 void erase(const T& elem) { | |
40 if (set_.find(elem) == set_.end()) | |
41 return; | |
42 set_.erase(elem); | |
43 typename std::list<T>::iterator it = | |
44 std::find(list_.begin(), list_.end(), elem); | |
45 DCHECK(it != list_.end()); | |
46 list_.erase(it); | |
47 } | |
48 | |
49 bool has(const T& elem) { return set_.find(elem) != set_.end(); } | |
50 | |
51 size_t size() const { | |
52 DCHECK_EQ(list_.size(), set_.size()); | |
53 return set_.size(); | |
54 } | |
55 | |
56 bool empty() const { | |
57 DCHECK_EQ(list_.empty(), set_.empty()); | |
58 return set_.empty(); | |
59 } | |
60 | |
61 class const_iterator; | |
62 | |
63 class iterator { | |
64 public: | |
65 typedef iterator self_type; | |
66 typedef T value_type; | |
67 typedef T& reference; | |
68 typedef T* pointer; | |
69 typedef std::bidirectional_iterator_tag iterator_category; | |
70 typedef std::ptrdiff_t difference_type; | |
71 | |
72 inline iterator(typename std::list<T>::iterator it) : it_(it) {} | |
73 inline self_type& operator++() { | |
74 ++it_; | |
75 return *this; | |
76 } | |
77 inline self_type operator++(int) { | |
78 self_type result(*this); | |
79 ++(*this); | |
80 return result; | |
81 } | |
82 inline self_type& operator--() { | |
83 --it_; | |
84 return *this; | |
85 } | |
86 inline self_type operator--(int) { | |
87 self_type result(*this); | |
88 --(*this); | |
89 return result; | |
90 } | |
91 inline value_type& operator*() { return *it_; } | |
92 inline value_type* operator->() { return &(*it_); } | |
93 inline bool operator==(const iterator& rhs) const { return it_ == rhs.it_; } | |
94 inline bool operator!=(const iterator& rhs) const { return it_ != rhs.it_; } | |
95 | |
96 inline operator const_iterator() const { return const_iterator(it_); } | |
97 | |
98 private: | |
99 typename std::list<T>::iterator it_; | |
100 }; | |
101 | |
102 class const_iterator { | |
103 public: | |
104 typedef const_iterator self_type; | |
105 typedef T value_type; | |
106 typedef T& reference; | |
107 typedef T* pointer; | |
108 typedef std::bidirectional_iterator_tag iterator_category; | |
109 typedef std::ptrdiff_t difference_type; | |
110 | |
111 inline const_iterator(typename std::list<T>::const_iterator it) : it_(it) {} | |
112 inline self_type& operator++() { | |
113 ++it_; | |
114 return *this; | |
115 } | |
116 inline self_type operator++(int) { | |
117 self_type result(*this); | |
118 ++(*this); | |
119 return result; | |
120 } | |
121 inline self_type& operator--() { | |
122 --it_; | |
123 return *this; | |
124 } | |
125 inline self_type operator--(int) { | |
126 self_type result(*this); | |
127 --(*this); | |
128 return result; | |
129 } | |
130 inline const value_type& operator*() { return *it_; } | |
131 inline const value_type* operator->() { return &(*it_); } | |
132 inline bool operator==(const const_iterator& rhs) const { | |
133 return it_ == rhs.it_; | |
134 } | |
135 inline bool operator!=(const const_iterator& rhs) const { | |
136 return it_ != rhs.it_; | |
137 } | |
138 | |
139 private: | |
140 typename std::list<T>::const_iterator it_; | |
141 }; | |
142 | |
143 iterator begin() { return iterator(list_.begin()); } | |
144 iterator end() { return iterator(list_.end()); } | |
145 const_iterator begin() const { return const_iterator(list_.begin()); } | |
146 const_iterator end() const { return const_iterator(list_.end()); } | |
147 | |
148 private: | |
149 std::list<T> list_; | |
150 std::set<T> set_; | |
151 }; | |
152 | |
153 // Prevent instantiation of list_set<scoped_ptr<T>> as the current | |
154 // implementation would fail. | |
155 // TODO(jsbell): Support scoped_ptr through specialization. | |
156 template <typename T> class list_set<scoped_ptr<T> >; | |
157 | |
158 #endif // CONTENT_BROWSER_INDEXED_DB_LIST_SET_H_ | |
OLD | NEW |