cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
6  Author(s): Anton Deguet
7  Created on: 2012-07-09
8 
9  (C) Copyright 2012-2013 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 
22 #pragma once
23 
24 #ifndef _cmnDataFunctionsVectorHelpers_h
25 #define _cmnDataFunctionsVectorHelpers_h
26 
27 template <class _vectorType>
28 void cmnDataVectorCopy(_vectorType & data,
29  const _vectorType & source)
30 {
31  // resize destination if needed
32  data.resize(source.size());
33  typedef _vectorType VectorType;
34  typedef typename VectorType::iterator iterator;
35  typedef typename VectorType::const_iterator const_iterator;
36  const const_iterator endSource = source.end();
37  const_iterator iterSource = source.begin();
38  iterator iterData = data.begin();
39  for (;
40  iterSource != endSource;
41  ++iterSource, ++iterData) {
43  }
44 }
45 
46 template <class _vectorType>
47 std::string cmnDataVectorHumanReadable(const _vectorType & data)
48 {
49  std::stringstream stringStream;
50  stringStream << "[";
51  const size_t size = data.size();
52  for (size_t index = 0; index < size; ++index) {
53  if (index != 0) {
54  stringStream << ", ";
55  }
56  stringStream << cmnData<typename _vectorType::value_type>::HumanReadable(data[index]);
57  }
58  stringStream << "]";
59  return stringStream.str();
60 }
61 
62 template <class _vectorType>
63 void cmnDataVectorSerializeText(const _vectorType & data,
64  std::ostream & outputStream,
65  const char delimiter)
66  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  }
75  cmnData<typename _vectorType::value_type>::SerializeText(*iter, outputStream, delimiter);
76  }
77 }
78 
79 template <class _vectorType>
80 void 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 
96 template <class _vectorType>
97 void cmnDataVectorDeSerializeTextResize(_vectorType & data,
98  std::istream & inputStream,
99  const char delimiter)
100  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 
112 template <class _vectorType>
114  std::istream & inputStream,
115  const char delimiter)
116  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 
131 template <class _vectorType>
132 std::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 
157 template <class _vectorType>
158 void cmnDataVectorSerializeBinary(const _vectorType & data,
159  std::ostream & outputStream)
160  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 
170 template <class _vectorType>
171 void cmnDataVectorDeSerializeBinary(_vectorType & data,
172  std::istream & inputStream,
173  const cmnDataFormat & localFormat,
174  const cmnDataFormat & remoteFormat)
175  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 
186 template <class _vectorType>
187 void cmnDataVectorDeSerializeBinaryResize(_vectorType & data,
188  std::istream & inputStream,
189  const cmnDataFormat & localFormat,
190  const cmnDataFormat & remoteFormat)
191  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 
200 template <class _vectorType>
202  std::istream & inputStream,
203  const cmnDataFormat & localFormat,
204  const cmnDataFormat & remoteFormat)
205  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 
217 template <class _vectorType>
218 size_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 
237 template <class _vectorType, class _elementType>
238 bool 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 
277 template <class _vectorType>
278 std::string cmnDataVectorScalarDescription(const _vectorType & data,
279  const size_t index,
280  const std::string & userDescription)
281  throw (std::out_of_range)
282 {
283  size_t elementIndex, inElementIndex;
284  std::stringstream suffix;
285  const size_t scalarNumber = cmnData<_vectorType>::ScalarNumber(data);
286  if (cmnDataVectorScalarFindInVectorIndex<const _vectorType &, typename _vectorType::value_type>(data, data.size(), scalarNumber,
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 
295 template <class _vectorType>
296 double cmnDataVectorScalar(const _vectorType & data, const size_t index)
297  throw (std::out_of_range)
298 {
299  size_t elementIndex, inElementIndex;
300  const size_t scalarNumber = cmnData<_vectorType>::ScalarNumber(data);
301  if (cmnDataVectorScalarFindInVectorIndex<const _vectorType &, typename _vectorType::value_type>(data, data.size(), scalarNumber,
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:32
void cmnDataVectorSerializeText(const _vectorType &data, std::ostream &outputStream, const char delimiter)
Definition: cmnDataFunctionsVectorHelpers.h:63
size_t cmnDataVectorScalarNumber(const _vectorType &data)
Definition: cmnDataFunctionsVectorHelpers.h:218
std::string cmnDataVectorSerializeDescription(const _vectorType &data, const char delimiter, const std::string &userDescription, const bool serializeSize)
Definition: cmnDataFunctionsVectorHelpers.h:132
std::string cmnDataVectorHumanReadable(const _vectorType &data)
Definition: cmnDataFunctionsVectorHelpers.h:47
size_t CISST_EXPORT cmnDataDeSerializeBinary_size_t(size_t &data, const char *buffer, size_t bufferSize, const cmnDataFormat &localFormat, const cmnDataFormat &remoteFormat)
static void SerializeBinary(const DataType &data, std::ostream &outputStream)
std::string cmnDataVectorScalarDescription(const _vectorType &data, const size_t index, const std::string &userDescription)
Definition: cmnDataFunctionsVectorHelpers.h:278
void cmnDataVectorSerializeBinary(const _vectorType &data, std::ostream &outputStream)
Definition: cmnDataFunctionsVectorHelpers.h:158
static void SerializeText(const DataType &data, std::ostream &outputStream, const char delimiter= ',')
void cmnDataVectorDeSerializeTextResize(_vectorType &data, std::istream &inputStream, const char delimiter)
Definition: cmnDataFunctionsVectorHelpers.h:97
static size_t ScalarNumber(const DataType &data)
static double Scalar(const DataType &data, const size_t index)
Definition: cmnDataFunctions.h:53
void cmnDataVectorDeSerializeBinaryResize(_vectorType &data, std::istream &inputStream, const cmnDataFormat &localFormat, const cmnDataFormat &remoteFormat)
Definition: cmnDataFunctionsVectorHelpers.h:187
static std::string ScalarDescription(const DataType &data, const size_t index, const std::string &userDescription="")
#define cmnThrow(a)
Definition: MinimalCmn.h:4
double cmnDataVectorScalar(const _vectorType &data, const size_t index)
Definition: cmnDataFunctionsVectorHelpers.h:296
static void DeSerializeText(DataType &data, std::istream &inputStream, const char delimiter= ',')
static void DeSerializeBinary(DataType &data, std::istream &inputStream, const cmnDataFormat &localFormat, const cmnDataFormat &remoteFormat)
void cmnDataVectorCopy(_vectorType &data, const _vectorType &source)
Definition: cmnDataFunctionsVectorHelpers.h:28
void cmnDataVectorDeSerializeBinary(_vectorType &data, std::istream &inputStream, const cmnDataFormat &localFormat, const cmnDataFormat &remoteFormat)
Definition: cmnDataFunctionsVectorHelpers.h:171
void cmnDataVectorDeSerializeTextCheckSize(_vectorType &data, std::istream &inputStream, const char delimiter)
Definition: cmnDataFunctionsVectorHelpers.h:113
void cmnDataVectorDeSerializeBinaryCheckSize(_vectorType &data, std::istream &inputStream, const cmnDataFormat &localFormat, const cmnDataFormat &remoteFormat)
Definition: cmnDataFunctionsVectorHelpers.h:201
void cmnDataVectorDeSerializeText(_vectorType &data, std::istream &inputStream, const char delimiter)
Definition: cmnDataFunctionsVectorHelpers.h:80
static void Copy(DataType &data, const DataType &source)
void CISST_EXPORT cmnDataDeSerializeTextDelimiter(std::istream &inputStream, const char delimiter, const char *className)
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