Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Unified Diff: src/base/iterator.h

Issue 810683003: [base] Add iterator_range helper class. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « BUILD.gn ('k') | test/unittests/base/iterator-unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/base/iterator.h
diff --git a/src/base/iterator.h b/src/base/iterator.h
new file mode 100644
index 0000000000000000000000000000000000000000..e380dc338ffc1bfa245bf6d911a65fd6baa61f45
--- /dev/null
+++ b/src/base/iterator.h
@@ -0,0 +1,56 @@
+// Copyright 2014 the V8 project 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 V8_BASE_ITERATOR_H_
+#define V8_BASE_ITERATOR_H_
+
+#include <iterator>
+
+#include "src/base/macros.h"
+
+namespace v8 {
+namespace base {
+
+// The intention of the base::iterator_range class is to encapsulate two
+// iterators so that the range defined by the iterators can be used like
+// a regular STL container (actually only a subset of the full container
+// functionality is available usually).
+template <typename ForwardIterator>
+class iterator_range {
+ public:
+ typedef ForwardIterator iterator;
+ typedef ForwardIterator const_iterator;
+ typedef typename std::iterator_traits<iterator>::pointer pointer;
+ typedef typename std::iterator_traits<iterator>::reference reference;
+ typedef typename std::iterator_traits<iterator>::value_type value_type;
+ typedef
+ typename std::iterator_traits<iterator>::difference_type difference_type;
+
+ iterator_range() : begin_(), end_() {}
+ template <typename ForwardIterator2>
+ iterator_range(ForwardIterator2 const& begin, ForwardIterator2 const& end)
+ : begin_(begin), end_(end) {}
+
+ iterator begin() { return begin_; }
+ iterator end() { return end_; }
+ const_iterator begin() const { return begin_; }
+ const_iterator end() const { return end_; }
+ const_iterator cbegin() const { return begin_; }
+ const_iterator cend() const { return end_; }
+
+ bool empty() const { return cbegin() == cend(); }
+
+ // Random Access iterators only.
+ reference operator[](difference_type n) { return begin()[n]; }
+ difference_type size() const { return cend() - cbegin(); }
+
+ private:
+ const_iterator const begin_;
+ const_iterator const end_;
+};
+
+} // namespace base
+} // namespace v8
+
+#endif // V8_BASE_ITERATOR_H_
« no previous file with comments | « BUILD.gn ('k') | test/unittests/base/iterator-unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698