Code

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