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

Last change on this file since 880 was 880, checked in by Peter, 16 years ago

refs #244

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1#ifndef _theplu_yat_utility_iterator_
2#define _theplu_yat_utility_iterator_
3
4// $Id: Iterator.h 880 2007-09-21 23:43:22Z 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 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    reference operator*(void) const { return container_->operator()(index_); }
55    const Iterator& operator++(void) { ++index_; return *this; }
56    Iterator operator++(int) 
57    { Iterator<Container> tmp(*this); ++index_; return tmp;}
58    Iterator& operator+=(int n) { index_+=n; return *this; }
59    Iterator operator+(int n) 
60    { return Iterator<Container>(*container_, index_+n); }
61    Iterator operator--(int) 
62    { Iterator<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<Container>(*container_, index_-n); }
67    size_t operator-(const Iterator& rhs) 
68    { return index_-rhs.index_; }
69
70    bool equal(const Iterator<Container>& other) const
71    { return container_==other.container_ && index_==other.index_; }
72
73    bool less(const Iterator<Container>& other) const
74    { return index_<other.index_; }
75
76  private:
77    Container* container_;
78    size_t index_;
79
80    // Using compiler generated copy
81    //Iterator(const Iterator&);
82    //Iterator& operator=(const Iterator&);
83  };
84
85    template <typename T>
86    inline bool operator==(const Iterator<T>& lhs, 
87                           const Iterator<T>& rhs)
88    {
89      return lhs.equal(rhs);
90    }
91
92    template <typename T>
93    inline bool operator!=(const Iterator<T>& lhs, 
94                           const Iterator<T>& rhs)
95    {
96      return !(lhs==rhs);
97    }
98
99
100    template <typename T>
101    inline bool operator<(const Iterator<T>& lhs, 
102                          const Iterator<T>& rhs)
103    {
104      return lhs<rhs;
105    }
106
107
108    template <typename T>
109    inline bool operator<=(const Iterator<T>& lhs, 
110                           const Iterator<T>& rhs)
111    {
112      return !(rhs<lhs);
113    }
114
115
116    template <typename T>
117    inline bool operator>(const Iterator<T>& lhs, 
118                          const Iterator<T>& rhs)
119    {
120      return rhs<lhs;
121    }
122
123
124    template <typename T>
125    inline bool operator>=(const Iterator<T>& lhs, 
126                           const Iterator<T>& rhs)
127    {
128      return !(lhs<rhs);
129    }
130
131
132}}} // of namespace utility, yat, and theplu
133
134#endif
Note: See TracBrowser for help on using the repository browser.