1 /*
2 * ciculator.h
3 *
4 * Copyright 2006 MenTaLguY <mental@rydia.net>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it either under the terms of the GNU Lesser General Public
8 * License version 2.1 as published by the Free Software Foundation
9 * (the "LGPL") or, at your option, under the terms of the Mozilla
10 * Public License Version 1.1 (the "MPL"). If you do not alter this
11 * notice, a recipient may use your version of this file under either
12 * the MPL or the LGPL.
13 *
14 * You should have received a copy of the LGPL along with this library
15 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
19 *
20 * The contents of this file are subject to the Mozilla Public License
21 * Version 1.1 (the "License"); you may not use this file except in
22 * compliance with the License. You may obtain a copy of the License at
23 * http://www.mozilla.org/MPL/
24 *
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27 * the specific language governing rights and limitations.
28 *
29 */
31 #ifndef SEEN_Circulator_H
32 #define SEEN_Circulator_H
34 #include <iterator>
36 namespace Geom {
38 template <typename Iterator>
39 class Circulator {
40 public:
41 typedef std::random_access_iterator_tag std::iterator_category;
42 typedef std::iterator_traits<Iterator>::value_type value_type;
43 typedef std::iterator_traits<Iterator>::difference_type difference_type;
44 typedef std::iterator_traits<Iterator>::pointer pointer;
45 typedef std::iterator_traits<Iterator>::reference reference;
47 Circulator(Iterator const &first,
48 Iterator const &last,
49 Iterator const &pos)
50 : _first(first), _last(last), _pos(pos)
51 {
52 match_random_access(std::iterator_category(first));
53 }
55 reference operator*() const {
56 return *_pos;
57 }
58 pointer operator->() const {
59 return &*_pos;
60 }
62 Circulator &operator++() {
63 if ( _first == _last ) return *this;
64 ++_pos;
65 if ( _pos == _last ) _pos = _first;
66 return *this;
67 }
68 Circulator operator++(int) {
69 Circulator saved=*this;
70 ++(*this);
71 return saved;
72 }
74 Circulator &operator--() {
75 if ( _pos == _first ) _pos = _last;
76 --_pos;
77 return *this;
78 }
79 Circulator operator--(int) {
80 Circulator saved=*this;
81 --(*this);
82 return saved;
83 }
85 Circulator &operator+=(int n) {
86 _pos = _offset(n);
87 return *this;
88 }
89 Circulator operator+(int n) const {
90 return Circulator(_first, _last, _offset(n));
91 }
92 Circulator &operator-=(int n) {
93 _pos = _offset(-n);
94 return *this;
95 }
96 Circulator operator-(int n) const {
97 return Circulator(_first, _last, _offset(-n));
98 }
100 difference_type operator-(Circulator const &other) {
101 return _pos - other._pos;
102 }
104 reference operator[n] const {
105 return *_offset(n);
106 }
108 private:
109 void match_random_access(random_access_iterator_tag) {}
111 Iterator _offset(int n) {
112 difference_type range=( _last - _first );
113 difference_type offset=( _pos - _first + n );
115 if ( offset < 0 ) {
116 // modulus not well-defined for negative numbers in C++
117 offset += ( ( -offset / range ) + 1 ) * range;
118 } else if ( offset >= range ) {
119 offset %= range;
120 }
121 return _first + offset;
122 }
124 Iterator _first;
125 Iterator _last;
126 Iterator _pos;
127 };
129 }
131 template <typename T>
132 Geom::Circulator<T> operator+(int n, Geom::Circulator<T> const &c) {
133 return c + n;
134 }
136 #endif // SEEN_Circulator_H
138 /*
139 Local Variables:
140 mode:c++
141 c-file-style:"stroustrup"
142 c-file-offsets:((innamespace . 0)(substatement-open . 0))
143 indent-tabs-mode:nil
144 c-brace-offset:0
145 fill-column:99
146 End:
147 vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
148 */