Snowman  0.1.0
vector-wrapper.h
1 #pragma once
2 #include <cstdint>
3 #include <matrix-types.h>
4 #include <snowboy-debug.h>
5 #include <stdexcept>
6 #include <string>
7 
8 namespace snowboy {
9  struct MatrixBase;
10  class SubVector;
11  class VectorBase {
12  protected:
13  size_t m_size{0};
14  float* m_data{nullptr};
15 
16  public:
17  float* begin() const noexcept { return m_data; }
18  float* end() const noexcept { return m_data + m_size; }
19  size_t size() const noexcept { return m_size; }
20  float* data() const noexcept { return m_data; }
21  float& operator[](size_t index) const noexcept {
22  SNOWBOY_ASSERT(index < m_size);
23  return m_data[index];
24  }
25  float& operator()(size_t index) const noexcept {
26  SNOWBOY_ASSERT(index < m_size);
27  return m_data[index];
28  }
29  bool empty() const noexcept { return size() == 0; }
30 
31  void Add(float x) noexcept;
32  void AddDiagMat2(float, const MatrixBase&, MatrixTransposeType, float) noexcept;
33  void AddMatVec(float alpha, const MatrixBase& a, MatrixTransposeType trans, const VectorBase& x, float beta) noexcept;
34  void AddVec(float, const VectorBase&) noexcept;
35  void AddVec2(float, const VectorBase&) noexcept;
36  void ApplyFloor(float) noexcept;
37  void ApplyLog() noexcept;
38  void ApplyPow(float) noexcept;
39  float ApplySoftmax() noexcept;
40  void CopyColsFromMat(const MatrixBase&) noexcept;
41  void CopyFromVec(const VectorBase&) noexcept;
42  void CopyRowsFromMat(const MatrixBase&) noexcept;
43  float CosineDistance(const VectorBase&) const;
44  float DotVec(const VectorBase&) const;
45  float EuclideanDistance(const VectorBase&) const;
46  bool IsZero(float cutoff) const noexcept;
47  float Max() const noexcept;
48  float Max(int* e) const noexcept;
49  float Min() const noexcept;
50  float Min(int* e) const noexcept;
51  void MulElements(const VectorBase&) noexcept;
52  float Norm(float) const noexcept;
53  SubVector Range(size_t offset, size_t size) const noexcept;
54  void Scale(float) noexcept;
55  void Set(float) noexcept;
56  void SetRandomGaussian();
57  void SetRandomUniform();
58  float Sum() const noexcept;
59  void Write(bool, std::ostream*) const;
60 
61  bool HasNan() const noexcept;
62  bool HasInfinity() const noexcept;
63  };
64  class Vector : public VectorBase {
65  protected:
66  size_t m_cap{0};
67 
68  public:
69  Vector() noexcept {}
70  Vector(const VectorBase& other) {
71  Resize(other.size(), MatrixResizeType::kUndefined);
72  CopyFromVec(other);
73  }
74  Vector(const Vector& other) {
75  Resize(other.m_size, MatrixResizeType::kUndefined);
76  CopyFromVec(other);
77  }
78  Vector(Vector&& other) noexcept {
79  m_size = other.m_size;
80  m_cap = other.m_cap;
81  m_data = other.m_data;
82  other.m_data = nullptr;
83  other.m_size = 0;
84  other.m_cap = 0;
85  }
86 
87  size_t capacity() const noexcept { return m_cap; }
88  void Resize(size_t size, MatrixResizeType resize = MatrixResizeType::kSetZero);
89  ~Vector() noexcept;
90 
91  Vector& operator=(const Vector& other);
92  Vector& operator=(const VectorBase& other);
93  Vector& operator=(Vector&& other) noexcept {
94  Swap(&other);
95  return *this;
96  }
97 
98  void Read(bool, bool, std::istream*);
99  void Read(bool, std::istream*);
100  void Swap(Vector* other) noexcept;
101  void RemoveElement(size_t index) noexcept;
102 
103  static void PrintAllocStats(std::ostream&);
104  static void ResetAllocStats();
105  };
106  class SubVector : public VectorBase {
107  public:
108  SubVector(const VectorBase& parent, size_t offset, size_t size) noexcept;
109  SubVector(const MatrixBase& parent, size_t row) noexcept;
110  SubVector(const SubVector& other) noexcept {
111  m_data = other.m_data;
112  m_size = other.m_size;
113  }
114  };
115  template <size_t N>
116  class FixedVector : public VectorBase {
117  float m_storage[N];
118 
119  public:
120  FixedVector() noexcept {
121  m_data = m_storage;
122  }
123  FixedVector(const VectorBase& other) {
124  m_data = m_storage;
125  Resize(other.size(), MatrixResizeType::kUndefined);
126  CopyFromVec(other);
127  }
128  FixedVector(const FixedVector& other) {
129  m_data = m_storage;
130  Resize(other.m_size, MatrixResizeType::kUndefined);
131  CopyFromVec(other);
132  }
133  FixedVector(size_t size, MatrixResizeType resize = MatrixResizeType::kSetZero) {
134  m_data = m_storage;
135  Resize(size, resize);
136  }
137 
138  constexpr size_t capacity() const noexcept { return N; }
139  void Resize(size_t size, MatrixResizeType resize = MatrixResizeType::kSetZero) {
140  if (size > N) throw std::invalid_argument("new size exceeds fixed capacity");
141  m_size = size;
142  if (resize == MatrixResizeType::kSetZero) Set(0.0f);
143  }
144 
145  FixedVector& operator=(const FixedVector& other) {
146  Resize(other.m_size, MatrixResizeType::kUndefined);
147  CopyFromVec(other);
148  return *this;
149  }
150  FixedVector& operator=(const VectorBase& other) {
151  Resize(other.size(), MatrixResizeType::kUndefined);
152  CopyFromVec(other);
153  return *this;
154  }
155  };
156 } // namespace snowboy
Definition: vector-wrapper.h:116
Definition: vector-wrapper.h:106
Definition: vector-wrapper.h:11
Definition: vector-wrapper.h:64
Definition: matrix-wrapper.h:11