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 typedef Category iterator_category; 17 typedef Type value_type; 18 typedef Diff difference_type; 19 typedef Pointer pointer; 20 typedef 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 typedef ForwardIterator iterator; 31 typedef ForwardIterator const_iterator; 32 typedef typename std::iterator_traits<iterator>::pointer pointer; 33 typedef typename std::iterator_traits<iterator>::reference reference; 34 typedef typename std::iterator_traits<iterator>::value_type value_type; 35 typedef 36 typename std::iterator_traits<iterator>::difference_type difference_type; 37 iterator_range()38 iterator_range() : begin_(), end_() {} 39 template <typename ForwardIterator1, typename ForwardIterator2> iterator_range(ForwardIterator1 && begin,ForwardIterator2 && end)40 iterator_range(ForwardIterator1&& begin, ForwardIterator2&& end) 41 : begin_(std::forward<ForwardIterator1>(begin)), 42 end_(std::forward<ForwardIterator2>(end)) {} 43 begin()44 iterator begin() { return begin_; } end()45 iterator end() { return end_; } begin()46 const_iterator begin() const { return begin_; } end()47 const_iterator end() const { return end_; } cbegin()48 const_iterator cbegin() const { return begin_; } cend()49 const_iterator cend() const { return end_; } 50 empty()51 bool empty() const { return cbegin() == cend(); } 52 53 // Random Access iterators only. 54 reference operator[](difference_type n) { return begin()[n]; } size()55 difference_type size() const { return cend() - cbegin(); } 56 57 private: 58 const_iterator const begin_; 59 const_iterator const end_; 60 }; 61 62 } // namespace base 63 } // namespace v8 64 65 #endif // V8_BASE_ITERATOR_H_ 66