cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vctDynamicMatrixRefOwner.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-2007 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 _vctDynamicMatrixRefOwner_h
23 #define _vctDynamicMatrixRefOwner_h
24 
33 
38 template<class _elementType>
40 {
41 public:
42  /* define most types from vctContainerTraits */
43  VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
44  enum {DIMENSION = 2};
46 
48 
49  /* iterators are container specific */
54 
55 
57  SizesMember(0, 0),
58  StridesMember(1, 1),
59  Data(0)
60  {
61  this->UpdateCachedData();
62  }
63 
65  stride_type rowStride, stride_type colStride,
66  pointer data):
67  SizesMember(nsize_type(rows, cols)),
68  StridesMember(nstride_type(rowStride, colStride)),
69  Data(data)
70  {
71  this->UpdateCachedData();
72  }
73 
75  pointer dataPointer,
76  bool storageOrder) {
77  this->SetRef(rows, cols,
78  storageOrder == VCT_ROW_MAJOR ? cols : 1,
79  storageOrder == VCT_ROW_MAJOR ? 1 : rows,
80  dataPointer);
81  }
82 
83  void SetRef(const nsize_type & sizes,
84  pointer dataPointer,
85  bool storageOrder) {
86  this->SetRef(sizes,
87  storageOrder == VCT_ROW_MAJOR ? sizes.Element(1) : 1,
88  storageOrder == VCT_ROW_MAJOR ? 1 : sizes.Element(0),
89  dataPointer);
90  }
91 
93  stride_type rowStride, stride_type colStride,
94  pointer data) {
95  this->SetRef(nsize_type(rows, cols), nstride_type(rowStride, colStride), data);
96  }
97 
98  void SetRef(const nsize_type & sizes,
99  stride_type rowStride, stride_type colStride,
100  pointer data) {
101  this->SetRef(sizes, nstride_type(rowStride, colStride), data);
102  }
103 
104  void SetRef(const nsize_type & sizes, const nstride_type & strides, pointer data) {
105  SizesMember.Assign(sizes);
106  StridesMember.Assign(strides);
107  Data = data;
108  this->UpdateCachedData();
109  }
110 
111  size_type size(void) const {
112  return SizesMember.ProductOfElements();
113  }
114 
115  const nsize_type & sizes(void) const {
116  return SizesMember;
117  }
118 
119  size_type rows(void) const {
120  return SizesMember.Element(0);
121  }
122 
123  size_type cols(void) const {
124  return SizesMember.Element(1);
125  }
126 
127  const nstride_type & strides(void) const {
128  return StridesMember;
129  }
130 
132  return StridesMember.Element(0);
133  }
134 
136  return StridesMember.Element(1);
137  }
138 
139  pointer Pointer(index_type rowIndex, index_type colIndex) {
140  return Data + rowIndex * row_stride() + colIndex * col_stride();
141  }
142 
143  pointer Pointer(void) {
144  return Data;
145  }
146 
147  const_pointer Pointer(index_type rowIndex, index_type colIndex) const {
148  return Data + rowIndex * row_stride() + colIndex * col_stride();
149  }
150 
151  const_pointer Pointer(void) const {
152  return Data;
153  }
154 
155  const_iterator begin(void) const {
156  return const_iterator(Data, col_stride(), cols(), row_stride());
157  }
158 
159  const_iterator end(void) const {
160  return const_iterator(Data, col_stride(), cols(), row_stride())
161  + rows() * cols();
162  }
163 
164  iterator begin(void) {
165  return iterator(Data, col_stride(), cols(), row_stride());
166  }
167 
168  iterator end(void) {
169  return iterator(Data, col_stride(), cols(), row_stride())
170  + rows() * cols();
171  }
172 
174  return const_reverse_iterator(Data + row_stride() * (rows() - 1) + col_stride() * (cols() - 1),
175  -col_stride(), cols(), -row_stride());
176  }
177 
179  return const_reverse_iterator(Data - row_stride() + col_stride() * (cols() - 1),
180  -col_stride(), cols(), -row_stride());
181  }
182 
184  return reverse_iterator(Data + row_stride() * (rows() - 1) + col_stride() * (cols() - 1),
185  -col_stride(), cols(), -row_stride());
186  }
187 
189  return reverse_iterator(Data - row_stride() + col_stride() * (cols() - 1),
190  -col_stride(), cols(), -row_stride());
191  }
192 
193  inline bool IsColMajor(void) const {
194  return (this->row_stride() <= this->col_stride());
195  }
196 
197  inline bool IsRowMajor(void) const {
198  return (this->col_stride() <= this->row_stride());
199  }
200 
201  inline bool IsCompact(void) const {
202  return IsCompactMember;
203  }
204 
205  inline bool StorageOrder(void) const {
206  return this->IsRowMajor();
207  }
208 
209  protected:
210  nsize_type SizesMember;
211  nstride_type StridesMember;
213  value_type* Data;
214 
215  inline void UpdateCachedData(void) {
216  IsCompactMember = (((this->row_stride() == 1) && (this->col_stride() == static_cast<stride_type>(this->rows())))
217  || ((this->col_stride() == 1) && (this->row_stride() == static_cast<stride_type>(this->cols()))));
218  }
219 
220 private:
221  // copy constructor private to prevent any call
222  vctDynamicMatrixRefOwner(const ThisType & CMN_UNUSED(other)) {};
223 
224 };
225 
226 
227 #endif // _vctDynamicMatrixRefOwner_h
228 
size_t index_type
Definition: vctContainerTraits.h:36
difference_type col_stride(void) const
Definition: vctDynamicMatrixRefOwner.h:135
Definition: vctDynamicMatrixRefOwner.h:44
bool IsColMajor(void) const
Definition: vctDynamicMatrixRefOwner.h:193
const_iterator end(void) const
Definition: vctDynamicMatrixRefOwner.h:159
const bool VCT_ROW_MAJOR
Definition: vctForwardDeclarations.h:43
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
size_type cols(void) const
Definition: vctDynamicMatrixRefOwner.h:123
Forward declarations and #define for cisstVector.
iterator begin(void)
Definition: vctDynamicMatrixRefOwner.h:164
nstride_type StridesMember
Definition: vctDynamicMatrixRefOwner.h:211
bool StorageOrder(void) const
Definition: vctDynamicMatrixRefOwner.h:205
size_type size(void) const
Definition: vctDynamicMatrixRefOwner.h:111
VCT_NARRAY_TRAITS_TYPEDEFS(DIMENSION)
vctDynamicMatrixRefOwner()
Definition: vctDynamicMatrixRefOwner.h:56
const_reverse_iterator rend(void) const
Definition: vctDynamicMatrixRefOwner.h:178
size_t size_type
Definition: vctContainerTraits.h:35
vctDynamicMatrixRefOwner< value_type > ThisType
Definition: vctDynamicMatrixRefOwner.h:47
reverse_iterator rbegin(void)
Definition: vctDynamicMatrixRefOwner.h:183
value_type * Data
Definition: vctDynamicMatrixRefOwner.h:213
difference_type row_stride(void) const
Definition: vctDynamicMatrixRefOwner.h:131
size_type rows(void) const
Definition: vctDynamicMatrixRefOwner.h:119
nsize_type SizesMember
Definition: vctDynamicMatrixRefOwner.h:210
vctVarStrideMatrixConstIterator< value_type > const_reverse_iterator
Definition: vctDynamicMatrixRefOwner.h:51
ptrdiff_t difference_type
Definition: vctContainerTraits.h:38
vctVarStrideMatrixConstIterator< value_type > const_iterator
Definition: vctDynamicMatrixRefOwner.h:50
vctVarStrideMatrixIterator< value_type > reverse_iterator
Definition: vctDynamicMatrixRefOwner.h:53
void SetRef(size_type rows, size_type cols, stride_type rowStride, stride_type colStride, pointer data)
Definition: vctDynamicMatrixRefOwner.h:92
reverse_iterator rend(void)
Definition: vctDynamicMatrixRefOwner.h:188
Declaration of vctDynamicMatrixOwner.
iterator end(void)
Definition: vctDynamicMatrixRefOwner.h:168
const_iterator begin(void) const
Definition: vctDynamicMatrixRefOwner.h:155
void SetRef(const nsize_type &sizes, pointer dataPointer, bool storageOrder)
Definition: vctDynamicMatrixRefOwner.h:83
pointer Pointer(void)
Definition: vctDynamicMatrixRefOwner.h:143
bool IsCompact(void) const
Definition: vctDynamicMatrixRefOwner.h:201
void SetRef(const nsize_type &sizes, stride_type rowStride, stride_type colStride, pointer data)
Definition: vctDynamicMatrixRefOwner.h:98
const_pointer Pointer(index_type rowIndex, index_type colIndex) const
Definition: vctDynamicMatrixRefOwner.h:147
const nstride_type & strides(void) const
Definition: vctDynamicMatrixRefOwner.h:127
const nsize_type & sizes(void) const
Definition: vctDynamicMatrixRefOwner.h:115
bool IsCompactMember
Definition: vctDynamicMatrixRefOwner.h:212
Definition: vctDynamicMatrixRefOwner.h:39
void UpdateCachedData(void)
Definition: vctDynamicMatrixRefOwner.h:215
const_pointer Pointer(void) const
Definition: vctDynamicMatrixRefOwner.h:151
ptrdiff_t stride_type
Definition: vctContainerTraits.h:37
vctVarStrideMatrixIterator< value_type > iterator
Definition: vctDynamicMatrixRefOwner.h:52
const_reverse_iterator rbegin(void) const
Definition: vctDynamicMatrixRefOwner.h:173
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
pointer Pointer(index_type rowIndex, index_type colIndex)
Definition: vctDynamicMatrixRefOwner.h:139
void SetRef(size_type rows, size_type cols, pointer dataPointer, bool storageOrder)
Definition: vctDynamicMatrixRefOwner.h:74
Definition: vctVarStrideMatrixIterator.h:40
Declaration of vctVarStrideMatrixConstIterator and vctVarStrideMatrixIterator.
bool IsRowMajor(void) const
Definition: vctDynamicMatrixRefOwner.h:197
Definition: vctVarStrideMatrixIterator.h:287
void SetRef(const nsize_type &sizes, const nstride_type &strides, pointer data)
Definition: vctDynamicMatrixRefOwner.h:104
vctDynamicMatrixRefOwner(size_type rows, size_type cols, stride_type rowStride, stride_type colStride, pointer data)
Definition: vctDynamicMatrixRefOwner.h:64