Changed the interface to TIterator one (Laurent)
[u/mrichter/AliRoot.git] / MUON / AliMUON2DMapIterator.cxx
1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 *                                                                        *
4 * Author: The ALICE Off-line Project.                                    *
5 * Contributors are mentioned in the code where appropriate.              *
6 *                                                                        *
7 * Permission to use, copy, modify and distribute this software and its   *
8 * documentation strictly for non-commercial purposes is hereby granted   *
9 * without fee, provided that the above copyright notice appears in all   *
10 * copies and that both the copyright notice and this permission notice   *
11 * appear in the supporting documentation. The authors make no claims     *
12 * about the suitability of this software for any purpose. It is          *
13 * provided "as is" without express or implied warranty.                  *
14 **************************************************************************/
15
16 // $Id$
17
18 #include "AliMUON2DMapIterator.h"
19
20 /// \class AliMUON2DMapIterator
21 /// Implementation of TIterator for 2Dmaps
22 /// 
23 /// A simple implementation of VDataIterator for 2Dmaps.
24 ///
25 /// \author Laurent Aphecetche
26
27 #include "AliMpExMap.h"
28
29 /// \cond CLASSIMP
30 ClassImp(AliMUON2DMapIterator)
31 /// \endcond
32
33 //_____________________________________________________________________________
34 AliMUON2DMapIterator::AliMUON2DMapIterator(const AliMpExMap& theMap)
35 : TIterator(), 
36 fIter(theMap.GetIterator()),
37 fIter2(0x0),
38 fCurrentI(-1),
39 fCurrentJ(-1)
40 {
41   /// default ctor
42   Reset();
43 }
44
45 //_____________________________________________________________________________
46 AliMUON2DMapIterator::AliMUON2DMapIterator(const AliMUON2DMapIterator& rhs)
47 : TIterator(rhs),
48 fIter(rhs.fIter),
49 fIter2(0x0),
50 fCurrentI(rhs.fCurrentI),
51 fCurrentJ(rhs.fCurrentJ)
52 {
53   /// copy ctor
54   if ( rhs.fIter2 ) fIter2 = new TExMapIter(*rhs.fIter2);
55
56 }
57 //_____________________________________________________________________________
58 AliMUON2DMapIterator& 
59 AliMUON2DMapIterator::operator=(const AliMUON2DMapIterator& rhs)
60 {
61   /// assignment operator
62   if ( this != &rhs ) 
63   {
64     fIter = rhs.fIter;
65     fIter2 = 0x0;
66     if ( rhs.fIter2 ) fIter2 = new TExMapIter(*rhs.fIter2);
67     fCurrentI = rhs.fCurrentI;
68     fCurrentJ = rhs.fCurrentJ;
69   }
70   return *this;
71 }
72
73 //_____________________________________________________________________________
74 TIterator& 
75 AliMUON2DMapIterator::operator=(const TIterator& rhs)
76 {
77   /// overriden operator= (imposed by Root's definition of TIterator::operator= ?)
78   
79   if ( this != &rhs && rhs.IsA() == AliMUON2DMapIterator::Class() ) 
80   {
81     const AliMUON2DMapIterator& rhs1 = static_cast<const AliMUON2DMapIterator&>(rhs);
82     
83     fIter = rhs1.fIter;
84     fIter2 = 0x0;
85     if ( rhs1.fIter2 ) fIter2 = new TExMapIter(*rhs1.fIter2);
86     fCurrentI = rhs1.fCurrentI;
87     fCurrentJ = rhs1.fCurrentJ;
88   }
89   return *this;
90 }
91
92 //_____________________________________________________________________________
93 AliMUON2DMapIterator::~AliMUON2DMapIterator()
94 {
95   /// dtor
96   delete fIter2;
97 }
98
99 //_____________________________________________________________________________
100 const TCollection* 
101 AliMUON2DMapIterator::GetCollection() const
102 {
103   /// Return 0 as we're not really dealing with a Root TCollection...
104   return 0x0;
105 }
106
107 //_____________________________________________________________________________
108 TObject*
109 AliMUON2DMapIterator::GetValue(TExMapIter& iter, Int_t& theKey) const
110 {
111   /// return the value corresponding to theKey in iterator iter
112   theKey = -1;
113   Long_t key, value;
114   Bool_t ok = iter.Next(key,value);
115   if (!ok) return 0x0;
116   theKey = (Int_t)(key & 0xFFFF);
117   return reinterpret_cast<TObject*>(value);
118 }
119
120 //_____________________________________________________________________________
121 AliMpExMap*
122 AliMUON2DMapIterator::GetMap(TExMapIter& iter, Int_t& key)  const
123 {
124   /// get the map corresponding to key
125   AliMpExMap* rv(0x0);
126   TObject* o = GetValue(iter,key);
127   if (o)
128   {
129     rv = dynamic_cast<AliMpExMap*>(o);
130   }
131   return rv;
132 }
133
134 //_____________________________________________________________________________
135 TObject*
136 AliMUON2DMapIterator::Next()
137 {
138   /// logic :
139   /// get TObject* from fIter2
140   /// if null, increment fIter2 by getting next on fIter
141   
142   if (!fIter2) return 0x0;
143   
144   TObject* o = GetValue(*fIter2,fCurrentJ);
145   if (!o)
146   {
147     // fIter2 exhausted, try to get the next one
148     AliMpExMap* m = GetMap(fIter,fCurrentI);
149     if (!m)
150     {
151       // nothing left, we are done.
152       return 0x0;
153     }
154     delete fIter2;
155     fIter2 = new TExMapIter(m->GetIterator());
156     o = GetValue(*fIter2,fCurrentJ);
157   }
158   
159   return o;
160 }
161
162 //_____________________________________________________________________________
163 void
164 AliMUON2DMapIterator::Reset()
165 {
166   /// rewind the iterator
167   delete fIter2;
168   fIter.Reset();
169   AliMpExMap* m = GetMap(fIter,fCurrentI);
170   if (m)
171   {
172     fIter2 = new TExMapIter(m->GetIterator());
173   }  
174 }