ALBA
albaTimeMapScalarUX.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: ALBA (Agile Library for Biomedical Applications)
4 Module: albaTimeMapScalarUX
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#define ALBA_SCALAR_MIN -1.0e+299
20#define ALBA_SCALAR_MAX 1.0e+299
21
22//----------------------------------------------------------------------------
23// includes :
24//----------------------------------------------------------------------------
25#include "albaObject.h"
26#include "albaTimeStamped.h"
27
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 ALBA_EXPORT albaTimeMapScalar : public albaObject, public albaTimeStamped
53{
54public:
55 typedef std::map<albaTimeStamp, T > TimeMapScalars;
56 typedef std::pair<albaTimeStamp, T > mmuTimePairScalars;
57
59 virtual ~albaTimeMapScalar();
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
79 typename albaTimeMapScalar<T>::TimeMapScalars::iterator FindItemByValue(T m);
80
86 virtual void InsertItem(albaTimeStamp t, T m);
87
89 virtual void RemoveItem(typename albaTimeMapScalar<T>::TimeMapScalars::iterator it);
90
92 int RemoveItem(int idx);
93
95 virtual void RemoveAllItems();
96
98 albaTimeStamp GetItemTime(int idx);
99
101 void GetTimeStamps(mmuTimeVectorScalars &kframes) const;
102
104 int GetNumberOfItems() const {return m_TimeMap.size();};
105
107 void GetTimeBounds(albaTimeStamp tbounds[2]);
108
110 void DeepCopy(albaTimeMapScalar *vitem);
111
115 bool Equals(albaTimeMapScalar *vmat);
116
119
122
127
129 typename albaTimeMapScalar<T>::TimeMapScalars::iterator FindItemByIndex(int idx);
130
132 albaID FindItemIndex(albaTimeStamp t);
133
135 T GetItem(albaTimeStamp t);
136
140 T GetNearestItem(albaTimeStamp t);
141
145 T GetItemBefore(albaTimeStamp t);
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 (typename 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 typename 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
349//-------------------------------------------------------------------------
350template <class T>
352//-------------------------------------------------------------------------
353{
354 if (m_TimeMap.size()>0)
355 {
356 typename albaTimeMapScalar<T>::TimeMapScalars::iterator it=m_TimeMap.lower_bound(t); // find first item >= t
357 if (it==m_TimeMap.end()||it!=m_TimeMap.begin()&&it->first>t) // if > t get the previous
358 --it;
359
360 return it;
361 }
362 else
363 {
364 return m_TimeMap.end();
365 }
366
367}
368
369
370//-------------------------------------------------------------------------
371template <class T>
373//-------------------------------------------------------------------------
374{
375 return m_TimeMap.find(t);
376}
377
378//----------------------------------------------------------------------------
379template <class T>
381//----------------------------------------------------------------------------
382{
383 for (typename albaTimeMapScalar<T>::TimeMapScalars::iterator it = m_TimeMap.begin(); it != m_TimeMap.end(); it++)
384 {
385 T m2 = it->second;
386 if (m == m2)
387 return it;
388 }
389
390 return m_TimeMap.end();
391}
392
393//----------------------------------------------------------------------------
394template <class T>
395void albaTimeMapScalar<T>::Print(std::ostream& os, const int tabs) const
396//----------------------------------------------------------------------------
397{
398 albaIndent indent(tabs);
399 os << indent << "Number of Items:"<<GetNumberOfItems()<<"\n";
400 mmuTimeVectorScalars tvector;
401 os << indent << "Time Stamps: {";
402 GetTimeStamps(tvector);
403 for (unsigned int i=0;i<tvector.size();i++)
404 {
405 if (i!=0)
406 os << ", ";
407 os << tvector[i];
408 }
409
410 os << "}\n";
411}
412
413//----------------------------------------------------------------------------
414template <class T>
416//----------------------------------------------------------------------------
417{
418 typename albaTimeMapScalar<T>::TimeMapScalars::iterator it = FindItemByIndex(idx);
419 return (it != m_TimeMap.end()) ? it->first : -1;
420}
421
422//----------------------------------------------------------------------------
423template <class T>
425//----------------------------------------------------------------------------
426{
427 if ((unsigned int)idx<m_TimeMap.size() && idx>=0)
428 {
429 typename albaTimeMapScalar<T>::TimeMapScalars::iterator it=m_TimeMap.begin();
430 std::advance(it,idx);
431 return it;
432 }
433
434 return m_TimeMap.end();
435}
436
437//----------------------------------------------------------------------------
438template <class T>
440//----------------------------------------------------------------------------
441{
443 return it!=m_TimeMap.end()?std::distance(m_TimeMap.begin(),it):-1;
444}
445
446//----------------------------------------------------------------------------
447template <class T>
449//----------------------------------------------------------------------------
450{
451 typename albaTimeMapScalar<T>::TimeMapScalars::iterator it=FindItemByIndex(idx);
452 return (it!=m_TimeMap.end())?it->second:ALBA_SCALAR_MIN;
453}
454
455//----------------------------------------------------------------------------
456template <class T>
458//----------------------------------------------------------------------------
459{
461 return (it!=m_TimeMap.end())?it->second:ALBA_SCALAR_MIN;
462}
463//----------------------------------------------------------------------------
464template <class T>
466//----------------------------------------------------------------------------
467{
468 typename albaTimeMapScalar<T>::TimeMapScalars::iterator it=FindNearestItem(t);
469 return (it!=m_TimeMap.end())?it->second:ALBA_SCALAR_MIN;
470}
471//----------------------------------------------------------------------------
472template <class T>
474//----------------------------------------------------------------------------
475{
476 typename albaTimeMapScalar<T>::TimeMapScalars::iterator it=FindItemBefore(t);
477 return (it!=m_TimeMap.end())?it->second:ALBA_SCALAR_MIN;
478}
479#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
#define ALBA_SCALAR_MIN
std::vector< albaTimeStamp > mmuTimeVectorScalars
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
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 scalars indexed by their "timestamp".
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.
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.
T GetItemByIndex(int idx)
return the item with given its order index.
T GetNearestItem(albaTimeStamp t)
Return the pointer to the item with timestamp nearest the given one.
albaTimeMapScalar(const albaTimeMapScalar< T > &)
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...