DGtal  0.9.2
InputIteratorWithRankOnSequence.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file InputIteratorWithRankOnSequence.ih
19  * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
20  * Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
21  *
22  * @date 2012/04/02
23  *
24  * Implementation of inline methods defined in InputIteratorWithRankOnSequence.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 //////////////////////////////////////////////////////////////////////////////
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
40 
41 //-----------------------------------------------------------------------------
42 template <typename TSequence, typename TRank>
43 inline
44 DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::
45 ~InputIteratorWithRankOnSequence()
46 {
47 }
48 //-----------------------------------------------------------------------------
49 template <typename TSequence, typename TRank>
50 inline
51 DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::
52 InputIteratorWithRankOnSequence( const Sequence & seq, ConstIterator it )
53  : mySequence( new Sequence( seq ) ), myIterator( it )
54 {
55 }
56 //-----------------------------------------------------------------------------
57 template <typename TSequence, typename TRank>
58 inline
59 DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::
60 InputIteratorWithRankOnSequence( Sequence* ptrSeq, ConstIterator it )
61  : mySequence( ptrSeq ), myIterator( it )
62 {
63 }
64 //-----------------------------------------------------------------------------
65 template <typename TSequence, typename TRank>
66 inline
67 DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::
68 InputIteratorWithRankOnSequence
69 ( const CountedPtr<Sequence> & ptrSeq, ConstIterator it )
70  : mySequence( ptrSeq ), myIterator( it )
71 {
72 }
73 //-----------------------------------------------------------------------------
74 template <typename TSequence, typename TRank>
75 inline
76 DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::
77 InputIteratorWithRankOnSequence( const Self & other )
78  : mySequence( other.mySequence ), myIterator( other.myIterator )
79 {
80 }
81 //-----------------------------------------------------------------------------
82 template <typename TSequence, typename TRank>
83 inline
84 typename DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::Self &
85 DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::
86 operator= ( const Self & other )
87 {
88  if ( this != &other )
89  {
90  mySequence = other.mySequence;
91  myIterator = other.myIterator;
92  }
93  return *this;
94 }
95 //-----------------------------------------------------------------------------
96 template <typename TSequence, typename TRank>
97 inline
98 typename DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::Value
99 DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::
100 operator*() const
101 {
102  return std::make_pair( *myIterator, (Rank) ( myIterator - mySequence->begin() ) );
103 }
104 //-----------------------------------------------------------------------------
105 template <typename TSequence, typename TRank>
106 inline
107 typename DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::Pointer
108 DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::
109 operator->() const
110 {
111  myTmpValue = this->operator*();
112  return &myTmpValue;
113 }
114 //-----------------------------------------------------------------------------
115 template <typename TSequence, typename TRank>
116 inline
117 typename DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::Self &
118 DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::
119 operator++()
120 {
121  ++myIterator;
122  return *this;
123 }
124 //-----------------------------------------------------------------------------
125 template <typename TSequence, typename TRank>
126 inline
127 typename DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::Self
128 DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::
129 operator++( int )
130 {
131  Self tmp = *this;
132  ++myIterator;
133  return tmp;
134 }
135 //-----------------------------------------------------------------------------
136 template <typename TSequence, typename TRank>
137 inline
138 bool
139 DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::
140 operator==( const Self & other ) const
141 {
142  if ( mySequence.get() != 0 )
143  {
144  if ( other.mySequence.get() != 0 )
145  return ( myIterator == other.myIterator );
146  else
147  return ( myIterator == mySequence->end() );
148  }
149  else
150  {
151  if ( other.mySequence.get() != 0 )
152  return ( other.myIterator == other.mySequence->end() );
153  else
154  return true;
155  }
156 }
157 //-----------------------------------------------------------------------------
158 template <typename TSequence, typename TRank>
159 inline
160 bool
161 DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::
162 operator!=( const Self & other ) const
163 {
164  return ! this->operator==( other );
165 }
166 
167 ///////////////////////////////////////////////////////////////////////////////
168 // Interface - public :
169 
170 /**
171  * Writes/Displays the object on an output stream.
172  * @param out the output stream where the object is written.
173  */
174 template <typename TSequence, typename TRank>
175 inline
176 void
177 DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::
178 selfDisplay ( std::ostream & out ) const
179 {
180  out << "[InputIteratorWithRankOnSequence]";
181 }
182 
183 /**
184  * Checks the validity/consistency of the object.
185  * @return 'true' if the object is valid, 'false' otherwise.
186  */
187 template <typename TSequence, typename TRank>
188 inline
189 bool
190 DGtal::InputIteratorWithRankOnSequence<TSequence,TRank>::
191 isValid() const
192 {
193  return true;
194 }
195 
196 
197 
198 ///////////////////////////////////////////////////////////////////////////////
199 // Implementation of inline functions //
200 
201 template <typename TSequence, typename TRank>
202 inline
203 std::ostream&
204 DGtal::operator<< ( std::ostream & out,
205  const InputIteratorWithRankOnSequence<TSequence,TRank> & object )
206 {
207  object.selfDisplay( out );
208  return out;
209 }
210 
211 // //
212 ///////////////////////////////////////////////////////////////////////////////
213 
214