cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vctDynamicNArray.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): Daniel Li, Ofri Sadowsky, Anton Deguet
7  Created on: 2006-07-10
8 
9  (C) Copyright 2006-2009 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 _vctDynamicNArray_h
23 #define _vctDynamicNArray_h
24 
33 
121 template<class _elementType, vct::size_type _dimension>
123  public vctDynamicNArrayBase<vctDynamicNArrayOwner<_elementType, _dimension>, _elementType, _dimension>
124 {
125 
126  friend class vctReturnDynamicNArray<_elementType, _dimension>;
127 
128 public:
129  /* define most types from vctContainerTraits and vctNArrayTraits */
130  VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
131  VCT_NARRAY_TRAITS_TYPEDEFS(_dimension);
132 
135 
138  // The default initialization of vctDynamicNArrayOwner is empty.
139  {}
140 
143  vctDynamicNArray(const nsize_type & sizes)
144  {
145  this->NArray.SetSize(sizes);
146  }
147 
150  vctDynamicNArray(const nsize_type & sizes, value_type value)
151  {
152  this->NArray.SetSize(sizes);
153  SetAll(value);
154  }
155 
161 
162 
166  vctDynamicNArray(const ThisType & otherNArray):
167  BaseType()
168  {
169  SetSize(otherNArray.sizes());
170  this->Assign(otherNArray);
171  }
172  template <class _otherNArrayOwnerType>
174  {
175  SetSize(otherNArray.sizes());
176  this->Assign(otherNArray);
177  }
179 
184  template <class _otherNArrayOwnerType, typename _otherNArrayElementType>
186  {
187  SetSize(otherNArray.sizes());
188  this->Assign(otherNArray);
189  }
190 
191 
197  template <class __nArrayOwnerType, typename __elementType>
199  {
200  SetSize(otherNArray.sizes());
201  this->Assign(otherNArray);
202  return *this;
203  }
204 
211  ThisType & operator = (const ThisType & otherNArray)
212  {
213  SetSize(otherNArray.sizes());
214  this->Assign(otherNArray);
215  return *this;
216  }
217 
218 
229 
231  inline ThisType & operator = (const value_type & value)
232  {
233  this->SetAll(value);
234  return *this;
235  }
236 
237  // documented in base class
238  template <class __nArrayOwnerType, typename __elementType>
240  this->SetSize(other.size());
241  return this->Assign(other);
242  }
243 
247  void SetSize(const nsize_type & sizes)
248  {
249  this->NArray.SetSize(sizes);
250  }
252 };
253 
254 
270 template <class _elementType, vct::size_type _dimension>
271 class vctReturnDynamicNArray : public vctDynamicNArray<_elementType, _dimension>
272 {
273 public:
274  VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
275  VCT_NARRAY_TRAITS_TYPEDEFS(_dimension);
276 
279 
280  explicit vctReturnDynamicNArray(const BaseType & other)
281  {
282  BaseType & nonConstOther = const_cast<BaseType &>(other);
283  // if we don't save it in a variable, it will be destroyed in the Release operation
284 
285  const nsize_type sizes = other.sizes();
286  this->NArray.Own(sizes, nonConstOther.NArray.Release());
287  }
288 };
289 
290 
291 // implementation of the special copy constructor of vctDynamicNArray
292 template <class _elementType, vct::size_type _dimension>
294 {
297  // if we don't save it in a variable, it will be destroyed in the Release operation
298 
299  const nsize_type sizes = other.sizes();
300  this->NArray.Own(sizes, nonConstOther.NArray.Release());
301 }
302 
303 
304 // implementation of the special assignment operator from vctReturnDynamicNArray to vctDynamicNArray
305 template <class _elementType, vct::size_type _dimension>
308 {
311  // if we don't save it in a variable, it will be destroyed in the Release operation
312 
313  const nsize_type sizes = other.sizes();
314  this->NArray.clear();
315  this->NArray.Own(sizes, nonConstOther.NArray.Release());
316  return *this;
317 }
318 
319 
327 template <class _nArrayOwnerType1, class _nArrayOwnerType2, class _elementType, vct::size_type _dimension>
331 {
332  typedef _elementType value_type;
333  vctDynamicNArray<value_type, _dimension> resultStorage(inputNArray1);
334  resultStorage.Add(inputNArray2);
336 }
337 
338 /* documented above */
339 template <class _nArrayOwnerType1, class _nArrayOwnerType2, class _elementType, vct::size_type _dimension>
343 {
344  typedef _elementType value_type;
345  vctDynamicNArray<value_type, _dimension> resultStorage(inputNArray1);
346  resultStorage.Subtract(inputNArray2);
348 }
350 
351 
352 
361 template <class _nArrayOwnerType, class _elementType, vct::size_type _dimension>
364  const _elementType & inputScalar)
365 {
366  typedef _elementType value_type;
367  vctDynamicNArray<value_type, _dimension> resultStorage(inputNArray);
368  resultStorage.Add(inputScalar);
370 }
371 
372 /* documented above */
373 template <class _nArrayOwnerType, class _elementType, vct::size_type _dimension>
376  const _elementType & inputScalar)
377 {
378  typedef _elementType value_type;
379  vctDynamicNArray<value_type, _dimension> resultStorage(inputNArray);
380  resultStorage.Subtract(inputScalar);
382 }
383 
384 /* documented above */
385 template <class _nArrayOwnerType, class _elementType, vct::size_type _dimension>
388  const _elementType & inputScalar)
389 {
390  typedef _elementType value_type;
391  vctDynamicNArray<value_type, _dimension> resultStorage(inputNArray);
392  resultStorage.Multiply(inputScalar);
394 }
395 
396 /* documented above */
397 template <class _nArrayOwnerType, class _elementType, vct::size_type _dimension>
400  const _elementType & inputScalar)
401 {
402  typedef _elementType value_type;
403  vctDynamicNArray<value_type, _dimension> resultStorage(inputNArray);
404  resultStorage.Divide(inputScalar);
406 }
408 
409 
410 
418 template <class _nArrayOwnerType, class _elementType, vct::size_type _dimension>
420 operator + (const _elementType & inputScalar,
422 {
423  typedef _elementType value_type;
424  vctDynamicNArray<value_type, _dimension> resultStorage(inputNArray.sizes());
425  resultStorage.SumOf(inputScalar, inputNArray);
427 }
428 
429 /* documented above */
430 template <class _nArrayOwnerType, class _elementType, vct::size_type _dimension>
432 operator - (const _elementType & inputScalar,
434 {
435  typedef _elementType value_type;
436  vctDynamicNArray<value_type, _dimension> resultStorage(inputNArray.sizes());
437  resultStorage.DifferenceOf(inputScalar, inputNArray);
439 }
440 
441 /* documented above */
442 template <class _nArrayOwnerType, class _elementType, vct::size_type _dimension>
444 operator * (const _elementType & inputScalar,
446 {
447  typedef _elementType value_type;
448  vctDynamicNArray<value_type, _dimension> resultStorage(inputNArray.sizes());
449  resultStorage.ProductOf(inputScalar, inputNArray);
451 }
452 
453 /* documented above */
454 template <class _nArrayOwnerType, class _elementType, vct::size_type _dimension>
456 operator / (const _elementType & inputScalar,
458 {
459  typedef _elementType value_type;
460  vctDynamicNArray<value_type, _dimension> resultStorage(inputNArray.sizes());
461  resultStorage.RatioOf(inputScalar, inputNArray);
463 }
465 
466 
473 template <class _nArrayOwnerType, class _elementType, vct::size_type _dimension>
476 {
477  typedef _elementType value_type;
478  vctDynamicNArray<value_type, _dimension> resultStorage(inputNArray.sizes());
479  resultStorage.NegationOf(inputNArray);
481 }
483 
484 
485 
486 #ifndef DOXYGEN
487 template <class _nArrayOwnerType1, class _nArrayOwnerType2, class _elementType, vct::size_type _dimension>
491 {
492  typedef _elementType value_type;
493  vctDynamicNArray<value_type, _dimension> resultStorage(inputNArray1.sizes());
494  resultStorage.ProductOf(inputNArray1, inputNArray2);
496 }
497 
498 
499 /*
500  Methods declared previously and implemented here because they require vctReturnDynamicNArray
501 */
502 
503 
504 /* documented in class vctDynamicConstNArrayBase */
505 template <class _nArrayOwnerType, class _elementType, vct::size_type _dimension>
508 {
509  typedef _elementType value_type;
510  vctDynamicNArray<value_type, _dimension> resultStorage(this->sizes());
512  NoNi<typename vctUnaryOperations<value_type>::AbsValue>::
513  Run(resultStorage, *this);
515 }
516 
517 /* documented in class vctDynamicConstNArrayBase */
518 template <class _nArrayOwnerType, class _elementType, vct::size_type _dimension>
521 {
522  typedef _elementType value_type;
523  vctDynamicNArray<value_type, _dimension> resultStorage(this->sizes());
525  NoNi<typename vctUnaryOperations<value_type>::Negation>::
526  Run(resultStorage, *this);
528 }
529 
530 /* documented in class vctDynamicConstNArrayBase */
531 template <class _nArrayOwnerType, class _elementType, vct::size_type _dimension>
534 {
535  typedef _elementType value_type;
536  vctDynamicNArray<value_type, _dimension> resultStorage(this->sizes());
538  NoNi<typename vctUnaryOperations<value_type>::Floor>::
539  Run(resultStorage, *this);
541 }
542 
543 /* documented in class vctDynamicConstNArrayBase */
544 template <class _nArrayOwnerType, class _elementType, vct::size_type _dimension>
547 {
548  typedef _elementType value_type;
549  vctDynamicNArray<value_type, _dimension> resultStorage(this->sizes());
551  NoNi<typename vctUnaryOperations<value_type>::Ceil>::
552  Run(resultStorage, *this);
554 }
555 
556 /* Documented in class vctDynamicConstNArrayBase */
557 template <class _nArrayOwnerType, class __nArrayOwnerType, class _elementType,
558  class _elementOperationType, vct::size_type _dimension>
562 {
563  vctDynamicNArray<bool, _dimension> result(nArray1.sizes());
565  NoNiNi<_elementOperationType>::Run(result, nArray1, nArray2);
567 }
568 
569 /* documented in class vctDynamicConstNArrayBase */
570 template <class _nArrayOwnerType, class _elementType, class _elementOperationType, vct::size_type _dimension>
573  const _elementType & scalar)
574 {
575  vctDynamicNArray<bool, _dimension> result(nArray.sizes());
577  NoNiSi<_elementOperationType>::Run(result, nArray, scalar);
579 }
580 
581 
582 /* documented in class vctDynamicConstNArrayBase */
583 template <class _nArrayOwnerType, class _elementType, vct::size_type _dimension>
584 inline vctDynamicNArrayRef<_elementType, _dimension - 1>
586  vct::size_type dimension,
587  vct::index_type index)
588 {
589  vctDynamicNArrayRef<_elementType, _dimension - 1> slice;
590  slice.SliceOf(input, dimension, index);
591  return slice;
592 }
593 
594 /* documented in class vctDynamicConstNArrayBase */
595 template <class _nArrayOwnerType, class _elementType>
596 inline _elementType &
598  vct::index_type index)
599 {
600  return input.Element(vctFixedSizeVector<vct::index_type, 1>(index));
601 }
602 
603 /* documented in class vctDynamicConstNArrayBase */
604 template <class _nArrayOwnerType, class _elementType, vct::size_type _dimension>
605 inline vctDynamicConstNArrayRef<_elementType, _dimension - 1>
607  vct::size_type dimension,
608  vct::index_type index)
609 {
610  vctDynamicConstNArrayRef<_elementType, _dimension - 1> slice;
611  slice.SliceOf(input, dimension, index);
612  return slice;
613 }
614 
615 /* documented in class vctDynamicConstNArrayBase */
616 template <class _nArrayOwnerType, class _elementType>
617 inline const _elementType &
619  vct::index_type index)
620 {
621  return input.Element(vctFixedSizeVector<vct::index_type, 1>(index));
622 }
623 
624 #endif // DOXYGEN
625 
626 
627 #endif // _vctDynamicNArray_h
628 
size_t index_type
Definition: vctContainerTraits.h:36
VCT_NARRAY_TRAITS_TYPEDEFS(_dimension)
vctReturnDynamicNArray< _elementType, _dimension > operator*(const vctDynamicConstNArrayBase< _nArrayOwnerType, _elementType, _dimension > &inputNArray, const _elementType &inputScalar)
Definition: vctDynamicNArray.h:387
ThisType & Divide(const value_type scalar)
Definition: vctDynamicNArrayBase.h:938
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
Definition: vctDynamicNArray.h:271
ThisType & Add(const vctDynamicConstNArrayBase< __nArrayOwnerType, value_type, DIMENSION > &otherNArray)
Definition: vctDynamicNArrayBase.h:657
ThisType & SumOf(const vctDynamicConstNArrayBase< __nArrayOwnerType1, value_type, DIMENSION > &nArray1, const vctDynamicConstNArrayBase< __nArrayOwnerType2, value_type, DIMENSION > &nArray2)
Definition: vctDynamicNArrayBase.h:573
An nArray object of dynamic size.
Definition: vctDynamicNArray.h:122
const nsize_type & sizes(void) const
Definition: vctDynamicConstNArrayBase.h:308
ThisType & ProductOf(const vctDynamicConstNArrayBase< __nArrayOwnerType, value_type, DIMENSION > &nArray, const value_type scalar)
Definition: vctDynamicNArrayBase.h:769
void SliceOf(const vctDynamicConstNArrayBase< __ownerType, value_type, DIMENSION+1 > &otherNArray, dimension_type dimension, size_type index)
Definition: vctDynamicConstNArrayRef.h:256
const _elementType & vctDynamicNArrayConstElementSlice(const vctDynamicConstNArrayBase< _nArrayOwnerType, _elementType, 1 > &input, vct::index_type index)
Definition: vctDynamicNArray.h:618
vctReturnDynamicNArray(const BaseType &other)
Definition: vctDynamicNArray.h:280
Container class for the dynamic nArray engines.
Definition: vctDynamicNArrayLoopEngines.h:43
vctDynamicNArray< _elementType, _dimension > BaseType
Definition: vctDynamicNArray.h:278
Definition: vctDynamicConstNArrayBase.h:155
vctDynamicNArray(const ThisType &otherNArray)
Definition: vctDynamicNArray.h:166
NArrayReturnType Abs(void) const
Definition: vctDynamicNArray.h:507
ThisType & Subtract(const vctDynamicConstNArrayBase< __nArrayOwnerType, value_type, DIMENSION > &otherNArray)
Definition: vctDynamicNArrayBase.h:667
vctDynamicConstNArrayRef< _elementType, _dimension-1 > vctDynamicNArrayConstNArraySlice(const vctDynamicConstNArrayBase< _nArrayOwnerType, _elementType, _dimension > &input, vct::size_type dimension, vct::index_type index)
Definition: vctDynamicNArray.h:606
ThisType & ForceAssign(const vctDynamicConstNArrayBase< __nArrayOwnerType, __elementType, _dimension > &other)
Definition: vctDynamicNArray.h:239
vctDynamicNArray(const nsize_type &sizes)
Definition: vctDynamicNArray.h:143
size_t size_type
Definition: vctContainerTraits.h:35
Declaration of vctDynamicNArrayOwner.
vctDynamicNArray(const vctDynamicConstNArrayBase< _otherNArrayOwnerType, value_type, _dimension > &otherNArray)
Definition: vctDynamicNArray.h:173
Dynamic nArray referencing existing memory (const)
Definition: vctDynamicConstNArrayRef.h:89
Implementation of a fixed-size vector using template metaprogramming.
Definition: vctFixedSizeVector.h:52
ThisType & RatioOf(const vctDynamicConstNArrayBase< __nArrayOwnerType, value_type, DIMENSION > &nArray, const value_type scalar)
Definition: vctDynamicNArrayBase.h:780
vctDynamicNArray(const vctDynamicConstNArrayBase< _otherNArrayOwnerType, _otherNArrayElementType, _dimension > &otherNArray)
Definition: vctDynamicNArray.h:185
ThisType & operator=(const vctDynamicConstNArrayBase< __nArrayOwnerType, __elementType, _dimension > &otherNArray)
Definition: vctDynamicNArray.h:198
value_type * Release(void)
Definition: vctDynamicNArrayOwner.h:234
VCT_NARRAY_TRAITS_TYPEDEFS(_dimension)
Declaration of vctDynamicNArrayRef.
vctReturnDynamicNArray< bool, _dimension > vctDynamicNArrayElementwiseCompareNArray(const vctDynamicConstNArrayBase< _nArrayOwnerType, _elementType, _dimension > &nArray1, const vctDynamicConstNArrayBase< _nArrayOwnerType, _elementType, _dimension > &nArray2)
Definition: vctDynamicNArray.h:560
NArrayReturnType Ceil(void) const
Definition: vctDynamicNArray.h:546
ThisType & Assign(const vctDynamicConstNArrayBase< __nArrayOwnerType, value_type, DIMENSION > &other)
Definition: vctDynamicNArrayBase.h:374
reference Element(const nsize_type &coordinates)
Definition: vctDynamicNArrayBase.h:241
Declaration of vctDynamicNArrayBase.
vctDynamicNArrayRef< _elementType, _dimension-1 > vctDynamicNArrayNArraySlice(vctDynamicNArrayBase< _nArrayOwnerType, _elementType, _dimension > &input, vct::size_type dimension, vct::index_type index)
Definition: vctDynamicNArray.h:585
Dynamic nArray referencing existing memory.
Definition: vctDynamicNArrayRef.h:87
vctDynamicNArrayBase< vctDynamicNArrayOwner< _elementType, _dimension >, _elementType, _dimension > BaseType
Definition: vctDynamicNArray.h:133
const_reference Element(const nsize_type &coordinates) const
Definition: vctDynamicConstNArrayBase.h:455
vctDynamicNArray()
Definition: vctDynamicNArray.h:137
vctReturnDynamicNArray< _elementType, _dimension > operator+(const vctDynamicConstNArrayBase< _nArrayOwnerType1, _elementType, _dimension > &inputNArray1, const vctDynamicConstNArrayBase< _nArrayOwnerType2, _elementType, _dimension > &inputNArray2)
Definition: vctDynamicNArray.h:329
vctReturnDynamicNArray< _elementType, _dimension > operator/(const vctDynamicConstNArrayBase< _nArrayOwnerType, _elementType, _dimension > &inputNArray, const _elementType &inputScalar)
Definition: vctDynamicNArray.h:399
Definition: vctDynamicNArrayBase.h:42
size_type size(void) const
Definition: vctDynamicConstNArrayBase.h:302
NArrayReturnType Floor(void) const
Definition: vctDynamicNArray.h:533
void SetSize(const nsize_type &sizes)
Definition: vctDynamicNArray.h:247
OwnerType NArray
Definition: vctDynamicConstNArrayBase.h:227
vctReturnDynamicNArray< bool, _dimension > vctDynamicNArrayElementwiseCompareScalar(const vctDynamicConstNArrayBase< _nArrayOwnerType, _elementType, _dimension > &nArray, const _elementType &scalar)
Definition: vctDynamicNArray.h:572
value_type SetAll(const value_type value)
Definition: vctDynamicNArrayBase.h:341
ThisType & NegationOf(const vctDynamicConstNArrayBase< __nArrayOwnerType, value_type, DIMENSION > &otherNArray)
Definition: vctDynamicNArrayBase.h:1039
vctDynamicNArray< _elementType, _dimension > ThisType
Definition: vctDynamicNArray.h:134
vctReturnDynamicNArray< _elementType, _dimension > operator-(const vctDynamicConstNArrayBase< _nArrayOwnerType1, _elementType, _dimension > &inputNArray1, const vctDynamicConstNArrayBase< _nArrayOwnerType2, _elementType, _dimension > &inputNArray2)
Definition: vctDynamicNArray.h:341
NArrayReturnType Negation(void) const
Definition: vctDynamicNArray.h:520
_elementType & vctDynamicNArrayElementSlice(vctDynamicNArrayBase< _nArrayOwnerType, _elementType, 1 > &input, vct::index_type index)
Definition: vctDynamicNArray.h:597
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
ThisType & Multiply(const value_type scalar)
Definition: vctDynamicNArrayBase.h:929
vctDynamicNArray(const nsize_type &sizes, value_type value)
Definition: vctDynamicNArray.h:150
ThisType & DifferenceOf(const vctDynamicConstNArrayBase< __nArrayOwnerType1, value_type, DIMENSION > &nArray1, const vctDynamicConstNArrayBase< __nArrayOwnerType2, value_type, DIMENSION > &nArray2)
Definition: vctDynamicNArrayBase.h:584
void SliceOf(vctDynamicNArrayBase< __ownerType, _elementType, DIMENSION+1 > &otherNArray, dimension_type dimension, size_type index)
Definition: vctDynamicNArrayRef.h:248