cisst-saw
Loading...
Searching...
No Matches
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
14This software is provided "as is" under an open source license, with
15no warranty. The complete license can be found in license.txt and
16http://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
29
33
37template<class _elementType>
39{
40public:
41 /* define most types from vctContainerTraits */
43 enum {DIMENSION = 2};
45
47
48 /* iterators are container specific */
53
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
70 vctDynamicMatrixOwner(size_type rows, size_type cols, bool rowMajor = VCT_DEFAULT_STORAGE):
71 SizesMember(0, 0),
72 StridesMember(0, 1),
74 Data(0)
75 {
76 SetSize(nsize_type(rows, cols), rowMajor);
77 }
78
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
103 difference_type row_stride(void) const {
104 return StridesMember.Element(0);
105 }
106
107 difference_type col_stride(void) const {
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 {
129 }
130
131 const_iterator end(void) const {
133 + rows() * cols();
134 }
135
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 }
186
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 }
221
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
250protected:
251 nsize_type SizesMember;
252 nstride_type StridesMember;
255
256private:
257 // copy constructor private to prevent any call
258 vctDynamicMatrixOwner(const ThisType & CMN_UNUSED(other)) {};
259};
260
261
262#endif // _vctDynamicMatrixOwner_h
263
const nstride_type & strides(void) const
Definition vctDynamicMatrixOwner.h:99
nsize_type SizesMember
Definition vctDynamicMatrixOwner.h:251
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
const_pointer Pointer(index_type rowIndex, index_type colIndex) const
Definition vctDynamicMatrixOwner.h:119
const_iterator end(void) const
Definition vctDynamicMatrixOwner.h:131
VCT_NARRAY_TRAITS_TYPEDEFS(DIMENSION)
void Disown(void)
Definition vctDynamicMatrixOwner.h:226
iterator begin(void)
Definition vctDynamicMatrixOwner.h:136
reverse_iterator rbegin(void)
Definition vctDynamicMatrixOwner.h:155
pointer Pointer(index_type rowIndex, index_type colIndex)
Definition vctDynamicMatrixOwner.h:111
~vctDynamicMatrixOwner()
Definition vctDynamicMatrixOwner.h:79
vctVarStrideMatrixConstIterator< value_type > const_reverse_iterator
Definition vctDynamicMatrixOwner.h:50
bool IsRowMajor(void) const
Definition vctDynamicMatrixOwner.h:238
const_reverse_iterator rbegin(void) const
Definition vctDynamicMatrixOwner.h:145
pointer Pointer(void)
Definition vctDynamicMatrixOwner.h:115
bool StorageOrder(void) const
Definition vctDynamicMatrixOwner.h:246
value_type * Data
Definition vctDynamicMatrixOwner.h:254
bool RowMajor
Definition vctDynamicMatrixOwner.h:253
size_type size(void) const
Definition vctDynamicMatrixOwner.h:83
const_reverse_iterator rend(void) const
Definition vctDynamicMatrixOwner.h:150
vctDynamicMatrixOwner< value_type > ThisType
Definition vctDynamicMatrixOwner.h:46
vctDynamicMatrixOwner()
Definition vctDynamicMatrixOwner.h:54
vctDynamicMatrixOwner(size_type rows, size_type cols, bool rowMajor=VCT_DEFAULT_STORAGE)
Definition vctDynamicMatrixOwner.h:70
vctDynamicMatrixOwner(const nsize_type &newSizes, bool rowMajor=VCT_DEFAULT_STORAGE)
Definition vctDynamicMatrixOwner.h:61
pointer Own(size_type rows, size_type cols, bool rowMajor, pointer data)
Definition vctDynamicMatrixOwner.h:208
reverse_iterator rend(void)
Definition vctDynamicMatrixOwner.h:160
vctVarStrideMatrixIterator< value_type > iterator
Definition vctDynamicMatrixOwner.h:51
void SetSize(const nsize_type &newSizes, bool rowMajor)
Definition vctDynamicMatrixOwner.h:180
difference_type row_stride(void) const
Definition vctDynamicMatrixOwner.h:103
size_type cols(void) const
Definition vctDynamicMatrixOwner.h:95
nstride_type StridesMember
Definition vctDynamicMatrixOwner.h:252
void SetSize(size_type rows, size_type cols, bool rowMajor)
Definition vctDynamicMatrixOwner.h:176
pointer Own(const nsize_type &newSizes, bool rowMajor, pointer data)
Definition vctDynamicMatrixOwner.h:212
const nsize_type & sizes(void) const
Definition vctDynamicMatrixOwner.h:87
vctVarStrideMatrixConstIterator< value_type > const_iterator
Definition vctDynamicMatrixOwner.h:49
const_iterator begin(void) const
Definition vctDynamicMatrixOwner.h:127
iterator end(void)
Definition vctDynamicMatrixOwner.h:140
size_type rows(void) const
Definition vctDynamicMatrixOwner.h:91
bool IsColMajor(void) const
Definition vctDynamicMatrixOwner.h:234
difference_type col_stride(void) const
Definition vctDynamicMatrixOwner.h:107
pointer Release()
Definition vctDynamicMatrixOwner.h:192
bool IsCompact(void) const
Definition vctDynamicMatrixOwner.h:242
@ DIMENSION
Definition vctDynamicMatrixOwner.h:43
vctVarStrideMatrixIterator< value_type > reverse_iterator
Definition vctDynamicMatrixOwner.h:52
const_pointer Pointer(void) const
Definition vctDynamicMatrixOwner.h:123
Definition vctVarStrideMatrixIterator.h:40
Definition vctVarStrideMatrixIterator.h:283
#define CMN_UNUSED(argument)
Definition cmnPortability.h:497
Declaration of vctDynamicMatrixRefOwner.
Forward declarations and #define for cisstVector.
const bool VCT_DEFAULT_STORAGE
Definition vctForwardDeclarations.h:47
Declaration of vctVarStrideMatrixConstIterator and vctVarStrideMatrixIterator.