cisst-saw
Loading...
Searching...
No Matches
cmnDataFunctionsVectorHelpers.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: 2012-07-09
7
8 (C) Copyright 2012-2020 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
21#ifndef _cmnDataFunctionsVectorHelpers_h
22#define _cmnDataFunctionsVectorHelpers_h
23
24template <class _vectorType>
25void cmnDataVectorCopy(_vectorType & data,
26 const _vectorType & source)
27{
28 // resize destination if needed
29 data.resize(source.size());
30 typedef _vectorType VectorType;
31 typedef typename VectorType::iterator iterator;
32 typedef typename VectorType::const_iterator const_iterator;
33 const const_iterator endSource = source.end();
34 const_iterator iterSource = source.begin();
35 iterator iterData = data.begin();
36 for (;
37 iterSource != endSource;
38 ++iterSource, ++iterData) {
40 }
41}
42
43template <class _vectorType>
44std::string cmnDataVectorHumanReadable(const _vectorType & data)
45{
46 std::stringstream stringStream;
47 stringStream << "[";
48 typedef _vectorType VectorType;
49 typedef typename VectorType::const_iterator const_iterator;
50 const const_iterator end = data.end();
51 const const_iterator begin = data.begin();
52 for (const_iterator iter = begin; iter != end; ++iter) {
53 if (iter != begin) {
54 stringStream << ", ";
55 }
56 stringStream << cmnData<typename _vectorType::value_type>::HumanReadable(*iter);
57 }
58 stringStream << "]";
59 return stringStream.str();
60}
61
62template <class _vectorType>
63void cmnDataVectorSerializeText(const _vectorType & data,
64 std::ostream & outputStream,
65 const char delimiter)
66 CISST_THROW(std::runtime_error)
67{
68 typedef typename _vectorType::const_iterator const_iterator;
69 const const_iterator begin = data.begin();
70 const const_iterator end = data.end();
71 for (const_iterator iter = begin; iter != end; ++iter) {
72 if (iter != begin) {
73 outputStream << delimiter;
74 }
76 }
77}
78
79template <class _vectorType>
80void cmnDataVectorDeSerializeText(_vectorType & data,
81 std::istream & inputStream,
82 const char delimiter)
83{
84 // get data
85 typedef typename _vectorType::iterator iterator;
86 const iterator begin = data.begin();
87 const iterator end = data.end();
88 for (iterator iter = begin; iter != end; ++iter) {
89 if (iter != begin) {
90 cmnDataDeSerializeTextDelimiter(inputStream, delimiter, "std::vector");
91 }
93 }
94}
95
96template <class _vectorType>
97void cmnDataVectorDeSerializeTextResize(_vectorType & data,
98 std::istream & inputStream,
99 const char delimiter)
100 CISST_THROW(std::runtime_error)
101{
102 // deserialize size
103 size_t size;
104 cmnData<size_t>::DeSerializeText(size, inputStream, delimiter);
105 data.resize(size);
106 if (size > 0) {
107 cmnDataDeSerializeTextDelimiter(inputStream, delimiter, "vctFixedSizeVectorBase");
108 cmnDataVectorDeSerializeText(data, inputStream, delimiter);
109 }
110}
111
112template <class _vectorType>
114 std::istream & inputStream,
115 const char delimiter)
116 CISST_THROW(std::runtime_error)
117{
118 // deserialize size
119 size_t size;
120 cmnData<size_t>::DeSerializeText(size, inputStream, delimiter);
121 if (data.size() != size) {
122 cmnThrow("cmnDataDeSerializeText: vector size doesn't match");
123 return;
124 }
125 if (size > 0) {
126 cmnDataDeSerializeTextDelimiter(inputStream, delimiter, "vctFixedSizeVectorBase");
127 cmnDataVectorDeSerializeText(data, inputStream, delimiter);
128 }
129}
130
131template <class _vectorType>
132std::string cmnDataVectorSerializeDescription(const _vectorType & data,
133 const char delimiter,
134 const std::string & userDescription,
135 const bool serializeSize)
136{
137 std::string prefix = (userDescription == "") ? "v[" : (userDescription + "[");
138 std::stringstream indexSuffix;
139 std::stringstream description;
140 const size_t size = data.size();
141 if (serializeSize) {
142 description << cmnData<size_t>::SerializeDescription(size, delimiter, (userDescription == "") ? "v.size" : (userDescription + ".size"));
143 }
144 size_t index;
145 for (index = 0; index < size; ++index) {
146 if (index != 0 || serializeSize) {
147 description << delimiter;
148 }
149 indexSuffix.clear();
150 indexSuffix.str(std::string());
151 indexSuffix << index << "]";
152 description << cmnData<typename _vectorType::value_type>::SerializeDescription(data[index], delimiter, prefix + indexSuffix.str());
153 }
154 return description.str();
155}
156
157template <class _vectorType>
158void cmnDataVectorSerializeBinary(const _vectorType & data,
159 std::ostream & outputStream)
160 CISST_THROW(std::runtime_error)
161{
162 typedef typename _vectorType::const_iterator const_iterator;
163 const const_iterator end = data.end();
164 const_iterator iter = data.begin();
165 for (; iter != end; ++iter) {
167 }
168}
169
170template <class _vectorType>
171void cmnDataVectorDeSerializeBinary(_vectorType & data,
172 std::istream & inputStream,
173 const cmnDataFormat & localFormat,
174 const cmnDataFormat & remoteFormat)
175 CISST_THROW(std::runtime_error)
176{
177 // deserialize data
178 typedef typename _vectorType::iterator iterator;
179 const iterator end = data.end();
180 iterator iter = data.begin();
181 for (; iter != end; ++iter) {
182 cmnData<typename _vectorType::value_type>::DeSerializeBinary(*iter, inputStream, localFormat, remoteFormat);
183 }
184}
185
186template <class _vectorType>
188 std::istream & inputStream,
189 const cmnDataFormat & localFormat,
190 const cmnDataFormat & remoteFormat)
191 CISST_THROW(std::runtime_error)
192{
193 // deserialize size and resize
194 size_t size;
195 cmnDataDeSerializeBinary_size_t(size, inputStream, localFormat, remoteFormat);
196 data.resize(size);
197 cmnDataVectorDeSerializeBinary(data, inputStream, localFormat, remoteFormat);
198}
199
200template <class _vectorType>
202 std::istream & inputStream,
203 const cmnDataFormat & localFormat,
204 const cmnDataFormat & remoteFormat)
205 CISST_THROW(std::runtime_error)
206{
207 // deserialize size and check
208 size_t size;
209 cmnDataDeSerializeBinary_size_t(size, inputStream, localFormat, remoteFormat);
210 if (data.size() != size) {
211 cmnThrow("cmnDataDeSerializeBinary: vector size doesn't match");
212 return;
213 }
214 cmnDataVectorDeSerializeBinary(data, inputStream, localFormat, remoteFormat);
215}
216
217template <class _vectorType>
218size_t cmnDataVectorScalarNumber(const _vectorType & data)
219{
220 if (data.size() == 0) {
221 return 0;
222 }
223 typedef typename _vectorType::value_type value_type;
225 return data.size() * cmnData<value_type>::ScalarNumber(data[0]);
226 }
227 size_t result = 0;
228 typedef typename _vectorType::const_iterator const_iterator;
229 const const_iterator end = data.end();
230 const_iterator iter = data.begin();
231 for (; iter != end; ++iter) {
232 result += cmnData<value_type>::ScalarNumber(*iter);
233 }
234 return result;
235}
236
237template <class _vectorType, class _elementType>
238bool cmnDataVectorScalarFindInVectorIndex(_vectorType data, const size_t size, const size_t scalarNumber,
239 const size_t index,
240 size_t & elementIndex, size_t & inElementIndex)
241{
243 const size_t scalarNumberPerElement = cmnData<_elementType>::ScalarNumber(data[0]);
244 if (scalarNumberPerElement == 0) {
245 return false;
246 }
247 if (index < scalarNumber) {
248 elementIndex = index / scalarNumberPerElement;
249 inElementIndex = index % scalarNumberPerElement;
250 return true;
251 }
252 return false;
253 }
254 bool indexFound = false;
255 size_t scalarCounter = 0;
256 size_t lastScalarInElement = 0;
257 size_t firstScalarInElement = 0;
258 size_t numberOfScalarsInElement = 0;
259 elementIndex = 0;
260 do {
261 numberOfScalarsInElement = cmnData<_elementType>::ScalarNumber(data[elementIndex]);
262 firstScalarInElement = scalarCounter;
263 lastScalarInElement = scalarCounter + numberOfScalarsInElement - 1;
264 scalarCounter = lastScalarInElement + 1;
265 elementIndex++;
266 indexFound = ((index >= firstScalarInElement) && (index <= lastScalarInElement));
267 } while ((!indexFound)
268 && (elementIndex < size));
269 if (indexFound) {
270 elementIndex--;
271 inElementIndex = index - firstScalarInElement;
272 return true;
273 }
274 return false;
275}
276
277template <class _vectorType>
278std::string cmnDataVectorScalarDescription(const _vectorType & data,
279 const size_t index,
280 const std::string & userDescription)
281 CISST_THROW(std::out_of_range)
282{
283 size_t elementIndex, inElementIndex;
284 std::stringstream suffix;
285 const size_t scalarNumber = cmnData<_vectorType>::ScalarNumber(data);
287 index, elementIndex, inElementIndex)) {
288 suffix << "[" << index << "]";
289 return cmnData<typename _vectorType::value_type>::ScalarDescription(data[elementIndex], inElementIndex, userDescription + suffix.str());
290 }
291 cmnThrow(std::out_of_range("cmnDataScalarDescription: vector index out of range"));
292 return "";
293}
294
295template <class _vectorType>
296double cmnDataVectorScalar(const _vectorType & data, const size_t index)
297 CISST_THROW(std::out_of_range)
298{
299 size_t elementIndex, inElementIndex;
300 const size_t scalarNumber = cmnData<_vectorType>::ScalarNumber(data);
302 index, elementIndex, inElementIndex)) {
303 return cmnData<typename _vectorType::value_type>::Scalar(data[elementIndex], inElementIndex);
304 } else {
305 cmnThrow(std::out_of_range("cmnDataScalar: vector index out of range"));
306 }
307 return 0.123456789; // unreachable, just to avoid compiler warnings
308}
309
310#endif // _cmnDataFunctionsVectorHelpers_h
Definition cmnDataFormat.h:30
Definition cmnDataFunctions.h:51
static void Copy(DataType &data, const DataType &source)
static size_t DeSerializeBinary(DataType &data, const char *buffer, size_t bufferSize, const cmnDataFormat &CMN_UNUSED(localFormat), const cmnDataFormat &CMN_UNUSED(remoteFormat))
static bool ScalarNumberIsFixed(const DataType &data)
static size_t SerializeBinary(const DataType &data, char *buffer, size_t bufferSize)
static std::string static ScalarDescription(const DataType &data, const size_t index, const std::string &userDescription="") CISST_THROW(std double static Scalar(const DataType &data, const size_t index) CISST_THROW(std size_t ScalarNumber(const DataType &data)
Definition cmnDataFunctions.h:162
size_t CISST_EXPORT cmnDataDeSerializeBinary_size_t(size_t &data, const char *buffer, size_t bufferSize, const cmnDataFormat &localFormat, const cmnDataFormat &remoteFormat)
void cmnDataVectorCopy(_vectorType &data, const _vectorType &source)
Definition cmnDataFunctionsVectorHelpers.h:25
void cmnDataVectorSerializeText(const _vectorType &data, std::ostream &outputStream, const char delimiter) CISST_THROW(std
Definition cmnDataFunctionsVectorHelpers.h:63
void cmnDataVectorDeSerializeBinary(_vectorType &data, std::istream &inputStream, const cmnDataFormat &localFormat, const cmnDataFormat &remoteFormat) CISST_THROW(std
Definition cmnDataFunctionsVectorHelpers.h:171
void cmnDataVectorSerializeBinary(const _vectorType &data, std::ostream &outputStream) CISST_THROW(std
Definition cmnDataFunctionsVectorHelpers.h:158
std::string cmnDataVectorSerializeDescription(const _vectorType &data, const char delimiter, const std::string &userDescription, const bool serializeSize)
Definition cmnDataFunctionsVectorHelpers.h:132
size_t cmnDataVectorScalarNumber(const _vectorType &data)
Definition cmnDataFunctionsVectorHelpers.h:218
void cmnDataVectorDeSerializeBinaryCheckSize(_vectorType &data, std::istream &inputStream, const cmnDataFormat &localFormat, const cmnDataFormat &remoteFormat) CISST_THROW(std
Definition cmnDataFunctionsVectorHelpers.h:201
void cmnDataVectorDeSerializeBinaryResize(_vectorType &data, std::istream &inputStream, const cmnDataFormat &localFormat, const cmnDataFormat &remoteFormat) CISST_THROW(std
Definition cmnDataFunctionsVectorHelpers.h:187
void cmnDataVectorDeSerializeTextCheckSize(_vectorType &data, std::istream &inputStream, const char delimiter) CISST_THROW(std
Definition cmnDataFunctionsVectorHelpers.h:113
bool cmnDataVectorScalarFindInVectorIndex(_vectorType data, const size_t size, const size_t scalarNumber, const size_t index, size_t &elementIndex, size_t &inElementIndex)
Definition cmnDataFunctionsVectorHelpers.h:238
void cmnDataVectorDeSerializeTextResize(_vectorType &data, std::istream &inputStream, const char delimiter) CISST_THROW(std
Definition cmnDataFunctionsVectorHelpers.h:97
std::string cmnDataVectorScalarDescription(const _vectorType &data, const size_t index, const std::string &userDescription) CISST_THROW(std
Definition cmnDataFunctionsVectorHelpers.h:278
void cmnDataVectorDeSerializeText(_vectorType &data, std::istream &inputStream, const char delimiter)
Definition cmnDataFunctionsVectorHelpers.h:80
double cmnDataVectorScalar(const _vectorType &data, const size_t index) CISST_THROW(std
Definition cmnDataFunctionsVectorHelpers.h:296
std::string cmnDataVectorHumanReadable(const _vectorType &data)
Definition cmnDataFunctionsVectorHelpers.h:44
#define CISST_THROW(exceptionParameter)
Somewhat portable compilation warning message. This works with very recent versions of gcc (4....
Definition cmnPortability.h:559
void cmnThrow(const _exceptionType &except, cmnLogLevel lod=CMN_LOG_LEVEL_INIT_ERROR)
Definition cmnThrow.h:76
const_iterator end(void) const
Definition vctDynamicConstMatrixBase.h:209
OwnerType::const_iterator const_iterator
Definition vctDynamicConstMatrixBase.h:95
OwnerType::iterator iterator
Definition vctDynamicConstMatrixBase.h:92
size_type size(void) const
Definition vctDynamicConstMatrixBase.h:228
const_iterator begin(void) const
Definition vctDynamicConstMatrixBase.h:203