ALBA
albaTimeMapWIN.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: ALBA (Agile Library for Biomedical Applications)
4 Module: albaTimeMapWIN
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//------------------------------------------------------------------------------
35// Forward declarations
36//------------------------------------------------------------------------------
37typedef std::vector<albaTimeStamp> mmuTimeVector;
38
48template <class T>
49class albaTimeMap : public albaObject, public albaTimeStamped
50{
51public:
52 typedef std::map<albaTimeStamp, albaAutoPointer<T> > TimeMap;
53 typedef std::pair<albaTimeStamp, albaAutoPointer<T> > mmuTimePair;
54
56 virtual ~albaTimeMap();
57
59 void operator=(const albaTimeMap<T>&){};
60
61 //albaAbstractTypeMacro(albaTimeMap<T>,albaObject);
62
64 void SetItemTypeName(const char *tname) {m_ItemTypeName=tname;}
65 const char *GetItemTypeName() {return m_ItemTypeName;}
66
70 virtual void AppendItem(T *m);
71
73 virtual void AppendAndSetItem(T *m);
74
78 virtual void PrependItem(T *m);
79
82
88 virtual void InsertItem(T *m);
89
92
94 int RemoveItem(int idx);
95
97 virtual void RemoveAllItems();
98
101
103 void GetTimeStamps(mmuTimeVector &kframes) const;
104
106 int GetNumberOfItems() const {return m_TimeMap.size();};
107
109 //albaTimeStamp GetTimeStamp() {return m_CurrentTime;}
110 //void SetTimeStamp(albaTimeStamp t);
111
113 void GetTimeBounds(albaTimeStamp tbounds[2]);
114
116 void DeepCopy(albaTimeMap *vitem);
117
121 bool Equals(albaTimeMap *vmat);
122
125
128
134
137
140
143
148
153
155 T *GetItemByIndex(int idx);
156
157 virtual void Print(std::ostream& os, const int tabs=0) const;
158
161 typename albaTimeMap<T>::TimeMap::iterator Last() {return --(m_TimeMap.end());}
162
163protected:
166};
167
168
169
171
172//-----------------------------------------------------------------------
173template <class T>
175//-----------------------------------------------------------------------
176{
177}
178
179//-----------------------------------------------------------------------
180template <class T>
182//-----------------------------------------------------------------------
183{
184}
185
186//-----------------------------------------------------------------------
187template <class T>
189//-----------------------------------------------------------------------
190{
191 assert(m);
192 if (this->GetNumberOfItems()>0)
193 {
194 // Get last item
195 assert(m_TimeMap.rbegin()->second);
196 // append adding 1 to the last time
197 m->SetTimeStamp(m_TimeMap.rbegin()->first+1);
198 }
199 else
200 {
201 m->SetTimeStamp(0);
202 }
203 AppendItem(m);
204}
205
206//-----------------------------------------------------------------------
207template <class T>
209//-----------------------------------------------------------------------
210{
211 assert(m);
212 if (!m_ItemTypeName.IsEmpty())
213 {
214 assert(m->IsA(m_ItemTypeName));
215 if (!m->IsA(m_ItemTypeName))
216 {
217 albaErrorMacro("Unsupported Item type \""<<m->GetTypeName()<<"\", allowed type is \""<<m_ItemTypeName<<"\": cannot Append item!");
218 return;
219 }
220 }
221 m_TimeMap.insert(m_TimeMap.end(),mmuTimePair(m->GetTimeStamp(),m));
222 Modified();
223}
224
225//-----------------------------------------------------------------------
226template <class T>
228//-----------------------------------------------------------------------
229{
230 assert(m);
231 if (!m_ItemTypeName.IsEmpty())
232 {
233 assert(m->IsA(m_ItemTypeName));
234 if (!m->IsA(m_ItemTypeName))
235 {
236 albaErrorMacro("Unsupported Item type \""<<m->GetTypeName()<<"\", allowed type is \""<<m_ItemTypeName<<"\": cannot Prepend item!");
237 return;
238 }
239 }
240 m_TimeMap.insert(m_TimeMap.begin(),mmuTimePair(m->GetTimeStamp(),m));
241 Modified();
242}
243//-------------------------------------------------------------------------
244template <class T>
246//-------------------------------------------------------------------------
247{
248 assert(m);
249 if (!m_ItemTypeName.IsEmpty())
250 {
251 assert(m->IsA(m_ItemTypeName));
252 if (!m->IsA(m_ItemTypeName))
253 {
254 albaErrorMacro("Unsupported Item type \""<<m->GetTypeName()<<"\", allowed type is \""<<m_ItemTypeName<<"\": cannot Insert item!");
255 return;
256 }
257 }
258
259 m_TimeMap[m->GetTimeStamp()]=m;
260 Modified();
261}
262
263//-------------------------------------------------------------------------
264template <class T>
266//-------------------------------------------------------------------------
267{
268 // this is a sorted array
269 if (m_TimeMap.size()>0)
270 {
271 tbounds[0]=m_TimeMap.begin()->first;
272 tbounds[1]=m_TimeMap.rbegin()->first;
273 }
274 else
275 {
276 tbounds[0]=-1;
277 tbounds[1]=-1;
278 }
279}
280
281//-------------------------------------------------------------------------
282template <class T>
284//-------------------------------------------------------------------------
285{
286 kframes.clear();
287
288 for (albaTimeMap<T>::TimeMap::const_iterator it=m_TimeMap.begin();it!=m_TimeMap.end();it++)
289 {
290 kframes.push_back(it->first);
291 }
292}
293
294//-------------------------------------------------------------------------
295template <class T>
297//-------------------------------------------------------------------------
298{
299 RemoveAllItems();
300 //m_TimeMap=o->m_TimeMap;
302 for (it=o->Begin();it!=o->End();it++)
303 {
304 T *m=it->second;
305 T *new_item=m->NewInstance();
306 new_item->DeepCopy(m);
307 AppendItem(new_item);
308 }
309 Modified();
310}
311
312//-------------------------------------------------------------------------
313template <class T>
315//-------------------------------------------------------------------------
316{
317 if (o==NULL)
318 return false;
319
320 if (GetNumberOfItems()!=o->GetNumberOfItems())
321 return false;
322
325 for (it=m_TimeMap.begin(),it2=o->m_TimeMap.begin();it!=m_TimeMap.end();it++,it2++)
326 {
327 T *m=it->second;
328 T *m2=it2->second;
329
330 if (!m->Equals(m2))
331 return false;
332 }
333
334 return true;
335}
336
337//-------------------------------------------------------------------------
338template <class T>
340//-------------------------------------------------------------------------
341{
342 m_TimeMap.erase(it);
343 Modified();
344}
345
346//-------------------------------------------------------------------------
347template <class T>
349//-------------------------------------------------------------------------
350{
351 albaTimeMap<T>::TimeMap::iterator it=FindItemByIndex(idx);
352 if (it!=m_TimeMap.end())
353 {
354 RemoveItem(it);
355 return ALBA_OK;
356 }
357
358 return ALBA_ERROR;
359}
360
361//-------------------------------------------------------------------------
362template <class T>
364//-------------------------------------------------------------------------
365{
366 m_TimeMap.clear();
367 Modified();
368}
369
370//-------------------------------------------------------------------------
371template <class T>
373//-------------------------------------------------------------------------
374{
375 std::pair<albaTimeMap<T>::TimeMap::iterator,albaTimeMap<T>::TimeMap::iterator> range=m_TimeMap.equal_range(t);
376 if (range.first!=m_TimeMap.end())
377 {
378 if (range.second!=m_TimeMap.end())
379 {
380 if (fabs(range.first->first-t)>fabs(range.second->first-t))
381 return range.second;
382 }
383 return range.first;
384 }
385 else if (range.second!=m_TimeMap.end())
386 {
387 return range.second;
388 }
389
390 return --range.second;
391}
392
393
394//-------------------------------------------------------------------------
395template <class T>
397//-------------------------------------------------------------------------
398{
399 if (m_TimeMap.size()>0)
400 {
401 albaTimeMap<T>::TimeMap::iterator it=m_TimeMap.lower_bound(t); // find first item >= t
402 if (it==m_TimeMap.end()||it!=m_TimeMap.begin()&&it->first>t) // if > t get the previous
403 --it;
404
405 return it;
406 }
407 else
408 {
409 return m_TimeMap.end();
410 }
411
412}
413
414
415//-------------------------------------------------------------------------
416template <class T>
418//-------------------------------------------------------------------------
419{
420 return m_TimeMap.find(t);
421}
422
423//----------------------------------------------------------------------------
424template <class T>
426//----------------------------------------------------------------------------
427{
428 assert(m);
429 return m_TimeMap.find(m->GetTimeStamp());
430}
431
432//----------------------------------------------------------------------------
433template <class T>
434void albaTimeMap<T>::Print(std::ostream& os, const int tabs) const
435//----------------------------------------------------------------------------
436{
437 albaIndent indent(tabs);
438 os << indent << "Number of Items:"<<GetNumberOfItems()<<"\n";
439 mmuTimeVector tvector;
440 os << indent << "Time Stamps: {";
441 GetTimeStamps(tvector);
442 for (unsigned int i=0;i<tvector.size();i++)
443 {
444 if (i!=0)
445 os << ", ";
446 os << tvector[i];
447 }
448
449 os << "}\n";
450}
451
452//----------------------------------------------------------------------------
453template <class T>
455//----------------------------------------------------------------------------
456{
457 T *m=GetItemByIndex(idx);
458 if (m) return m->GetTimeStamp();
459 return -1;
460}
461
462//----------------------------------------------------------------------------
463template <class T>
465//----------------------------------------------------------------------------
466{
467 if ((unsigned int)idx<m_TimeMap.size() && idx>=0)
468 {
469 albaTimeMap<T>::TimeMap::iterator it=m_TimeMap.begin();
470 std::advance(it,idx);
471 return it;
472 }
473
474 return m_TimeMap.end();
475}
476
477//----------------------------------------------------------------------------
478template <class T>
480//----------------------------------------------------------------------------
481{
483 return it!=m_TimeMap.end()?std::distance(m_TimeMap.begin(),it):-1;
484}
485
486//----------------------------------------------------------------------------
487template <class T>
489//----------------------------------------------------------------------------
490{
491 albaTimeMap<T>::TimeMap::iterator it=FindItemByIndex(idx);
492 return (it!=m_TimeMap.end())?it->second:NULL;
493}
494
495//----------------------------------------------------------------------------
496template <class T>
498//----------------------------------------------------------------------------
499{
501 return (it!=m_TimeMap.end())?it->second:NULL;
502}
503//----------------------------------------------------------------------------
504template <class T>
506//----------------------------------------------------------------------------
507{
508 albaTimeMap<T>::TimeMap::iterator it=FindNearestItem(t);
509 return (it!=m_TimeMap.end())?it->second:NULL;
510}
511//----------------------------------------------------------------------------
512template <class T>
514//----------------------------------------------------------------------------
515{
516 albaTimeMap<T>::TimeMap::iterator it=FindItemBefore(t);
517 return (it!=m_TimeMap.end())?it->second:NULL;
518}
519
520
521#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
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.
virtual void AppendItem(T *m)
Insert an item to the vector trying to append it, anyway the array is kept sorted.
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
albaTimeMap< T >::TimeMap::iterator FindItem(albaTimeStamp t)
Find the item with the timestamp==t.
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.
int RemoveItem(int idx)
Remove an item given its index.
albaTimeMap< T >::TimeMap::iterator End()
std::pair< albaTimeStamp, albaAutoPointer< T > > mmuTimePair
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()
virtual ~albaTimeMap()
void SetItemTypeName(const char *tname)
set the TypeName of the kind of item accepted by this container
virtual void AppendAndSetItem(T *m)
append item setting its timestamp to the highest one + 1
virtual void AppendAndSetItem(T *m)
append item setting its timestamp to the highest one + 1
virtual void RemoveItem(typename albaTimeMap< T >::TimeMap::iterator it)
Remove an item given its iterator.
virtual void InsertItem(T *m)
Set the item for a specified time.
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.
virtual void RemoveAllItems()
Remove all the items.
const char * GetItemTypeName()
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
virtual void PrependItem(T *m)
Insert an item to the vector trying to prepend it, anyway the array is kept sorted.
virtual ~albaTimeMap()
class acting as an interface for timestamped objects This object simply defines few methods for manag...