Nsound  0.9.4
CircularIterators.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: CircularIterators.h 916 2015-08-22 16:31:39Z weegreenblobbie $
4 //
5 // Nsound is a C++ library and Python module for audio synthesis featuring
6 // dynamic digital filters. Nsound lets you easily shape waveforms and write
7 // to disk or plot them. Nsound aims to be as powerful as Csound but easy to
8 // use.
9 //
10 // Copyright (c) 2004-Present Nick Hilton
11 //
12 // weegreenblobbie_yahoo_com (replace '_' with '@' and '.')
13 //
14 //-----------------------------------------------------------------------------
15 
16 //-----------------------------------------------------------------------------
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 // This program is distributed in the hope that it will be useful,
24 // but WITHOUT ANY WARRANTY; without even the implied warranty of
25 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 // GNU Library General Public License for more details.
27 //
28 // You should have received a copy of the GNU General Public License
29 // along with this program; if not, write to the Free Software
30 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 //
32 //-----------------------------------------------------------------------------
33 #ifndef _NSOUND_CIRCULAR_ITERATORS_H_
34 #define _NSOUND_CIRCULAR_ITERATORS_H_
35 
36 #include <Nsound/Nsound.h>
37 
38 #include <iterator>
39 #include <vector>
40 
41 namespace Nsound
42 {
43 
44 typedef std::vector< uint32 > Uint32Vector;
45 typedef std::vector< float64 > FloatVector;
46 
48 //
64 {
65 
66 public:
67 
69  :
70  end_(v.end()),
71  itor_(v.begin()),
72  data_(&v)
73  {}
74 
76  :
77  end_(copy.end_),
78  itor_(copy.itor_),
79  data_(copy.data_)
80  {}
81 
83  {
84  if(this == &rhs) return *this;
85 
86  end_ = rhs.end_;
87  itor_ = rhs.itor_;
88  data_ = rhs.data_;
89 
90  return *this;
91  }
92 
93  void reset() { itor_ = data_->begin(); }
94 
95  bool operator == (const circular_iterator & rhs) const
96  {
97  return
98  data_ == rhs.data_ &&
99  itor_ == rhs.itor_;
100  }
101 
102  bool operator != (const circular_iterator & rhs) const
103  {
104  return
105  data_ != rhs.data_ ||
106  itor_ != rhs.itor_;
107  }
108 
110  {
111  ++itor_;
112 
113  if(itor_ >= end_) itor_ = data_->begin();
114 
115  return *this;
116  }
117 
119  {
120  if(itor_ == data_->begin()) itor_ = data_->end() - 1;
121  else --itor_;
122 
123  return *this;
124  }
125 
127  {
128  circular_iterator tmp(*this);
129  operator++();
130  return tmp;
131  }
132 
134  {
135  if(i < 0) return *this -= -i;
136  auto pos = std::distance(data_->begin(), itor_);
137  pos = (pos + i) % data_->size();
138  itor_ = data_->begin() + pos;
139  return *this;
140  }
141 
143  {
144  if(i < 0) return *this += -i;
145  auto pos = std::distance(data_->begin(), itor_);
146  pos -= (i % data_->size());
147  if(pos < 0) pos += data_->size();
148  itor_ = data_->begin() + pos;
149  return *this;
150  }
151 
152  FloatVector::value_type & operator*()
153  {
154  return *itor_;
155  }
156 
158  {
159  return *itor_;
160  }
161 
162  std::string __str__() const
163  {
164  std::stringstream ss;
165 
166  ss
167  << "Nsound::Buffer::circular_iterator(vector at 0x0"
168  << std::hex
169  << reinterpret_cast<uint64>(data_)
170  << ", index = ";
171 
172  uint64 pos = itor_ - data_->begin();
173 
174  ss << std::dec << pos << ")";
175 
176  return ss.str();
177  }
178 
179 private:
180 
181  FloatVector::const_iterator end_;
182  FloatVector::iterator itor_;
184 };
185 
186 
187 inline
189 {
190  circular_iterator tmp(lhs);
191 
192  tmp += rhs;
193 
194  return tmp;
195 }
196 
197 
198 inline
200 {
201  circular_iterator tmp(lhs);
202 
203  tmp -= rhs;
204 
205  return tmp;
206 }
207 
208 
209 // Maybe one day I'll figure out how to define only one class for both
210 // const and non-const.
211 
213 {
214 public:
215 
217  :
218  end_(v.end()),
219  itor_(v.begin()),
220  data_(const_cast<FloatVector*>(&v))
221  {}
222 
224  :
225  end_(copy.end_),
226  itor_(copy.itor_),
227  data_(copy.data_)
228  {}
229 
231  {
232  if(this == &rhs) return *this;
233 
234  end_ = rhs.end_;
235  itor_ = rhs.itor_;
236  data_ = rhs.data_;
237 
238  return *this;
239  }
240 
241  void reset() { itor_ = data_->begin(); }
242 
243  bool operator == (const const_circular_iterator & rhs) const
244  {
245  return
246  data_ == rhs.data_ &&
247  itor_ == rhs.itor_;
248  }
249 
250  bool operator != (const const_circular_iterator & rhs) const
251  {
252  return
253  data_ != rhs.data_ ||
254  itor_ != rhs.itor_;
255  }
256 
258  {
259  ++itor_;
260 
261  if(itor_ == end_) itor_ = data_->begin();
262 
263  return *this;
264  }
265 
267  {
268  if(itor_ == data_->begin()) itor_ = data_->end() - 1;
269  else --itor_;
270 
271  return *this;
272  }
273 
275  {
276  const_circular_iterator tmp(*this);
277  operator++();
278  return tmp;
279  }
280 
282  {
283  if(i < 0) return *this -= -i;
284  auto pos = std::distance(const_cast<const FloatVector *>(data_)->begin(), itor_);
285  pos = (pos + i) % data_->size();
286  itor_ = data_->begin() + pos;
287  return *this;
288  }
289 
291  {
292  if(i < 0) return *this += -i;
293  auto pos = std::distance(const_cast<const FloatVector *>(data_)->begin(), itor_);
294  pos -= (i % data_->size());
295  if(pos < 0) pos += data_->size();
296  itor_ = data_->begin() + pos;
297  return *this;
298  }
299 
300  const FloatVector::value_type & operator*()
301  {
302  return *itor_;
303  }
304 
306  {
307  return *itor_;
308  }
309 
310 private:
311 
312  FloatVector::const_iterator end_;
313  FloatVector::const_iterator itor_;
315 };
316 
317 
318 inline
320 {
321  const_circular_iterator tmp(lhs);
322 
323  tmp += rhs;
324 
325  return tmp;
326 }
327 
328 
329 inline
331 {
332  const_circular_iterator tmp(lhs);
333 
334  tmp -= rhs;
335 
336  return tmp;
337 }
338 
339 } // namespace
340 
341 // :mode=c++: jEdit modeline
342 #endif
const_circular_iterator & operator++()
FloatVector::const_iterator itor_
AudioStream operator-(const AudioStream &lhs, const AudioStream &rhs)
Definition: AudioStream.h:635
const_circular_iterator operator++(int)
const_circular_iterator & operator=(const const_circular_iterator &rhs)
FloatVector::const_iterator end_
AudioStream operator+(const AudioStream &lhs, const AudioStream &rhs)
Definition: AudioStream.h:629
const_circular_iterator & operator-=(int32 i)
circular_iterator(const circular_iterator &copy)
std::vector< float64 > FloatVector
circular_iterator & operator--()
const_circular_iterator(const const_circular_iterator &copy)
A circulator iterator for class Buffer.
FloatVector::value_type & operator*()
circular_iterator operator++(int)
bool operator==(const const_circular_iterator &rhs) const
double float64
Definition: Nsound.h:146
const_circular_iterator & operator+=(int32 i)
std::string __str__() const
bool operator==(const circular_iterator &rhs) const
FloatVector::const_iterator end_
FloatVector::iterator itor_
circular_iterator(FloatVector &v)
unsigned long long uint64
Definition: Nsound.h:154
std::vector< uint32 > Uint32Vector
bool operator!=(const const_circular_iterator &rhs) const
const_circular_iterator & operator--()
const FloatVector::value_type & operator*()
signed int int32
Definition: Nsound.h:142
circular_iterator & operator=(const circular_iterator &rhs)
circular_iterator & operator++()
circular_iterator & operator-=(int32 i)
bool operator!=(const circular_iterator &rhs) const
const_circular_iterator(const FloatVector &v)
circular_iterator & operator+=(int32 i)