Machine Learning Library
keyed_iterator.hpp
Go to the documentation of this file.
1 /*
2  COPYRIGHT (C) 2003 APPLIED NEUROINFORMATIC GROUP - UNIVERSITY OF BIELEFELD.
3 
4  ALL RIGHTS RESERVED.
5 
6  REDISTRIBUTION AND USE IN SOURCE AND BINARY FORM, WITH OR WITHOUT
7  MODIFICATION, REQUIRE THE PERMISSION OF THE COPYRIGHT HOLDERS.
8 
9  COMMERCIAL USE WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT HOLDERS
10  IS FORBIDDEN
11 
12  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
13  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23 
24 
25 
26 #ifndef __KEYED_ITERATOR_HPP
27 #define __KEYED_ITERATOR_HPP
28 
29 #include <utility>
30 #include <iterator>
31 
32 struct keyed_iterator_tag : public random_access_iterator_tag {};
33 
34 // TODO specialize implementation for <const Type>
35 
36 template<typename Type>
37 class keyed_iterator
38 {
39 public:
40  typedef pair<int, Type> pair_type;
42  typedef pair_type* pointer;
43  typedef pair_type& reference;
44  typedef ptrdiff_t difference_type;
46 
47 private:
48  pair<int, Type>* it;
49  int _size;
50  difference_type _max;
51 
52 public:
53 
55  keyed_iterator(pair<int, Type>* it, int size, difference_type max) : it(it), _size(size), _max(max) {};
56 
57  // basic iterator ops
58  reference operator*() const { return *it; }
59  pointer operator->() const { return it; }
60  keyed_iterator& operator++() { ++it; return *this; }
62  keyed_iterator tmp = *this;
63  ++it;
64  return tmp;
65  }
66  keyed_iterator& operator--() { --it; return *this; }
68  keyed_iterator tmp = *this;
69  --it;
70  return tmp;
71  }
72  // comparisons
73  bool operator==(const keyed_iterator<Type>& o) const {
74  return (it->first == o.it->first &&
75  it->second == o.it->second);
76  }
77  bool operator!=(const keyed_iterator& o) const { return !(operator==(o)); }
78  bool operator<(const keyed_iterator<Type>& o) {
79  return it < o.it;
80  }
81 
82  // random access ops
84  it += n;
85  return *this;
86  }
88  it -= n;
89  return *this;
90  }
91  keyed_iterator operator+(int n) const {
92  keyed_iterator tmp = *this;
93  tmp += n;
94  return tmp;
95  }
96  keyed_iterator operator-(int n) const {
97  keyed_iterator tmp = *this;
98  tmp -= n;
99  return tmp;
100  }
101  const reference operator[](int n) const { return it[n]; }
102  reference operator[](int n) { return it[n]; }
103  difference_type operator-(const keyed_iterator<Type>& o) { return it - o.it; }
104 
105 
106  // special pair operations
107  int key() const { return it->first; }
108  Type val() const { return it->second; }
109  Type& val() { return it->second; }
110  int size() const { return _size; }
111  difference_type max() const { return _max; }
112 };
113 
114 template<typename Type> bool operator==(const keyed_iterator<Type>& l,
115  const keyed_iterator<const Type>& r) {
116  return l.key() == r.key() && l.val() == r.val();
117 }
118 template<typename Type> bool operator==(const keyed_iterator<const Type>& l,
119  const keyed_iterator<Type>& r) {
120  return l.key() == r.key() && l.val() == r.val();
121 }
122 template<typename Type> bool operator!=(const keyed_iterator<Type>& l,
123  const keyed_iterator<const Type>& r) {
124  return l.key() != r.key() || l.val() != r.val();
125 }
126 template<typename Type> bool operator!=(const keyed_iterator<const Type>& l,
127  const keyed_iterator<Type>& r) {
128  return l.key() != r.key() || l.val() != r.val();
129 }
130 template<typename Type> bool operator<(const keyed_iterator<Type>& l,
131  const keyed_iterator<const Type>& r) {
132  return &(*l) < &(*r);
133 }
134 template<typename Type> bool operator<(const keyed_iterator<const Type>& l,
135  const keyed_iterator<Type>& r) {
136  return &(*l) < &(*r);
137 }
138 
139 
140 namespace std {
141 template<typename Type>
142 struct iterator_traits< keyed_iterator<Type> > {
148 };
149 
150 template<typename Type>
151 struct iterator_traits< const keyed_iterator<Type> > {
157 };
158 } // namespace std
159 
160 #endif // #ifndef __KEYED_ITERATOR_HPP
keyed_iterator operator+(int n) const
Definition: keyed_iterator.hpp:91
keyed_iterator()
Definition: keyed_iterator.hpp:54
int key() const
Definition: keyed_iterator.hpp:107
pair_type * pointer
Definition: keyed_iterator.hpp:42
bool operator!=(const keyed_iterator< Type > &l, const keyed_iterator< const Type > &r)
Definition: keyed_iterator.hpp:122
keyed_iterator operator--(int)
Definition: keyed_iterator.hpp:67
difference_type max() const
Definition: keyed_iterator.hpp:111
int size() const
Definition: keyed_iterator.hpp:110
pointer operator->() const
Definition: keyed_iterator.hpp:59
bool operator!=(const keyed_iterator &o) const
Definition: keyed_iterator.hpp:77
difference_type operator-(const keyed_iterator< Type > &o)
Definition: keyed_iterator.hpp:103
reference operator*() const
Definition: keyed_iterator.hpp:58
keyed_iterator< Type >::iterator_category iterator_category
Definition: keyed_iterator.hpp:156
keyed_iterator & operator++()
Definition: keyed_iterator.hpp:60
Definition: CDenseVector.h:35
keyed_iterator operator-(int n) const
Definition: keyed_iterator.hpp:96
keyed_iterator< Type >::pointer pointer
Definition: keyed_iterator.hpp:144
pair_type value_type
Definition: keyed_iterator.hpp:41
keyed_iterator & operator--()
Definition: keyed_iterator.hpp:66
bool operator==(const keyed_iterator< Type > &l, const keyed_iterator< const Type > &r)
Definition: keyed_iterator.hpp:114
const reference operator[](int n) const
Definition: keyed_iterator.hpp:101
keyed_iterator_tag iterator_category
Definition: keyed_iterator.hpp:45
pair_type & reference
Definition: keyed_iterator.hpp:43
keyed_iterator & operator-=(int n)
Definition: keyed_iterator.hpp:87
keyed_iterator< Type >::pair_type value_type
Definition: keyed_iterator.hpp:152
pair< int, Type > pair_type
Definition: keyed_iterator.hpp:40
keyed_iterator< Type >::iterator_category iterator_category
Definition: keyed_iterator.hpp:147
keyed_iterator< Type >::pair_type value_type
Definition: keyed_iterator.hpp:143
keyed_iterator & operator+=(int n)
Definition: keyed_iterator.hpp:83
keyed_iterator operator++(int)
Definition: keyed_iterator.hpp:61
Type val() const
Definition: keyed_iterator.hpp:108
keyed_iterator< Type >::difference_type difference_type
Definition: keyed_iterator.hpp:146
ptrdiff_t difference_type
Definition: keyed_iterator.hpp:44
keyed_iterator< Type >::reference reference
Definition: keyed_iterator.hpp:145
keyed_iterator< Type >::difference_type difference_type
Definition: keyed_iterator.hpp:155
keyed_iterator(pair< int, Type > *it, int size, difference_type max)
Definition: keyed_iterator.hpp:55
keyed_iterator< Type >::reference reference
Definition: keyed_iterator.hpp:154
keyed_iterator< Type >::pointer pointer
Definition: keyed_iterator.hpp:153
reference operator[](int n)
Definition: keyed_iterator.hpp:102
bool operator==(const keyed_iterator< Type > &o) const
Definition: keyed_iterator.hpp:73
Type & val()
Definition: keyed_iterator.hpp:109
Definition: keyed_iterator.hpp:32