trase
ColumnIterator.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2018, University of Oxford.
3 All rights reserved.
4 
5 University of Oxford means the Chancellor, Masters and Scholars of the
6 University of Oxford, having an administrative office at Wellington
7 Square, Oxford OX1 2JD, UK.
8 
9 This file is part of trase.
10 
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright notice, this
14  list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16  this list of conditions and the following disclaimer in the documentation
17  and/or other materials provided with the distribution.
18 * Neither the name of the copyright holder nor the names of its
19  contributors may be used to endorse or promote products derived from
20  this software without specific prior written permission.
21 
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
35 
36 #ifndef COLUMNITERATOR_H_
37 #define COLUMNITERATOR_H_
38 
39 #include <vector>
40 
41 namespace trase {
42 
46 public:
47  using pointer = float const *;
48  using iterator_category = std::random_access_iterator_tag;
49  using reference = float const &;
50  using value_type = float const;
51  using difference_type = std::ptrdiff_t;
52 
53  ColumnIterator() = default;
54 
55  ColumnIterator(const std::vector<float>::const_iterator &p, const int stride)
56  : m_p(&(*p)), m_stride(stride) {}
57 
58  reference operator*() const { return dereference(); }
59 
60  reference operator->() const { return dereference(); }
61 
62  ColumnIterator &operator++() {
63  increment();
64  return *this;
65  }
66 
67  const ColumnIterator operator++(int) {
68  ColumnIterator tmp(*this);
69  operator++();
70  return tmp;
71  }
72 
73  ColumnIterator operator+(int n) const {
74  ColumnIterator tmp(*this);
75  tmp.increment(n);
76  return tmp;
77  }
78 
79  reference operator[](const int i) const { return operator+(i).dereference(); }
80 
81  size_t operator-(const ColumnIterator &start) const {
82  return (m_p - start.m_p) / m_stride;
83  }
84 
85  inline bool operator==(const ColumnIterator &rhs) const { return equal(rhs); }
86 
87  inline bool operator!=(const ColumnIterator &rhs) const {
88  return !operator==(rhs);
89  }
90 
91 private:
92  bool equal(ColumnIterator const &other) const { return m_p == other.m_p; }
93 
94  reference dereference() const { return *m_p; }
95 
96  void increment() { std::advance(m_p, m_stride); }
97 
98  void increment(const int n) { std::advance(m_p, n * m_stride); }
99 
100  pointer m_p;
101  int m_stride;
102 };
103 
104 } // namespace trase
105 
106 #endif // COLUMNITERATOR_H_
A const iterator that iterates through a single column of the raw data class Impliments an random acc...
Definition: ColumnIterator.hpp:45
Definition: Backend.cpp:39