Subversion Repositories Scribus

Rev

Rev 15347 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
15347 fschmid 1
/*
2
 * ciculator.h
12065 fschmid 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
18122 mrdocs 16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
12065 fschmid 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
 */
30
 
31
#ifndef SEEN_Circulator_H
32
#define SEEN_Circulator_H
33
 
34
#include <iterator>
35
 
36
namespace Geom {
37
 
38
template <typename Iterator>
39
class Circulator {
40
public:
41
    typedef std::random_access_iterator_tag iterator_category;
42
    typedef typename std::iterator_traits<Iterator>::value_type value_type;
43
    typedef typename std::iterator_traits<Iterator>::difference_type difference_type;
44
    typedef typename std::iterator_traits<Iterator>::pointer pointer;
45
    typedef typename std::iterator_traits<Iterator>::reference reference;
46
 
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(iterator_category(first));
53
    }
54
 
55
    reference operator*() const {
56
        return *_pos;
57
    }
58
    pointer operator->() const {
59
        return &*_pos;
60
    }
61
 
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
    }
73
 
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
    }
84
 
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
    }
99
 
100
    difference_type operator-(Circulator const &other) {
101
        return _pos - other._pos;
102
    }
103
 
104
    reference operator[](int n) const {
105
        return *_offset(n);
106
    }
107
 
108
private:
109
    void match_random_access(iterator_category) {}
110
 
111
    Iterator _offset(int n) {
112
        difference_type range=( _last - _first );
113
        difference_type offset=( _pos - _first + n );
114
 
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
    }
123
 
124
    Iterator _first;
125
    Iterator _last;
126
    Iterator _pos;
127
};
128
 
129
}
130
 
131
template <typename T>
132
Geom::Circulator<T> operator+(int n, Geom::Circulator<T> const &c) {
133
    return c + n;
134
}
135
 
136
#endif // SEEN_Circulator_H
137
 
138
/*
139
  Local Variables:
140
  mode:c++
141
  c-file-style:"stroustrup"
15347 fschmid 142
  c-file-offsets:((innamespace . 0)(substatement-open . 0))
12065 fschmid 143
  indent-tabs-mode:nil
15347 fschmid 144
  c-brace-offset:0
12065 fschmid 145
  fill-column:99
146
  End:
15347 fschmid 147
  vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
12065 fschmid 148
*/
15347 fschmid 149