MyraMath
Array2.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_UTILITY_ARRAY2_H
7 #define MYRAMATH_UTILITY_ARRAY2_H
8 
14 #include <myramath/utility/detail/ssize.h>
15 
16 // Serialization interface.
17 #include <myramath/io/Streams.h>
19 #include <myramath/io/detail/vector.h>
20 
21 // Custom exceptions.
23 
24 #include <vector>
25 #include <iostream>
26 
27 namespace myra {
28 
30 template<class T> class Array2
31  {
32  public:
33 
36  { I = J = 0; }
37 
39  Array2(int in_I, int in_J, T filler)
40  {
41  I = in_I;
42  J = in_J;
43  contents.resize(I*J,filler);
44  }
45 
46 #ifdef MYRAMATH_ENABLE_CPP11
47 
49  Array2(int II, int JJ, std::initializer_list<T> list)
50  {
51  I = II;
52  J = JJ;
53  int IJ = ssize(list);
54  if (I*J != IJ)
55  throw eprintf("Array2(I,J,initializer_list<T>), size mismatch I*J != size [%d*%d != %d]", I, J, IJ);
56  contents.resize(IJ,T());
57  const T* p = list.begin();
58  for (int i = 0; i < I; ++i)
59  for (int j = 0; j < J; ++j)
60  (*this)(i,j) = *p++;
61  }
62 
63 #endif
64 
66  explicit Array2(InputStream& in)
67  { in >> I >> J >> contents; }
68 
70  void write(OutputStream& out) const
71  { out << I << J << contents; }
72 
74  const T& operator () (int i, int j) const
75  { return contents[offset(i,j)]; }
76  T& operator () (int i, int j)
77  { return contents[offset(i,j)]; }
78 
80  void swap(Array2& that)
81  {
82  std::swap(this->I, that.I);
83  std::swap(this->J, that.J);
84  this->contents.swap(that.contents);
85  }
86 
88  std::pair<int,int> size() const
89  { return std::pair<int,int>(I,J); }
90 
91  private:
92 
93  // Defines internal layout.
94  int offset(int i, int j) const
95  { return j*I+i; }
96 
97  // Size.
98  int I;
99  int J;
100 
101  // Numeric data.
102  typedef std::vector<T> Contents;
103  Contents contents;
104 
105  };
106 
108 template<class T> std::ostream& operator << (std::ostream& out, const Array2<T>& a)
109  {
110  int I = a.size().first;
111  int J = a.size().second;
112  out << I << " by " << J << ":" << std::endl;
113  for (int i = 0; i < I; ++i)
114  {
115  out << "[ ";
116  for (int j = 0; j < J; ++j)
117  out << a(i,j) << " ";
118  out << "]" << std::endl;
119  }
120  return out;
121  }
122 
123 } // namespace myra
124 
125 #endif
Container of values, allows random (i,j) access.
Definition: Array2.h:30
Returns a std::runtime_error() whose message has been populated using printf()-style formatting...
Array2(InputStream &in)
InputStream constructor.
Definition: Array2.h:66
const T & operator()(int i, int j) const
Random accessor/mutator.
Definition: Array2.h:74
ReaderWriter<T>, encapsulates a read()/write() pair for type T.
Definition: syntax.dox:1
Abstraction layer, serializable objects write themselves to these.
Definition: Streams.h:39
std::pair< int, int > size() const
Size inspector.
Definition: Array2.h:88
Abstraction layer, deserializable objects read themselves from these.
Definition: Streams.h:47
void write(OutputStream &out) const
Writes to OutputStream.
Definition: Array2.h:70
Array2(int in_I, int in_J, T filler)
Constructs with specified size and filler.
Definition: Array2.h:39
void swap(Array2 &that)
Member swap.
Definition: Array2.h:80
Array2()
Default constructs to 0x0 size.
Definition: Array2.h:35
Array2(int II, int JJ, std::initializer_list< T > list)
Constructs from std::initializer_list<T> in row-major order.
Definition: Array2.h:49
Bases classes for binary input/output streams.