source: trunk/yat/utility/Iterator.h @ 883

Last change on this file since 883 was 883, checked in by Peter, 14 years ago

fixing problems with iterator -> const_iterator conversion

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1#ifndef _theplu_yat_utility_iterator_
2#define _theplu_yat_utility_iterator_
3
4// $Id: Iterator.h 883 2007-09-22 22:05:41Z peter $
5
6/*
7  Copyright (C) 2007 Peter Johansson
8
9  This file is part of the yat library, http://trac.thep.lu.se/trac/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include <iterator>
28#include <stddef.h>
29
30namespace theplu {
31namespace yat {
32namespace utility {
33
34  /**
35     @brief Iterator
36  */
37  template<typename return_type, typename Container>
38  class Iterator
39  {
40  public:
41    typedef std::random_access_iterator_tag iterator_category;
42    typedef double value_type;
43    typedef size_t difference_type;
44    typedef double* pointer;
45    typedef double& reference;
46
47    /**
48       @brief Constructor
49    */
50    Iterator(void) {};
51    Iterator(Container& container, difference_type index)
52      : container_(&container), index_(index) {}
53
54    return_type operator*(void) const { return container_->operator()(index_); }
55    const Iterator& operator++(void) { ++index_; return *this; }
56    Iterator operator++(int) 
57    { Iterator<return_type, Container> tmp(*this); ++index_; return tmp;}
58    Iterator& operator+=(int n) { index_+=n; return *this; }
59    Iterator operator+(int n) 
60    { return Iterator<return_type, Container>(*container_, index_+n); }
61    Iterator operator--(int) 
62    { Iterator<return_type, Container> tmp(*this); --index_; return tmp;}
63    Iterator operator--(void) { --index_; return *this; }
64    Iterator& operator-=(int n) { index_-=n; return *this; }
65    Iterator operator-(size_t n) 
66    { return Iterator<return_type, Container>(*container_, index_-n); }
67    size_t operator-(const Iterator& rhs) 
68    { return index_-rhs.index_; }
69
70    bool less(const Iterator<return_type, Container>& other) const
71    { return index_<other.index_; }
72
73    operator Iterator<const double, const Container> () 
74    { return Iterator<const double, const Container>(*container_, index_); } 
75
76
77  private:
78    Container* container_;
79    size_t index_;
80    // Using compiler generated copy
81    //Iterator(const Iterator&);
82    //Iterator& operator=(const Iterator&);
83
84   
85  public:
86    friend bool operator==(const Iterator<return_type, Container>& lhs, 
87                           const Iterator<return_type, Container>& rhs)
88    { return lhs.container_==rhs.container_ && lhs.index_==rhs.index_; }
89   
90    friend bool operator!=(const Iterator<return_type, Container>& lhs, 
91                           const Iterator<return_type, Container>& rhs)
92    { return !(lhs.container_==rhs.container_ && lhs.index_==rhs.index_); }
93   
94    friend bool operator<(const Iterator<return_type, Container>& lhs, 
95                           const Iterator<return_type, Container>& rhs)
96    { return lhs.index_<rhs.index_; }
97   
98    friend bool operator<=(const Iterator<return_type, Container>& lhs, 
99                           const Iterator<return_type, Container>& rhs)
100    { return lhs.index_<=rhs.index_; }
101   
102    friend bool operator>(const Iterator<return_type, Container>& lhs, 
103                           const Iterator<return_type, Container>& rhs)
104    { return lhs.index_>rhs.index_; }
105   
106    friend bool operator>=(const Iterator<return_type, Container>& lhs, 
107                           const Iterator<return_type, Container>& rhs)
108    { return lhs.index_>=rhs.index_; }
109   
110
111
112  };
113
114    /*
115  template <typename T1, typename T2>
116  bool operator==(const Iterator<T1,T2>& lhs,
117                         const Iterator<T1,T2>& rhs)
118  {
119    return lhs.equal(rhs);
120  }
121
122  template <typename T1, typename T2>
123  bool operator!=(const Iterator<T1,T2>& lhs,
124                         const Iterator<T1,T2>& rhs)
125  {
126    return !(lhs==rhs);
127  }
128 
129 
130  template <typename T1, typename T2>
131  bool operator<(const Iterator<T1,T2>& lhs,
132                 const Iterator<T1,T2>& rhs)
133  {
134    return lhs<rhs;
135  }
136 
137 
138  template <typename T1, typename T2>
139  bool operator<=(const Iterator<T1,T2>& lhs,
140                  const Iterator<T1,T2>& rhs)
141  {
142    return !(rhs<lhs);
143  }
144 
145 
146  template <typename T1, typename T2>
147  bool operator>(const Iterator<T1,T2>& lhs,
148                 const Iterator<T1,T2>& rhs)
149  {
150    return rhs<lhs;
151  }
152 
153 
154  template <typename T1, typename T2>
155  bool operator>=(const Iterator<T1,T2>& lhs,
156                  const Iterator<T1,T2>& rhs)
157  {
158    return !(lhs<rhs);
159  }
160    */
161
162}}} // of namespace utility, yat, and theplu
163
164#endif
Note: See TracBrowser for help on using the repository browser.