MyraMath
Array1.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_ARRAY1_H
7 #define MYRAMATH_UTILITY_ARRAY1_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 #include <vector>
22 #include <iostream>
23 
24 
25 #ifdef MYRAMATH_ENABLE_CPP11
26 #include <initializer_list>
27 #endif
28 
29 namespace myra {
30 
32 template<class T> class Array1
33  {
34  public:
35 
38  { I = 0; }
39 
41  explicit Array1(int in_I, T filler)
42  {
43  I = in_I;
44  contents.resize(I,filler);
45  }
46 
47 #ifdef MYRAMATH_ENABLE_CPP11
48 
50  Array1(std::initializer_list<T> list)
51  {
52  I = ssize(list);
53  contents.reserve(I);
54  const T* p = list.begin();
55  for (int i = 0; i < I; ++i)
56  contents.push_back(*p++);
57  }
58 
59 #endif
60 
62  explicit Array1(InputStream& in)
63  { in >> I >> contents; }
64 
66  void write(OutputStream& out) const
67  { out << I << contents; }
68 
70  const T& operator () (int i) const
71  { return contents[i]; }
72  const T& operator [] (int i) const
73  { return contents[i]; }
74  T& operator () (int i)
75  { return contents[i]; }
76  T& operator [] (int i)
77  { return contents[i]; }
78 
80  void swap(Array1& that)
81  {
82  std::swap(this->I, that.I);
83  this->contents.swap(that.contents);
84  }
85 
87  int size() const
88  { return I; }
89 
90  private:
91 
92  // Size.
93  int I;
94 
95  // Numeric data.
96  typedef std::vector<T> Contents;
97  Contents contents;
98 
99  };
100 
102 template<class T> std::ostream& operator << (std::ostream& out, const Array1<T>& a)
103  {
104  int I = a.size();
105  out << "[ ";
106  for (int i = 0; i < I; ++i)
107  out << a(i) << " ";
108  out << "]";
109  return out;
110  }
111 
112 } // namespace myra
113 
114 #endif
int size() const
Size inspector.
Definition: Array1.h:87
void swap(Array1 &that)
Member swap.
Definition: Array1.h:80
Array1(std::initializer_list< T > list)
Constructs from std::initializer_list<T>
Definition: Array1.h:50
void write(OutputStream &out) const
Writes to OutputStream.
Definition: Array1.h:66
ReaderWriter<T>, encapsulates a read()/write() pair for type T.
Definition: syntax.dox:1
Array1(int in_I, T filler)
Constructs with specified size and filler.
Definition: Array1.h:41
Abstraction layer, serializable objects write themselves to these.
Definition: Streams.h:39
Abstraction layer, deserializable objects read themselves from these.
Definition: Streams.h:47
Array1(InputStream &in)
InputStream constructor.
Definition: Array1.h:62
Container of values, allows random (i) access.
Definition: Array1.h:32
Bases classes for binary input/output streams.
Array1()
Default constructs to 0 size.
Definition: Array1.h:37
const T & operator()(int i) const
Random accessor/mutator.
Definition: Array1.h:70