ALBA
albaTimeMapUX.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: ALBA (Agile Library for Biomedical Applications)
4 Module: albaTimeMapUX
5 Authors: Marco Petrone
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 __albaTimeMap_h
17#define __albaTimeMap_h
18//----------------------------------------------------------------------------
19// includes :
20//----------------------------------------------------------------------------
21#include "albaObject.h"
22#include "albaTimeStamped.h"
23#include "albaSmartPointer.h"
24#include "albaMTime.h"
25#include "albaString.h"
26#include <map>
27#include <vector>
28#include "albaTimeMap.h"
29#include "albaIndent.h"
30#include <math.h>
31#include <assert.h>
32
33//------------------------------------------------------------------------------
34// Forward declarations
35//------------------------------------------------------------------------------
36typedef std::vector<albaTimeStamp> mmuTimeVector;
37
47template <class T>
48class ALBA_EXPORT albaTimeMap : public albaObject, public albaTimeStamped
49{
50public:
51 typedef std::map<albaTimeStamp, albaAutoPointer<T> > TimeMap;
52 typedef std::pair<albaTimeStamp, albaAutoPointer<T> > mmuTimePair;
53
54 albaTimeMap();
55 virtual ~albaTimeMap();
56
59
60 //albaAbstractTypeMacro(albaTimeMap<T>,albaObject);
61
63 void SetItemTypeName(const char *tname) {m_ItemTypeName=tname;}
64 const char *GetItemTypeName() {return m_ItemTypeName;}
65
69 virtual void AppendItem(T *m);
70
72 virtual void AppendAndSetItem(T *m);
73
77 virtual void PrependItem(T *m);
78
80 typename albaTimeMap<T>::TimeMap::iterator FindItem(T *m);
81
87 virtual void InsertItem(T *m);
88
90 virtual void RemoveItem(typename albaTimeMap<T>::TimeMap::iterator it);
91
93 int RemoveItem(int idx);
94
96 virtual void RemoveAllItems();
97
99 albaTimeStamp GetItemTime(int idx);
100
102 void GetTimeStamps(mmuTimeVector &kframes) const;
103
105 int GetNumberOfItems() const {return m_TimeMap.size();};
106
108 //albaTimeStamp GetTimeStamp() {return m_CurrentTime;}
109 //void SetTimeStamp(albaTimeStamp t);
110
112 void GetTimeBounds(albaTimeStamp tbounds[2]);
113
115 void DeepCopy(albaTimeMap *vitem);
116
120 bool Equals(albaTimeMap *vmat);
121
123 typename albaTimeMap<T>::TimeMap::iterator FindNearestItem(albaTimeStamp t);
124
126 typename albaTimeMap<T>::TimeMap::iterator FindItemBefore(albaTimeStamp t);
127
133
135 typename albaTimeMap<T>::TimeMap::iterator FindItemByIndex(int idx);
136
138 albaID FindItemIndex(albaTimeStamp t);
139
141 T *GetItem(albaTimeStamp t);
142
146 T *GetNearestItem(albaTimeStamp t);
147
151 T *GetItemBefore(albaTimeStamp t);
152
154 T *GetItemByIndex(int idx);
155
156 virtual void Print(std::ostream& os, const int tabs=0) const;
157
158 typename albaTimeMap<T>::TimeMap::iterator Begin() {return m_TimeMap.begin();}
159 typename albaTimeMap<T>::TimeMap::iterator End() {return m_TimeMap.end();}
160 typename albaTimeMap<T>::TimeMap::iterator Last() {return --(m_TimeMap.end());}
161
162protected:
165};
166
168
169//-----------------------------------------------------------------------
170template <class T>
172//-----------------------------------------------------------------------
173{
174}
175
176//-----------------------------------------------------------------------
177template <class T>
179//-----------------------------------------------------------------------
180{
181}
182
183//-----------------------------------------------------------------------
184template <class T>
186//-----------------------------------------------------------------------
187{
188 assert(m);
189 if (this->GetNumberOfItems()>0)
190 {
191 // Get last item
192 assert(m_TimeMap.rbegin()->second);
193 // append adding 1 to the last time
194 m->SetTimeStamp(m_TimeMap.rbegin()->first+1);
195 }
196 else
197 {
198 m->SetTimeStamp(0);
199 }
200 AppendItem(m);
201}
202
203//-----------------------------------------------------------------------
204template <class T>
206//-----------------------------------------------------------------------
207{
208 assert(m);
209 if (!m_ItemTypeName.IsEmpty())
210 {
211 assert(m->IsA(m_ItemTypeName));
212 if (!m->IsA(m_ItemTypeName))
213 {
214 albaErrorMacro("Unsupported Item type \""<<m->GetTypeName()<<"\", allowed type is \""<<m_ItemTypeName<<"\": cannot Append item!");
215 return;
216 }
217 }
218 m_TimeMap.insert(m_TimeMap.end(),mmuTimePair(m->GetTimeStamp(),m));
219 Modified();
220}
221
222//-----------------------------------------------------------------------
223template <class T>
225//-----------------------------------------------------------------------
226{
227 assert(m);
228 if (!m_ItemTypeName.IsEmpty())
229 {
230 assert(m->IsA(m_ItemTypeName));
231 if (!m->IsA(m_ItemTypeName))
232 {
233 albaErrorMacro("Unsupported Item type \""<<m->GetTypeName()<<"\", allowed type is \""<<m_ItemTypeName<<"\": cannot Prepend item!");
234 return;
235 }
236 }
237 m_TimeMap.insert(m_TimeMap.begin(),mmuTimePair(m->GetTimeStamp(),m));
238 Modified();
239}
240//-------------------------------------------------------------------------
241template <class T>
243//-------------------------------------------------------------------------
244{
245 assert(m);
246 if (!m_ItemTypeName.IsEmpty())
247 {
248 assert(m->IsA(m_ItemTypeName));
249 if (!m->IsA(m_ItemTypeName))
250 {
251 albaErrorMacro("Unsupported Item type \""<<m->GetTypeName()<<"\", allowed type is \""<<m_ItemTypeName<<"\": cannot Insert item!");
252 return;
253 }
254 }
255
256 m_TimeMap[m->GetTimeStamp()]=m;
257 Modified();
258}
259
260//-------------------------------------------------------------------------
261template <class T>
263//-------------------------------------------------------------------------
264{
265 // this is a sorted array
266 if (m_TimeMap.size()>0)
267 {
268 tbounds[0]=m_TimeMap.begin()->first;
269 tbounds[1]=m_TimeMap.rbegin()->first;
270 }
271 else
272 {
273 tbounds[0]=-1;
274 tbounds[1]=-1;
275 }
276}
277
278//-------------------------------------------------------------------------
279template <class T>
281//-------------------------------------------------------------------------
282{
283 kframes.clear();
284
285 for (typename albaTimeMap<T>::TimeMap::const_iterator it=m_TimeMap.begin();it!=m_TimeMap.end();it++)
286 {
287 kframes.push_back(it->first);
288 }
289}
290
291//-------------------------------------------------------------------------
292template <class T>
294//-------------------------------------------------------------------------
295{
296 RemoveAllItems();
297 //m_TimeMap=o->m_TimeMap;
299 for (it=o->Begin();it!=o->End();it++)
300 {
301 T *m=it->second;
302 T *new_item=m->NewInstance();
303 new_item->DeepCopy(m);
304 AppendItem(new_item);
305 }
306 Modified();
307}
308
309//-------------------------------------------------------------------------
310template <class T>
312//-------------------------------------------------------------------------
313{
314 if (o==NULL)
315 return false;
316
317 if (GetNumberOfItems()!=o->GetNumberOfItems())
318 return false;
319
322 for (it=m_TimeMap.begin(),it2=o->m_TimeMap.begin();it!=m_TimeMap.end();it++,it2++)
323 {
324 T *m=it->second;
325 T *m2=it2->second;
326
327 if (!m->Equals(m2))
328 return false;
329 }
330
331 return true;
332}
333
334//-------------------------------------------------------------------------
335template <class T>
337//-------------------------------------------------------------------------
338{
339 m_TimeMap.erase(it);
340 Modified();
341}
342
343//-------------------------------------------------------------------------
344template <class T>
346//-------------------------------------------------------------------------
347{
348 typename albaTimeMap<T>::TimeMap::iterator it=FindItemByIndex(idx);
349 if (it!=m_TimeMap.end())
350 {
351 RemoveItem(it);
352 return ALBA_OK;
353 }
354
355 return ALBA_ERROR;
356}
357
358//-------------------------------------------------------------------------
359template <class T>
361//-------------------------------------------------------------------------
362{
363 m_TimeMap.clear();
364 Modified();
365}
366
367//-------------------------------------------------------------------------
368template <class T>
370//-------------------------------------------------------------------------
371{
372 std::pair<typename albaTimeMap<T>::TimeMap::iterator,typename albaTimeMap<T>::TimeMap::iterator> range=m_TimeMap.equal_range(t);
373 if (range.first!=m_TimeMap.end())
374 {
375 if (range.second!=m_TimeMap.end())
376 {
377 if (fabs(range.first->first-t)>fabs(range.second->first-t))
378 return range.second;
379 }
380 return range.first;
381 }
382 else if (range.second!=m_TimeMap.end())
383 {
384 return range.second;
385 }
386
387 return --range.second;
388}
389
390
391//-------------------------------------------------------------------------
392template <class T>
394//-------------------------------------------------------------------------
395{
396 if (m_TimeMap.size()>0)
397 {
398 typename albaTimeMap<T>::TimeMap::iterator it=m_TimeMap.lower_bound(t); // find first item >= t
399 if (it==m_TimeMap.end()||it!=m_TimeMap.begin()&&it->first>t) // if > t get the previous
400 --it;
401
402 return it;
403 }
404 else
405 {
406 return m_TimeMap.end();
407 }
408
409}
410
411
412//-------------------------------------------------------------------------
413template <class T>
415//-------------------------------------------------------------------------
416{
417 return m_TimeMap.find(t);
418}
419
420//----------------------------------------------------------------------------
421template <class T>
423//----------------------------------------------------------------------------
424{
425 assert(m);
426 return m_TimeMap.find(m->GetTimeStamp());
427}
428
429//----------------------------------------------------------------------------
430template <class T>
431void albaTimeMap<T>::Print(std::ostream& os, const int tabs) const
432//----------------------------------------------------------------------------
433{
434 albaIndent indent(tabs);
435 os << indent << "Number of Items:"<<GetNumberOfItems()<<"\n";
436 mmuTimeVector tvector;
437 os << indent << "Time Stamps: {";
438 GetTimeStamps(tvector);
439 for (unsigned int i=0;i<tvector.size();i++)
440 {
441 if (i!=0)
442 os << ", ";
443 os << tvector[i];
444 }
445
446 os << "}\n";
447}
448
449//----------------------------------------------------------------------------
450template <class T>
452//----------------------------------------------------------------------------
453{
454 T *m=GetItemByIndex(idx);
455 if (m) return m->GetTimeStamp();
456 return -1;
457}
458
459//----------------------------------------------------------------------------
460template <class T>
462//----------------------------------------------------------------------------
463{
464 if ((unsigned int)idx<m_TimeMap.size() && idx>=0)
465 {
466 typename albaTimeMap<T>::TimeMap::iterator it=m_TimeMap.begin();
467 std::advance(it,idx);
468 return it;
469 }
470
471 return m_TimeMap.end();
472}
473
474//----------------------------------------------------------------------------
475template <class T>
477//----------------------------------------------------------------------------
478{
479 typename albaTimeMap<T>::TimeMap::iterator it=FindItem(t);
480 return it!=m_TimeMap.end()?std::distance(m_TimeMap.begin(),it):-1;
481}
482
483//----------------------------------------------------------------------------
484template <class T>
486//----------------------------------------------------------------------------
487{
488 typename albaTimeMap<T>::TimeMap::iterator it=FindItemByIndex(idx);
489 return (it!=m_TimeMap.end())?it->second:NULL;
490}
491
492//----------------------------------------------------------------------------
493template <class T>
495//----------------------------------------------------------------------------
496{
497 typename albaTimeMap<T>::TimeMap::iterator it=FindItem(t);
498 return (it!=m_TimeMap.end())?it->second:NULL;
499}
500//----------------------------------------------------------------------------
501template <class T>
503//----------------------------------------------------------------------------
504{
505 typename albaTimeMap<T>::TimeMap::iterator it=FindNearestItem(t);
506 return (it!=m_TimeMap.end())?it->second:NULL;
507}
508//----------------------------------------------------------------------------
509template <class T>
511//----------------------------------------------------------------------------
512{
513 typename albaTimeMap<T>::TimeMap::iterator it=FindItemBefore(t);
514 return (it!=m_TimeMap.end())?it->second:NULL;
515}
516
517
518#endif
ALBA_OK
Definition: albaDefines.h:66
#define albaErrorMacro(x)
Macro for printing Error messages in log area.
Definition: albaDefines.h:259
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 > mmuTimeVector
Definition: albaTimeMapUX.h:36
std::vector< albaTimeStamp > mmuTimeVector
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
virtual void Print(std::ostream &os, const int indent=0) const
print debug information for this object
albaString - performs common string operations on c-strings.
Definition: albaString.h:43
a dynamic associative sorted array of timestamped objects indexed by their "timestamp".
Definition: albaTimeMapUX.h:49
virtual void RemoveAllItems()
Remove all the items.
bool Equals(albaTimeMap *vmat)
Compare two different arrays for equality.
TimeMap m_TimeMap
the set storing the datasets
virtual void Print(std::ostream &os, const int tabs=0) const
print debug information for this object
virtual void PrependItem(T *m)
Insert an item to the vector trying to prepend it, anyway the array is kept sorted.
virtual void RemoveItem(typename albaTimeMap< T >::TimeMap::iterator it)
Remove an item given its iterator.
void GetTimeBounds(albaTimeStamp tbounds[2])
Set/Get the Current time for this object.
int GetNumberOfItems() const
Return the number of ITEMS stored in this object.
albaTimeMap< T >::TimeMap::iterator Last()
albaTimeMap(const albaTimeMap< T > &)
void GetTimeStamps(mmuTimeVector &kframes) const
Return the list of timestamp of the key matrixes in the given vector.
albaTimeMap< T >::TimeMap::iterator End()
std::pair< albaTimeStamp, albaAutoPointer< T > > mmuTimePair
Definition: albaTimeMapUX.h:52
albaTimeStamp GetItemTime(int idx)
Return the timestamp of the i-th item.
albaString m_ItemTypeName
the name of the item type accepted by this container
void DeepCopy(albaTimeMap *vitem)
Copy data from another array.
albaTimeMap< T >::TimeMap::iterator FindItemByIndex(int idx)
return iterator of item with given index
T * GetItem(albaTimeStamp t)
find and return item corresponding to timestamp t.
albaTimeMap< T >::TimeMap::iterator Begin()
void SetItemTypeName(const char *tname)
set the TypeName of the kind of item accepted by this container
Definition: albaTimeMapUX.h:63
virtual void AppendAndSetItem(T *m)
append item setting its timestamp to the highest one + 1
T * GetNearestItem(albaTimeStamp t)
Return the pointer to the item with timestamp nearest the given one.
albaTimeMap< T >::TimeMap::iterator FindItem(T *m)
Find an item index given its pointer.
virtual void AppendItem(T *m)
Insert an item to the vector trying to append it, anyway the array is kept sorted.
T * GetItemByIndex(int idx)
return the item with given its order index.
void operator=(const albaTimeMap< T > &)
T * GetItemBefore(albaTimeStamp t)
Return the pointer to the item with timestamp nearest the given one.
const char * GetItemTypeName()
Definition: albaTimeMapUX.h:64
albaTimeMap< T >::TimeMap::iterator FindNearestItem(albaTimeStamp t)
Find the item with the timestamp nearest to t.
virtual void InsertItem(T *m)
Set the item for a specified time.
albaID FindItemIndex(albaTimeStamp t)
return index of the given item.
albaTimeMap< T >::TimeMap::iterator FindItemBefore(albaTimeStamp t)
Find the item with timestamp <=t.
std::map< albaTimeStamp, albaAutoPointer< T > > TimeMap
Definition: albaTimeMapUX.h:51
virtual ~albaTimeMap()
class acting as an interface for timestamped objects This object simply defines few methods for manag...