/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */ /* $Id: vctDynamicNArrayRefOwner.h,v 1.3 2007/04/26 19:33:57 anton Exp $ Author(s): Daniel Li Created on: 2006-07-06 (C) Copyright 2006-2007 Johns Hopkins University (JHU), All Rights Reserved. --- begin cisst license - do not edit --- This software is provided "as is" under an open source license, with no warranty. The complete license can be found in license.txt and http://www.cisst.org/cisst/license.txt. --- end cisst license --- */ #ifndef _vctDynamicNArrayRefOwner_h #define _vctDynamicNArrayRefOwner_h /*! \file \brief Declaration of vctDynamicNArrayRefOwner */ #include /*! This templated class owns a dynamically allocated array, but does not provide any other operations */ template class vctDynamicNArrayRefOwner { public: /* define most types from vctContainerTraits and vctNArrayTraits */ enum {DIMENSION = _dimension}; VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType); VCT_NARRAY_TRAITS_TYPEDEFS(DIMENSION); /*! The type of this owner. */ typedef vctDynamicNArrayRefOwner<_elementType, DIMENSION> ThisType; /* iterators are container specific */ typedef vctVarStrideNArrayIterator iterator; typedef vctVarStrideNArrayConstIterator const_iterator; typedef vctVarStrideNArrayIterator reverse_iterator; typedef vctVarStrideNArrayConstIterator const_reverse_iterator; vctDynamicNArrayRefOwner(): SizesMember(0), StridesMember(0), Data(0) {} vctDynamicNArrayRefOwner(pointer data, const nsize_type & sizes, const nstride_type & strides) { SetRef(data, sizes, strides); } void SetRef(pointer data, const nsize_type & sizes, const nstride_type & strides) { Data = data; SizesMember = sizes; StridesMember = strides; } /*! Return the number of dimensions of the nArray. \return the number of dimensions of this nArray */ dimension_type dimension(void) const { return DIMENSION; } /*! Return the size of each dimension. \return a vector containing the size of the nArray in each dimension */ const nsize_type & sizes(void) const { return SizesMember; } /*! Return the size of a dimension. \param dimension the dimension of interest \return the size of the nArray in the given dimension \note the dimension is zero-based */ size_type size(dimension_type dimension) const { if (dimension < DIMENSION) return SizesMember[dimension]; else return 1; } /*! Return the size of the nArray. \return the number of elements \note This method returns 0 if the dimension of the nArray is zero. */ size_type size(void) const { if (SizesMember.size() == 0) return 0; else return SizesMember.ProductOfElements(); } /*! Return the strides of each dimension. \return a vector containing the strides of the nArray in each dimension */ const nstride_type & strides(void) const { return StridesMember; } /*! Return the stride of a dimension. \param dimension the dimension of interest \return the stride of the nArray in the given dimension */ difference_type stride(dimension_type dimension) const { if (dimension < DIMENSION) return StridesMember[dimension]; else return 1; } /*! Return a pointer to the nArray's data. \return a pointer to this nArray's data */ pointer Pointer(void) { return Data; } /*! Return a pointer to the nArray's data at the specified element. \return a pointer to this nArray's data at the specified element */ pointer Pointer(const nindex_type & indices) { vctFixedSizeConstVectorRef indicesRef(reinterpret_cast(const_cast(indices.Pointer()))); return (Data + StridesMember.DotProduct(indicesRef)); } /*! Return a const pointer to the nArray's data. \return a const pointer to this nArray's data */ const_pointer Pointer(void) const { return Data; } /*! Return a const pointer to the nArray's data at the specified element. \return a const pointer to this nArray's data at the specified element */ const_pointer Pointer(const nsize_type & indices) const { vctFixedSizeConstVectorRef indicesRef(reinterpret_cast(const_cast(indices.Pointer()))); return Data + StridesMember.DotProduct(indicesRef); } iterator begin(void) { return iterator(this, 0); } iterator end(void) { return iterator(this, 0) + size(); } const_iterator begin(void) const { return const_iterator(this, 0); } const_iterator end(void) const { return const_iterator(this, 0) + size(); } reverse_iterator rbegin(void) { size_type endIndex = size() - 1; return reverse_iterator(this, endIndex); } reverse_iterator rend(void) { return reverse_iterator(this, 0) + 1; } const_reverse_iterator rbegin(void) const { size_type endIndex = size() - 1; return const_reverse_iterator(this, endIndex); } const_reverse_iterator rend(void) const { return const_reverse_iterator(this, 0) + 1; } protected: nsize_type SizesMember; nstride_type StridesMember; value_type* Data; }; #endif // _vctDynamicNArrayRefOwner_h // **************************************************************************** // Change History // **************************************************************************** // // $Log: vctDynamicNArrayRefOwner.h,v $ // Revision 1.3 2007/04/26 19:33:57 anton // All files in libraries: Applied new license text, separate copyright and // updated dates, added standard header where missing. // // Revision 1.2 2007/03/08 04:29:25 anton // cisstVector NArray: Now templated by dimension (see #258). Added template // specialization for slices of dimension 1 NArray to return value_type. Used // Ofri's implementation of IncrementPointer() in loop engines (previous one did // not handle more than dimension 3). Also renamed DimSize, DimStride and // Dimension to size(d), stride(d), and dimensioni (for STL flavor). // // Revision 1.1 2006/12/09 04:13:25 anton // cisstVector: Imported code for NArray from Daniel Li with minor cleanup // for license, dos2unix, a few renamed methods and port to gcc. // // // ****************************************************************************