Chromium Code Reviews| Index: base/containers/adapters.h |
| diff --git a/base/containers/adapters.h b/base/containers/adapters.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..01545a68dec74b4aee95da8c0e9b36000ff8bfd8 |
| --- /dev/null |
| +++ b/base/containers/adapters.h |
| @@ -0,0 +1,49 @@ |
| +// 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. |
| + |
| +// This file contains adapters to iterate containers in alternative orders. |
|
brettw
2014/09/29 16:55:14
I'd just remove this line. We could potentially ad
mdempsky
2014/09/29 17:47:40
Done.
|
| +// |
| +// Example: |
| +// |
| +// std::vector<int> v = ...; |
| +// for (int i : base::reversed(v)) { |
| +// // iterates through v in reverse order |
| +// } |
| + |
| +#ifndef BASE_CONTAINERS_ADAPTERS_H_ |
| +#define BASE_CONTAINERS_ADAPTERS_H_ |
| + |
| +#include "base/macros.h" |
| + |
| +namespace base { |
| + |
| +namespace internal { |
| + |
| +template <typename T> |
|
brettw
2014/09/29 16:55:14
Can you include an example here?
mdempsky
2014/09/29 17:47:40
This class isn't intended for end users. I've adde
|
| +class ReversedAdapter { |
| + public: |
| + typedef decltype(static_cast<T*>(nullptr)->rbegin()) Iterator; |
| + |
| + explicit ReversedAdapter(T& t) : t_(t) {} |
| + ReversedAdapter(const ReversedAdapter& ra) : t_(ra.t_) {} |
| + |
| + friend Iterator begin(const ReversedAdapter& ra) { return ra.t_.rbegin(); } |
|
brettw
2014/09/29 16:55:14
I don't understand what "friend" does in this cont
mdempsky
2014/09/29 17:47:41
Turns out it's not necessary; I was looking at an
|
| + friend Iterator end(const ReversedAdapter& ra) { return ra.t_.rend(); } |
| + |
| + private: |
| + T& t_; |
| + |
| + DISALLOW_ASSIGN(ReversedAdapter); |
| +}; |
| + |
| +} // namespace internal |
| + |
| +template <typename T> |
| +internal::ReversedAdapter<T> reversed(T& t) { |
|
brettw
2014/09/29 16:55:14
Google-style would say this should be capitalized.
mdempsky
2014/09/29 17:47:41
Done.
|
| + return internal::ReversedAdapter<T>(t); |
| +} |
| + |
| +} // namespace base |
| + |
| +#endif // BASE_CONTAINERS_ADAPTERS_H_ |