#
source:
libs/suds/src/Vector.h
@
204

Revision 204, 6.7 KB checked in by dave, 10 years ago (diff) |
---|

Line | |
---|---|

1 | // Copyright (C) 2009 foam |

2 | // |

3 | // This program is free software; you can redistribute it and/or modify |

4 | // it under the terms of the GNU General Public License as published by |

5 | // the Free Software Foundation; either version 2 of the License, or |

6 | // (at your option) any later version. |

7 | // |

8 | // This program is distributed in the hope that it will be useful, |

9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |

10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |

11 | // GNU General Public License for more details. |

12 | // |

13 | // You should have received a copy of the GNU General Public License |

14 | // along with this program; if not, write to the Free Software |

15 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |

16 | |

17 | #include <assert.h> |

18 | #include <math.h> |

19 | #include <iostream> |

20 | |

21 | #ifndef FOAM_VECTOR |

22 | #define FOAM_VECTOR |

23 | |

24 | template<class T> |

25 | bool feq(T a, T b, T t=0.001) |

26 | { |

27 | return fabs(a-b)<t; |

28 | } |

29 | |

30 | template<class T> |

31 | class Vector |

32 | { |

33 | public: |

34 | Vector(); |

35 | Vector(unsigned int s); |

36 | ~Vector(); |

37 | Vector(const Vector &other); |

38 | |

39 | T &operator[](unsigned int i) |

40 | { |

41 | assert(i<m_Size); |

42 | return m_Data[i]; |

43 | } |

44 | |

45 | const T &operator[](unsigned int i) const |

46 | { |

47 | assert(i<m_Size); |

48 | return m_Data[i]; |

49 | } |

50 | |

51 | unsigned int Size() const { return m_Size; } |

52 | T *GetRawData() { return m_Data; } |

53 | const T *GetRawDataConst() const { return m_Data; } |

54 | |

55 | void Print() const; |

56 | void SetAll(T s); |

57 | void Zero() { SetAll(0); } |

58 | bool IsInf(); |

59 | T Mean(); |

60 | T DistanceFrom(const Vector &other) const; |

61 | T Magnitude() const; |

62 | T Dot(const Vector<T> &other); |

63 | Vector Normalised() const; |

64 | |

65 | Vector &operator=(const Vector &other); |

66 | Vector operator+(const Vector &other) const; |

67 | Vector operator-(const Vector &other) const; |

68 | Vector operator+(T v) const; |

69 | Vector operator-(T v) const; |

70 | Vector operator*(T v) const; |

71 | Vector operator/(T v) const; |

72 | Vector &operator+=(const Vector &other); |

73 | Vector &operator-=(const Vector &other); |

74 | Vector &operator+=(T v); |

75 | Vector &operator-=(T v); |

76 | Vector &operator*=(T v); |

77 | Vector &operator/=(T v); |

78 | |

79 | void Save(FILE *f) const; |

80 | void Load(FILE *f); |

81 | |

82 | static void RunTests(); |

83 | |

84 | private: |

85 | |

86 | unsigned int m_Size; |

87 | T *m_Data; |

88 | }; |

89 | |

90 | template<class T> |

91 | Vector<T>::Vector() : |

92 | m_Size(0) |

93 | { |

94 | m_Data=NULL; |

95 | } |

96 | |

97 | template<class T> |

98 | Vector<T>::Vector(unsigned int s) : |

99 | m_Size(s) |

100 | { |

101 | m_Data=new T[s]; |

102 | } |

103 | |

104 | template<class T> |

105 | Vector<T>::~Vector() |

106 | { |

107 | delete[] m_Data; |

108 | } |

109 | |

110 | template<class T> |

111 | Vector<T>::Vector(const Vector &other) |

112 | { |

113 | m_Size = other.m_Size; |

114 | m_Data=new T[m_Size]; |

115 | memcpy(m_Data,other.m_Data,m_Size*sizeof(T)); |

116 | } |

117 | |

118 | template<class T> |

119 | Vector<T> &Vector<T>::operator=(const Vector &other) |

120 | { |

121 | if (m_Data!=NULL) |

122 | { |

123 | delete[] m_Data; |

124 | } |

125 | |

126 | m_Size = other.m_Size; |

127 | m_Data=new T[m_Size]; |

128 | memcpy(m_Data,other.m_Data,m_Size*sizeof(T)); |

129 | |

130 | return *this; |

131 | } |

132 | |

133 | template<class T> |

134 | void Vector<T>::Print() const |

135 | { |

136 | for (unsigned int i=0; i<m_Size; i++) |

137 | { |

138 | std::cerr<<(*this)[i]<<" "; |

139 | } |

140 | std::cerr<<std::endl; |

141 | } |

142 | |

143 | template<class T> |

144 | void Vector<T>::SetAll(T s) |

145 | { |

146 | for (unsigned int i=0; i<m_Size; i++) |

147 | { |

148 | (*this)[i]=s; |

149 | } |

150 | } |

151 | |

152 | template<class T> |

153 | bool Vector<T>::IsInf() |

154 | { |

155 | for (unsigned int i=0; i<m_Size; i++) |

156 | { |

157 | if (isinf((*this)[i])) return true; |

158 | if (isnan((*this)[i])) return true; |

159 | } |

160 | return false; |

161 | } |

162 | |

163 | template<class T> |

164 | T Vector<T>::DistanceFrom(const Vector &other) const |

165 | { |

166 | assert(m_Size==other.m_Size); |

167 | |

168 | float acc=0; |

169 | for (unsigned int i=0; i<m_Size; i++) |

170 | { |

171 | acc+=(other[i]-(*this)[i]) * (other[i]-(*this)[i]); |

172 | } |

173 | |

174 | return sqrt(acc); |

175 | } |

176 | |

177 | template<class T> |

178 | T Vector<T>::Magnitude() const |

179 | { |

180 | float acc=0; |

181 | for (unsigned int i=0; i<m_Size; i++) |

182 | { |

183 | acc+=(*this)[i] * (*this)[i]; |

184 | } |

185 | |

186 | return sqrt(acc); |

187 | } |

188 | |

189 | template<class T> |

190 | T Vector<T>::Dot(const Vector<T> &other) |

191 | { |

192 | assert(m_Size==other.m_Size); |

193 | T acc=0; |

194 | for (unsigned int i=0; i<m_Size; i++) |

195 | { |

196 | acc+=(*this)[i]*other[i]; |

197 | } |

198 | return acc; |

199 | } |

200 | |

201 | template<class T> |

202 | Vector<T> Vector<T>::Normalised() const |

203 | { |

204 | Vector<T> ret(*this); |

205 | ret/=ret.Magnitude(); |

206 | return ret; |

207 | } |

208 | |

209 | template<class T> |

210 | Vector<T> Vector<T>::operator+(const Vector &other) const |

211 | { |

212 | assert(m_Size==other.m_Size); |

213 | |

214 | Vector<T> ret(m_Size); |

215 | for (unsigned int i=0; i<m_Size; i++) |

216 | { |

217 | ret[i]=(*this)[i]+other[i]; |

218 | } |

219 | return ret; |

220 | } |

221 | |

222 | template<class T> |

223 | Vector<T> Vector<T>::operator-(const Vector &other) const |

224 | { |

225 | assert(m_Size==other.m_Size); |

226 | |

227 | Vector<T> ret(m_Size); |

228 | for (unsigned int i=0; i<m_Size; i++) |

229 | { |

230 | ret[i]=(*this)[i]-other[i]; |

231 | } |

232 | return ret; |

233 | } |

234 | |

235 | template<class T> |

236 | Vector<T> Vector<T>::operator+(T v) const |

237 | { |

238 | Vector<T> ret(m_Size); |

239 | for (unsigned int i=0; i<m_Size; i++) |

240 | { |

241 | ret[i]=(*this)[i]+v; |

242 | } |

243 | return ret; |

244 | } |

245 | |

246 | template<class T> |

247 | Vector<T> Vector<T>::operator-(T v) const |

248 | { |

249 | Vector<T> ret(m_Size); |

250 | for (unsigned int i=0; i<m_Size; i++) |

251 | { |

252 | ret[i]=(*this)[i]-v; |

253 | } |

254 | return ret; |

255 | } |

256 | |

257 | template<class T> |

258 | Vector<T> Vector<T>::operator*(T v) const |

259 | { |

260 | Vector<T> ret(m_Size); |

261 | for (unsigned int i=0; i<m_Size; i++) |

262 | { |

263 | ret[i]=(*this)[i]*v; |

264 | } |

265 | return ret; |

266 | } |

267 | |

268 | template<class T> |

269 | Vector<T> Vector<T>::operator/(T v) const |

270 | { |

271 | Vector<T> ret(m_Size); |

272 | for (unsigned int i=0; i<m_Size; i++) |

273 | { |

274 | ret[i]=(*this)[i]/v; |

275 | } |

276 | return ret; |

277 | } |

278 | |

279 | template<class T> |

280 | Vector<T> &Vector<T>::operator+=(const Vector &other) |

281 | { |

282 | assert(m_Size==other.m_Size); |

283 | for (unsigned int i=0; i<m_Size; i++) |

284 | { |

285 | (*this)[i]+=other[i]; |

286 | } |

287 | return *this; |

288 | } |

289 | |

290 | template<class T> |

291 | Vector<T> &Vector<T>::operator-=(const Vector &other) |

292 | { |

293 | assert(m_Size==other.m_Size); |

294 | for (unsigned int i=0; i<m_Size; i++) |

295 | { |

296 | (*this)[i]-=other[i]; |

297 | } |

298 | return *this; |

299 | } |

300 | |

301 | template<class T> |

302 | Vector<T> &Vector<T>::operator+=(T v) |

303 | { |

304 | for (unsigned int i=0; i<m_Size; i++) |

305 | { |

306 | (*this)[i]+=v; |

307 | } |

308 | return *this; |

309 | } |

310 | |

311 | template<class T> |

312 | Vector<T> &Vector<T>::operator-=(T v) |

313 | { |

314 | for (unsigned int i=0; i<m_Size; i++) |

315 | { |

316 | (*this)[i]-=v; |

317 | } |

318 | return *this; |

319 | } |

320 | |

321 | template<class T> |

322 | Vector<T> &Vector<T>::operator*=(T v) |

323 | { |

324 | for (unsigned int i=0; i<m_Size; i++) |

325 | { |

326 | (*this)[i]*=v; |

327 | } |

328 | return *this; |

329 | } |

330 | |

331 | template<class T> |

332 | Vector<T> &Vector<T>::operator/=(T v) |

333 | { |

334 | for (unsigned int i=0; i<m_Size; i++) |

335 | { |

336 | (*this)[i]/=v; |

337 | } |

338 | return *this; |

339 | } |

340 | |

341 | template<class T> |

342 | T Vector<T>::Mean() |

343 | { |

344 | T acc=0; |

345 | for (unsigned int i=0; i<m_Size; i++) |

346 | { |

347 | acc+=(*this)[i]; |

348 | } |

349 | return acc/(float)m_Size; |

350 | } |

351 | |

352 | template<class T> |

353 | void Vector<T>::Save(FILE* f) const |

354 | { |

355 | int version = 1; |

356 | fwrite(&version,sizeof(version),1,f); |

357 | fwrite(&m_Size,sizeof(m_Size),1,f); |

358 | fwrite(m_Data,sizeof(T)*m_Size,1,f); |

359 | } |

360 | |

361 | template<class T> |

362 | void Vector<T>::Load(FILE* f) |

363 | { |

364 | int version; |

365 | fread(&version,sizeof(version),1,f); |

366 | fread(&m_Size,sizeof(m_Size),1,f); |

367 | m_Data=new T[m_Size]; |

368 | fread(m_Data,sizeof(T)*m_Size,1,f); |

369 | } |

370 | |

371 | template<class T> |

372 | void Vector<T>::RunTests() |

373 | { |

374 | Vector<T> m(10); |

375 | m.SetAll(0); |

376 | assert(m[0]==0); |

377 | m[5]=0.5; |

378 | assert(m[5]==0.5); |

379 | Vector<T> om(m); |

380 | assert(om[5]==0.5); |

381 | |

382 | assert(feq(m.Magnitude(),0.5f)); |

383 | Vector<T> a(10); |

384 | a.Zero(); |

385 | a[5]=-10; |

386 | assert(feq(a.DistanceFrom(m),10.5f)); |

387 | } |

388 | |

389 | #endif |

**Note:**See TracBrowser for help on using the repository browser.