weight.hpp
1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef WEIGHT_HPP
#define WEIGHT_HPP
#include <array>
#include <vector>
#include <iostream>
constexpr size_t WEIGHTS_SIZE = 3;
//TODO: peut être passer ça en std::vector ?
using Weight = std::array<float, WEIGHTS_SIZE>;
using Weights = std::vector<Weight>;
bool operator>(const Weight& w1, const Weight& w2)
{
for (size_t i = 0; i < WEIGHTS_SIZE; ++i)
{
if (w1[i] <= w2[i])
return false;
}
return true;
}
bool operator<= (const Weight& w1, const Weight& w2)
{
for (size_t i = 0; i < WEIGHTS_SIZE; ++i)
{
if (w1[i] > w2[i])
return false;
}
return true;
}
bool operator<= (const Weight& w, const Weights& weights)
{
for (const Weight& W : weights)
{
if (!(w <= W))
return false;
}
return true;
}
bool operator<= (const Weights& weights, const Weight& W)
{
for (const Weight& w : weights)
{
if (!(w <= W))
return false;
}
return true;
}
Weight& operator+= (Weight& w1, const Weight& w2)
{
for (size_t i = 0; i < WEIGHTS_SIZE; ++i)
w1[i] += w2[i];
return w1;
}
Weight operator+ (const Weight& w1, const Weight& w2)
{
Weight w (w1);
w += w2;
return w;
}
Weight& operator-= (Weight& w1, const Weight& w2)
{
for (size_t i = 0; i < WEIGHTS_SIZE; ++i)
w1[i] -= w2[i];
return w1;
}
Weight operator- (const Weight& w1, const Weight& w2)
{
Weight w (w1);
w -= w2;
return w;
}
Weights& operator+= (Weights& weights, const Weights& toAdd)
{
if (weights.empty())
weights = toAdd;
else
{
Weights newWeights;
newWeights.reserve(weights.size() * toAdd.size());
std::swap(weights,newWeights);
for (const Weight& w1: toAdd)
{
for (const Weight& w2: newWeights)
weights.emplace_back(w1 + w2);
}
}
return weights;
}
std::ostream& operator<< (std::ostream& stream, const Weight& w)
{
stream << "{" << w[0];
for (size_t i = 1; i < WEIGHTS_SIZE; ++i)
stream << ", " << w[i];
stream << "}";
return stream;
}
#endif