FlexELA
Loading...
Searching...
No Matches
element.h
1#ifndef ELEMENT_H
2#define ELEMENT_H
3
4#include <assert.h>
5#include <cmath>
6#include <limits>
7
8#include "types.h"
9
10namespace svec {
11
23struct Element {
29
35
37 constexpr operator Value() const
38 {
39 return v;
40 }
41
43 Element& operator+=(const Value& rhs);
45 Element& operator+=(const Element& rhs);
46
48 Element& operator-=(const Value& rhs);
50 Element& operator-=(const Element& rhs);
51
53 Element& operator*=(const Value& rhs);
55 Element& operator/=(const Value& rhs);
56
60 bool isEnd() const;
61
62 friend constexpr Element operator+(const Element& a, const Element& b);
63 friend constexpr Element operator-(const Element& a, const Element& b);
64 friend constexpr Element operator*(const Element& a, const Value& C);
65 friend constexpr Element fma(const Element& a, const Value& C, const Element& b);
66};
67
82constexpr Element operator+(const Element& a, const Element& b)
83{
84 assert(a.l == b.l);
85 return {a.l, a.v + b.v};
86}
87
102constexpr Element operator-(const Element& a, const Element& b)
103{
104 assert(a.l == b.l);
105 return {a.l, a.v - b.v};
106}
107
120constexpr Element operator*(const Element& a, const Value& C)
121{
122 return {a.l, a.v * C};
123}
124
140constexpr Element fma(const Element& a, const Value& C, const Element& b)
141{
142 assert(a.l == b.l);
143 return {a.l, std::fma(a.v, C, b.v)};
144}
145
152constexpr Element END_ELEMENT = {std::numeric_limits<Label>::max(), 0.0};
153
155{
156 this->v += rhs;
157 return *this;
158};
160{
161 assert(this->l == rhs.l);
162 return operator+=(rhs.v);
163};
164
166{
167 this->v -= rhs;
168 return *this;
169};
171{
172 assert(this->l == rhs.l);
173 return operator-=(rhs.v);
174};
175
177{
178 this->v *= rhs;
179 return *this;
180};
182{
183 this->v /= rhs;
184 return *this;
185}
186
187inline bool Element::isEnd() const
188{
189 // checking the label is sufficient
190 return this->l == END_ELEMENT.l;
191}
192
193} // namespace svec
194
195#endif
For sparse vector containers and operations.
Definition element.h:10
double Value
Definition types.h:21
constexpr Element fma(const Element &a, const Value &C, const Element &b)
Fused Multiply and Add, C*a+b.
Definition element.h:140
unsigned int Label
Definition types.h:15
constexpr Element operator-(const Element &a, const Element &b)
Subtraction, a-b.
Definition element.h:102
constexpr Element operator*(const Element &a, const Value &C)
Multiplication, C*a.
Definition element.h:120
constexpr Element operator+(const Element &a, const Element &b)
Addition, a+b.
Definition element.h:82
Contains the label and value of a vector element.
Definition element.h:23
bool isEnd() const
Definition element.h:187
Element & operator-=(const Value &rhs)
Subtraction assignment.
Definition element.h:165
Element & operator+=(const Value &rhs)
Addition assignment.
Definition element.h:154
friend constexpr Element operator-(const Element &a, const Element &b)
Subtraction, a-b.
Definition element.h:102
Element & operator*=(const Value &rhs)
Multiplication assignment.
Definition element.h:176
friend constexpr Element fma(const Element &a, const Value &C, const Element &b)
Fused Multiply and Add, C*a+b.
Definition element.h:140
Value v
The value of the vector element.
Definition element.h:34
Element & operator/=(const Value &rhs)
Division assignment.
Definition element.h:181
Label l
The label of the vector element.
Definition element.h:28
friend constexpr Element operator*(const Element &a, const Value &C)
Multiplication, C*a.
Definition element.h:120
friend constexpr Element operator+(const Element &a, const Element &b)
Addition, a+b.
Definition element.h:82
Define types used be svec namespace.