Chromium Code Reviews| Index: tools/gn/unique_vector.h |
| diff --git a/tools/gn/unique_vector.h b/tools/gn/unique_vector.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..18578059b1e102e64df8adbe44d4f42f27f34bb8 |
| --- /dev/null |
| +++ b/tools/gn/unique_vector.h |
| @@ -0,0 +1,184 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef TOOLS_GN_UNIQUE_VECTOR_H_ |
| +#define TOOLS_GN_UNIQUE_VECTOR_H_ |
| + |
| +#include <algorithm> |
| + |
| +#include "base/containers/hash_tables.h" |
| + |
| +namespace internal { |
| + |
| +// This struct allows us to insert things by reference into a hash table. This |
|
viettrungluu
2014/08/06 19:43:52
Which struct are you talking about?
(Actually, th
|
| +// is used to avoid copying strings just to uniquify them. The hash function |
| +// of a UniquifyRef is that of the object it points to. |
| +// |
| +// It also caches the hash value which allows us to query and then insert |
| +// without recomputing the hash. |
| +template<typename T> |
| +class UniquifyRef { |
| + public: |
| + UniquifyRef() |
| + : value_(NULL), |
| + vect_(NULL), |
| + index_(static_cast<size_t>(-1)), |
| + hash_val_(0) { |
| + } |
| + |
| + // Initialize with a pointer to a value. |
| + UniquifyRef(const T* v) |
| + : value_(v), |
| + vect_(NULL), |
| + index_(static_cast<size_t>(-1)) { |
| + FillHashValue(); |
| + } |
| + |
| + // Initialize with an array + index. |
| + UniquifyRef(const std::vector<T>* v, size_t i) |
|
viettrungluu
2014/08/06 19:43:52
Why would you use this instead of passing &v[i] to
brettw
2014/08/06 20:13:03
I clarified the class-level comment. The vector*+i
|
| + : value_(NULL), |
| + vect_(v), |
| + index_(i) { |
| + FillHashValue(); |
| + } |
| + |
| + // Initialize with an array + index and a know hash value to prevent |
|
viettrungluu
2014/08/06 19:43:52
s/know/known/ I assume.
|
| + // re-hashing. |
| + UniquifyRef(const std::vector<T>* v, size_t i, size_t hash_value) |
| + : value_(NULL), |
| + vect_(v), |
| + index_(i), |
| + hash_val_(hash_value) { |
| + } |
| + |
| + const T& value() const { return value_ ? *value_ : (*vect_)[index_]; } |
| + size_t hash_val() const { return hash_val_; } |
| + size_t index() const { return index_; } |
| + |
| + private: |
| + void FillHashValue() { |
| +#if defined(COMPILER_GCC) |
| + BASE_HASH_NAMESPACE::hash<T> h; |
| + hash_val_ = h(value()); |
| +#elif defined(COMPILER_MSVC) |
| + hash_val_ = BASE_HASH_NAMESPACE::hash_value(value()); |
| +#else |
| + #error write me |
| +#endif // COMPILER... |
| + } |
| + |
| + // When non-null, points to the object. |
| + const T* value_; |
| + |
| + // When value is null these are used. |
| + const std::vector<T>* vect_; |
| + size_t index_; |
| + |
| + size_t hash_val_; |
| +}; |
| + |
| +template<typename T> inline bool operator==(const UniquifyRef<T>& a, |
| + const UniquifyRef<T>& b) { |
| + return a.value() == b.value(); |
| +} |
| + |
| +template<typename T> inline bool operator<(const UniquifyRef<T>& a, |
| + const UniquifyRef<T>& b) { |
| + return a.value() < b.value(); |
| +} |
| + |
| +} // namespace internal |
| + |
| +namespace BASE_HASH_NAMESPACE { |
| + |
| +#if defined(COMPILER_GCC) |
| +template<typename T> struct hash< internal::UniquifyRef<T> > { |
| + std::size_t operator()(const internal::UniquifyRef<T>& v) const { |
| + return v.hash_val(); |
| + } |
| +}; |
| +#elif defined(COMPILER_MSVC) |
| +template<typename T> |
| +inline size_t hash_value(const internal::UniquifyRef<T>& v) { |
| + return v.hash_val(); |
| +} |
| +#endif // COMPILER... |
| + |
| +} // namespace BASE_HASH_NAMESPACE |
| + |
| +// An ordered set optimized for GN's usage. Such sets are used to store lists |
| +// of configs and libraries, and are appended to but not |
|
viettrungluu
2014/08/06 19:43:52
appended to but not ... what?
|
| +template<typename T> |
| +class UniqueVector { |
| + public: |
| + typedef std::vector<T> Vector; |
| + typedef typename Vector::iterator iterator; |
| + typedef typename Vector::const_iterator const_iterator; |
| + const size_t npos = static_cast<size_t>(-1); |
| + |
| + const Vector& vector() const { return vector_; } |
| + size_t size() const { return vector_.size(); } |
| + bool empty() const { return vector_.empty(); } |
| + void clear() { vector_.clear(); set_.clear(); } |
| + void reserve(size_t s) { vector_.reserve(s); } |
| + |
| + const T& operator[](size_t index) const { return vector_[index]; } |
| + |
| + const_iterator begin() const { return vector_.begin(); } |
| + iterator begin() { return vector_.begin(); } |
| + const_iterator end() const { return vector_.end(); } |
| + iterator end() { return vector_.end(); } |
| + |
| + // Returns true if the item was appended, false if it already existed (and |
| + // thus the vector was not modified). |
| + bool push_back(const T& t) { |
| + Ref ref(&t); |
| + if (set_.find(ref) != set_.end()) |
| + return false; // Already have this one. |
| + |
| + vector_.push_back(t); |
| + set_.insert(Ref(&vector_, vector_.size() - 1, ref.hash_val())); |
| + return true; |
| + } |
| + |
| + // Like push_back but swaps in the type to avoid a copy. |
| + bool PushBackViaSwap(T* t) { |
| + using std::swap; |
| + |
| + Ref ref(t); |
| + if (set_.find(ref) != set_.end()) |
| + return false; // Already have this one. |
| + |
| + size_t new_index = vector_.size(); |
| + vector_.resize(new_index + 1); |
| + swap(vector_[new_index], *t); |
| + set_.insert(Ref(&vector_, vector_.size() - 1, ref.hash_val())); |
| + return true; |
| + } |
| + |
| + // Appends a range of items from an iterator. |
| + template<typename iter> void Append(const iter& begin, const iter& end) { |
| + for (iter i = begin; i < end; i++) |
|
viettrungluu
2014/08/06 19:43:52
Generally preferred:
!= end
++i
|
| + push_back(*i); |
|
viettrungluu
2014/08/06 19:43:52
Maybe you should have a (debug-only) assertion abo
brettw
2014/08/06 20:13:03
This calls push_back which handles duplicate eleme
|
| + } |
| + |
| + // Returns the index of the item matching the given value in the list, or |
| + // npos if it's not found. |
| + size_t IndexOf(const T& t) const { |
| + Ref ref(&t); |
| + typename HashSet::const_iterator found = set_.find(ref); |
| + if (found == set_.end()) |
| + return npos; |
| + return found->index(); |
| + } |
| + |
| + private: |
| + typedef internal::UniquifyRef<T> Ref; |
| + typedef base::hash_set<Ref> HashSet; |
| + |
| + HashSet set_; |
| + Vector vector_; |
| +}; |
| + |
| +#endif // TOOLS_GN_UNIQUE_VECTOR_H_ |