VectorT.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libbasicvector - A simple vector library
  4. * See file License for license information.
  5. */
  6. #ifndef _EVECTOR_BASICVECTOR_H
  7. #define _EVECTOR_BASICVECTOR_H
  8. #include <string>
  9. #include <sstream>
  10. #include <vector>
  11. #include <stdexcept>
  12. #include <cstddef> // needed for ptrdiff_t
  13. #include "core/vector/ippwrapper.h"
  14. #include <core/basics/binstream.h>
  15. #ifdef NICE_USELIB_LINAL
  16. #include <LinAl/vectorC.h>
  17. #endif
  18. #ifdef NICE_USELIB_OPENMP
  19. #include <omp.h>
  20. #endif
  21. #include "core/vector/VectorBase.h"
  22. namespace NICE {
  23. template<class ElementType>
  24. class MatrixT;
  25. template<class ElementType>
  26. class RowMatrixT;
  27. /**
  28. * @class VectorT
  29. * @brief VectorT is a simple vector template class
  30. * @author Ferid Bajramovic (based on \c extarray from ckolumbus)
  31. *
  32. * \c VectorT is a simple vector class which provides the (optional) ability
  33. * to use externally allocated memory as storage.
  34. * The main features are:
  35. * - In concept an efficient implementation of a mathematical vector
  36. * - An array-like storage class
  37. * - Support for externally allocated memory
  38. * - Ability to get pointer to internal memory
  39. * - Thus the ability to <b>directly</b> interface with other libraries such as
  40. * LinAl without time consuming data copying
  41. *
  42. * @note VectorT objects can only be copied and assigned to each other to
  43. * a limited degree. This means that care has to be taken when putting
  44. * VectorT objects into STL containers (such as std::vector).
  45. * Example: Resizing a std::vector<VectorT> will work, as elements will be
  46. * copied to new EVectors. However, std::vector<VectorT>::erase() will
  47. * only work when all (subsequent) VectorT objects have the same size.
  48. * (Details may depend on the implemenation of the STL.)
  49. *
  50. * @example basicvector_vector_overview.cpp
  51. */
  52. template<class ElementType>
  53. class VectorT : public VectorBase {
  54. public:
  55. //! STL-like typedef for type of elements
  56. typedef ElementType value_type;
  57. //! STL-like typedef for const element reference
  58. typedef const ElementType& const_reference;
  59. //! STL-like typedef for iterator
  60. typedef ElementType* iterator;
  61. //! STL-like typedef for const iterator
  62. typedef const ElementType* const_iterator;
  63. //! STL-like typedef for element reference
  64. typedef ElementType& reference;
  65. /**
  66. * @name constructor
  67. * \{
  68. */
  69. /**
  70. * @brief Create an empty \c VectorT with size zero.
  71. *
  72. * Such an object is actually useless, but sometimes you may need to have
  73. * such instances to initalize some data structure.
  74. * The only way to turn such an object into something useful is operator=().
  75. */
  76. VectorT();
  77. /**
  78. * @brief Create an \c VectorT with size \c size.
  79. * Data will not be initialized.
  80. * @param size Size of the \c VectorT
  81. */
  82. explicit VectorT(const size_t size);
  83. /**
  84. * @brief Create an \c VectorT with size \c size.
  85. * Data will be initialized to \c element.
  86. * @param size Size of the \c VectorT
  87. * @param element Initial value of all elements
  88. */
  89. VectorT(const size_t size, const ElementType& element);
  90. /**
  91. * @brief Create an \c VectorT of size \c size.
  92. * It will be initialized with the data pointed to by \c _data.
  93. *
  94. * That is the first \c size elements of \c _data will be copied.
  95. * @param _data Data to be copied
  96. * @param size Size
  97. */
  98. VectorT(const ElementType* _data, const size_t size);
  99. /**
  100. * @brief Create an \c VectorT of size \c size
  101. * with optionally external storage.
  102. *
  103. * It will be initialized with the data pointed to by \c _data.
  104. * The parameter _external control whether the data will be copied or
  105. * the pointer \c _data is directly kept as storage for the \c VectorT.
  106. * In case of external storage the data will not be deleted in the destructor.
  107. * @param _data External data storage
  108. * @param size Size of the external storage
  109. * @param mode Storage mode: keep external memory or copy data.
  110. */
  111. VectorT(ElementType* _data, const size_t size,
  112. const VectorBase::Mode mode = copy);
  113. /**
  114. * @brief Create an \c VectorT as a copy of the data in \c v
  115. * @param v data
  116. */
  117. explicit VectorT(const std::vector<ElementType>& v);
  118. /**
  119. * @brief Read from input stream.
  120. * @param input Read from here
  121. */
  122. explicit VectorT(std::istream& input, const bool & AwAFormat=false);
  123. //! copy constructor
  124. VectorT(const VectorT<ElementType>& v);
  125. /**
  126. * \}
  127. * @name destructor
  128. * \{
  129. */
  130. virtual ~VectorT();
  131. /**
  132. * \}
  133. * @name access functions
  134. * \{
  135. */
  136. /**
  137. * @brief Create a const VectorT of size \c size
  138. * with <b>external</b> storage.
  139. *
  140. * It will be initialized with the data pointed to by %_data.
  141. * The data will not be deleted in the destructor.
  142. * @param _data External data storage
  143. * @param size Size of the external storage
  144. */
  145. inline static const VectorT<ElementType>* createConst(const ElementType* _data,
  146. const size_t size);
  147. /**
  148. * @brief Read access to elements.
  149. * @param i Index
  150. * @return Element (as const reference)
  151. * @example basicvector_vector_access_operator1.cpp
  152. */
  153. inline const_reference operator[](const ptrdiff_t i) const {
  154. return constData[i];
  155. }
  156. /**
  157. * @brief Read access to range checked elements.
  158. * @param i Index
  159. * @return Element (as const reference)
  160. * @example basicvector_vector_access_operator2.cpp
  161. */
  162. inline const_reference operator()(const ptrdiff_t i) const;
  163. /**
  164. * @brief Get a const pointer to the internal memory.
  165. */
  166. // operator ElementType const* () const { return constData; }
  167. /**
  168. * @brief Get a const_iterator pointing to the first element.
  169. *
  170. * (Actually the same as \c getDataPointer())
  171. * @example basicvector_vector_access_iterator.cpp
  172. */
  173. inline const_iterator begin() const { return constData; }
  174. /**
  175. * @brief Get an iterator pointing beyond the last element.
  176. */
  177. inline const_iterator end() const { return constData + dataSize; }
  178. /**
  179. * @brief Access to elements.
  180. * @param i Index
  181. * @return Element
  182. */
  183. inline const ElementType& get(const ptrdiff_t i) { return data[i]; }
  184. /**
  185. * @brief Access to elements.
  186. * @param i Index
  187. * @param v value to assign
  188. * @return Element
  189. */
  190. inline void set(const ptrdiff_t i, const ElementType& v) { data[i] = v; }
  191. /**
  192. * @brief Access to elements.
  193. * @param i Index
  194. * @return Element (as reference)
  195. */
  196. inline reference operator[](const ptrdiff_t i) { return data[i]; }
  197. /**
  198. * @brief Access to range checked elements.
  199. * @param i Index
  200. * @return Element (as reference)
  201. */
  202. inline reference operator()(const ptrdiff_t i);
  203. /**
  204. * @brief append a vector (this function is extremely inefficient,
  205. * because we have to dealloc and copy the whole vector.
  206. * For efficiency reasons std::vector might be the better solution.
  207. * @param v vector which we appended to the current vector
  208. */
  209. void append ( const VectorT<ElementType> & v );
  210. /**
  211. * @brief append a value to the vector (this function is extremely inefficient,
  212. * because we have to dealloc and copy the whole vector.
  213. * For efficiency reasons std::vector might be the better solution.
  214. * @param v value which is appended to the current vector
  215. */
  216. void append ( const ElementType & v );
  217. /**
  218. * @brief copy a range of the vector
  219. * @param i start index
  220. * @param k end index
  221. */
  222. VectorT<ElementType> getRange ( const ptrdiff_t i, const ptrdiff_t j ) const;
  223. /**
  224. * @brief get a vector which uses the same memory as the current
  225. * vector in a specific range
  226. * @param i start index
  227. * @param j end index
  228. */
  229. VectorT<ElementType> getRangeRef ( const ptrdiff_t i, const ptrdiff_t j );
  230. /**
  231. * @brief Get a pointer to the internal memory.
  232. */
  233. // inline operator ElementType* () { return data; }
  234. /**
  235. * Get a pointer to the internal memory.
  236. * @example basicvector_vector_access_pointer.cpp
  237. */
  238. inline ElementType* getDataPointer() { return data; }
  239. /**
  240. * Get a const pointer to the internal memory.
  241. * @example basicvector_ippusage.cpp
  242. */
  243. inline const ElementType* getDataPointer() const { return constData; }
  244. /**
  245. * @brief Get an iterator pointing to the first element.
  246. *
  247. * (Actually the same as \c getDataPointer())
  248. */
  249. inline iterator begin() { return data; }
  250. /**
  251. * @brief Get an iterator pointing beyond the last element.
  252. */
  253. inline iterator end() { return data + this->dataSize; }
  254. /**
  255. * @brief Return the size of this VectorT.
  256. * @return Size of this VectorT.
  257. */
  258. inline size_t size() const { return dataSize; }
  259. /**
  260. * \}
  261. * @name statistical functions
  262. * \{
  263. */
  264. /**
  265. * Scalar product "this * v"
  266. * @param v Second factor
  267. * @return this * v
  268. */
  269. inline ElementType scalarProduct(const VectorT<ElementType>& v) const;
  270. /**
  271. * Sum of elements
  272. * @return Sum of elements
  273. */
  274. inline ElementType Sum() const;
  275. /**
  276. * Mean of elements
  277. * @return Mean of elements
  278. * @example basicvector_vector_meanstddev.cpp
  279. */
  280. inline ElementType Mean() const;
  281. /**
  282. * Median of elements
  283. * @return Median of elements
  284. */
  285. inline ElementType Median() const;
  286. /**
  287. * Median of elements
  288. * @return Median of elements
  289. */
  290. VectorT<ElementType> CircularMean(const Ipp32s& size);
  291. /**
  292. * Maximum of elements
  293. * @return Max of elements
  294. * @example basicvector_vector_maxmin.cpp
  295. */
  296. inline ElementType Max() const;
  297. /**
  298. * Return the Index of the maximum of elements
  299. * @return index of maximum
  300. */
  301. inline int MaxIndex() const;
  302. /**
  303. * Minimum of elements
  304. * @return Min of elements
  305. */
  306. inline ElementType Min() const;
  307. /**
  308. * Return the Index of the mimimum of elements
  309. * @return index of mimimum
  310. */
  311. inline int MinIndex() const;
  312. /**
  313. * StdDev of elements
  314. * @return StdDev of elements
  315. */
  316. inline ElementType StdDev() const;
  317. /**
  318. * \}
  319. * @name sorting
  320. * \{
  321. */
  322. /**
  323. * Sort elements in an ascending order.
  324. * @example basicvector_vector_sort.cpp
  325. */
  326. inline void sortAscend();
  327. /**
  328. * @brief sort elements in an descending order.
  329. */
  330. inline void sortDescend();
  331. /**
  332. * @brief sort elements in an descending order. Permutation is only correct if all elements are different.
  333. */
  334. inline void sortDescend(VectorT<int> & permutation);
  335. /**
  336. * \}
  337. * @name norms
  338. * \{
  339. */
  340. /**
  341. * computes the infinity norm of the vector (maximum norm)
  342. * @return infinity vector norm
  343. * @example basicvector_vector_norm.cpp
  344. */
  345. inline ElementType normInf() const;
  346. /**
  347. * computes the L1 norm of the vector (manhattan norm)
  348. * @return L1 vector norm
  349. */
  350. inline ElementType normL1() const;
  351. /**
  352. * computes the L2 norm of the vector (euclidean norm)
  353. * @return L2 vector norm
  354. */
  355. inline ElementType normL2() const;
  356. /**
  357. * \}
  358. * @name normalization
  359. * \{
  360. */
  361. /**
  362. * Normalize this vector with respect to the infinity norm (maximum norm).
  363. */
  364. inline void normalizeInf() {
  365. operator/=(normInf());
  366. }
  367. /**
  368. * Normalize this vector with respect to the L1 norm (manhattan norm).
  369. */
  370. inline void normalizeL1() {
  371. operator/=(normL1());
  372. }
  373. /**
  374. * Normalize this vector with respect to the L2 norm (euclidean norm).
  375. */
  376. inline void normalizeL2() {
  377. operator/=(normL2());
  378. }
  379. /**
  380. * \}
  381. * @name operators
  382. * \{
  383. */
  384. /**
  385. * @brief Compare \c v with \c this.
  386. * @pre Size of \c v and \c this must be equal
  387. * @param v data to compare with
  388. * @return true if \c v and \c this are equal
  389. */
  390. inline bool operator== (const VectorT<ElementType>& v) const;
  391. /**
  392. * @brief Compare \c v with \c this.
  393. * @pre Size of \c v and \c this must be equal
  394. * @param v data to compare with
  395. * @return true if \c v and \c this are not equal
  396. */
  397. inline bool operator!= (const VectorT<ElementType>& v) const;
  398. /**
  399. * @brief Copy data from \c v to \c this.
  400. * @param v New data
  401. * @return \c *this
  402. */
  403. inline VectorT<ElementType>& operator=(const VectorT<ElementType>& v);
  404. /**
  405. * @brief Set all elements to value \c element
  406. * @param element New value of all elements
  407. */
  408. inline VectorT<ElementType>& operator=(const ElementType& element);
  409. /**
  410. * @brief Add \c e to each element of \c this.
  411. * @param e value
  412. * @return \c *this
  413. * @example basicvector_vector_operator.cpp
  414. */
  415. inline VectorT<ElementType>& operator+= (const ElementType& e);
  416. /**
  417. * @brief Subtract \c e from each element of \c this.
  418. * @param e value
  419. * @return \c *this
  420. */
  421. inline VectorT<ElementType>& operator-= (const ElementType& e);
  422. /**
  423. * @brief Multiply each element of \c this with \c e.
  424. * @param e value
  425. * @return \c *this
  426. */
  427. inline VectorT<ElementType>& operator*= (const ElementType& e);
  428. /**
  429. * @brief Divide each element of \c this through \c e.
  430. * @param e value
  431. * @return \c *this
  432. */
  433. inline VectorT<ElementType>& operator/= (const ElementType& e);
  434. /**
  435. * @brief Add \c v to \c this.
  436. * @pre Size of \c v and \c this must be equal
  437. * @param v New data
  438. * @return \c *this
  439. */
  440. inline VectorT<ElementType>& operator+= (const VectorT<ElementType>& v);
  441. /**
  442. * @brief Subtract \c v from \c this.
  443. * @pre Size of \c v and \c this must be equal
  444. * @param v New data
  445. * @return \c *this
  446. */
  447. inline VectorT<ElementType>& operator-= (const VectorT<ElementType>& v);
  448. /**
  449. * @brief Multiplicate elementwise \c v to \c this.
  450. * @pre Size of \c v and \c this must be equal
  451. * @param v New data
  452. * @return \c *this
  453. */
  454. inline VectorT<ElementType>& operator*= (const VectorT<ElementType>& v);
  455. /**
  456. * @brief Divide elementwise \c v from \c this.
  457. * @pre Size of \c v and \c this must be equal
  458. * @param v New data
  459. * @return \c *this
  460. */
  461. inline VectorT<ElementType>& operator/= (const VectorT<ElementType>& v);
  462. /**
  463. * \}
  464. * @name random vector data initialization
  465. * \{
  466. */
  467. /**
  468. * @brief Create a vector with uniform distributed values.
  469. * @param size vector length
  470. * @param min minimum value
  471. * @param max maximum value
  472. * @param seed seed for random generator
  473. * @return a vector with uniform distributed values
  474. */
  475. inline static VectorT<ElementType> UniformRandom(const size_t size, ElementType min,
  476. ElementType max, unsigned int seed);
  477. /**
  478. * @brief Create a vector with uniform distributed values with the global seed and
  479. * without using IPP
  480. * @param size vector length
  481. * @param min minimum value
  482. * @param max maximum value
  483. * @return a vector with uniform distributed values
  484. */
  485. inline static VectorT<ElementType> UniformRandom(const size_t size, ElementType min,
  486. ElementType max);
  487. /**
  488. * @brief Create a vector with Gaussian distributed values using IPP if available.
  489. * Note: IPP seems to be not as robust as the standard Box-Muller method implemented
  490. * in randGaussDouble. To use this method you can run GaussRandom without a seed
  491. * and set the seed manually with initRand(true, seed) from numerictools.
  492. * @param size vector length
  493. * @param mean mean of Gaussian distribution
  494. * @param stdev standard derivation of Gaussian distribution
  495. * @param seed seed for random generator
  496. * @return a vector with Gaussian distributed values
  497. */
  498. inline static VectorT<ElementType> GaussRandom(const size_t size, ElementType mean,
  499. ElementType stdev, unsigned int seed);
  500. /**
  501. * @brief Create a vector with Gaussian distributed values without using the IPP function.
  502. * Set the seed manually with initRand (numerictools).
  503. * @param size vector length
  504. * @param mean mean of Gaussian distribution
  505. * @param stdev standard derivation of Gaussian distribution
  506. * @return a vector with Gaussian distributed values
  507. */
  508. inline static VectorT<ElementType> GaussRandom(const size_t size, ElementType mean,
  509. ElementType stdev);
  510. /**
  511. * \}
  512. * @name math functions
  513. * \{
  514. */
  515. /**
  516. * Matrix Vector multiplication: this = a^{T if atranspose} * v
  517. * The formats of this, a and v must be consistent.
  518. * If this is empty (size 0x0), it will be resize.
  519. * @param a matrix to multiply
  520. * @param v vector to multiply
  521. * @param atranspose use a.transpose() instead of a
  522. * @throw Exception if formats are inconsistent
  523. * @example basicvector_vector_multiply.cpp
  524. */
  525. void multiply(const MatrixT<ElementType>& a,
  526. const VectorT<ElementType>& v,
  527. bool atranspose=false);
  528. /**
  529. * Vector Matrix multiplication: this = v * a^{T if atranspose}
  530. * The formats of this, v and a must be consistent.
  531. * If this is empty (size 0x0), it will be resize.
  532. * @param v vector to multiply
  533. * @param a matrix to multiply
  534. * @param atranspose use a.transpose() instead of a
  535. * @throw Exception if formats are inconsistent
  536. */
  537. void multiply(const VectorT<ElementType>& v,
  538. const MatrixT<ElementType>& a,
  539. bool atranspose=false);
  540. /**
  541. * Matrix Vector multiplication: this = a^{T if atranspose} * v
  542. * The formats of this, a and v must be consistent.
  543. * If this is empty (size 0x0), it will be resize.
  544. * @param a matrix to multiply
  545. * @param v vector to multiply
  546. * @param atranspose use a.transpose() instead of a
  547. * @throw Exception if formats are inconsistent
  548. */
  549. void multiply(const RowMatrixT<ElementType>& a,
  550. const VectorT<ElementType>& v,
  551. bool atranspose=false);
  552. /**
  553. * Vector Matrix multiplication: this = v * a^{T if atranspose}
  554. * The formats of this, v and a must be consistent.
  555. * If this is empty (size 0x0), it will be resize.
  556. * @param v vector to multiply
  557. * @param a matrix to multiply
  558. * @param atranspose use a.transpose() instead of a
  559. * @throw Exception if formats are inconsistent
  560. */
  561. void multiply(const VectorT<ElementType>& v,
  562. const RowMatrixT<ElementType>& a,
  563. bool atranspose=false);
  564. /**
  565. * Return a matrix (size: size() x size()) representing
  566. * the cross product of this vector with another vector:
  567. * "result * v = this x v".
  568. */
  569. MatrixT<ElementType> toCrossProductMatrix() const;
  570. /**
  571. * \}
  572. * @name shift functions
  573. * \{
  574. */
  575. /**
  576. * left shift a vector circular by \c b bins
  577. * @param b number of bins
  578. * @return left shifted vector
  579. */
  580. VectorT<ElementType> LShiftCircular(const uint b);
  581. /**
  582. * left shift a vector circular by \c b bins (inplace)
  583. * @param b number of bins
  584. */
  585. void LShiftCircularInplace(const uint b);
  586. /**
  587. * right shift a vector circular by \c b bins
  588. * @param b number of bins
  589. * @return left shifted vector
  590. */
  591. VectorT<ElementType> RShiftCircular(const uint b);
  592. /**
  593. * right shift a vector circular by \c b bins (inplace)
  594. * @param b number of bins
  595. */
  596. void RShiftCircularInplace(const uint b);
  597. /**
  598. * \}
  599. * @name other functions
  600. * \{
  601. */
  602. /**
  603. * @brief Change the number of VectorT elements
  604. * @param size New size of the \c VectorT
  605. * @throw std::runtime_error if using external storage and \c size > \c size()
  606. * @note Changing the size of the vector invalidates all iterators
  607. * and pointers held to the data. resize(0) empties the vector.
  608. */
  609. inline void resize(size_t size);
  610. /**
  611. * @brief set vector size to zero and free memory
  612. */
  613. inline void clear(void);
  614. /**
  615. * @brief Reverse the order of the VectorT elements.
  616. */
  617. inline void flip();
  618. /**
  619. * @brief Set all elements to value \c element
  620. * @param element New value of all elements
  621. */
  622. inline void set(const ElementType& element) {
  623. *this = element;
  624. }
  625. /**
  626. * @brief replace all elements by their absolute value
  627. */
  628. inline void absInplace();
  629. /**
  630. * @brief return a vector with the absolute values of this vector
  631. */
  632. inline VectorT<ElementType> abs() const;
  633. /**
  634. * @brief Returns 'true' if the memory is allocated externally
  635. * @return true if external storage
  636. */
  637. inline bool isExternal() const { return externalStorage; }
  638. /**
  639. * @brief Compare \c v with \c this.
  640. * @pre Size of \c v and \c this must be equal
  641. * @param a data to compare with
  642. * @param threshold difference of each entry which can be neglected
  643. * @return true if \c v and \c this are equal
  644. */
  645. inline bool isEqual(const VectorT<ElementType> &v, ElementType threshold=static_cast<ElementType>(0)) const;
  646. /**
  647. * @brief convert to a STL vector
  648. *
  649. * @return copy of the vector as a STL vector
  650. */
  651. inline std::vector<ElementType> std_vector () const;
  652. /**
  653. * @brief convert the vector to a really simple hash value
  654. *
  655. * @return hash value
  656. */
  657. unsigned long getHashValue () const;
  658. #ifdef NICE_USELIB_LINAL
  659. /**
  660. * \}
  661. * @name LinAl functions
  662. * \{
  663. */
  664. /**
  665. * Copy data form \c v.
  666. */
  667. explicit VectorT(const LinAl::VectorC<ElementType>& v);
  668. /**
  669. * @brief Copy data from \c v to \c this.
  670. * @param v New data
  671. * @return \c *this
  672. */
  673. inline VectorT<ElementType>& operator=(const LinAl::VectorCC<ElementType>& v);
  674. /**
  675. * Shallow copy data to LinAl vector
  676. */
  677. inline LinAl::VectorCC<ElementType> linalCol() {
  678. return LinAl::VectorCC<ElementType>(data, size(), true);
  679. }
  680. /**
  681. * Copy data to LinAl vector
  682. */
  683. inline LinAl::VectorCC<ElementType> linalCol() const {
  684. return LinAl::VectorCC<ElementType>(data, size());
  685. }
  686. /**
  687. * Shallow copy data to LinAl vector
  688. */
  689. inline LinAl::VectorCR<ElementType> linalRow() {
  690. return LinAl::VectorCR<ElementType>(data, size(), true);
  691. }
  692. /**
  693. * Copy data to LinAl vector
  694. */
  695. inline LinAl::VectorCR<ElementType> linalRow() const {
  696. return LinAl::VectorCR<ElementType>(data, size());
  697. }
  698. #endif // NICE_USELIB_LINAL
  699. /**
  700. * \}
  701. */
  702. protected:
  703. /**
  704. * Are we using external storage?
  705. */
  706. bool externalStorage;
  707. /**
  708. * Const pointer to the data.
  709. */
  710. const ElementType* constData;
  711. /**
  712. * Pointer to the data.
  713. * @note This has to point to the same adress as constData!
  714. * (NULL is only const data is available)
  715. */
  716. ElementType* data;
  717. /**
  718. * Size of the vector
  719. */
  720. size_t dataSize;
  721. void setDataPointer(ElementType* _data, size_t _size,
  722. bool _externalStorage) {
  723. data = _data;
  724. constData = _data;
  725. dataSize = _size;
  726. externalStorage = _externalStorage;
  727. }
  728. void setConstDataPointer(const ElementType* _data, size_t _size) {
  729. data = NULL;
  730. constData = _data;
  731. dataSize = _size;
  732. externalStorage = true;
  733. }
  734. };
  735. template <class ElementType>
  736. inline std::istream&
  737. operator >> (std::istream& input, VectorT<ElementType>& v) {
  738. VectorT<ElementType> result(input);
  739. v.resize(result.size());
  740. v = result;
  741. return input;
  742. }
  743. template<class ElementType>
  744. inline std::ostream&
  745. operator << (std::ostream& output, const VectorT<ElementType>& v) {
  746. output << v.size() << " < ";
  747. for (size_t i = 0; i < v.size(); i++) {
  748. output << v[i] << " ";
  749. }
  750. output << ">";
  751. return output;
  752. }
  753. //#ifdef NICE_USELIB_ZLIB
  754. template <class Tp>
  755. inline NICE::ibinstream& operator>>(NICE::ibinstream& s, VectorT<Tp> &r)
  756. {
  757. unsigned int size;
  758. s >> size;
  759. r.resize(size);
  760. typename VectorT<Tp>::iterator it=r.begin();
  761. for(;it!=r.end();it++)
  762. s>>*it;
  763. return s;
  764. }
  765. template <class Tp>
  766. inline NICE::obinstream& operator<<(NICE::obinstream& s, const VectorT<Tp>& r)
  767. {
  768. unsigned int size=r.size();
  769. s<<size;
  770. typename VectorT<Tp>::const_iterator it=r.begin();
  771. for(;it!=r.end();it++)
  772. s << *it;
  773. return s;
  774. }
  775. //#endif
  776. typedef VectorT<bool> BoolVector;
  777. typedef VectorT<char> CharVector;
  778. typedef VectorT<int> IntVector;
  779. typedef VectorT<float> FloatVector;
  780. typedef VectorT<double> Vector;
  781. // additional operators
  782. /** vector addition */
  783. template <class Tp>
  784. inline VectorT<Tp> operator+ ( const VectorT<Tp> & x, const VectorT<Tp> & y )
  785. {
  786. VectorT<Tp> dst (x);
  787. dst += y;
  788. return dst;
  789. }
  790. /** vector substratction */
  791. template <class Tp>
  792. inline VectorT<Tp> operator- ( const VectorT<Tp> & x, const VectorT<Tp> & y )
  793. {
  794. VectorT<Tp> dst (x);
  795. dst -= y;
  796. return dst;
  797. }
  798. /** multiply a vector with a scalar */
  799. template <class Tp>
  800. inline VectorT<Tp> operator* ( const VectorT<Tp> & x, double s )
  801. {
  802. VectorT<Tp> dst (x);
  803. dst *= s;
  804. return dst;
  805. }
  806. /** multiply a vector with a scalar */
  807. template <class Tp>
  808. inline VectorT<Tp> operator* ( double s, const VectorT<Tp> & x )
  809. {
  810. VectorT<Tp> dst (x);
  811. dst *= s;
  812. return dst;
  813. }
  814. } // namespace
  815. #include "core/vector/MatrixT.h"
  816. #include "core/vector/RowMatrixT.h"
  817. //#ifdef __GNUC__
  818. #include "core/vector/VectorT.tcc"
  819. //#endif
  820. #endif // _EVECTOR_BASICVECTOR_H