summaryrefslogtreecommitdiffstats
path: root/engine/include/symbasic/vector.tpp
blob: d62294010a737aab8d89dc2a6d936db66adbaedc (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#ifndef __VECTOR_TPP__
#define __VECTOR_TPP__
	
/* for auto loop */

template<class T>
typename Vector<T>::iterator Vector<T>::begin()
{
    return iterator(this);
}

template<class T>
typename Vector<T>::iterator Vector<T>::end()
{
    return iterator(this, this->size() - 1);
}

template<class T>
typename Vector<T>::const_iterator Vector<T>::begin() const
{
    return iterator(this);
}

template<class T>
typename Vector<T>::const_iterator Vector<T>::end() const
{
    return iterator(this, this->size() - 1);
}

template<class T>
bool Vector<T>::operator==(const Vector<T>& v) const {
	
	if(this->size() != v.size())
		return false;
	
	//Retourne déjà faux si les deux vecteurs n'ont pas la même dimension
	for(size_t i(0); i < v.size(); ++i){
		
		if ((*this)[i] != v[i])
			return false;
			//Lors que le programme rencontre deux coordonnées différenentes respectivement pour la même dimension des deux vecteurs
			//la fonction retourne false
	}
	return true;
}

template<class T>
bool Vector<T>::operator>=(const Vector<T>& v) const
{
    return sq_module() >= v.sq_module();
}

template<class T>
bool Vector<T>::operator<=(const Vector<T>& v) const
{
    return sq_module() <= v.sq_module();
}

template<class T>
bool Vector<T>::operator>=(double m) const
{
    return sq_module() >= m * m;
}

template<class T>
bool Vector<T>::operator<=(double m) const
{
    return sq_module() <= m * m;
}

template<class T>
bool Vector<T>::operator>(const Vector<T>& v) const
{
    return sq_module() > v.sq_module();
}

template<class T>
bool Vector<T>::operator<(const Vector<T>& v) const
{
    return sq_module() < v.sq_module();
}

template<class T>
bool Vector<T>::operator>(double m) const
{
    return sq_module() > m * m;
}

template<class T>
bool Vector<T>::operator<(double m) const
{
    return sq_module() < m * m;
}

template<class T>
bool Vector<T>::operator!=(const Vector<T>& w) const
{
    return !(*this == w);
}

//Operateurs somme et produit

template<class T>
Vector<T>& Vector<T>::add(const Vector<T>& w)
{
    size_t n = (this->size() <= w.size()) ? this->size() : w.size();

    for (size_t i= 0; i < n; ++i)
        (*this)[i] += w[i]; 

    return *this;
}


template<class T>
Vector<T>& Vector<T>::sub(const Vector<T>& w)
{
    size_t n = (this->size() <= w.size()) ? this->size() : w.size();

    for (size_t i = 0; i < n; ++i)
        (*this)[i] -= w[i]; 

    return *this;
}

template<class T>
Vector<T>& Vector<T>::mult(double k)
{
    for (size_t i = 0; i < this->size(); ++i)
        (*this)[i] *= k; 

    return *this;
}

template<class T>
Vector<T>& Vector<T>::divide(double k)
{
    for (size_t i = 0; i < this->size(); ++i)
        (*this)[i] /= k; 

    return *this;
}

template<class T>
Vector<T>& Vector<T>::_unit() const
{
    return (*this) /= this->module();
}

/* Dot product */
template<class T>
double Vector<T>::operator*(const Vector<T>& w) const
{
    double x = 0;
    size_t n = (this->size() <= w.size()) ? this->size() : w.size();

    for (size_t i = 0; i < n; ++i)
        x += (*this)[i] * w[i];

    return x;
}

template<class T>
Vector<T>& Vector<T>::reverse()
{
    return (*this)*=-1.0;
}

template<class T>
double Vector<T>::sq_module() const 
{
    return (*this) * (*this);
}

template<class T>
double Vector<T>::module() const
{
    return sqrt(this->sq_module());
}

/* Implementation of ostream operator overloading */

/* Le prototype je le mets dans un header séparé parce que
 * il ne fait pas partie de l'implementation d'un vecteur
 * Une classe l'utilisant n'as pas besoin de l'imprimer
 */

#ifdef _USE_VECTOR_OSTREAM
#include <ostream>

template<class T>
void Vector<T>::affiche(std::ostream& os) const
{
    os << "(";

    for (size_t i(0); i < (size() - 1); ++i)
    {
        os << (*this)[i] << ", ";
    }

    os << (*this)[size()-1] << ")";
}

// Flux de sortie permettant d'afficher les coordonnées du vecteur
template<class T>
std::ostream& operator<<(std::ostream& os, const Vector<T>& v)
{
    v.affiche(os);
    return os;
}
#endif

#endif //__VECTOR_TPP__