24 #ifndef _cmnDataFunctionsVectorHelpers_h
25 #define _cmnDataFunctionsVectorHelpers_h
27 template <
class _vectorType>
29 const _vectorType & source)
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();
40 iterSource != endSource;
41 ++iterSource, ++iterData) {
46 template <
class _vectorType>
49 std::stringstream stringStream;
51 const size_t size = data.size();
52 for (
size_t index = 0; index < size; ++index) {
56 stringStream << cmnData<typename _vectorType::value_type>::HumanReadable(data[index]);
59 return stringStream.str();
62 template <
class _vectorType>
64 std::ostream & outputStream,
66 throw (std::runtime_error)
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) {
73 outputStream << delimiter;
79 template <
class _vectorType>
81 std::istream & inputStream,
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) {
96 template <
class _vectorType>
98 std::istream & inputStream,
100 throw (std::runtime_error)
112 template <
class _vectorType>
114 std::istream & inputStream,
115 const char delimiter)
116 throw (std::runtime_error)
121 if (data.size() != size) {
122 cmnThrow(
"cmnDataDeSerializeText: vector size doesn't match");
131 template <
class _vectorType>
133 const char delimiter,
134 const std::string & userDescription,
135 const bool serializeSize)
137 std::string prefix = (userDescription ==
"") ?
"v[" : (userDescription +
"[");
138 std::stringstream indexSuffix;
139 std::stringstream description;
140 const size_t size = data.size();
142 description << cmnData<size_t>::SerializeDescription(size, delimiter, (userDescription ==
"") ?
"v.size" : (userDescription +
".size"));
145 for (index = 0; index < size; ++index) {
146 if (index != 0 || serializeSize) {
147 description << delimiter;
150 indexSuffix.str(std::string());
151 indexSuffix << index <<
"]";
152 description << cmnData<typename _vectorType::value_type>::SerializeDescription(data[index], delimiter, prefix + indexSuffix.str());
154 return description.str();
157 template <
class _vectorType>
159 std::ostream & outputStream)
160 throw (std::runtime_error)
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) {
170 template <
class _vectorType>
172 std::istream & inputStream,
175 throw (std::runtime_error)
178 typedef typename _vectorType::iterator iterator;
179 const iterator end = data.end();
180 iterator iter = data.begin();
181 for (; iter != end; ++iter) {
186 template <
class _vectorType>
188 std::istream & inputStream,
191 throw (std::runtime_error)
200 template <
class _vectorType>
202 std::istream & inputStream,
205 throw (std::runtime_error)
210 if (data.size() != size) {
211 cmnThrow(
"cmnDataDeSerializeBinary: vector size doesn't match");
217 template <
class _vectorType>
220 if (data.size() == 0) {
223 typedef typename _vectorType::value_type value_type;
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) {
237 template <
class _vectorType,
class _elementType>
240 size_t & elementIndex,
size_t & inElementIndex)
244 if (scalarNumberPerElement == 0) {
247 if (index < scalarNumber) {
248 elementIndex = index / scalarNumberPerElement;
249 inElementIndex = index % scalarNumberPerElement;
254 bool indexFound =
false;
255 size_t scalarCounter = 0;
256 size_t lastScalarInElement = 0;
257 size_t firstScalarInElement = 0;
258 size_t numberOfScalarsInElement = 0;
262 firstScalarInElement = scalarCounter;
263 lastScalarInElement = scalarCounter + numberOfScalarsInElement - 1;
264 scalarCounter = lastScalarInElement + 1;
266 indexFound = ((index >= firstScalarInElement) && (index <= lastScalarInElement));
267 }
while ((!indexFound)
268 && (elementIndex < size));
271 inElementIndex = index - firstScalarInElement;
277 template <
class _vectorType>
280 const std::string & userDescription)
281 throw (std::out_of_range)
283 size_t elementIndex, inElementIndex;
284 std::stringstream suffix;
286 if (cmnDataVectorScalarFindInVectorIndex<const _vectorType &, typename _vectorType::value_type>(data, data.size(), scalarNumber,
287 index, elementIndex, inElementIndex)) {
288 suffix <<
"[" << index <<
"]";
291 cmnThrow(std::out_of_range(
"cmnDataScalarDescription: vector index out of range"));
295 template <
class _vectorType>
297 throw (std::out_of_range)
299 size_t elementIndex, inElementIndex;
301 if (cmnDataVectorScalarFindInVectorIndex<const _vectorType &, typename _vectorType::value_type>(data, data.size(), scalarNumber,
302 index, elementIndex, inElementIndex)) {
305 cmnThrow(std::out_of_range(
"cmnDataScalar: vector index out of range"));
310 #endif // _cmnDataFunctionsVectorHelpers_h
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