SHOGUN  6.1.3
iterators.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * Authors: 2017 Viktor Gal
31  */
32 
33 #ifndef SHOGUN_ITERATORS_H_
34 #define SHOGUN_ITERATORS_H_
35 
36 namespace shogun
37 {
38  template<typename T>
40  {
41  public:
42  // iterator traits
43  using difference_type = std::ptrdiff_t;
44  using value_type = T;
45  using pointer = T*;
46  using reference = T&;
47  using iterator_category = std::random_access_iterator_tag;
48 
49  explicit RandomIterator(pointer ptr) : m_ptr(ptr) {}
50 
51  RandomIterator& operator++() { m_ptr++; return *this; }
52  RandomIterator operator++(int) { RandomIterator retval = *this; m_ptr++; return retval;}
53  RandomIterator& operator--() { m_ptr--; return *this; }
54  RandomIterator operator--(int) { RandomIterator retval = *this; m_ptr--; return retval;}
55 
56  bool operator==(const RandomIterator& other) const { return m_ptr == other.m_ptr; }
57  bool operator!=(const RandomIterator& other) const { return m_ptr != other.m_ptr; }
58 
59  reference operator*() {return *m_ptr;}
60  pointer operator->() { return m_ptr; }
61 
62  RandomIterator &operator += (difference_type d) { m_ptr += d; return *this; }
63  RandomIterator &operator -= (difference_type d) { m_ptr -= d; return *this; }
64 
67 
68  reference operator [] (difference_type d) const { return m_ptr[d]; }
69 
70  bool operator < (const RandomIterator &other) const { return m_ptr < other.m_ptr; }
71  bool operator > (const RandomIterator &other) const { return m_ptr > other.m_ptr; }
72  bool operator <= (const RandomIterator &other) const { return m_ptr <= other.m_ptr; }
73  bool operator >= (const RandomIterator &other) const { return m_ptr >= other.m_ptr; }
74 
75  difference_type operator - (const RandomIterator &other) const { return m_ptr - other.m_ptr; }
76 
77  private:
78  pointer m_ptr;
79  };
80 }
81 
82 #endif
reference operator[](difference_type d) const
Definition: iterators.h:68
bool operator!=(const RandomIterator &other) const
Definition: iterators.h:57
bool operator>=(const RandomIterator &other) const
Definition: iterators.h:73
RandomIterator & operator++()
Definition: iterators.h:51
RandomIterator & operator+=(difference_type d)
Definition: iterators.h:62
RandomIterator operator++(int)
Definition: iterators.h:52
RandomIterator(pointer ptr)
Definition: iterators.h:49
bool operator<=(const RandomIterator &other) const
Definition: iterators.h:72
reference operator*()
Definition: iterators.h:59
std::ptrdiff_t difference_type
Definition: iterators.h:43
RandomIterator operator--(int)
Definition: iterators.h:54
bool operator>(const RandomIterator &other) const
Definition: iterators.h:71
RandomIterator & operator-=(difference_type d)
Definition: iterators.h:63
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
RandomIterator operator+(difference_type d) const
Definition: iterators.h:65
bool operator==(const RandomIterator &other) const
Definition: iterators.h:56
bool operator<(const RandomIterator &other) const
Definition: iterators.h:70
std::random_access_iterator_tag iterator_category
Definition: iterators.h:47
RandomIterator & operator--()
Definition: iterators.h:53
pointer operator->()
Definition: iterators.h:60
RandomIterator operator-(difference_type d) const
Definition: iterators.h:66

SHOGUN Machine Learning Toolbox - Documentation