• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace v8 {
11 namespace base {
12 
13 template <class Category, class Type, class Diff = std::ptrdiff_t,
14           class Pointer = Type*, class Reference = Type&>
15 struct iterator {
16   using iterator_category = Category;
17   using value_type = Type;
18   using difference_type = Diff;
19   using pointer = Pointer;
20   using reference = Reference;
21 };
22 
23 // The intention of the base::iterator_range class is to encapsulate two
24 // iterators so that the range defined by the iterators can be used like
25 // a regular STL container (actually only a subset of the full container
26 // functionality is available usually).
27 template <typename ForwardIterator>
28 class iterator_range {
29  public:
30   using iterator = ForwardIterator;
31   using const_iterator = ForwardIterator;
32   using pointer = typename std::iterator_traits<iterator>::pointer;
33   using reference = typename std::iterator_traits<iterator>::reference;
34   using value_type = typename std::iterator_traits<iterator>::value_type;
35   using difference_type =
36       typename std::iterator_traits<iterator>::difference_type;
37 
iterator_range()38   iterator_range() : begin_(), end_() {}
iterator_range(ForwardIterator begin,ForwardIterator end)39   iterator_range(ForwardIterator begin, ForwardIterator end)
40       : begin_(begin), end_(end) {}
41 
begin()42   iterator begin() { return begin_; }
end()43   iterator end() { return end_; }
begin()44   const_iterator begin() const { return begin_; }
end()45   const_iterator end() const { return end_; }
cbegin()46   const_iterator cbegin() const { return begin_; }
cend()47   const_iterator cend() const { return end_; }
48 
empty()49   bool empty() const { return cbegin() == cend(); }
50 
51   // Random Access iterators only.
52   reference operator[](difference_type n) { return begin()[n]; }
size()53   difference_type size() const { return cend() - cbegin(); }
54 
55  private:
56   const_iterator const begin_;
57   const_iterator const end_;
58 };
59 
60 template <typename ForwardIterator>
make_iterator_range(ForwardIterator begin,ForwardIterator end)61 auto make_iterator_range(ForwardIterator begin, ForwardIterator end) {
62   return iterator_range<ForwardIterator>{begin, end};
63 }
64 
65 // {Reversed} returns a container adapter usable in a range-based "for"
66 // statement for iterating a reversible container in reverse order.
67 //
68 // Example:
69 //
70 //   std::vector<int> v = ...;
71 //   for (int i : base::Reversed(v)) {
72 //     // iterates through v from back to front
73 //   }
74 template <typename T>
Reversed(T & t)75 auto Reversed(T& t) {  // NOLINT(runtime/references): match {rbegin} and {rend}
76   return make_iterator_range(std::rbegin(t), std::rend(t));
77 }
78 
79 }  // namespace base
80 }  // namespace v8
81 
82 #endif  // V8_BASE_ITERATOR_H_
83