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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « BUILD.gn ('k') | test/unittests/base/iterator-unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 the V8 project 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.
4
5 #ifndef V8_BASE_ITERATOR_H_
6 #define V8_BASE_ITERATOR_H_
7
8 #include <iterator>
9
10 #include "src/base/macros.h"
11
12 namespace v8 {
13 namespace base {
14
15 // The intention of the base::iterator_range class is to encapsulate two
16 // iterators so that the range defined by the iterators can be used like
17 // a regular STL container (actually only a subset of the full container
18 // functionality is available usually).
19 template <typename ForwardIterator>
20 class iterator_range {
21 public:
22 typedef ForwardIterator iterator;
23 typedef ForwardIterator const_iterator;
24 typedef typename std::iterator_traits<iterator>::pointer pointer;
25 typedef typename std::iterator_traits<iterator>::reference reference;
26 typedef typename std::iterator_traits<iterator>::value_type value_type;
27 typedef
28 typename std::iterator_traits<iterator>::difference_type difference_type;
29
30 iterator_range() : begin_(), end_() {}
31 template <typename ForwardIterator2>
32 iterator_range(ForwardIterator2 const& begin, ForwardIterator2 const& end)
33 : begin_(begin), end_(end) {}
34
35 iterator begin() { return begin_; }
36 iterator end() { return end_; }
37 const_iterator begin() const { return begin_; }
38 const_iterator end() const { return end_; }
39 const_iterator cbegin() const { return begin_; }
40 const_iterator cend() const { return end_; }
41
42 bool empty() const { return cbegin() == cend(); }
43
44 // Random Access iterators only.
45 reference operator[](difference_type n) { return begin()[n]; }
46 difference_type size() const { return cend() - cbegin(); }
47
48 private:
49 const_iterator const begin_;
50 const_iterator const end_;
51 };
52
53 } // namespace base
54 } // namespace v8
55
56 #endif // V8_BASE_ITERATOR_H_
OLDNEW
« 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