cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vctDynamicVector.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Ofri Sadowsky, Anton Deguet
7  Created on: 2004-07-01
8 
9  (C) Copyright 2004-2012 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 */
20 
21 #pragma once
22 #ifndef _vctDynamicVector_h
23 #define _vctDynamicVector_h
24 
31 
35 
126 template <class _elementType>
127 class vctDynamicVector : public vctDynamicVectorBase<vctDynamicVectorOwner<_elementType>, _elementType>
128 {
129 
130  friend class vctReturnDynamicVector<_elementType>;
131 
132 public:
133  VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
136  typedef typename BaseType::CopyType CopyType;
139 
142  // The default initialization of vctDynamicVectorOwner is empty.
143  {}
144 
145 
149  this->SetSize(size);
150  }
151 
154  vctDynamicVector(size_type size, value_type value) {
155  this->SetSize(size);
156  this->SetAll(value);
157  }
158 
171  vctDynamicVector(size_type size, value_type element0, value_type element1, ...) throw(std::runtime_error) {
172  if (size < 2) {
173  cmnThrow(std::runtime_error("vctDynamicVector: Constructor from va_list requires size >= 2"));
174  }
175  this->SetSize(size);
176  this->at(0) = element0;
177  this->at(1) = element1;
178  va_list nextArg;
179  va_start(nextArg, element1);
180  for (index_type i = 2; i < size; ++i) {
181  this->at(i) = value_type( va_arg(nextArg, ElementVaArgPromotion) );
182  }
183  va_end(nextArg);
184  }
185 
188  vctDynamicVector(size_type size, const value_type * values) {
189  this->SetSize(size);
190  this->Assign(values);
191  }
192 
198 
199 
204  vctDynamicVector(const ThisType & otherVector):
205  BaseType()
206  {
207  this->SetSize(otherVector.size());
208  this->Assign(otherVector);
209  }
210 
211 
215  template <class _otherVectorOwnerType>
217  this->SetSize(otherVector.size());
218  this->Assign(otherVector);
219  }
220 
221 
226  template <class _otherVectorOwnerType, typename _otherVectorElementType>
228  this->SetSize(otherVector.size());
229  this->Assign(otherVector);
230  }
231 
233  template <size_type __size, stride_type __stride, class __elementType, class __dataPtrType>
235  this->SetSize(__size);
236  this->Assign(fixedVector);
237  }
238 
244  template <class __vectorOwnerType, typename __elementType>
246  this->SetSize(otherVector.size());
247  this->Assign(otherVector);
248  return *this;
249  }
250 
251 
257  ThisType & operator = (const ThisType& other) {
258  this->SetSize(other.size());
259  this->Assign(other);
260  return *this;
261  }
262 
271 
273  inline ThisType & operator = (const value_type & value) {
274  this->SetAll(value);
275  return *this;
276  }
277 
278  // documented in base class
279  template <class __vectorOwnerType, typename __elementType>
281  this->SetSize(other.size());
282  this->Assign(other);
283  return *this;
284  }
285 
286  // documented in base class
287  template <size_type __size, stride_type __stride, class __elementType, class __dataPtrType>
289  & other) {
290  this->SetSize(other.size());
291  this->Assign(other);
292  return *this;
293  }
294 
300  const size_type oldSize = this->size();
301  if (oldSize == size)
302  return;
303  ThisType newData(size);
304  size_type minSizes = std::min(size, oldSize);
305  const size_type corner = 0;
306  vctDynamicConstVectorRef<value_type> myDataMinSpaceRef(*this, corner, minSizes);
307  vctDynamicVectorRef<value_type> newDataMinSpaceRef(newData, corner, minSizes);
308  newDataMinSpaceRef.Assign(myDataMinSpaceRef);
309  this->Vector.Disown();
310  this->Vector.Own(size, newData.Vector.Release());
311  }
312 
316  this->Vector.SetSize(size);
317  }
318 
322  bool FromStreamRaw(std::istream & inputStream, const char delimiter = ' ')
323  {
324  size_type size = this->size();
325  _elementType *temp = new _elementType[size];
326  size_type index;
327  bool valid = true;
328  for (index = 0; index < size; index++) {
329  inputStream >> temp[index]; // assumes that operator >> is defined for _elementType
330  if (inputStream.fail()) {
331  valid = false;
332  inputStream.clear();
333  break;
334  }
335  // Now, look for the delimiter
336  if (!isspace(delimiter)) {
337  if (index < size-1) {
338  char c;
339  inputStream >> c;
340  if (c != delimiter) {
341  valid = false;
342  break;
343  }
344  }
345  }
346  }
347  if (valid) {
348  // Only update the object if the parsing was successful for all elements.
349  for (index = 0; index < size; index++)
350  (*this)[index] = temp[index];
351  }
352  delete [] temp;
353  return valid;
354  }
355 
357  void DeSerializeRaw(std::istream & inputStream)
358  {
359  // get and set size
360  size_type mySize = 0;
361  cmnDeSerializeSizeRaw(inputStream, mySize);
362  this->SetSize(mySize);
363 
364  // get data
365  size_type index;
366  for (index = 0; index < mySize; ++index) {
367  cmnDeSerializeRaw(inputStream, this->Element(index));
368  }
369  }
370 
371 };
372 
373 
391 template <class _elementType>
392 class vctReturnDynamicVector : public vctDynamicVector<_elementType> {
393 public:
396  explicit vctReturnDynamicVector(const BaseType & other) {
397  BaseType & nonConstOther = const_cast<BaseType &>(other);
398  // if we don't save it in a variable, it will be destroyed in the Release operation
399  const vct::size_type size = other.size();
400  this->Vector.Own(size, nonConstOther.Vector.Release());
401  }
402 };
403 
404 
405 // implementation of the special copy constuctor of vctDynamicVector
406 template <class _elementType>
408  vctReturnDynamicVector<_elementType> & nonConstOther =
409  const_cast< vctReturnDynamicVector<_elementType> & >(other);
410  // if we don't save it in a variable, it will be destroyed in the Release operation
411  const size_type size = other.size();
412  this->Vector.Own(size, nonConstOther.Vector.Release());
413 }
414 
415 
416 // implementation of the special assignment operator from vctReturnDynamicVector to vctDynamicVector
417 template <class _elementType>
420  vctReturnDynamicVector<_elementType> & nonConstOther =
421  const_cast< vctReturnDynamicVector<_elementType> & >(other);
422  // if we don't save it in a variable, it will be destroyed in the Release operation
423  const vct::size_type size = other.size();
424  this->Vector.Disown();
425  this->Vector.Own(size, nonConstOther.Vector.Release());
426  return *this;
427 }
428 
429 
437 template <class _vectorOwnerType1, class _vectorOwnerType2, class _elementType>
441  typedef _elementType value_type;
442  vctDynamicVector<value_type> resultStorage(inputVector1);
443  resultStorage.Add(inputVector2);
444  return vctReturnDynamicVector<value_type>(resultStorage);
445 }
446 
447 /* documented above */
448 template <class _vectorOwnerType1, class _vectorOwnerType2, class _elementType>
452  typedef _elementType value_type;
453  vctDynamicVector<value_type> resultStorage(inputVector1);
454  resultStorage.Subtract(inputVector2);
455  return vctReturnDynamicVector<value_type>(resultStorage);
456 }
458 
459 
471 template <class _vectorOwnerType1, class _vectorOwnerType2, class _elementType>
475  typedef _elementType value_type;
476  vctDynamicVector<value_type> resultStorage(3);
477  resultStorage.CrossProductOf(inputVector1, inputVector2);
478  return vctReturnDynamicVector<value_type>(resultStorage);
479 }
480 
481 template <class _vectorOwnerType1, class _vectorOwnerType2, class _elementType>
485  typedef _elementType value_type;
486  vctDynamicVector<value_type> resultStorage(3);
487  resultStorage.CrossProductOf(inputVector1, inputVector2);
488  return vctReturnDynamicVector<value_type>(resultStorage);
489 }
491 
492 
493 
502 template <class _vectorOwnerType, class _elementType>
505  const _elementType & inputScalar) {
506  typedef _elementType value_type;
507  vctDynamicVector<value_type> resultStorage(inputVector);
508  resultStorage.Add(inputScalar);
509  return vctReturnDynamicVector<value_type>(resultStorage);
510 }
511 
512 /* documented above */
513 template <class _vectorOwnerType, class _elementType>
516  const _elementType & inputScalar) {
517  typedef _elementType value_type;
518  vctDynamicVector<value_type> resultStorage(inputVector);
519  resultStorage.Subtract(inputScalar);
520  return vctReturnDynamicVector<value_type>(resultStorage);
521 }
522 
523 /* documented above */
524 template <class _vectorOwnerType, class _elementType>
527  const _elementType & inputScalar) {
528  typedef _elementType value_type;
529  vctDynamicVector<value_type> resultStorage(inputVector);
530  resultStorage.Multiply(inputScalar);
531  return vctReturnDynamicVector<value_type>(resultStorage);
532 }
533 
534 /* documented above */
535 template <class _vectorOwnerType, class _elementType>
538  const _elementType & inputScalar) {
539  typedef _elementType value_type;
540  vctDynamicVector<value_type> resultStorage(inputVector);
541  resultStorage.Divide(inputScalar);
542  return vctReturnDynamicVector<value_type>(resultStorage);
543 }
545 
546 
547 
556 template <class _vectorOwnerType, class _elementType>
558 operator + (const _elementType & inputScalar,
560  typedef _elementType value_type;
561  vctDynamicVector<value_type> resultStorage(inputVector.size());
562  resultStorage.SumOf(inputScalar, inputVector);
563  return vctReturnDynamicVector<value_type>(resultStorage);
564 }
565 
566 /* documented above */
567 template <class _vectorOwnerType, class _elementType>
569 operator - (const _elementType & inputScalar,
571  typedef _elementType value_type;
572  vctDynamicVector<value_type> resultStorage(inputVector.size());
573  resultStorage.DifferenceOf(inputScalar, inputVector);
574  return vctReturnDynamicVector<value_type>(resultStorage);
575 }
576 
577 /* documented above */
578 template <class _vectorOwnerType, class _elementType>
580 operator * (const _elementType & inputScalar,
582  typedef _elementType value_type;
583  vctDynamicVector<value_type> resultStorage(inputVector.size());
584  resultStorage.ProductOf(inputScalar, inputVector);
585  return vctReturnDynamicVector<value_type>(resultStorage);
586 }
587 
588 /* documented above */
589 template <class _vectorOwnerType, class _elementType>
591 operator / (const _elementType & inputScalar,
593  typedef _elementType value_type;
594  vctDynamicVector<value_type> resultStorage(inputVector.size());
595  resultStorage.RatioOf(inputScalar, inputVector);
596  return vctReturnDynamicVector<value_type>(resultStorage);
597 }
599 
600 
607 template <class _vectorOwnerType, class _elementType>
610  typedef _elementType value_type;
611  vctDynamicVector<value_type> resultStorage(inputVector.size());
612  resultStorage.NegationOf(inputVector);
613  return vctReturnDynamicVector<value_type>(resultStorage);
614 }
616 
617 
618 
619 /*
620  Methods declared previously and implemented here because they require vctReturnDynamicVector
621 */
622 
623 #ifndef DOXYGEN
624 /* documented in class vctDynamicConstVectorBase */
625 template <class _vectorOwnerType, class _elementType>
628  typedef _elementType value_type;
629  vctDynamicVector<value_type> resultStorage(this->size());
632  Run(resultStorage, *this);
633  return vctReturnDynamicVector<value_type>(resultStorage);
634 }
635 
636 /* documented in class vctDynamicConstVectorBase */
637 template <class _vectorOwnerType, class _elementType>
640  typedef _elementType value_type;
641  vctDynamicVector<value_type> resultStorage(this->size());
644  Run(resultStorage, *this);
645  return vctReturnDynamicVector<value_type>(resultStorage);
646 }
647 
648 /* documented in class vctDynamicConstVectorBase */
649 template <class _vectorOwnerType, class _elementType>
652  typedef _elementType value_type;
653  vctDynamicVector<value_type> resultStorage(this->size());
656  Run(resultStorage, *this);
657  return vctReturnDynamicVector<value_type>(resultStorage);
658 }
659 
660 /* documented in class vctDynamicConstVectorBase */
661 template <class _vectorOwnerType, class _elementType>
664  typedef _elementType value_type;
665  vctDynamicVector<value_type> resultStorage(this->size());
668  Run(resultStorage, *this);
669  return vctReturnDynamicVector<value_type>(resultStorage);
670 }
671 
672 /* documented in class vctDynamicConstVectorBase */
673 template <class _vectorOwnerType, class _elementType>
676  vctDynamicVector<value_type> resultStorage(*this);
677  resultStorage.NormalizedSelf();
678  return vctReturnDynamicVector<value_type>(resultStorage);
679 }
680 
681 /* documented in class vctDynamicConstVectorBase */
682 template <class _vectorOwnerType, class __vectorOwnerType, class _elementType,
683  class _elementOperationType>
687  vctDynamicVector<bool> result(vector1.size());
689  VoViVi<_elementOperationType>::Run(result, vector1, vector2);
690  return vctReturnDynamicVector<bool>(result);
691 }
692 
693 /* documented in class vctDynamicConstVectorBase */
694 template <class _vectorOwnerType, class _elementType, class _elementOperationType>
697  const _elementType & scalar) {
698  vctDynamicVector<bool> result(vector.size());
700  VoViSi<_elementOperationType>::Run(result, vector, scalar);
701  return vctReturnDynamicVector<bool>(result);
702 }
703 
704 #endif // DOXYGEN
705 
706 #endif // _vctDynamicVector_h
size_t index_type
Definition: vctContainerTraits.h:36
OwnerType Vector
Definition: vctDynamicConstVectorBase.h:126
vctDynamicVector(size_type size, value_type element0, value_type element1,...)
Definition: vctDynamicVector.h:171
A vector object of dynamic size.
Definition: vctDynamicVector.h:127
Declaration of vctDynamicVectorOwner.
vctReturnDynamicVector(const BaseType &other)
Definition: vctDynamicVector.h:396
VectorReturnType Ceil(void) const
Definition: vctDynamicVector.h:663
Dynamic vector referencing existing memory (const)
Definition: vctDynamicConstVectorRef.h:79
ThisType & Subtract(const vctDynamicConstVectorBase< __vectorOwnerType, _elementType > &otherVector)
Definition: vctDynamicVectorBase.h:825
static void Run(_outputVectorType &outputVector, const _inputVectorType &inputVector, const _inputScalarType inputScalar)
Definition: vctDynamicVectorLoopEngines.h:337
static void Run(_outputVectorType &outputVector, const _input1VectorType &input1Vector, const _input2VectorType &input2Vector)
Definition: vctDynamicVectorLoopEngines.h:105
vctReturnDynamicVector< bool > vctDynamicVectorElementwiseCompareVector(const vctDynamicConstVectorBase< _vectorOwnerType, _elementType > &vector1, const vctDynamicConstVectorBase< _vectorOwnerType, _elementType > &vector2)
Definition: vctDynamicVector.h:685
BaseType::ElementVaArgPromotion ElementVaArgPromotion
Definition: vctDynamicVector.h:138
vctDynamicVector(const vctDynamicConstVectorBase< _otherVectorOwnerType, value_type > &otherVector)
Definition: vctDynamicVector.h:216
void cmnDeSerializeSizeRaw(std::istream &inputStream, size_t &data)
Definition: cmnDeSerializer.h:96
ThisType & SumOf(const vctDynamicConstVectorBase< __vectorOwnerType1, _elementType > &vector1, const vctDynamicConstVectorBase< __vectorOwnerType2, _elementType > &vector2)
Definition: vctDynamicVectorBase.h:728
ThisType & Assign(const vctDynamicConstVectorBase< __vectorOwnerType, value_type > &other)
Definition: vctDynamicVectorBase.h:242
Dynamic vector referencing existing memory.
Definition: vctDynamicVectorRef.h:77
Implement operation of the form for dynamic vectors.
Definition: vctDynamicVectorLoopEngines.h:535
size_t size_type
Definition: vctContainerTraits.h:35
vctDynamicVector< _elementType > BaseType
Definition: vctDynamicVector.h:395
TypeTraits::VaArgPromotion ElementVaArgPromotion
Definition: vctDynamicVectorBase.h:85
vctDynamicVector()
Definition: vctDynamicVector.h:141
vctDynamicVector(const vctFixedSizeConstVectorBase< __size, __stride, __elementType, __dataPtrType > &fixedVector)
Definition: vctDynamicVector.h:234
void SetSize(size_type size)
Definition: vctDynamicVector.h:315
vctReturnDynamicVector< _elementType > operator-(const vctDynamicConstVectorBase< _vectorOwnerType1, _elementType > &inputVector1, const vctDynamicConstVectorBase< _vectorOwnerType2, _elementType > &inputVector2)
Definition: vctDynamicVector.h:450
void DeSerializeRaw(std::istream &inputStream)
Definition: vctDynamicVector.h:357
ThisType & ForceAssign(const vctDynamicConstVectorBase< __vectorOwnerType, __elementType > &other)
Definition: vctDynamicVector.h:280
ThisType & Divide(const value_type scalar)
Definition: vctDynamicVectorBase.h:1118
BaseType::TypeTraits TypeTraits
Definition: vctDynamicVector.h:137
size_type size(void) const
Definition: vctDynamicConstVectorBase.h:164
ThisType & RatioOf(const vctDynamicConstVectorBase< __vectorOwnerType, _elementType > &vector, const value_type scalar)
Definition: vctDynamicVectorBase.h:951
vctReturnDynamicVector< _elementType > operator*(const vctDynamicConstVectorBase< _vectorOwnerType, _elementType > &inputVector, const _elementType &inputScalar)
Definition: vctDynamicVector.h:526
vctDynamicVector(const ThisType &otherVector)
Definition: vctDynamicVector.h:204
reference at(index_type index)
Definition: vctDynamicVectorBase.h:170
Definition: vctDynamicVector.h:392
void resize(size_type size)
Definition: vctDynamicVector.h:299
reference Element(index_type index)
Definition: vctDynamicVectorBase.h:195
void cmnDeSerializeRaw(std::istream &inputStream, _elementType &data)
Definition: cmnDeSerializer.h:82
ThisType & operator=(const vctDynamicConstVectorBase< __vectorOwnerType, __elementType > &otherVector)
Definition: vctDynamicVector.h:245
size_type size(void) const
Definition: vctFixedSizeConstVectorBase.h:205
VectorReturnType Negation(void) const
Definition: vctDynamicVector.h:639
ThisType & NormalizedSelf(void)
Definition: vctDynamicVectorBase.h:1361
ThisType & Add(const vctDynamicConstVectorBase< __vectorOwnerType, _elementType > &otherVector)
Definition: vctDynamicVectorBase.h:816
VectorReturnType Floor(void) const
Definition: vctDynamicVector.h:651
#define cmnThrow(a)
Definition: MinimalCmn.h:4
vctReturnDynamicVector< bool > vctDynamicVectorElementwiseCompareScalar(const vctDynamicConstVectorBase< _vectorOwnerType, _elementType > &vector, const _elementType &scalar)
Definition: vctDynamicVector.h:696
Declaration of vctDynamicVectorBase.
ThisType & ForceAssign(const vctFixedSizeConstVectorBase< __size, __stride, __elementType, __dataPtrType > &other)
Definition: vctDynamicVector.h:288
Declaration of vctDynamicVectorRef.
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
value_type SetAll(const value_type value)
Definition: vctDynamicVectorBase.h:209
vctReturnDynamicVector< _elementType > vctCrossProduct(const vctDynamicConstVectorBase< _vectorOwnerType1, _elementType > &inputVector1, const vctDynamicConstVectorBase< _vectorOwnerType2, _elementType > &inputVector2)
Definition: vctDynamicVector.h:483
vctDynamicVector(size_type size)
Definition: vctDynamicVector.h:148
VectorReturnType Normalized(void) const
Definition: vctDynamicVector.h:675
vctDynamicVectorBase< vctDynamicVectorOwner< _elementType >, _elementType > BaseType
Definition: vctDynamicVector.h:135
ThisType & DifferenceOf(const vctDynamicConstVectorBase< __vectorOwnerType1, _elementType > &vector1, const vctDynamicConstVectorBase< __vectorOwnerType2, _elementType > &vector2)
Definition: vctDynamicVectorBase.h:738
ThisType & Multiply(const value_type scalar)
Definition: vctDynamicVectorBase.h:1110
Definition: vctDynamicConstVectorBase.h:77
vctReturnDynamicVector< _elementType > operator%(const vctDynamicConstVectorBase< _vectorOwnerType1, _elementType > &inputVector1, const vctDynamicConstVectorBase< _vectorOwnerType2, _elementType > &inputVector2)
Definition: vctDynamicVector.h:473
A template for a fixed length vector with fixed spacing in memory.
Definition: vctFixedSizeConstVectorBase.h:107
BaseType::CopyType CopyType
Definition: vctDynamicVector.h:136
Declaration of cmnDeSerializer and functions cmnDeSerializeRaw.
VectorReturnType Abs(void) const
Definition: vctDynamicVector.h:627
A collection of useful information about the C++ basic types, represented in a generic programming wa...
Definition: cmnTypeTraits.h:155
bool FromStreamRaw(std::istream &inputStream, const char delimiter= ' ')
Definition: vctDynamicVector.h:322
ThisType & NegationOf(const vctDynamicConstVectorBase< __vectorOwnerType, _elementType > &otherVector)
Definition: vctDynamicVectorBase.h:1285
vctDynamicVector(const vctDynamicConstVectorBase< _otherVectorOwnerType, _otherVectorElementType > &otherVector)
Definition: vctDynamicVector.h:227
void CrossProductOf(const vctDynamicConstVectorBase< __vectorOwnerType1, _elementType > &inputVector1, const vctDynamicConstVectorBase< __vectorOwnerType2, _elementType > &inputVector2)
Definition: vctDynamicVectorBase.h:695
vctDynamicVector(size_type size, const value_type *values)
Definition: vctDynamicVector.h:188
ThisType & ProductOf(const vctDynamicConstVectorBase< __vectorOwnerType, _elementType > &vector, const value_type scalar)
Definition: vctDynamicVectorBase.h:941
vctDynamicVector(size_type size, value_type value)
Definition: vctDynamicVector.h:154
vctReturnDynamicVector< _elementType > operator+(const vctDynamicConstVectorBase< _vectorOwnerType1, _elementType > &inputVector1, const vctDynamicConstVectorBase< _vectorOwnerType2, _elementType > &inputVector2)
Definition: vctDynamicVector.h:439
vctDynamicVector< _elementType > ThisType
Definition: vctDynamicVector.h:134
vctReturnDynamicVector< _elementType > operator/(const vctDynamicConstVectorBase< _vectorOwnerType, _elementType > &inputVector, const _elementType &inputScalar)
Definition: vctDynamicVector.h:537
Definition: vctDynamicVectorBase.h:61