//-----------------------------------------------------------------------------
#include "AliMpExMap.h"
-#include "AliMpIntPair.h"
+#include "AliMpExMapIterator.h"
#include "AliLog.h"
#include <stdlib.h>
+using std::cout;
+using std::endl;
/// \cond CLASSIMP
ClassImp(AliMpExMap)
/// \endcond
const Int_t AliMpExMap::fgkSeparator1 = 10000;
const Int_t AliMpExMap::fgkSeparator2 = 100;
-const TString AliMpExMap::fgkCharacterMap
- = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-";
-
//
// static methods
//
-//______________________________________________________________________________
-Long_t AliMpExMap::GetIndex(const AliMpIntPair& pair)
+//_____________________________________________________________________________
+const TString& AliMpExMap::GetCharacterMap()
{
-/// Convert the pair of integers to integer.
-
- if (pair.GetFirst() >= fgkSeparator1 || pair.GetSecond() >= fgkSeparator1) {
- AliFatalClass("Index out of limit.");
- exit(1);
- }
-
- return pair.GetFirst()*fgkSeparator1 + pair.GetSecond() + 1;
-}
+ /// Return the string mapping characters to integers
+ static const TString kCharacterMap
+ = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-";
+ return kCharacterMap;
+}
//_____________________________________________________________________________
Long_t AliMpExMap::GetIndex(const TString& s)
Long_t index = 0;
for (Int_t i=s.Length()-1; i>=0; --i)
- index = index*fgkSeparator2 + fgkCharacterMap.First(s(i));
+ index = index*fgkSeparator2 + GetCharacterMap().First(s(i));
return index;
}
-//______________________________________________________________________________
-AliMpIntPair AliMpExMap::GetPair(Long_t index)
-{
-/// Convert the integer index to the pair of integers.
-
- return AliMpIntPair((index-1)/fgkSeparator1,(index-1)%fgkSeparator1);
-}
-
//_____________________________________________________________________________
TString AliMpExMap::GetString(Long_t index)
{
TString s;
while (index >0) {
- Char_t c = fgkCharacterMap(index%fgkSeparator2);
+ Char_t c = GetCharacterMap()(index%fgkSeparator2);
s += c;
index = index/fgkSeparator2;
}
//
//_____________________________________________________________________________
-AliMpExMap::AliMpExMap(Bool_t /*standardConstructor*/)
+AliMpExMap::AliMpExMap()
: TObject(),
fMap(fgkDefaultSize),
fObjects(fgkDefaultSize),
fKeys(fgkDefaultSize)
{
-/// Standard constructor
+ /// Default constructor
fObjects.SetOwner(fgkDefaultOwnership);
}
//_____________________________________________________________________________
-AliMpExMap::AliMpExMap()
+AliMpExMap::AliMpExMap(TRootIOCtor*)
: TObject(),
fMap(),
fObjects(),
fKeys()
{
-/// Default constructor
+ /// "Root - I/O" constructor
}
AliMpExMap::operator=(const AliMpExMap& rhs)
{
/// Assignment operator
- AliMpExMap tmp(rhs);
- tmp.Copy(*this);
+
+ // check assignment to self
+ if (this == &rhs) return *this;
+
+ rhs.Copy(*this);
return *this;
}
+//_____________________________________________________________________________
+AliMpExMap::~AliMpExMap()
+{
+/// Destructor
+}
+
+//
+// private static methods
+//
+
+//______________________________________________________________________________
+Long_t AliMpExMap::GetIndex(Int_t first, Int_t second)
+{
+/// Convert the pair of integers to integer.
+
+ if ( first >= 0xFFFF || second >= 0xFFFF )
+ {
+ AliFatalClass("Index out of limit");
+ return 0;
+ }
+
+ return 1 + ( first | ( second << 16 ) );
+
+// if (pair.GetFirst() >= fgkSeparator1 || pair.GetSecond() >= fgkSeparator1) {
+// AliFatalClass("Index out of limit.");
+// exit(1);
+// }
+//
+// return pair.GetFirst()*fgkSeparator1 + pair.GetSecond() + 1;
+}
+
+//______________________________________________________________________________
+Int_t AliMpExMap::GetPairFirst(Long_t index)
+{
+/// Return first integer from index (encoded pair)
+
+ return (index-1) & 0xFFFF ;
+}
+
+//______________________________________________________________________________
+Int_t AliMpExMap::GetPairSecond(Long_t index)
+{
+/// Return second integer from index (encoded pair)
+
+ return ( (index-1) & 0xFFFF0000 ) >> 16 ;
+}
+
+//
+// private methods
+//
+
+//_____________________________________________________________________________
+void AliMpExMap::FillMap()
+{
+/// Fill transient map from the arrays of objects and keys
+
+ for (Int_t i=0; i<fObjects.GetEntriesFast(); i++)
+ fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i));
+}
+
+//_____________________________________________________________________________
+void AliMpExMap::AddKey(Long_t key)
+{
+/// Add key in array with checking size
+
+ // Resize array if needed
+ if (fObjects.GetEntriesFast() == fKeys.GetSize()) {
+ fKeys.Set(2*fKeys.GetSize());
+ AliDebugStream(1) << "AliMpExMap::AddKey: resized Key array " << endl;
+ }
+
+ fKeys.AddAt(key, fObjects.GetEntriesFast());
+}
+
//_____________________________________________________________________________
void
AliMpExMap::Copy(TObject& dest) const
AliMpExMap& m = static_cast<AliMpExMap&>(dest);
m.fKeys = fKeys;
m.fMap.Delete();
- m.fObjects.Delete();
+ m.fObjects.Clear();
for ( Int_t i = 0; i <= fObjects.GetLast(); ++i )
{
m.fObjects.SetOwner(kTRUE);
}
-//_____________________________________________________________________________
-AliMpExMap::~AliMpExMap()
-{
-/// Destructor
-}
-
//
-// private methods
+// public methods
//
//_____________________________________________________________________________
-void AliMpExMap::FillMap()
+void AliMpExMap::Clear(Option_t* option)
{
-/// Fill transient map from the arrays of objects and keys
+/// Clear memory
- for (Int_t i=0; i<fObjects.GetEntriesFast(); i++)
- fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i));
+ fMap.Delete();
+ fObjects.Clear(option);
+ fKeys.Reset();
}
//_____________________________________________________________________________
-void AliMpExMap::AddKey(Long_t key)
+void AliMpExMap::Print(Option_t* opt) const
{
-/// Add key in array with checking size
+/// Print out
- // Resize array if needed
- if (fObjects.GetEntriesFast() == fKeys.GetSize()) {
- fKeys.Set(2*fKeys.GetSize());
- AliWarningStream() << "AliMpExMap::AddKey: resized Key array " << endl;
- }
-
- fKeys.AddAt(key, fObjects.GetEntriesFast());
+ cout << Form("fMap size/capacity %d/%d",fMap.GetSize(),fMap.Capacity())
+ << Form(" fObjects.GetSize/Entries %d/%d",fObjects.GetSize(),fObjects.GetEntries())
+ << Form(" fKeys.GetSize %d",fKeys.GetSize()) << endl;
+
+ TString sopt(opt);
+ sopt.ToUpper();
+
+ if ( sopt.Contains("FULL") )
+ {
+ TIter next(CreateIterator());
+ TObject* o;
+ while ( ( o = next() ) )
+ {
+ o->Print();
+ }
+ }
}
-//
-// public methods
-//
-
//_____________________________________________________________________________
-void AliMpExMap::Add(const AliMpIntPair& key, TObject* object)
+void AliMpExMap::Add(Int_t keyFirst, Int_t keySecond, TObject* object)
{
/// Add object with its key to the map and arrays
- fMap.Add(GetIndex(key), (Long_t)object);
- AddKey(GetIndex(key));
+ fMap.Add(GetIndex(keyFirst, keySecond), (Long_t)object);
+ AddKey(GetIndex(keyFirst, keySecond));
fObjects.Add(object);
}
}
//_____________________________________________________________________________
-TExMapIter AliMpExMap::GetIterator() const
+Int_t AliMpExMap::GetCapacity() const
{
-/// Return TExMap iterator set to the beginning of the map
-
- return TExMapIter(&fMap);
+ /// Return the map capacity
+
+ return fObjects.GetSize();
}
//_____________________________________________________________________________
-TObject* AliMpExMap::GetObject(Int_t index) const
+AliMpExMapIterator*
+AliMpExMap::CreateIterator() const
{
-/// Return the object via its index in the array
-/// (This function makes possible looping over map as over an array)
-
- if ( index < 0 || index >= fObjects.GetEntriesFast() ) {
- AliErrorStream() << "Index outside limits" << endl;
- return 0;
- }
-
- return fObjects.UncheckedAt(index);
-}
+/// Return iterator set to the beginning of the map
-//_____________________________________________________________________________
-TObject* AliMpExMap::GetObjectFast(Int_t index) const
-{
- /// Return the object via its index in the array
- /// (This function makes possible looping over map as over an array)
- /// without bound checking.
-
- return fObjects.UncheckedAt(index);
-}
+ return new AliMpExMapIterator(*this);
+}
//_____________________________________________________________________________
-TObject* AliMpExMap::GetValue(const AliMpIntPair& key) const
+TObject* AliMpExMap::GetValue(Int_t keyFirst, Int_t keySecond) const
{
/// Return the object associated with the given key if found,
/// otherwise return 0
- return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
+ return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(keyFirst, keySecond)));
}
//_____________________________________________________________________________