www.mooseframework.org
Enumerate.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include <iterator>
13 #include <iostream>
14 
15 namespace Moose
16 {
17 // Forward Declarations of helper objects
18 template <class Iterator>
20 
21 template <class Iterator>
23 
24 template <class Iterator>
26 
50 template <class Iterator>
52 enumerate(Iterator first,
53  Iterator last,
54  typename std::iterator_traits<Iterator>::difference_type initial)
55 {
56  return _enumerate_range<Iterator>(first, last, initial);
57 }
58 
59 template <class Container>
60 _enumerate_range<typename Container::iterator>
61 enumerate(Container & content)
62 {
63  return _enumerate_range<typename Container::iterator>(std::begin(content), std::end(content), 0);
64 }
65 
66 template <class Container>
67 _enumerate_range<typename Container::const_iterator>
68 enumerate(const Container & content)
69 {
71  std::begin(content), std::end(content), 0);
72 }
73 
75 // Helper object
76 template <class Iterator>
77 struct _enumerate_struct
78 {
79  using iterator = Iterator;
80  using index_type = typename std::iterator_traits<iterator>::difference_type;
81  using reference = typename std::iterator_traits<iterator>::reference;
82 
84 
85  index_type index() { return l_index; }
86 
87  reference value() { return *l_iter; }
88 
89 private:
92 };
93 
94 // Helper object
95 template <class Iterator>
97 {
98  using iterator = Iterator;
99  using index_type = typename std::iterator_traits<iterator>::difference_type;
100  using reference = typename std::iterator_traits<iterator>::reference;
101 
103 
105  {
106  ++index;
107  ++iter;
108  return *this;
109  }
110 
111  bool operator!=(const _enumerate_iterator & other) const { return iter != other.iter; }
112 
113 #if __cplusplus > 201402L
114  std::pair<index_type &, reference> operator*() { return {index, *iter}; }
115 #else
117 #endif
118 
119 private:
122 };
123 
124 template <class Iterator>
125 struct _enumerate_range
126 {
127  using index_type = typename std::iterator_traits<Iterator>::difference_type;
129 
132  {
133  }
134 
135  iterator begin() const { return iterator(initial, first); }
136 
137  iterator end() const { return iterator(0, last); }
138 
139 private:
140  Iterator first;
141  Iterator last;
143 };
144 }
145 
_enumerate_struct< iterator > operator*()
Definition: Enumerate.h:116
_enumerate_iterator & operator++()
Definition: Enumerate.h:104
_enumerate_iterator< Iterator > iterator
Definition: Enumerate.h:128
_enumerate_range< Iterator > enumerate(Iterator first, Iterator last, typename std::iterator_traits< Iterator >::difference_type initial)
Enumerate function for iterating over a range and obtaining both a reference to the underlying type a...
Definition: Enumerate.h:52
iterator begin() const
Definition: Enumerate.h:135
_enumerate_range(Iterator first, Iterator last, index_type initial)
Definition: Enumerate.h:130
_enumerate_struct(index_type index, iterator iterator)
Definition: Enumerate.h:83
typename std::iterator_traits< Iterator >::difference_type index_type
Definition: Enumerate.h:127
_enumerate_iterator(index_type index, iterator iterator)
Definition: Enumerate.h:102
typename std::iterator_traits< iterator >::difference_type index_type
Definition: Enumerate.h:80
typename std::iterator_traits< iterator >::reference reference
Definition: Enumerate.h:100
bool operator!=(const _enumerate_iterator &other) const
Definition: Enumerate.h:111
std::pair< index_type &, reference > operator*()
Definition: Enumerate.h:114
typename std::iterator_traits< iterator >::difference_type index_type
Definition: Enumerate.h:99
index_type index()
Definition: Enumerate.h:85
Definition: Moose.h:112
iterator end() const
Definition: Enumerate.h:137
typename std::iterator_traits< iterator >::reference reference
Definition: Enumerate.h:81