Make it independent from (loading the) mapping
[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 //-----------------------------------------------------------------------------
21 /// \class AliMUON2DMapIterator
22 /// Implementation of TIterator for 2Dmaps
23 /// 
24 /// A simple implementation of VDataIterator for 2Dmaps.
25 ///
26 /// \author Laurent Aphecetche
27 //-----------------------------------------------------------------------------
28
29 #include "AliMpExMap.h"
30
31 /// \cond CLASSIMP
32 ClassImp(AliMUON2DMapIterator)
33 /// \endcond
34
35 //_____________________________________________________________________________
36 AliMUON2DMapIterator::AliMUON2DMapIterator(const AliMpExMap& theMap)
37 : TIterator(), 
38 fIter(theMap.GetIterator()),
39 fIter2(0x0),
40 fCurrentI(-1),
41 fCurrentJ(-1)
42 {
43   /// default ctor
44   Reset();
45 }
46
47 //_____________________________________________________________________________
48 AliMUON2DMapIterator::AliMUON2DMapIterator(const AliMUON2DMapIterator& rhs)
49 : TIterator(rhs),
50 fIter(rhs.fIter),
51 fIter2(0x0),
52 fCurrentI(rhs.fCurrentI),
53 fCurrentJ(rhs.fCurrentJ)
54 {
55   /// copy ctor
56   if ( rhs.fIter2 ) fIter2 = new TExMapIter(*rhs.fIter2);
57
58 }
59 //_____________________________________________________________________________
60 AliMUON2DMapIterator& 
61 AliMUON2DMapIterator::operator=(const AliMUON2DMapIterator& rhs)
62 {
63   /// assignment operator
64   if ( this != &rhs ) 
65   {
66     fIter = rhs.fIter;
67     fIter2 = 0x0;
68     if ( rhs.fIter2 ) fIter2 = new TExMapIter(*rhs.fIter2);
69     fCurrentI = rhs.fCurrentI;
70     fCurrentJ = rhs.fCurrentJ;
71   }
72   return *this;
73 }
74
75 //_____________________________________________________________________________
76 TIterator& 
77 AliMUON2DMapIterator::operator=(const TIterator& rhs)
78 {
79   /// overriden operator= (imposed by Root's definition of TIterator::operator= ?)
80   
81   if ( this != &rhs && rhs.IsA() == AliMUON2DMapIterator::Class() ) 
82   {
83     const AliMUON2DMapIterator& rhs1 = static_cast<const AliMUON2DMapIterator&>(rhs);
84     
85     fIter = rhs1.fIter;
86     fIter2 = 0x0;
87     if ( rhs1.fIter2 ) fIter2 = new TExMapIter(*rhs1.fIter2);
88     fCurrentI = rhs1.fCurrentI;
89     fCurrentJ = rhs1.fCurrentJ;
90   }
91   return *this;
92 }
93
94 //_____________________________________________________________________________
95 AliMUON2DMapIterator::~AliMUON2DMapIterator()
96 {
97   /// dtor
98   delete fIter2;
99 }
100
101 //_____________________________________________________________________________
102 const TCollection* 
103 AliMUON2DMapIterator::GetCollection() const
104 {
105   /// Return 0 as we're not really dealing with a Root TCollection...
106   return 0x0;
107 }
108
109 //_____________________________________________________________________________
110 TObject*
111 AliMUON2DMapIterator::GetValue(TExMapIter& iter, Int_t& theKey) const
112 {
113   /// return the value corresponding to theKey in iterator iter
114   theKey = -1;
115   Long_t key, value;
116   Bool_t ok = iter.Next(key,value);
117   if (!ok) return 0x0;
118   theKey = (Int_t)(key & 0xFFFF);
119   return reinterpret_cast<TObject*>(value);
120 }
121
122 //_____________________________________________________________________________
123 AliMpExMap*
124 AliMUON2DMapIterator::GetMap(TExMapIter& iter, Int_t& key)  const
125 {
126   /// get the map corresponding to key
127   AliMpExMap* rv(0x0);
128   TObject* o = GetValue(iter,key);
129   if (o)
130   {
131     rv = dynamic_cast<AliMpExMap*>(o);
132   }
133   return rv;
134 }
135
136 //_____________________________________________________________________________
137 TObject*
138 AliMUON2DMapIterator::Next()
139 {
140   /// logic :
141   /// get TObject* from fIter2
142   /// if null, increment fIter2 by getting next on fIter
143   
144   if (!fIter2) return 0x0;
145   
146   TObject* o = GetValue(*fIter2,fCurrentJ);
147   if (!o)
148   {
149     // fIter2 exhausted, try to get the next one
150     AliMpExMap* m = GetMap(fIter,fCurrentI);
151     if (!m)
152     {
153       // nothing left, we are done.
154       return 0x0;
155     }
156     delete fIter2;
157     fIter2 = new TExMapIter(m->GetIterator());
158     o = GetValue(*fIter2,fCurrentJ);
159   }
160   
161   return o;
162 }
163
164 //_____________________________________________________________________________
165 void
166 AliMUON2DMapIterator::Reset()
167 {
168   /// rewind the iterator
169   delete fIter2;
170   fIter.Reset();
171   AliMpExMap* m = GetMap(fIter,fCurrentI);
172   if (m)
173   {
174     fIter2 = new TExMapIter(m->GetIterator());
175   }  
176 }