cisst-saw
Loading...
Searching...
No Matches
vctFastCopy.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 Author(s): Anton Deguet
6 Created on: 2006-11-10
7
8 (C) Copyright 2006-2021 Johns Hopkins University (JHU), All Rights Reserved.
9
10--- begin cisst license - do not edit ---
11
12This software is provided "as is" under an open source license, with
13no warranty. The complete license can be found in license.txt and
14http://www.cisst.org/cisst/license.txt.
15
16--- end cisst license ---
17*/
18
19#pragma once
20#ifndef _vctFastCopy_h
21#define _vctFastCopy_h
22
26
37
38protected:
42 template <class _vector1Type, class _vector2Type>
43 inline static bool VectorSizeCompatible(const _vector1Type & vector1,
44 const _vector2Type & vector2)
45 {
46 return (vector1.size() == vector2.size());
47 }
48
49 template <class _container1Type, class _container2Type>
50 inline static bool ContainerSizesCompatible(const _container1Type & container1,
51 const _container2Type & container2)
52 {
53 return (container1.sizes() == container2.sizes());
54 }
55
56 template <class _vector1Type, class _vector2Type>
57 inline static bool VectorStrideCompatible(const _vector1Type & vector1,
58 const _vector2Type & vector2)
59 {
60 return ((vector1.stride() == 1) && (vector2.stride() == 1));
61 }
62
63
64 template <class _matrix1Type, class _matrix2Type>
65 inline static bool MatrixStridesCompatible(const _matrix1Type & matrix1,
66 const _matrix2Type & matrix2)
67 {
68 return (
69 /* compact and same storage order */
70 (matrix1.IsCompact() && matrix2.IsCompact()
71 && (matrix1.strides() == matrix2.strides()))
72 ||
73 /* or row compact */
74 ((matrix1.row_stride() == 1) && matrix2.row_stride() == 1)
75 ||
76 /* or column compact */
77 ((matrix1.col_stride() == 1) && matrix2.col_stride() == 1));
78 }
79
80 template <class _nArray1Type, class _nArray2Type>
81 inline static bool NArrayStridesCompatible(const _nArray1Type & nArray1,
82 const _nArray2Type & nArray2)
83 {
84 return (nArray1.IsCompact() && nArray2.IsCompact()
85 && (nArray1.strides() == nArray2.strides()));
86 }
87
88
89
93 template <class _vector1Type, class _vector2Type>
94 inline static void ThrowUnlessValidVectorSizes(const _vector1Type & vector1,
95 const _vector2Type & vector2)
96 CISST_THROW(std::runtime_error)
97 {
98 if (!VectorSizeCompatible(vector1, vector2)) {
99 cmnThrow(std::runtime_error("vctFastCopy: Vector sizes mismatch"));
100 }
101 }
102
103 template <class _container1Type, class _container2Type>
104 inline static void ThrowUnlessValidContainerSizes(const _container1Type & container1,
105 const _container2Type & container2)
106 CISST_THROW(std::runtime_error)
107 {
108 if (!ContainerSizesCompatible(container1, container2)) {
109 cmnThrow(std::runtime_error("vctFastCopy: Container sizes mismatch"));
110 }
111 }
112
113
114public:
115
118 static const bool SkipChecks = false;
119 static const bool PerformChecks = true;
121
124 template <class _vector1Type, class _vector2Type>
125 inline static bool VectorCopyCompatible(const _vector1Type & vector1,
126 const _vector2Type & vector2)
127 {
130 && VectorSizeCompatible(vector1, vector2)
131 && VectorStrideCompatible(vector1, vector2));
132 }
133
134 template <class _matrix1Type, class _matrix2Type>
135 inline static bool MatrixCopyCompatible(const _matrix1Type & matrix1,
136 const _matrix2Type & matrix2)
137 {
140 && ContainerSizesCompatible(matrix1, matrix2)
141 && MatrixStridesCompatible(matrix1, matrix2));
142 }
143
144 template <class _nArray1Type, class _nArray2Type>
145 inline static bool NArrayCopyCompatible(const _nArray1Type & nArray1,
146 const _nArray2Type & nArray2)
147 {
150 && ContainerSizesCompatible(nArray1, nArray2)
151 && NArrayStridesCompatible(nArray1, nArray2));
152 }
153
154
155
160 template <class _destinationVectorType, class _sourceVectorType>
161 inline static bool VectorCopy(_destinationVectorType & destination,
162 const _sourceVectorType & source,
163 bool performSafetyChecks)
164 {
165 typedef _sourceVectorType SourceVectorType;
166 typedef typename SourceVectorType::value_type value_type;
167
168 if (performSafetyChecks) {
169 // test size
170 ThrowUnlessValidVectorSizes(source, destination);
171
172 // test layout
173 if (! VectorStrideCompatible(destination, source)) {
174 return false;
175 }
176 }
177 cmnMemcpy(destination.Pointer(), source.Pointer(), source.size() * sizeof(value_type));
178 return true;
179 }
180
181
207 template <class _destinationMatrixType, class _sourceMatrixType>
208 inline static bool MatrixCopy(_destinationMatrixType & destination,
209 const _sourceMatrixType & source,
210 bool performSafetyChecks)
211 {
212 typedef _sourceMatrixType SourceMatrixType;
213 typedef typename SourceMatrixType::value_type value_type;
214
215 if (performSafetyChecks) {
216 // test size
217 ThrowUnlessValidContainerSizes(source, destination);
218
219 // test layout
220 if (! MatrixStridesCompatible(destination, source)) {
221 return false;
222 }
223 }
224 // at that point we know the matrices are compatible. We need to know if we memcpy all or by rows or column
225 if (destination.IsCompact() && source.IsCompact()) {
226 cmnMemcpy(destination.Pointer(), source.Pointer(), source.size() * sizeof(value_type));
227 return true;
228 } else {
229 typedef _destinationMatrixType DestinationMatrixType;
230 typedef _sourceMatrixType SourceMatrixType;
231
232 typedef typename DestinationMatrixType::size_type size_type;
233 typedef typename DestinationMatrixType::stride_type stride_type;
234
235 typedef typename DestinationMatrixType::pointer DestinationPointerType;
236 typedef typename SourceMatrixType::const_pointer SourcePointerType;
237
238 const size_type rows = destination.rows();
239 const size_type cols = destination.cols();
240
241 DestinationPointerType destinationPointer = destination.Pointer();
242 SourcePointerType sourcePointer = source.Pointer();
243
244 const stride_type destinationRowStride = destination.row_stride();
245 const stride_type sourceRowStride = source.row_stride();
246 const stride_type destinationColStride = destination.col_stride();
247 const stride_type sourceColStride = source.col_stride();
248
249
250 if ((destinationColStride == 1) && (sourceColStride == 1)) {
251 /* copy row by row */
252 const size_type sizeOfRow = cols * sizeof(value_type);
253
254 const DestinationPointerType destinationRowEnd = destinationPointer + rows * destinationRowStride;
255
256 for (;
257 destinationPointer != destinationRowEnd;
258 destinationPointer += destinationRowStride, sourcePointer += sourceRowStride) {
259 cmnMemcpy(destinationPointer, sourcePointer, sizeOfRow);
260 }
261 return true;
262 } else {
263 /* copy column by column */
264 const size_type sizeOfCol = rows * sizeof(value_type);
265 const DestinationPointerType destinationColEnd = destinationPointer + cols * destinationColStride;
266
267 for (;
268 destinationPointer != destinationColEnd;
269 destinationPointer += destinationColStride, sourcePointer += sourceColStride) {
270 cmnMemcpy(destinationPointer, sourcePointer, sizeOfCol);
271 }
272 return true;
273 }
274 }
275 }
276
277
283 template <class _destinationNArrayType, class _sourceNArrayType>
284 inline static bool NArrayCopy(_destinationNArrayType & destination,
285 const _sourceNArrayType & source,
286 bool performSafetyChecks)
287 {
288 typedef _sourceNArrayType SourceNArrayType;
289 typedef typename SourceNArrayType::value_type value_type;
290
291 if (performSafetyChecks) {
292 // test size
293 ThrowUnlessValidContainerSizes(source, destination);
294
295 // test layout
296 if (! NArrayStridesCompatible(destination, source)) {
297 return false;
298 }
299 }
300 cmnMemcpy(destination.Pointer(), source.Pointer(), source.size() * sizeof(value_type));
301 return true;
302 }
303};
304
305
306#endif // _vctFastCopy_h
Container class for fast copy related methods.
Definition vctFastCopy.h:36
static const bool SkipChecks
Definition vctFastCopy.h:118
static bool NArrayCopyCompatible(const _nArray1Type &nArray1, const _nArray2Type &nArray2)
Definition vctFastCopy.h:145
static bool MatrixCopyCompatible(const _matrix1Type &matrix1, const _matrix2Type &matrix2)
Definition vctFastCopy.h:135
static bool ContainerSizesCompatible(const _container1Type &container1, const _container2Type &container2)
Definition vctFastCopy.h:50
static bool NArrayCopy(_destinationNArrayType &destination, const _sourceNArrayType &source, bool performSafetyChecks)
Definition vctFastCopy.h:284
static bool NArrayStridesCompatible(const _nArray1Type &nArray1, const _nArray2Type &nArray2)
Definition vctFastCopy.h:81
static bool VectorCopyCompatible(const _vector1Type &vector1, const _vector2Type &vector2)
Definition vctFastCopy.h:125
static bool VectorStrideCompatible(const _vector1Type &vector1, const _vector2Type &vector2)
Definition vctFastCopy.h:57
static bool VectorCopy(_destinationVectorType &destination, const _sourceVectorType &source, bool performSafetyChecks)
Definition vctFastCopy.h:161
static bool VectorSizeCompatible(const _vector1Type &vector1, const _vector2Type &vector2)
Definition vctFastCopy.h:43
static void ThrowUnlessValidContainerSizes(const _container1Type &container1, const _container2Type &container2) CISST_THROW(std
Definition vctFastCopy.h:104
static bool MatrixCopy(_destinationMatrixType &destination, const _sourceMatrixType &source, bool performSafetyChecks)
Definition vctFastCopy.h:208
static const bool PerformChecks
Definition vctFastCopy.h:119
static void ThrowUnlessValidVectorSizes(const _vector1Type &vector1, const _vector2Type &vector2) CISST_THROW(std
Definition vctFastCopy.h:94
static bool MatrixStridesCompatible(const _matrix1Type &matrix1, const _matrix2Type &matrix2)
Definition vctFastCopy.h:65
Portability across compilers and operating systems tools.
#define CISST_THROW(exceptionParameter)
Somewhat portable compilation warning message. This works with very recent versions of gcc (4....
Definition cmnPortability.h:559
Declaration of cmnRequiresDeepCopy.
bool cmnRequiresDeepCopy(void)
Definition cmnRequiresDeepCopy.h:39
void cmnMemcpy(_elementType *destination, const _elementType *source, const size_t size)
Definition cmnRequiresDeepCopy.h:62
Declaration of the template function cmnThrow.
void cmnThrow(const _exceptionType &except, cmnLogLevel lod=CMN_LOG_LEVEL_INIT_ERROR)
Definition cmnThrow.h:76
size_type cols() const
Definition vctDynamicConstMatrixBase.h:243
size_type rows() const
Definition vctDynamicConstMatrixBase.h:238