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.
5 #ifndef TOOLS_GN_ORDERED_SET_H_
6 #define TOOLS_GN_ORDERED_SET_H_
10 // An ordered set of items. Only appending is supported. Iteration is designed
15 typedef std::set
<T
> set_type
;
16 typedef typename
set_type::const_iterator set_iterator
;
17 typedef std::vector
<set_iterator
> vector_type
;
20 static const size_t npos
= static_cast<size_t>(-1);
25 const T
& operator[](size_t index
) const {
26 return *ordering_
[index
];
29 return ordering_
.size();
32 return ordering_
.empty();
35 bool has_item(const T
& t
) const {
36 return set_
.find(t
) != set_
.end();
39 // Returns true if the item was inserted. False if it was already in the
41 bool push_back(const T
& t
) {
42 std::pair
<set_iterator
, bool> result
= set_
.insert(t
);
44 ordering_
.push_back(result
.first
);
48 // Appends a range of items, skipping ones that already exist.
49 template<class InputIterator
>
50 void append(const InputIterator
& insert_begin
,
51 const InputIterator
& insert_end
) {
52 for (InputIterator i
= insert_begin
; i
!= insert_end
; ++i
) {
58 // Appends all items from the given other set.
59 void append(const OrderedSet
<T
>& other
) {
60 for (size_t i
= 0; i
< other
.size(); i
++)
66 vector_type ordering_
;
69 #endif // TOOLS_GN_ORDERED_SET_H_