cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vctDynamicMatrixOwner.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 _vctDynamicMatrixOwner_h
23 #define _vctDynamicMatrixOwner_h
24 
33 
37 template<class _elementType>
39 {
40 public:
41  /* define most types from vctContainerTraits */
42  VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
43  enum {DIMENSION = 2};
45 
47 
48  /* iterators are container specific */
53 
55  SizesMember(0, 0),
56  StridesMember(0, 1),
58  Data(0)
59  {}
60 
61  vctDynamicMatrixOwner(const nsize_type & newSizes, bool rowMajor = VCT_DEFAULT_STORAGE):
62  SizesMember(0, 0),
63  StridesMember(0, 1),
65  Data(0)
66  {
67  SetSize(newSizes, rowMajor);
68  }
69 
71  SizesMember(0, 0),
72  StridesMember(0, 1),
74  Data(0)
75  {
76  SetSize(nsize_type(rows, cols), rowMajor);
77  }
78 
80  Disown();
81  }
82 
83  size_type size(void) const {
84  return SizesMember.ProductOfElements();
85  }
86 
87  const nsize_type & sizes(void) const {
88  return SizesMember;
89  }
90 
91  size_type rows(void) const {
92  return SizesMember.Element(0);
93  }
94 
95  size_type cols(void) const {
96  return SizesMember.Element(1);
97  }
98 
99  const nstride_type & strides(void) const {
100  return StridesMember;
101  }
102 
104  return StridesMember.Element(0);
105  }
106 
108  return StridesMember.Element(1);
109  }
110 
111  pointer Pointer(index_type rowIndex, index_type colIndex) {
112  return Data + rowIndex * row_stride() + colIndex * col_stride();
113  }
114 
115  pointer Pointer(void) {
116  return Data;
117  }
118 
119  const_pointer Pointer(index_type rowIndex, index_type colIndex) const {
120  return Data + rowIndex * row_stride() + colIndex * col_stride();
121  }
122 
123  const_pointer Pointer(void) const {
124  return Data;
125  }
126 
127  const_iterator begin(void) const {
128  return const_iterator(Data, col_stride(), cols(), row_stride());
129  }
130 
131  const_iterator end(void) const {
132  return const_iterator(Data, col_stride(), cols(), row_stride())
133  + rows() * cols();
134  }
135 
136  iterator begin(void) {
137  return iterator(Data, col_stride(), cols(), row_stride());
138  }
139 
140  iterator end(void) {
141  return iterator(Data, col_stride(), cols(), row_stride())
142  + rows() * cols();
143  }
144 
146  return const_reverse_iterator(Data + row_stride() * (rows() - 1) + col_stride() * (cols() - 1),
147  -col_stride(), cols(), -row_stride());
148  }
149 
151  return const_reverse_iterator(Data - row_stride() + col_stride() * (cols() - 1),
152  -col_stride(), cols(), -row_stride());
153  }
154 
156  return reverse_iterator(Data + row_stride() * (rows() - 1) + col_stride() * (cols() - 1),
157  -col_stride(), cols(), -row_stride());
158  }
159 
161  return reverse_iterator(Data - row_stride() + col_stride() * (cols() - 1),
162  -col_stride(), cols(), -row_stride());
163  }
164 
176  void SetSize(size_type rows, size_type cols, bool rowMajor) {
177  this->SetSize(nsize_type(rows, cols), rowMajor);
178  }
179 
180  void SetSize(const nsize_type & newSizes, bool rowMajor) {
181  if ((newSizes == this->sizes()) && (rowMajor == RowMajor)) return;
182  Disown();
183  const size_type totalSize = newSizes.ProductOfElements();
184  Own(newSizes, rowMajor, (totalSize == 0) ? 0 : new value_type[totalSize]);
185  }
187 
192  pointer Release() {
193  pointer oldData = Data;
194  Data = 0;
195  SizesMember.SetAll(0);
197  return oldData;
198  }
199 
208  pointer Own(size_type rows, size_type cols, bool rowMajor, pointer data) {
209  return this->Own(nsize_type(rows, cols), rowMajor, data);
210  }
211 
212  pointer Own(const nsize_type & newSizes, bool rowMajor, pointer data) {
213  pointer oldData = Data;
214  SizesMember.Assign(newSizes);
215  StridesMember.Element(0) = rowMajor ? this->cols() : 1;
216  StridesMember.Element(1) = rowMajor ? 1 : this->rows();
217  RowMajor = rowMajor;
218  Data = data;
219  return oldData;
220  }
222 
226  void Disown(void) {
227  delete[] Data;
228  SizesMember.SetAll(0);
229  StridesMember.Element(0) = RowMajor ? 0 : 1;
230  StridesMember.Element(1) = RowMajor ? 1 : 0;
231  Data = 0;
232  }
233 
234  inline bool IsColMajor(void) const {
235  return !RowMajor;
236  }
237 
238  inline bool IsRowMajor(void) const {
239  return RowMajor;
240  }
241 
242  inline bool IsCompact(void) const {
243  return true;
244  }
245 
246  inline bool StorageOrder(void) const {
247  return RowMajor;
248  }
249 
250 protected:
251  nsize_type SizesMember;
252  nstride_type StridesMember;
253  bool RowMajor;
254  value_type* Data;
255 
256 private:
257  // copy constructor private to prevent any call
258  vctDynamicMatrixOwner(const ThisType & CMN_UNUSED(other)) {};
259 };
260 
261 
262 #endif // _vctDynamicMatrixOwner_h
263 
vctDynamicMatrixOwner()
Definition: vctDynamicMatrixOwner.h:54
size_t index_type
Definition: vctContainerTraits.h:36
pointer Own(size_type rows, size_type cols, bool rowMajor, pointer data)
Definition: vctDynamicMatrixOwner.h:208
pointer Own(const nsize_type &newSizes, bool rowMajor, pointer data)
Definition: vctDynamicMatrixOwner.h:212
pointer Pointer(index_type rowIndex, index_type colIndex)
Definition: vctDynamicMatrixOwner.h:111
nstride_type StridesMember
Definition: vctDynamicMatrixOwner.h:252
size_type cols(void) const
Definition: vctDynamicMatrixOwner.h:95
Definition: vctDynamicMatrixOwner.h:43
void Disown(void)
Definition: vctDynamicMatrixOwner.h:226
pointer Release()
Definition: vctDynamicMatrixOwner.h:192
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
vctDynamicMatrixOwner(size_type rows, size_type cols, bool rowMajor=VCT_DEFAULT_STORAGE)
Definition: vctDynamicMatrixOwner.h:70
Forward declarations and #define for cisstVector.
const_reverse_iterator rend(void) const
Definition: vctDynamicMatrixOwner.h:150
const nstride_type & strides(void) const
Definition: vctDynamicMatrixOwner.h:99
~vctDynamicMatrixOwner()
Definition: vctDynamicMatrixOwner.h:79
difference_type col_stride(void) const
Definition: vctDynamicMatrixOwner.h:107
const_reverse_iterator rbegin(void) const
Definition: vctDynamicMatrixOwner.h:145
size_t size_type
Definition: vctContainerTraits.h:35
vctVarStrideMatrixIterator< value_type > iterator
Definition: vctDynamicMatrixOwner.h:51
const bool VCT_DEFAULT_STORAGE
Definition: vctForwardDeclarations.h:49
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
bool StorageOrder(void) const
Definition: vctDynamicMatrixOwner.h:246
bool IsRowMajor(void) const
Definition: vctDynamicMatrixOwner.h:238
const nsize_type & sizes(void) const
Definition: vctDynamicMatrixOwner.h:87
nsize_type SizesMember
Definition: vctDynamicMatrixOwner.h:251
void SetSize(const nsize_type &newSizes, bool rowMajor)
Definition: vctDynamicMatrixOwner.h:180
ptrdiff_t difference_type
Definition: vctContainerTraits.h:38
const_pointer Pointer(index_type rowIndex, index_type colIndex) const
Definition: vctDynamicMatrixOwner.h:119
size_type rows(void) const
Definition: vctDynamicMatrixOwner.h:91
vctVarStrideMatrixIterator< value_type > reverse_iterator
Definition: vctDynamicMatrixOwner.h:52
const_iterator begin(void) const
Definition: vctDynamicMatrixOwner.h:127
iterator begin(void)
Definition: vctDynamicMatrixOwner.h:136
Definition: vctDynamicMatrixOwner.h:38
value_type * Data
Definition: vctDynamicMatrixOwner.h:254
pointer Pointer(void)
Definition: vctDynamicMatrixOwner.h:115
Declaration of vctDynamicMatrixRefOwner.
bool IsColMajor(void) const
Definition: vctDynamicMatrixOwner.h:234
reverse_iterator rend(void)
Definition: vctDynamicMatrixOwner.h:160
reverse_iterator rbegin(void)
Definition: vctDynamicMatrixOwner.h:155
void SetSize(size_type rows, size_type cols, bool rowMajor)
Definition: vctDynamicMatrixOwner.h:176
size_type size(void) const
Definition: vctDynamicMatrixOwner.h:83
vctVarStrideMatrixConstIterator< value_type > const_reverse_iterator
Definition: vctDynamicMatrixOwner.h:50
vctDynamicMatrixOwner(const nsize_type &newSizes, bool rowMajor=VCT_DEFAULT_STORAGE)
Definition: vctDynamicMatrixOwner.h:61
bool RowMajor
Definition: vctDynamicMatrixOwner.h:253
vctDynamicMatrixOwner< value_type > ThisType
Definition: vctDynamicMatrixOwner.h:46
VCT_NARRAY_TRAITS_TYPEDEFS(DIMENSION)
difference_type row_stride(void) const
Definition: vctDynamicMatrixOwner.h:103
const_pointer Pointer(void) const
Definition: vctDynamicMatrixOwner.h:123
Definition: vctVarStrideMatrixIterator.h:40
vctVarStrideMatrixConstIterator< value_type > const_iterator
Definition: vctDynamicMatrixOwner.h:49
Declaration of vctVarStrideMatrixConstIterator and vctVarStrideMatrixIterator.
bool IsCompact(void) const
Definition: vctDynamicMatrixOwner.h:242
Definition: vctVarStrideMatrixIterator.h:287
const_iterator end(void) const
Definition: vctDynamicMatrixOwner.h:131
iterator end(void)
Definition: vctDynamicMatrixOwner.h:140