ALBA
albaTimeMapScalarWIN.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: ALBA (Agile Library for Biomedical Applications)
4 Module: albaTimeMapScalarWIN
5 Authors: Paolo Quadrani
6
7 Copyright (c) BIC
8 All rights reserved. See Copyright.txt or
9
10
11 This software is distributed WITHOUT ANY WARRANTY; without even
12 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE. See the above copyright notice for more information.
14
15=========================================================================*/
16#ifndef __albaTimeMapScalar_h
17#define __albaTimeMapScalar_h
18
19
20#define ALBA_SCALAR_MIN -1.0e+299
21#define ALBA_SCALAR_MAX 1.0e+299
22
23//----------------------------------------------------------------------------
24// includes :
25//----------------------------------------------------------------------------
26#include "albaObject.h"
27#include "albaTimeStamped.h"
28#include "albaMTime.h"
29#include "albaString.h"
30#include <map>
31#include <vector>
32#include "albaTimeMapScalar.h"
33#include "albaIndent.h"
34#include <math.h>
35#include <assert.h>
36
37//------------------------------------------------------------------------------
38// Forward declarations
39//------------------------------------------------------------------------------
40typedef std::vector<albaTimeStamp> mmuTimeVectorScalars;
41
51template <class T>
52class albaTimeMapScalar : public albaObject, public albaTimeStamped
53{
54public:
55 typedef std::map<albaTimeStamp, T > TimeMapScalars;
56 typedef std::pair<albaTimeStamp, T > mmuTimePairScalars;
57
60
63
65 void SetItemTypeName(const char *tname) {m_ItemTypeName = tname;}
66 const char *GetItemTypeName() {return m_ItemTypeName;}
67
71 virtual void AppendItem(albaTimeStamp t, T m);
72
76 virtual void PrependItem(albaTimeStamp t, T m);
77
80
86 virtual void InsertItem(albaTimeStamp t, T m);
87
90
92 int RemoveItem(int idx);
93
95 virtual void RemoveAllItems();
96
99
102
104 int GetNumberOfItems() const {return m_TimeMap.size();};
105
107 void GetTimeBounds(albaTimeStamp tbounds[2]);
108
111
116
119
122
127
130
133
136
141
146
148 T GetItemByIndex(int idx);
149
150 virtual void Print(std::ostream& os, const int tabs=0) const;
151
155
156protected:
159};
160
161
163
164
165//-----------------------------------------------------------------------
166template <class T>
168//-----------------------------------------------------------------------
169{
170}
171
172//-----------------------------------------------------------------------
173template <class T>
175//-----------------------------------------------------------------------
176{
177}
178
179//-----------------------------------------------------------------------
180template <class T>
182//-----------------------------------------------------------------------
183{
184 m_TimeMap.insert(m_TimeMap.end(), mmuTimePairScalars(t, m));
185 Modified();
186}
187
188//-----------------------------------------------------------------------
189template <class T>
191//-----------------------------------------------------------------------
192{
193 m_TimeMap.insert(m_TimeMap.begin(),mmuTimePairScalars(t, m));
194 Modified();
195}
196//-------------------------------------------------------------------------
197template <class T>
199//-------------------------------------------------------------------------
200{
201 m_TimeMap[t] = m;
202 Modified();
203}
204
205//-------------------------------------------------------------------------
206template <class T>
208//-------------------------------------------------------------------------
209{
210 // this is a sorted array
211 if (m_TimeMap.size() > 0)
212 {
213 tbounds[0] = m_TimeMap.begin()->first;
214 tbounds[1] = m_TimeMap.rbegin()->first;
215 }
216 else
217 {
218 tbounds[0] = -1;
219 tbounds[1] = -1;
220 }
221}
222
223//-------------------------------------------------------------------------
224template <class T>
226//-------------------------------------------------------------------------
227{
228 kframes.clear();
229
230 for (albaTimeMapScalar<T>::TimeMapScalars::const_iterator it = m_TimeMap.begin(); it != m_TimeMap.end(); it++)
231 {
232 kframes.push_back(it->first);
233 }
234}
235
236//-------------------------------------------------------------------------
237template <class T>
239//-------------------------------------------------------------------------
240{
241 RemoveAllItems();
242
244 for (it = o->BeginScalarVector(); it != o->EndScalarVector(); it++)
245 {
246 T new_item = it->second;
247 AppendItem(it->first, new_item);
248 }
249 Modified();
250}
251
252//-------------------------------------------------------------------------
253template <class T>
255//-------------------------------------------------------------------------
256{
257 if (o == NULL)
258 return false;
259
260 if (GetNumberOfItems() != o->GetNumberOfItems())
261 return false;
262
265 for (it = m_TimeMap.begin(), it2 = o->m_TimeMap.begin(); it != m_TimeMap.end(); it++, it2++)
266 {
267 T m = it->second;
268 T m2 = it2->second;
269
270 if (m != m2)
271 return false;
272 }
273
274 return true;
275}
276
277//-------------------------------------------------------------------------
278template <class T>
280//-------------------------------------------------------------------------
281{
282 m_TimeMap.erase(it);
283 Modified();
284}
285
286//-------------------------------------------------------------------------
287template <class T>
289//-------------------------------------------------------------------------
290{
291 albaTimeMapScalar<T>::TimeMapScalars::iterator it = FindItemByIndex(idx);
292 if (it != m_TimeMap.end())
293 {
294 RemoveItem(it);
295 return ALBA_OK;
296 }
297
298 return ALBA_ERROR;
299}
300
301//-------------------------------------------------------------------------
302template <class T>
304//-------------------------------------------------------------------------
305{
306 m_TimeMap.clear();
307 Modified();
308}
309
310//-------------------------------------------------------------------------
311template <class T>
313//-------------------------------------------------------------------------
314{
322
323 lowIt = FindItemBefore(t); // find first item < t
324 upIt = m_TimeMap.lower_bound(t); // find first item >= t
325
326 if(lowIt != m_TimeMap.end())
327 {
328 if(upIt != m_TimeMap.end())
329 {
330 if(fabs(lowIt->first - t) < fabs(upIt->first - t))
331 {
332 return lowIt;
333 }
334 else
335 {
336 return upIt;
337 }
338 }
339 return lowIt;
340 }
341 else if(upIt != m_TimeMap.end())
342 {
343 return upIt;
344 }
345 return --upIt;
346}
347
348//-------------------------------------------------------------------------
349template <class T>
351//-------------------------------------------------------------------------
352{
353 if (m_TimeMap.size() > 0)
354 {
355 albaTimeMapScalar<T>::TimeMapScalars::iterator it = m_TimeMap.lower_bound(t); // find first item >= t
356 if (it == m_TimeMap.end() || it != m_TimeMap.begin() && it->first > t) // if > t get the previous
357 --it;
358
359 return it;
360 }
361 else
362 {
363 return m_TimeMap.end();
364 }
365}
366
367//-------------------------------------------------------------------------
368template <class T>
370//-------------------------------------------------------------------------
371{
372 return m_TimeMap.find(t);
373}
374
375//-------------------------------------------------------------------------
376template <class T>
378//-------------------------------------------------------------------------
379{
380 for (albaTimeMapScalar<T>::TimeMapScalars::iterator it = m_TimeMap.begin(); it != m_TimeMap.end(); it++)
381 {
382 T m2 = it->second;
383 if (m == m2)
384 return it;
385 }
386
387 return m_TimeMap.end();
388}
389
390//----------------------------------------------------------------------------
391template <class T>
392void albaTimeMapScalar<T>::Print(std::ostream& os, const int tabs) const
393//----------------------------------------------------------------------------
394{
395 albaIndent indent(tabs);
396 os << indent << "Number of Items:" << GetNumberOfItems() << "\n";
397 mmuTimeVectorScalars tvector;
398 os << indent << "Time Stamps: {";
399 GetTimeStamps(tvector);
400 for (unsigned int i = 0; i < tvector.size(); i++)
401 {
402 if (i!=0)
403 os << ", ";
404 os << tvector[i];
405 }
406
407 os << "}\n";
408}
409
410//----------------------------------------------------------------------------
411template <class T>
413//----------------------------------------------------------------------------
414{
415 albaTimeMapScalar<T>::TimeMapScalars::iterator it = FindItemByIndex(idx);
416 return (it != m_TimeMap.end()) ? it->first : -1;
417}
418
419//----------------------------------------------------------------------------
420template <class T>
422//----------------------------------------------------------------------------
423{
424 if ((unsigned int)idx < m_TimeMap.size() && idx >= 0)
425 {
427 std::advance(it, idx);
428 return it;
429 }
430
431 return m_TimeMap.end();
432}
433
434//----------------------------------------------------------------------------
435template <class T>
437//----------------------------------------------------------------------------
438{
440 return it != m_TimeMap.end() ? std::distance(m_TimeMap.begin(), it) : -1;
441}
442
443//----------------------------------------------------------------------------
444template <class T>
446//----------------------------------------------------------------------------
447{
448 albaTimeMapScalar<T>::TimeMapScalars::iterator it = FindItemByIndex(idx);
449 return (it != m_TimeMap.end()) ? it->second : ALBA_SCALAR_MIN;
450}
451
452//----------------------------------------------------------------------------
453template <class T>
455//----------------------------------------------------------------------------
456{
458 return (it != m_TimeMap.end()) ? it->second : ALBA_SCALAR_MIN;
459}
460//----------------------------------------------------------------------------
461template <class T>
463//----------------------------------------------------------------------------
464{
466 return (it != m_TimeMap.end()) ? it->second : ALBA_SCALAR_MIN;
467}
468//----------------------------------------------------------------------------
469template <class T>
471//----------------------------------------------------------------------------
472{
474 return (it != m_TimeMap.end()) ? it->second : ALBA_SCALAR_MIN;
475}
476
477#endif
ALBA_OK
Definition: albaDefines.h:66
long albaID
type for IDs inside ALBA
Definition: albaDefines.h:58
ALBA_ERROR
Definition: albaDefines.h:67
double albaTimeStamp
type for time varying data timestamps (not for pipelines timestamps!)
Definition: albaDefines.h:57
std::vector< albaTimeStamp > mmuTimeVectorScalars
#define ALBA_SCALAR_MIN
std::vector< albaTimeStamp > mmuTimeVectorScalars
albaIndent - a simple class to control print indentation.
Definition: albaIndent.h:31
Abstract superclass for all ALBA classes implementing RTTI APIs.
Definition: albaObject.h:38
albaString - performs common string operations on c-strings.
Definition: albaString.h:43
a dynamic associative sorted array of scalars indexed by their "timestamp".
virtual void Print(std::ostream &os, const int tabs=0) const
print debug information for this object
virtual void InsertItem(albaTimeStamp t, T m)
Set the item for a specified time.
albaID FindItemIndex(albaTimeStamp t)
return index of the given item.
albaTimeMapScalar< T >::TimeMapScalars::iterator FindItemByValue(T m)
Find an item index given its pointer.
albaTimeMapScalar< T >::TimeMapScalars::iterator FindItemBefore(albaTimeStamp t)
Find the item with timestamp <= t.
void SetItemTypeName(const char *tname)
set the TypeName of the kind of item accepted by this container
albaTimeMapScalar< T >::TimeMapScalars::iterator FindItemByIndex(int idx)
return iterator of item with given index
bool Equals(albaTimeMapScalar *vmat)
Compare two different arrays for equality.
virtual void AppendItem(albaTimeStamp t, T m)
Insert an item to the vector trying to append it, anyway the array is kept sorted.
void GetTimeStamps(mmuTimeVectorScalars &kframes) const
Return the list of timestamp of the key scalars in the given vector.
void GetTimeBounds(albaTimeStamp tbounds[2])
Return the time bounds for this vector, i.e.
virtual void PrependItem(albaTimeStamp t, T m)
Insert an item to the vector trying to prepend it, anyway the array is kept sorted.
void DeepCopy(albaTimeMapScalar *vitem)
Copy data from another array.
T GetItem(albaTimeStamp t)
find and return item corresponding to timestamp t.
TimeMapScalars m_TimeMap
the set storing the scalars
virtual void Print(std::ostream &os, const int tabs=0) const
print debug information for this object
albaTimeMapScalar< T >::TimeMapScalars::iterator FindItem(albaTimeStamp t)
Find the item with the timestamp==t.
const char * GetItemTypeName()
virtual void RemoveItem(typename albaTimeMapScalar< T >::TimeMapScalars::iterator it)
Remove an item given its iterator.
albaTimeMapScalar< T >::TimeMapScalars::iterator EndScalarVector()
albaString m_ItemTypeName
the name of the item type accepted by this container
albaTimeStamp GetItemTime(int idx)
Return the timestamp of the i-th item.
int RemoveItem(int idx)
Remove an item given its index.
albaTimeMapScalar< T >::TimeMapScalars::iterator LastScalarVector()
albaTimeMapScalar< T >::TimeMapScalars::iterator FindNearestItem(albaTimeStamp t)
Find the item with the timestamp nearest to t.
virtual void InsertItem(albaTimeStamp t, T m)
Set the item for a specified time.
std::map< albaTimeStamp, T > TimeMapScalars
virtual void RemoveAllItems()
Remove all the items.
virtual void PrependItem(albaTimeStamp t, T m)
Insert an item to the vector trying to prepend it, anyway the array is kept sorted.
virtual ~albaTimeMapScalar()
T GetItemByIndex(int idx)
return the item with given its order index.
virtual void RemoveItem(typename albaTimeMapScalar< T >::TimeMapScalars::iterator it)
Remove an item given its iterator.
T GetNearestItem(albaTimeStamp t)
Return the pointer to the item with timestamp nearest the given one.
albaTimeMapScalar(const albaTimeMapScalar< T > &)
virtual void AppendItem(albaTimeStamp t, T m)
Insert an item to the vector trying to append it, anyway the array is kept sorted.
virtual void RemoveAllItems()
Remove all the items.
albaTimeMapScalar< T >::TimeMapScalars::iterator BeginScalarVector()
std::pair< albaTimeStamp, T > mmuTimePairScalars
T GetItemBefore(albaTimeStamp t)
Return the pointer to the item with timestamp nearest the given one.
int GetNumberOfItems() const
Return the number of ITEMS stored in this object.
void operator=(const albaTimeMapScalar< T > &)
class acting as an interface for timestamped objects This object simply defines few methods for manag...