cisst-saw
Loading...
Searching...
No Matches
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 Author(s): Ofri Sadowsky, Anton Deguet
6 Created on: 2004-07-01
7
8 (C) Copyright 2004-2018 Johns Hopkins University (JHU), All Rights Reserved.
9
10--- begin cisst license - do not edit ---
11
12This software is provided "as is" under an open source license, with
13no warranty. The complete license can be found in license.txt and
14http://www.cisst.org/cisst/license.txt.
15
16--- end cisst license ---
17*/
18
19#pragma once
20#ifndef _vctDynamicVector_h
21#define _vctDynamicVector_h
22
27
29
33
124template <class _elementType>
125class vctDynamicVector : public vctDynamicVectorBase<vctDynamicVectorOwner<_elementType>, _elementType>
126{
127
128 friend class vctReturnDynamicVector<_elementType>;
129
130public:
131 VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
134 typedef typename BaseType::CopyType CopyType;
135 typedef typename BaseType::TypeTraits TypeTraits;
136 typedef typename BaseType::ElementVaArgPromotion ElementVaArgPromotion;
137
140 // The default initialization of vctDynamicVectorOwner is empty.
141 {}
142
143
146 explicit vctDynamicVector(size_type size) {
147 this->SetSize(size);
148 }
149
152 vctDynamicVector(size_type size, value_type value) {
153 this->SetSize(size);
154 this->SetAll(value);
155 }
156
169 vctDynamicVector(size_type size, value_type element0, value_type element1, ...) CISST_THROW(std::runtime_error) {
170 if (size < 2) {
171 cmnThrow(std::runtime_error("vctDynamicVector: Constructor from va_list requires size >= 2"));
172 }
173 this->SetSize(size);
174 this->at(0) = element0;
175 this->at(1) = element1;
176 va_list nextArg;
177 va_start(nextArg, element1);
178 for (index_type i = 2; i < size; ++i) {
179 this->at(i) = value_type( va_arg(nextArg, ElementVaArgPromotion) );
180 }
181 va_end(nextArg);
182 }
183
186 vctDynamicVector(size_type size, const value_type * values) {
187 this->SetSize(size);
188 this->Assign(values);
189 }
190
196
197
202 vctDynamicVector(const ThisType & otherVector):
203 BaseType()
204 {
205 this->SetSize(otherVector.size());
206 this->Assign(otherVector);
207 }
208
209
213 template <class _otherVectorOwnerType>
215 this->SetSize(otherVector.size());
216 this->Assign(otherVector);
217 }
218
219
224 template <class _otherVectorOwnerType, typename _otherVectorElementType>
226 this->SetSize(otherVector.size());
227 this->Assign(otherVector);
228 }
229
231 template <size_type __size, stride_type __stride, class __elementType, class __dataPtrType>
233 this->SetSize(__size);
234 this->Assign(fixedVector);
235 }
236
242 template <class __vectorOwnerType, typename __elementType>
244 this->SetSize(otherVector.size());
245 this->Assign(otherVector);
246 return *this;
247 }
248
249
255 ThisType & operator = (const ThisType& other) {
256 this->SetSize(other.size());
257 this->Assign(other);
258 return *this;
259 }
260
269
271 inline ThisType & operator = (const value_type & value) {
272 this->SetAll(value);
273 return *this;
274 }
275
276 // documented in base class
277 template <class __vectorOwnerType, typename __elementType>
279 this->SetSize(other.size());
280 this->Assign(other);
281 return *this;
282 }
283
284 // documented in base class
285 template <size_type __size, stride_type __stride, class __elementType, class __dataPtrType>
287 & other) {
288 this->SetSize(other.size());
289 this->Assign(other);
290 return *this;
291 }
292
297 void resize(size_type size) {
298 const size_type oldSize = this->size();
299 if (oldSize == size)
300 return;
301 ThisType newData(size);
302 size_type minSizes = std::min(size, oldSize);
303 const size_type corner = 0;
304 vctDynamicConstVectorRef<value_type> myDataMinSpaceRef(*this, corner, minSizes);
305 vctDynamicVectorRef<value_type> newDataMinSpaceRef(newData, corner, minSizes);
306 newDataMinSpaceRef.Assign(myDataMinSpaceRef);
307 this->Vector.Disown();
308 this->Vector.Own(size, newData.Vector.Release());
309 }
310
313 void SetSize(size_type size) {
314 this->Vector.SetSize(size);
315 }
316
317 void SetSize(size_type size, const value_type newValue) {
318 this->Vector.SetSize(size);
319 this->SetAll(newValue);
320 }
321
325 bool FromStreamRaw(std::istream & inputStream, const char delimiter = ' ')
326 {
327 size_type size = this->size();
328 _elementType *temp = new _elementType[size];
329 size_type index;
330 bool valid = true;
331 for (index = 0; index < size; index++) {
332 inputStream >> temp[index]; // assumes that operator >> is defined for _elementType
333 if (inputStream.fail()) {
334 valid = false;
335 inputStream.clear();
336 break;
337 }
338 // Now, look for the delimiter
339 if (!isspace(delimiter)) {
340 if (index < size-1) {
341 char c;
342 inputStream >> c;
343 if (c != delimiter) {
344 valid = false;
345 break;
346 }
347 }
348 }
349 }
350 if (valid) {
351 // Only update the object if the parsing was successful for all elements.
352 for (index = 0; index < size; index++)
353 (*this)[index] = temp[index];
354 }
355 delete [] temp;
356 return valid;
357 }
358
360 void DeSerializeRaw(std::istream & inputStream)
361 {
362 // get and set size
363 size_type mySize = 0;
364 cmnDeSerializeSizeRaw(inputStream, mySize);
365 this->SetSize(mySize);
366
367 // get data
368 size_type index;
369 for (index = 0; index < mySize; ++index) {
370 cmnDeSerializeRaw(inputStream, this->Element(index));
371 }
372 }
373
374};
375
376
394template <class _elementType>
395class vctReturnDynamicVector : public vctDynamicVector<_elementType> {
396public:
399 explicit vctReturnDynamicVector(const BaseType & other) {
400 BaseType & nonConstOther = const_cast<BaseType &>(other);
401 // if we don't save it in a variable, it will be destroyed in the Release operation
402 const vct::size_type size = other.size();
403 this->Vector.Own(size, nonConstOther.Vector.Release());
404 }
405};
406
407
408// implementation of the special copy constuctor of vctDynamicVector
409template <class _elementType>
412 const_cast< vctReturnDynamicVector<_elementType> & >(other);
413 // if we don't save it in a variable, it will be destroyed in the Release operation
414 const size_type size = other.size();
415 this->Vector.Own(size, nonConstOther.Vector.Release());
416}
417
418
419// implementation of the special assignment operator from vctReturnDynamicVector to vctDynamicVector
420template <class _elementType>
424 const_cast< vctReturnDynamicVector<_elementType> & >(other);
425 // if we don't save it in a variable, it will be destroyed in the Release operation
426 const vct::size_type size = other.size();
427 this->Vector.Disown();
428 this->Vector.Own(size, nonConstOther.Vector.Release());
429 return *this;
430}
431
432
435
440template <class _vectorOwnerType1, class _vectorOwnerType2, class _elementType>
444 typedef _elementType value_type;
445 vctDynamicVector<value_type> resultStorage(inputVector1);
446 resultStorage.Add(inputVector2);
447 return vctReturnDynamicVector<value_type>(resultStorage);
448}
449
450/* documented above */
451template <class _vectorOwnerType1, class _vectorOwnerType2, class _elementType>
455 typedef _elementType value_type;
456 vctDynamicVector<value_type> resultStorage(inputVector1);
457 resultStorage.Subtract(inputVector2);
458 return vctReturnDynamicVector<value_type>(resultStorage);
459}
460
461
462
474template <class _vectorOwnerType1, class _vectorOwnerType2, class _elementType>
478 typedef _elementType value_type;
479 vctDynamicVector<value_type> resultStorage(3);
480 resultStorage.CrossProductOf(inputVector1, inputVector2);
481 return vctReturnDynamicVector<value_type>(resultStorage);
482}
483
484template <class _vectorOwnerType1, class _vectorOwnerType2, class _elementType>
488 typedef _elementType value_type;
489 vctDynamicVector<value_type> resultStorage(3);
490 resultStorage.CrossProductOf(inputVector1, inputVector2);
491 return vctReturnDynamicVector<value_type>(resultStorage);
492}
493
494
495
496
500
505template <class _vectorOwnerType, class _elementType>
508 const _elementType & inputScalar) {
509 typedef _elementType value_type;
510 vctDynamicVector<value_type> resultStorage(inputVector);
511 resultStorage.Add(inputScalar);
512 return vctReturnDynamicVector<value_type>(resultStorage);
513}
514
515/* documented above */
516template <class _vectorOwnerType, class _elementType>
519 const _elementType & inputScalar) {
520 typedef _elementType value_type;
521 vctDynamicVector<value_type> resultStorage(inputVector);
522 resultStorage.Subtract(inputScalar);
523 return vctReturnDynamicVector<value_type>(resultStorage);
524}
525
526/* documented above */
527template <class _vectorOwnerType, class _elementType>
530 const _elementType & inputScalar) {
531 typedef _elementType value_type;
532 vctDynamicVector<value_type> resultStorage(inputVector);
533 resultStorage.Multiply(inputScalar);
534 return vctReturnDynamicVector<value_type>(resultStorage);
535}
536
537/* documented above */
538template <class _vectorOwnerType, class _elementType>
541 const _elementType & inputScalar) {
542 typedef _elementType value_type;
543 vctDynamicVector<value_type> resultStorage(inputVector);
544 resultStorage.Divide(inputScalar);
545 return vctReturnDynamicVector<value_type>(resultStorage);
546}
547
548
549
550
554
559template <class _vectorOwnerType, class _elementType>
561operator + (const _elementType & inputScalar,
563 typedef _elementType value_type;
564 vctDynamicVector<value_type> resultStorage(inputVector.size());
565 resultStorage.SumOf(inputScalar, inputVector);
566 return vctReturnDynamicVector<value_type>(resultStorage);
567}
568
569/* documented above */
570template <class _vectorOwnerType, class _elementType>
572operator - (const _elementType & inputScalar,
574 typedef _elementType value_type;
575 vctDynamicVector<value_type> resultStorage(inputVector.size());
576 resultStorage.DifferenceOf(inputScalar, inputVector);
577 return vctReturnDynamicVector<value_type>(resultStorage);
578}
579
580/* documented above */
581template <class _vectorOwnerType, class _elementType>
583operator * (const _elementType & inputScalar,
585 typedef _elementType value_type;
586 vctDynamicVector<value_type> resultStorage(inputVector.size());
587 resultStorage.ProductOf(inputScalar, inputVector);
588 return vctReturnDynamicVector<value_type>(resultStorage);
589}
590
591/* documented above */
592template <class _vectorOwnerType, class _elementType>
594operator / (const _elementType & inputScalar,
596 typedef _elementType value_type;
597 vctDynamicVector<value_type> resultStorage(inputVector.size());
598 resultStorage.RatioOf(inputScalar, inputVector);
599 return vctReturnDynamicVector<value_type>(resultStorage);
600}
601
602
603
606
610template <class _vectorOwnerType, class _elementType>
613 typedef _elementType value_type;
614 vctDynamicVector<value_type> resultStorage(inputVector.size());
615 resultStorage.NegationOf(inputVector);
616 return vctReturnDynamicVector<value_type>(resultStorage);
617}
618
619
620
621
622/*
623 Methods declared previously and implemented here because they require vctReturnDynamicVector
624*/
625
626#ifndef DOXYGEN
627/* documented in class vctDynamicConstVectorBase */
628template <class _vectorOwnerType, class _elementType>
631 typedef _elementType value_type;
632 vctDynamicVector<value_type> resultStorage(this->size());
633 vctDynamicVectorLoopEngines::
634 VoVi<typename vctUnaryOperations<value_type>::AbsValue>::
635 Run(resultStorage, *this);
636 return vctReturnDynamicVector<value_type>(resultStorage);
637}
638
639/* documented in class vctDynamicConstVectorBase */
640template <class _vectorOwnerType, class _elementType>
643 typedef _elementType value_type;
644 vctDynamicVector<value_type> resultStorage(this->size());
645 vctDynamicVectorLoopEngines::
646 VoVi<typename vctUnaryOperations<value_type>::Negation>::
647 Run(resultStorage, *this);
648 return vctReturnDynamicVector<value_type>(resultStorage);
649}
650
651/* documented in class vctDynamicConstVectorBase */
652template <class _vectorOwnerType, class _elementType>
655 typedef _elementType value_type;
656 vctDynamicVector<value_type> resultStorage(this->size());
657 vctDynamicVectorLoopEngines::
658 VoVi<typename vctUnaryOperations<value_type>::Floor>::
659 Run(resultStorage, *this);
660 return vctReturnDynamicVector<value_type>(resultStorage);
661}
662
663/* documented in class vctDynamicConstVectorBase */
664template <class _vectorOwnerType, class _elementType>
667 typedef _elementType value_type;
668 vctDynamicVector<value_type> resultStorage(this->size());
669 vctDynamicVectorLoopEngines::
670 VoVi<typename vctUnaryOperations<value_type>::Ceil>::
671 Run(resultStorage, *this);
672 return vctReturnDynamicVector<value_type>(resultStorage);
673}
674
675/* documented in class vctDynamicConstVectorBase */
676template <class _vectorOwnerType, class _elementType>
679 vctDynamicVector<value_type> resultStorage(*this);
680 resultStorage.NormalizedSelf();
681 return vctReturnDynamicVector<value_type>(resultStorage);
682}
683
684/* documented in class vctDynamicConstVectorBase */
685template <class _vectorOwnerType, class __vectorOwnerType, class _elementType,
686 class _elementOperationType>
695
696/* documented in class vctDynamicConstVectorBase */
697template <class _vectorOwnerType, class _elementType, class _elementOperationType>
706
707#endif // DOXYGEN
708
709#endif // _vctDynamicVector_h
_elementType value_type
Definition mtsGenericObjectProxy.h:329
Definition vctForwardDeclarations.h:119
Dynamic vector referencing existing memory (const).
Definition vctDynamicConstVectorRef.h:79
Definition vctDynamicVectorBase.h:62
ThisType & Assign(const vctDynamicConstVectorBase< __vectorOwnerType, value_type > &other)
Definition vctDynamicVectorBase.h:242
Definition vctForwardDeclarations.h:131
static void Run(_outputVectorType &outputVector, const _inputVectorType &inputVector, const _inputScalarType inputScalar)
Definition vctDynamicVectorLoopEngines.h:339
static void Run(_outputVectorType &outputVector, const _input1VectorType &input1Vector, const _input2VectorType &input2Vector)
Definition vctDynamicVectorLoopEngines.h:106
Dynamic vector referencing existing memory.
Definition vctDynamicVectorRef.h:78
A template for a fixed length vector with fixed spacing in memory.
Definition vctFixedSizeConstVectorBase.h:108
size_type size(void) const
Definition vctFixedSizeConstVectorBase.h:205
Definition vctDynamicVector.h:395
vctReturnDynamicVector(const BaseType &other)
Definition vctDynamicVector.h:399
vctDynamicVector< BoolType > BaseType
Definition vctDynamicVector.h:398
Declaration of cmnDeSerializer and functions cmnDeSerializeRaw.
void cmnDeSerializeSizeRaw(std::istream &inputStream, size_t &data) CISST_THROW(std
Definition cmnDeSerializer.h:96
void cmnDeSerializeRaw(std::istream &inputStream, _elementType &data) CISST_THROW(std
Definition cmnDeSerializer.h:82
#define CISST_THROW(exceptionParameter)
Somewhat portable compilation warning message. This works with very recent versions of gcc (4....
Definition cmnPortability.h:559
void cmnThrow(const _exceptionType &except, cmnLogLevel lod=CMN_LOG_LEVEL_INIT_ERROR)
Definition cmnThrow.h:76
vctDynamicVector()
A vector object of dynamic size.
Definition vctDynamicVector.h:139
STL namespace.
size_t size_type
Definition vctContainerTraits.h:35
#define VCT_CONTAINER_TRAITS_TYPEDEFS(type)
Definition vctContainerTraits.h:50
const_reference at(size_type index) const CISST_THROW(std
Definition vctDynamicConstMatrixBase.h:289
cmnTypeTraits< value_type > TypeTraits
Definition vctDynamicConstMatrixBase.h:147
size_type size(void) const
Definition vctDynamicConstMatrixBase.h:228
const_reference Element(size_type rowIndex, size_type colIndex) const
Definition vctDynamicConstMatrixBase.h:362
vctDynamicConstMatrixBase< _matrixOwnerType, _elementType > ThisType
Definition vctDynamicConstMatrixBase.h:86
OwnerType Vector
Definition vctDynamicConstVectorBase.h:126
vctReturnDynamicVector< bool > vctDynamicVectorElementwiseCompareScalar(const vctDynamicConstVectorBase< _vectorOwnerType, _elementType > &vector, const _elementType &scalar)
Definition vctDynamicVector.h:699
vctReturnDynamicVector< bool > vctDynamicVectorElementwiseCompareVector(const vctDynamicConstVectorBase< _vectorOwnerType, _elementType > &vector1, const vctDynamicConstVectorBase< _vectorOwnerType, _elementType > &vector2)
Definition vctDynamicVector.h:688
vctDynamicVector< _elementType > CopyType
Definition vctDynamicConstVectorBase.h:102
ThisType & ForceAssign(const vctDynamicConstMatrixBase< __matrixOwnerType, __elementType > &other)
Definition vctDynamicMatrix.h:306
void SetSize(size_type rows, size_type cols, bool storageOrder)
Definition vctDynamicMatrix.h:364
void resize(size_type rows, size_type cols)
Definition vctDynamicMatrix.h:338
void DeSerializeRaw(std::istream &inputStream)
Definition vctDynamicMatrix.h:382
ThisType & operator=(const vctDynamicConstMatrixBase< __matrixOwnerType, __elementType > &otherMatrix)
Definition vctDynamicMatrix.h:255
vctDynamicNArrayBase< vctDynamicNArrayOwner< _elementType, _dimension >, _elementType, _dimension > BaseType
Definition vctDynamicNArray.h:133
vctReturnDynamicVector< _elementType > operator-(const vctDynamicConstVectorBase< _vectorOwnerType1, _elementType > &inputVector1, const vctDynamicConstVectorBase< _vectorOwnerType2, _elementType > &inputVector2)
Definition vctDynamicVector.h:453
bool FromStreamRaw(std::istream &inputStream, const char delimiter=' ')
Definition vctDynamicVector.h:325
vctReturnDynamicVector< _elementType > operator%(const vctDynamicConstVectorBase< _vectorOwnerType1, _elementType > &inputVector1, const vctDynamicConstVectorBase< _vectorOwnerType2, _elementType > &inputVector2)
Definition vctDynamicVector.h:476
vctReturnDynamicVector< _elementType > vctCrossProduct(const vctDynamicConstVectorBase< _vectorOwnerType1, _elementType > &inputVector1, const vctDynamicConstVectorBase< _vectorOwnerType2, _elementType > &inputVector2)
Definition vctDynamicVector.h:486
vctReturnDynamicVector< _elementType > operator+(const vctDynamicConstVectorBase< _vectorOwnerType1, _elementType > &inputVector1, const vctDynamicConstVectorBase< _vectorOwnerType2, _elementType > &inputVector2)
Definition vctDynamicVector.h:442
vctReturnDynamicVector< _elementType > operator/(const vctDynamicConstVectorBase< _vectorOwnerType, _elementType > &inputVector, const _elementType &inputScalar)
Definition vctDynamicVector.h:540
vctReturnDynamicVector< _elementType > operator*(const vctDynamicConstVectorBase< _vectorOwnerType, _elementType > &inputVector, const _elementType &inputScalar)
Definition vctDynamicVector.h:529
Declaration of vctDynamicVectorBase.
Declaration of vctDynamicVectorOwner.
Declaration of vctDynamicVectorRef.