source: rrlib_util/tIteratorRange.h @ 153:3e510dd2bd0f

17.03
Last change on this file since 153:3e510dd2bd0f was 153:3e510dd2bd0f, checked in by Max Reichardt <mreichardt@…>, 4 years ago

Adds tIteratorRange utility class that wraps start and end iterator

File size: 4.1 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    rrlib/util/tIteratorRange.h
23 *
24 * \author  Max Reichardt
25 * \author  Tobias Foehst
26 *
27 * \date    2017-02-01
28 *
29 * \brief   Contains tIteratorRange
30 *
31 * \b tIteratorRange
32 *
33 * Wraps a begin and end iterator (one past the last element).
34 *
35 */
36//----------------------------------------------------------------------
37#ifndef __rrlib__util__tIteratorRange_h__
38#define __rrlib__util__tIteratorRange_h__
39
40//----------------------------------------------------------------------
41// External includes (system with <>, local with "")
42//----------------------------------------------------------------------
43
44//----------------------------------------------------------------------
45// Internal includes with ""
46//----------------------------------------------------------------------
47
48//----------------------------------------------------------------------
49// Namespace declaration
50//----------------------------------------------------------------------
51namespace rrlib
52{
53namespace util
54{
55
56//----------------------------------------------------------------------
57// Forward declarations / typedefs / enums
58//----------------------------------------------------------------------
59
60//----------------------------------------------------------------------
61// Class declaration
62//----------------------------------------------------------------------
63//! Iterator range
64/*!
65 * Wraps a begin and end iterator (one past the last element).
66 */
67template <typename TIterator>
68class tIteratorRange
69{
70
71//----------------------------------------------------------------------
72// Public methods and typedefs
73//----------------------------------------------------------------------
74public:
75
76  tIteratorRange() : begin_iterator(), end_iterator()
77  {}
78
79  /*!
80   * \param begin Begin iterator
81   * \param end End iterator
82   */
83  tIteratorRange(const TIterator& begin, const TIterator& end) :
84    begin_iterator(begin),
85    end_iterator(end)
86  {}
87
88  /*!
89   * \return Begin iterator
90   */
91  const TIterator& Begin() const
92  {
93    return begin_iterator;
94  }
95  const TIterator& begin() const
96  {
97    return begin_iterator;
98  }
99
100  /*!
101   * \return Whether range is empty
102   */
103  bool Empty()
104  {
105    return begin_iterator == end_iterator;
106  }
107
108  /*!
109   * \return End iterator
110   */
111  const TIterator& End() const
112  {
113    return end_iterator;
114  }
115  const TIterator& end() const
116  {
117    return end_iterator;
118  }
119
120//----------------------------------------------------------------------
121// Private fields and methods
122//----------------------------------------------------------------------
123private:
124
125  /*! Begin and end iterator */
126  TIterator begin_iterator, end_iterator;
127};
128
129
130template <typename TIterator>
131tIteratorRange<TIterator> MakeRange(TIterator begin, TIterator end)
132{
133  return tIteratorRange<TIterator>(begin, end);
134}
135
136//----------------------------------------------------------------------
137// End of namespace declaration
138//----------------------------------------------------------------------
139}
140}
141
142namespace std
143{
144template <typename TIterator>
145TIterator begin(rrlib::util::tIteratorRange<TIterator>& range)
146{
147  return range.Begin();
148}
149template <typename TIterator>
150TIterator end(rrlib::util::tIteratorRange<TIterator>& range)
151{
152  return range.End();
153}
154}
155
156
157#endif
Note: See TracBrowser for help on using the repository browser.