Implemented iterator (Laurent)
authorivana <ivana@f7af4fe6-9843-0410-8265-dc069ae4e863>
Tue, 12 Sep 2006 14:32:46 +0000 (14:32 +0000)
committerivana <ivana@f7af4fe6-9843-0410-8265-dc069ae4e863>
Tue, 12 Sep 2006 14:32:46 +0000 (14:32 +0000)
MUON/AliMUON2DMap.cxx
MUON/AliMUON2DMap.h
MUON/AliMUON2DMapIterator.cxx [new file with mode: 0644]
MUON/AliMUON2DMapIterator.h [new file with mode: 0644]

index 8287d75..932e339 100644 (file)
 #include "AliMUON2DMap.h"
 
 #include "AliLog.h"
+#include "AliMUONVDataIterator.h"
+#include "AliMUON2DMapIterator.h"
 #include "AliMpExMap.h"
+#include "AliMpIntPair.h"
 
 /// \class AliMUON2DMap
 /// \brief Basic implementation of AliMUONV2DStore container using
@@ -92,6 +95,19 @@ AliMUON2DMap::Get(Int_t i, Int_t j) const
 }
 
 //_____________________________________________________________________________
+AliMUONVDataIterator*
+AliMUON2DMap::Iterator() const
+{
+  // Create and return an iterator on this map
+  // Returned iterator must be deleted by user.
+  if ( fMap ) 
+  {
+    return new AliMUON2DMapIterator(*fMap);
+  }
+  return 0x0;
+}
+
+//_____________________________________________________________________________
 void
 AliMUON2DMap::Print(Option_t*) const
 {
index b333516..a8b657e 100644 (file)
@@ -23,6 +23,9 @@ public:
   AliMUON2DMap();  
   virtual ~AliMUON2DMap();
 
+  /// The returned iterator is owned by the client.
+  AliMUONVDataIterator* Iterator() const;
+  
   virtual TObject* Get(Int_t i, Int_t j) const;
   virtual Bool_t Set(Int_t i, Int_t j, TObject* object, Bool_t replace);
   /// Whether or not this container is the owner of its contents.
@@ -35,8 +38,8 @@ protected:
   AliMUON2DMap&  operator = (const AliMUON2DMap& other);
 
 private:
-    void CopyTo(AliMUON2DMap& destination) const;
-  
+  void CopyTo(AliMUON2DMap& destination) const;
+
 private:
   AliMpExMap* fMap; ///< Our internal map (an AliMpExMap of AliMpExMaps)
   
diff --git a/MUON/AliMUON2DMapIterator.cxx b/MUON/AliMUON2DMapIterator.cxx
new file mode 100644 (file)
index 0000000..507b82f
--- /dev/null
@@ -0,0 +1,133 @@
+/**************************************************************************
+* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
+*                                                                        *
+* Author: The ALICE Off-line Project.                                    *
+* Contributors are mentioned in the code where appropriate.              *
+*                                                                        *
+* Permission to use, copy, modify and distribute this software and its   *
+* documentation strictly for non-commercial purposes is hereby granted   *
+* without fee, provided that the above copyright notice appears in all   *
+* copies and that both the copyright notice and this permission notice   *
+* appear in the supporting documentation. The authors make no claims     *
+* about the suitability of this software for any purpose. It is          *
+* provided "as is" without express or implied warranty.                  *
+**************************************************************************/
+
+// $Id$
+
+#include "AliMUON2DMapIterator.h"
+#include "AliMpExMap.h"
+#include "TMap.h"
+#include "AliLog.h"
+#include "AliMpIntPair.h"
+
+/// \class AliMUON2DMapIterator
+/// \brief Implementation of AliMUONVDataIterator for 2Dmaps
+/// 
+/// A simple implementation of VDataIterator for 2Dmaps.
+///
+/// \author Laurent Aphecetche
+
+/// \cond CLASSIMP
+ClassImp(AliMUON2DMapIterator)
+/// \endcond
+
+//_____________________________________________________________________________
+AliMUON2DMapIterator::AliMUON2DMapIterator(AliMpExMap& theMap)
+: AliMUONVDataIterator(), 
+fIter(theMap.GetIterator()),
+fIter2(0x0),
+fCurrentI(-1),
+fCurrentJ(-1)
+{
+  // default ctor
+  Reset();
+}
+
+//_____________________________________________________________________________
+AliMUON2DMapIterator::~AliMUON2DMapIterator()
+{
+  // dtor
+  delete fIter2;
+}
+
+//_____________________________________________________________________________
+TObject*
+AliMUON2DMapIterator::GetValue(TExMapIter& iter, Int_t& theKey) const
+{
+  // return the value corresponding to theKey in iterator iter
+  theKey = -1;
+  Long_t key, value;
+  Bool_t ok = iter.Next(key,value);
+  if (!ok) return 0x0;
+  theKey = (Int_t)(key & 0xFFFF);
+  return reinterpret_cast<TObject*>(value);
+}
+
+//_____________________________________________________________________________
+AliMpExMap*
+AliMUON2DMapIterator::GetMap(TExMapIter& iter, Int_t& key) 
+{
+  // get the map corresponding to key
+  AliMpExMap* rv(0x0);
+  TObject* o = GetValue(iter,key);
+  if (o)
+  {
+    rv = dynamic_cast<AliMpExMap*>(o);
+    if (!rv)
+    {
+      AliFatal("boom");
+    }
+  }
+  return rv;
+}
+
+//_____________________________________________________________________________
+TObject*
+AliMUON2DMapIterator::Next()
+{
+  // logic :
+  // get TObject* from fIter2
+  // if null, increment fIter2 by getting next on fIter
+  
+  if (!fIter2) return 0x0;
+  
+  TObject* o = GetValue(*fIter2,fCurrentJ);
+  if (!o)
+  {
+    // fIter2 exhausted, try to get the next one
+    AliMpExMap* m = GetMap(fIter,fCurrentI);
+    if (!m)
+    {
+      // nothing left, we are done.
+      return 0x0;
+    }
+    delete fIter2;
+    fIter2 = new TExMapIter(m->GetIterator());
+    o = GetValue(*fIter2,fCurrentJ);
+  }
+  return new TPair(new AliMpIntPair(fCurrentI,fCurrentJ),o);
+}
+
+//_____________________________________________________________________________
+void
+AliMUON2DMapIterator::Reset()
+{
+  // rewind the iterator
+  delete fIter2;
+  fIter.Reset();
+  AliMpExMap* m = GetMap(fIter,fCurrentI);
+  if (m)
+  {
+    fIter2 = new TExMapIter(m->GetIterator());
+  }  
+}
+
+//_____________________________________________________________________________
+Bool_t
+AliMUON2DMapIterator::Remove()
+{
+  // to be implemented if needed
+  AliInfo("Not supported yet");
+  return kFALSE;
+}
diff --git a/MUON/AliMUON2DMapIterator.h b/MUON/AliMUON2DMapIterator.h
new file mode 100644 (file)
index 0000000..93179ef
--- /dev/null
@@ -0,0 +1,65 @@
+#ifndef ALIMUON2DMAPITERATOR_H
+#define ALIMUON2DMAPITERATOR_H
+
+/* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
+* See cxx source for full Copyright notice                               */
+
+// $Id$
+
+/// \ingroup base
+/// \class AliMUON2DMapIterator
+/// \brief Implementation of AliMUONVDataIterator for 2D maps
+/// 
+/// \author Laurent Aphecetche
+
+#ifndef ROOT_TObject
+#  include "TObject.h"
+#endif
+#ifndef ROOT_TExMap
+#  include "TExMap.h"
+#endif
+#ifndef ALIMUONVDATAITERATOR_H
+#  include "AliMUONVDataIterator.h"
+#endif
+
+class AliMpExMap;
+
+//_____________________________________________________________________________
+class AliMUON2DMapIterator : public AliMUONVDataIterator
+{
+public:
+  AliMUON2DMapIterator(AliMpExMap& theMap);
+  
+  virtual ~AliMUON2DMapIterator();
+  
+  /** The object returned by this iterator is a TPair(TObject* key,TObject* value)
+    where key is an AliMpIntPair (detElemId,manuId), and value is 
+    an AliMUONVCalibParam.
+    The returned object must be deleted by the user.
+    */
+  virtual TObject* Next();
+  
+  virtual void Reset(); 
+  
+  virtual Bool_t Remove();
+  
+private:
+    // copy ctor will not implemented
+    AliMUON2DMapIterator(const AliMUON2DMapIterator&);
+  // assignement operator will not implemented
+  AliMUON2DMapIterator& operator=(const AliMUON2DMapIterator&);
+  
+    TObject* GetValue(TExMapIter& iter, Int_t& key) const;
+  AliMpExMap* GetMap(TExMapIter& iter, Int_t& key);
+  
+private:
+    TExMapIter fIter; //! first iterator
+  TExMapIter* fIter2; //! second iterator
+  Int_t fCurrentI; //! current index in direction i 
+  Int_t fCurrentJ; //! current index in direction j
+  
+  ClassDef(AliMUON2DMapIterator,0) // VDataIterator for 2D maps
+};
+
+
+#endif