MyraMath
intRange.h
Go to the documentation of this file.
1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
6 #ifndef MYRAMATH_DENSE_INTRANGE_H
7 #define MYRAMATH_DENSE_INTRANGE_H
8 
14 #include <myramath/utility/detail/LIBPUBLIC.h>
15 
16 #include <iosfwd>
17 #include <utility>
18 #include <vector>
19 #ifdef MYRAMATH_ENABLE_CPP11
20 #include <initializer_list>
21 #endif
22 
23 
24 namespace myra {
25 
26 // Forward declarations.
27 template<class T> class Array1;
28 class OutputStream;
29 class intRange;
30 class intCRange;
31 
33 class LIBPUBLIC intRange
34  {
35  public:
36 
38  int* begin;
40  int N;
42 
43  // Useful typedef.
44  typedef std::pair<intRange,intRange> Pair;
45 
46  // ------------------------------------ Construction and range semantics.
47 
49  intRange();
50 
52  intRange(int* in_begin, int in_N);
53 
55  intRange(std::vector<int>& v);
56 
58  void write(OutputStream& out) const;
59 
60  // ------------------------------------ Size queries.
61 
63  int size() const;
64 
66  int n_words() const;
67 
68  // ------------------------------------ General slicing.
69 
71  intCRange add_const() const;
72 
74  operator intCRange() const;
75 
77  intRange window(int n0, int n1) const;
78 
80  Array1<intRange> windows(const intCRange& n) const;
81 
83  int& operator() (int n) const;
84  int& operator[] (int n) const;
85 
87  int& at(int n) const;
88 
89  // ------------------------------------ Vector-like slicing.
90 
92  intRange first(int n) const;
93 
95  intRange cut_first(int n) const;
96 
98  Pair split_first(int n) const;
99 
101  intRange last(int n) const;
102 
104  intRange cut_last(int n) const;
105 
107  Pair split_last(int n) const;
108 
110  std::vector<int> make_vector() const;
111 
112  // ------------------------------------ Mutating underlying contents.
113 
115  // Functor f should be a have an operator(), that takes an int and returns an int.
116  template<class Functor> void transform(const Functor& f) const
117  {
118  for (int n = 0; n < N; ++n)
119  *pointer(n) = f( *pointer(n) );
120  }
121 
123  void assign(const intCRange& that) const;
124 
126  void zero() const;
127 
128  private:
129 
130  // Returns pointer to n'th entry.
131  int* pointer(int n) const;
132 
133  // Throws if that is not the same size as *this.
134  void check_size(const intCRange& that) const;
135 
136  // Throws if S is not the same size as *this.
137  void check_size(int S) const;
138 
139  };
140 
142 class LIBPUBLIC intCRange
143  {
144  public:
145 
147  const int* begin;
149  int N;
151 
152  // Useful typedef.
153  typedef std::pair<intCRange,intCRange> Pair;
154 
155  // ------------------------------------ Construction and range semantics.
156 
158  intCRange();
159 
161  intCRange(const int* in_begin, int in_N);
162 
164  intCRange(const std::vector<int>& v);
165 
166 #ifdef MYRAMATH_ENABLE_CPP11
167  intCRange(std::initializer_list<int> l);
169 #endif
170 
172  void write(OutputStream& out) const;
173 
174  // ------------------------------------ Size queries.
175 
177  int size() const;
178 
180  int n_words() const;
181 
182  // ------------------------------------ General slicing.
183 
185  intRange remove_const() const;
186 
188  intCRange window(int n0, int n1) const;
189 
191  Array1<intCRange> windows(const intCRange& n) const;
192 
194  const int& operator() (int n) const;
195  const int& operator[] (int n) const;
196 
198  const int& at(int n) const;
199 
200  // ------------------------------------ Vector-like slicing.
201 
203  intCRange first(int n) const;
204 
206  intCRange cut_first(int n) const;
207 
209  Pair split_first(int n) const;
210 
212  intCRange last(int n) const;
213 
215  intCRange cut_last(int n) const;
216 
218  Pair split_last(int n) const;
219 
221  std::vector<int> make_vector() const;
222 
223  private:
224 
225  // Returns pointer to n'th entry.
226  const int* pointer(int n) const;
227 
228  };
229 
231 LIBPUBLIC Array1<intCRange> add_const(const Array1<intRange>& ranges);
232 
234 LIBPUBLIC Array1<intRange> remove_const(const Array1<intCRange>& ranges);
235 
237 LIBPUBLIC std::ostream& operator << (std::ostream& out, const intCRange& r);
238 
240 std::vector<int> cumsum(const intCRange& input);
241 
242 } // namespace myra
243 
244 #endif
std::vector< T > cumsum(const std::vector< T > &v)
Returns cumulative sum. For example, cumsum({4,8,9,13}) = {0,4,12,21,34}.
Definition: cumsum.h:19
int N
---------— Data members, all public ----------------—
Definition: intRange.h:40
Definition: syntax.dox:1
void transform(const Functor &f) const
Overwrites every x(n) in this intRange with f(x(n)).
Definition: intRange.h:116
Abstraction layer, serializable objects write themselves to these.
Definition: Streams.h:39
Represents a mutable intRange.
Definition: intRange.h:33
int N
---------— Data members, all public ----------------—
Definition: intRange.h:149
Container of values, allows random (i) access.
Definition: Array1.h:32
Represents a const intRange.
Definition: intRange.h:142