File: Safe_circulator_from_iterator.h

package info (click to toggle)
cgal 4.0-5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 65,068 kB
  • sloc: cpp: 500,870; ansic: 102,544; sh: 321; python: 92; makefile: 75; xml: 2
file content (217 lines) | stat: -rw-r--r-- 7,333 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (c) 1997, 2007 
// GeometryFactory (France),
// Utrecht University (The Netherlands),
// ETH Zurich (Switzerland),
// INRIA Sophia-Antipolis (France),
// Max-Planck-Institute Saarbruecken (Germany),
// and Tel-Aviv University (Israel).  All rights reserved.
//
// This file is part of CGAL (www.cgal.org); you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 3 of the License,
// or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/next/Circulator/include/CGAL/Safe_circulator_from_iterator.h $
// $Id: Safe_circulator_from_iterator.h 67093 2012-01-13 11:22:39Z lrineau $
// 
//
// Author(s)     : Lutz Kettner  <kettner@inf.ethz.ch>
//                 Fernando Cacciola <fernando.cacciola@geometryfactory.com>
// 

#ifndef CGAL_SAFE_CIRCULATOR_FROM_ITERATOR_H
#define CGAL_SAFE_CIRCULATOR_FROM_ITERATOR_H

#include "CGAL/circulator.h"

#include "boost/optional.hpp"

namespace CGAL {


// Note: Tt, Ss, and Dd are here for backwards compatibility, they are
// not used.
template < class  I, class Tt = int, class Ss = int, class Dd = int>
class Safe_circulator_from_iterator {
public:
// TYPES

    typedef Safe_circulator_from_iterator<I,Tt,Ss,Dd> Self;
    typedef Circulator_from_iterator<I,Tt,Ss,Dd>      Unsafe;
    typedef I                                         iterator;
    typedef std::iterator_traits<iterator>            Traits;

    typedef typename Traits::value_type               value_type;
    typedef std::size_t                               size_type;
    typedef typename Traits::difference_type          difference_type;
    typedef typename Traits::reference                reference;
    typedef typename Traits::pointer                  pointer;

    typedef typename Traits::iterator_category           Icategory;
    typedef I_Circulator_from_iterator_traits<Icategory> CTraits;
    typedef typename CTraits::iterator_category          iterator_category;

private:

    boost::optional<I> m_begin;
    boost::optional<I> m_end;
    boost::optional<I> m_current;
    bool m_empty;

public:
// CREATION

    Safe_circulator_from_iterator() : m_begin(),
                                 m_end(),
                                 m_current(),
				 m_empty( true)
  {}

    Safe_circulator_from_iterator( const I& bgn, const I& end)
        : m_begin(bgn), m_end(end), m_current(bgn), m_empty(bgn==end) {}

    Safe_circulator_from_iterator( const I& bgn, const I& end, const I& cur)
        : m_begin(bgn), m_end(end), m_current(cur), m_empty(bgn==end) {}

    Safe_circulator_from_iterator( const Self& c, const I& cur)
        : m_begin( c.m_begin), m_end( c.m_end), m_current(cur), m_empty(c.m_empty) {}


    template <class II, class A1, class A2, class A3>
    // Allow construction from Circulator_from_iterator with
    // assignment compatible iterator II:
    Safe_circulator_from_iterator(
        const Safe_circulator_from_iterator<II,A1,A2,A3>& ii)
    : m_begin( ii.m_begin), m_end( ii.m_end),
        m_current(ii.m_current), m_empty(ii.m_empty) {}

//
// OPERATIONS

    bool operator==( Nullptr_t p) const {
        CGAL_assertion( p == NULL);
        return m_empty;
    }
    bool operator!=( Nullptr_t p) const { return !(*this == p); }
    
    bool operator==( const Self& c) const
    {
      CGAL_assertion( is_not_singular() ) ;
      return current_iterator() == c.current_iterator();
    }
    
    bool operator!=( const Self& c) const { return !(*this == c); }
    
    reference  operator*() const {
        CGAL_assertion( is_not_singular() ) ;
        CGAL_assertion( current_iterator() != end() );
        return *current_iterator();
    }
    pointer  operator->() const {
        CGAL_assertion( is_not_singular() ) ;
        CGAL_assertion( current_iterator() != end() );
        return &(*current_iterator());
    }
    Self& operator++() {
        CGAL_assertion( is_not_singular() ) ;
        CGAL_assertion( current_iterator() != end() );
        ++current_iterator();
        if ( current_iterator() == end() )
            current_iterator() = begin();
        return *this;
    }
    Self  operator++(int) {
        Self tmp= *this;
        ++*this;
        return tmp;
    }
    Self& operator--() {
        CGAL_assertion( is_not_singular() ) ;
        CGAL_assertion( current_iterator() != end() );
        if ( current_iterator() == begin() )
            current_iterator() = end();
        --current_iterator();
        return *this;
    }
    Self  operator--(int) {
        Self tmp = *this;
        --*this;
        return tmp;
    }
    Self& operator+=( difference_type n) {
        CGAL_assertion( is_not_singular() ) ;
        CGAL_assertion( current_iterator() != end() );
        difference_type i    = current_iterator() - begin();
        difference_type size = end()              - begin();
        CGAL_assertion( i    >= 0);
        CGAL_assertion( size >= 0);
        i = non_negative_mod( i + n, size);
        CGAL_assertion( i >= 0);
        CGAL_assertion( i < size);
        current_iterator() = begin() + i;
        return *this;
    }
    Self  operator+( difference_type n) const {
        Self tmp = *this;
        return tmp += n;
    }
    Self& operator-=( difference_type n) { return operator+=( -n); }
    Self  operator-( difference_type n) const {
        Self tmp = *this;
        return tmp += -n;
    }
    difference_type  operator-( const Self& i) const {
        CGAL_assertion((begin() == i.begin()) && (end() == i.end()));
        return current_iterator() - i.current_iterator();
    }
    reference  operator[](difference_type n) const {
        Self tmp = *this;
        tmp += n;
        return tmp.operator*();
    }
    
    iterator const& begin()            const { return *m_begin;}
    iterator const& end()              const { return *m_end;}
    iterator const& current_iterator() const { return *m_current;}
    Self            min_circulator()   const { return Self( m_begin, m_end, m_begin, m_empty); }
    
    Unsafe unsafe_circulator() const
    {
      CGAL_assertion( is_not_singular() ) ;
      
      return Unsafe(begin(),end(),current_iterator());
    }
    
private:

    bool is_not_singular() const { return !!m_begin && !!m_end && !!m_current ; }
    
    iterator& begin()            { return *m_begin;}
    iterator& end()              { return *m_end;}
    iterator& current_iterator() { return *m_current;}


};

//template < class I, class  T, class Size, class Dist>
//I Circulator_from_iterator< I, T, Size, Dist>::null_iterator = I();

template < class D, class I, class  T, class Size, class Dist> inline
Safe_circulator_from_iterator< I, T, Size, Dist>
operator+( D n, const
    Safe_circulator_from_iterator< I, T, Size, Dist>& circ) {
    Safe_circulator_from_iterator< I, T, Size, Dist>
        tmp = circ;
    return tmp += Dist(n);
}

} //namespace CGAL

#endif // CGAL_CIRCULATOR_H //
// EOF //