1 // Copyright 2014 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.
5 #ifndef TOOLS_GN_UNIQUE_VECTOR_H_
6 #define TOOLS_GN_UNIQUE_VECTOR_H_
10 #include "base/containers/hash_tables.h"
14 // This lass allows us to insert things by reference into a hash table which
15 // avoids copies. The hash function of a UniquifyRef is that of the object it
18 // There are two ways it can store data: (1) by (vector*, index) which is used
19 // to refer to the array in UniqueVector and make it work even when the
20 // underlying vector is reallocated; (2) by T* which is used to do lookups
21 // into the hash table of things that aren't in a vector yet.
23 // It also caches the hash value which allows us to query and then insert
24 // without recomputing the hash.
31 index_(static_cast<size_t>(-1)),
35 // Initialize with a pointer to a value.
36 UniquifyRef(const T
* v
)
39 index_(static_cast<size_t>(-1)) {
43 // Initialize with an array + index.
44 UniquifyRef(const std::vector
<T
>* v
, size_t i
)
51 // Initialize with an array + index and a known hash value to prevent
53 UniquifyRef(const std::vector
<T
>* v
, size_t i
, size_t hash_value
)
57 hash_val_(hash_value
) {
60 const T
& value() const { return value_
? *value_
: (*vect_
)[index_
]; }
61 size_t hash_val() const { return hash_val_
; }
62 size_t index() const { return index_
; }
65 void FillHashValue() {
66 BASE_HASH_NAMESPACE::hash
<T
> h
;
67 hash_val_
= h(value());
70 // When non-null, points to the object.
73 // When value is null these are used.
74 const std::vector
<T
>* vect_
;
80 template<typename T
> inline bool operator==(const UniquifyRef
<T
>& a
,
81 const UniquifyRef
<T
>& b
) {
82 return a
.value() == b
.value();
85 template<typename T
> inline bool operator<(const UniquifyRef
<T
>& a
,
86 const UniquifyRef
<T
>& b
) {
87 return a
.value() < b
.value();
90 } // namespace internal
92 namespace BASE_HASH_NAMESPACE
{
94 template<typename T
> struct hash
< internal::UniquifyRef
<T
> > {
95 std::size_t operator()(const internal::UniquifyRef
<T
>& v
) const {
100 } // namespace BASE_HASH_NAMESPACE
102 // An ordered set optimized for GN's usage. Such sets are used to store lists
103 // of configs and libraries, and are appended to but not randomly inserted
108 typedef std::vector
<T
> Vector
;
109 typedef typename
Vector::iterator iterator
;
110 typedef typename
Vector::const_iterator const_iterator
;
112 const Vector
& vector() const { return vector_
; }
113 size_t size() const { return vector_
.size(); }
114 bool empty() const { return vector_
.empty(); }
115 void clear() { vector_
.clear(); set_
.clear(); }
116 void reserve(size_t s
) { vector_
.reserve(s
); }
118 const T
& operator[](size_t index
) const { return vector_
[index
]; }
120 const_iterator
begin() const { return vector_
.begin(); }
121 iterator
begin() { return vector_
.begin(); }
122 const_iterator
end() const { return vector_
.end(); }
123 iterator
end() { return vector_
.end(); }
125 // Returns true if the item was appended, false if it already existed (and
126 // thus the vector was not modified).
127 bool push_back(const T
& t
) {
129 if (set_
.find(ref
) != set_
.end())
130 return false; // Already have this one.
132 vector_
.push_back(t
);
133 set_
.insert(Ref(&vector_
, vector_
.size() - 1, ref
.hash_val()));
137 // Like push_back but swaps in the type to avoid a copy.
138 bool PushBackViaSwap(T
* t
) {
142 if (set_
.find(ref
) != set_
.end())
143 return false; // Already have this one.
145 size_t new_index
= vector_
.size();
146 vector_
.resize(new_index
+ 1);
147 swap(vector_
[new_index
], *t
);
148 set_
.insert(Ref(&vector_
, vector_
.size() - 1, ref
.hash_val()));
152 // Appends a range of items from an iterator.
153 template<typename iter
> void Append(const iter
& begin
, const iter
& end
) {
154 for (iter i
= begin
; i
!= end
; ++i
)
158 // Returns the index of the item matching the given value in the list, or
159 // (size_t)(-1) if it's not found.
160 size_t IndexOf(const T
& t
) const {
162 typename
HashSet::const_iterator found
= set_
.find(ref
);
163 if (found
== set_
.end())
164 return static_cast<size_t>(-1);
165 return found
->index();
169 typedef internal::UniquifyRef
<T
> Ref
;
170 typedef base::hash_set
<Ref
> HashSet
;
176 #endif // TOOLS_GN_UNIQUE_VECTOR_H_