Reference documentation for deal.II version 9.0.0
linear_index_iterator.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2018 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_linear_index_iterator_h
17 #define dealii_linear_index_iterator_h
18 
19 #include <deal.II/base/config.h>
20 #include <deal.II/base/exceptions.h>
21 
22 
23 DEAL_II_NAMESPACE_OPEN
134 template <class DerivedIterator, class AccessorType>
136 {
137 public:
141  typedef std::random_access_iterator_tag iterator_category;
142 
147  typedef
148  AccessorType value_type;
149 
153  typedef std::ptrdiff_t difference_type;
154 
158  typedef const value_type &reference;
159 
163  typedef const value_type *pointer;
164 
168  typedef typename value_type::size_type size_type;
169 
173  DerivedIterator &
174  operator = (const DerivedIterator &it);
175 
179  DerivedIterator &operator ++ ();
180 
184  DerivedIterator operator ++ (int);
185 
189  DerivedIterator &operator -- ();
190 
194  DerivedIterator operator -- (int);
195 
199  DerivedIterator operator + (const difference_type n) const;
200 
204  DerivedIterator operator - (const difference_type n) const;
205 
209  DerivedIterator &operator += (const difference_type n);
210 
214  DerivedIterator &operator -= (const difference_type n);
215 
222  difference_type operator - (const DerivedIterator &p) const;
223 
227  reference operator * () const;
228 
232  pointer operator -> () const;
233 
238  bool operator == (const DerivedIterator &) const;
239 
243  bool operator != (const DerivedIterator &) const;
244 
252  bool operator <= (const DerivedIterator &) const;
253 
261  bool operator >= (const DerivedIterator &) const;
262 
270  bool operator < (const DerivedIterator &) const;
271 
276  bool operator > (const DerivedIterator &) const;
277 
278 protected:
282  LinearIndexIterator () = default;
283 
287  LinearIndexIterator (const AccessorType accessor);
288 
289 protected:
293  AccessorType accessor;
294 };
295 
296 
297 
298 template <class DerivedIterator, class AccessorType>
299 inline
300 DerivedIterator &
302 {
303  accessor.container = it.container;
304  accessor.linear_index = it.linear_index;
305  return static_cast<DerivedIterator &>(*this);
306 }
307 
308 
309 
310 template <class DerivedIterator, class AccessorType>
311 inline
312 DerivedIterator &
314 {
315  return operator+=(1);
316 }
317 
318 
319 
320 template <class DerivedIterator, class AccessorType>
321 inline
322 DerivedIterator
324 {
325  const DerivedIterator copy(this->accessor);
326  operator+=(1);
327  return copy;
328 }
329 
330 
331 
332 template <class DerivedIterator, class AccessorType>
333 inline
334 DerivedIterator &
336 {
337  return operator+=(-1);
338 }
339 
340 
341 
342 template <class DerivedIterator, class AccessorType>
343 inline
344 DerivedIterator
346 {
347  const DerivedIterator copy(this->accessor);
348  operator+=(-1);
349  return copy;
350 }
351 
352 
353 
354 template <class DerivedIterator, class AccessorType>
355 inline
356 DerivedIterator
359 {
360  DerivedIterator copy(this->accessor);
361  copy += n;
362  return copy;
363 }
364 
365 
366 
367 template <class DerivedIterator, class AccessorType>
368 inline
369 DerivedIterator
372 {
373  DerivedIterator copy(this->accessor);
374  copy += -n;
375  return copy;
376 }
377 
378 
379 
380 template <class DerivedIterator, class AccessorType>
381 inline
382 DerivedIterator &
384 {
385  accessor.linear_index += n;
386  return static_cast<DerivedIterator &>(*this);
387 }
388 
389 
390 
391 template <class DerivedIterator, class AccessorType>
392 inline
393 DerivedIterator &
395 {
396  return operator+=(-n);
397 }
398 
399 
400 
401 template <class DerivedIterator, class AccessorType>
402 inline
405 operator - (const DerivedIterator &other) const
406 {
407  Assert(this->accessor.container == other.accessor.container,
408  ExcMessage("Only iterators pointing to the same container can be compared."));
409  return this->accessor.linear_index - other.accessor.linear_index;
410 }
411 
412 
413 
414 template <class DerivedIterator, class AccessorType>
415 inline
418 {
419  return accessor;
420 }
421 
422 
423 
424 template <class DerivedIterator, class AccessorType>
425 inline
428 {
429  return &accessor;
430 }
431 
432 
433 
434 template <class DerivedIterator, class AccessorType>
435 inline
436 bool
438 operator == (const DerivedIterator &other) const
439 {
440  const auto &other_2 = static_cast<decltype(*this) &>(other);
441  return accessor.container == other_2.accessor.container &&
442  accessor.linear_index == other_2.accessor.linear_index;
443 }
444 
445 
446 
447 template <class DerivedIterator, class AccessorType>
448 inline
449 bool
451 operator != (const DerivedIterator &other) const
452 {
453  return !(*this == other);
454 }
455 
456 
457 
458 template <class DerivedIterator, class AccessorType>
459 inline
460 bool
462 operator <= (const DerivedIterator &other) const
463 {
464  return (*this == other) || (*this < other);
465 }
466 
467 
468 
469 template <class DerivedIterator, class AccessorType>
470 inline
471 bool
473 operator >= (const DerivedIterator &other) const
474 {
475  return !(*this < other);
476 }
477 
478 
479 
480 template <class DerivedIterator, class AccessorType>
481 inline
482 bool
484 operator < (const DerivedIterator &other) const
485 {
486  Assert(this->accessor.container == other.accessor.container,
487  ExcMessage("Only iterators pointing to the same container can be compared."));
488  return this->accessor.linear_index < other.accessor.linear_index;
489 }
490 
491 
492 
493 template <class DerivedIterator, class AccessorType>
494 inline
495 bool
497 operator > (const DerivedIterator &other) const
498 {
499  return other < *this;
500 }
501 
502 
503 
504 template <class DerivedIterator, class AccessorType>
505 inline
507 (const AccessorType accessor)
508  :
509  accessor(accessor)
510 {}
511 
512 
513 DEAL_II_NAMESPACE_CLOSE
514 
515 #endif
DerivedIterator & operator+=(const difference_type n)
bool operator<(const DerivedIterator &) const
DerivedIterator operator-(const difference_type n) const
DerivedIterator & operator=(const DerivedIterator &it)
bool operator!=(const DerivedIterator &) const
std::random_access_iterator_tag iterator_category
LinearIndexIterator()=default
static ::ExceptionBase & ExcMessage(std::string arg1)
DerivedIterator operator+(const difference_type n) const
bool operator>(const DerivedIterator &) const
bool operator<=(const DerivedIterator &) const
value_type::size_type size_type
const value_type & reference
#define Assert(cond, exc)
Definition: exceptions.h:1142
std::ptrdiff_t difference_type
bool operator==(const DerivedIterator &) const
DerivedIterator & operator++()
bool operator>=(const DerivedIterator &) const
const value_type * pointer
DerivedIterator & operator-=(const difference_type n)
DerivedIterator & operator--()
reference operator*() const