// $Id$
// $MpId: AliMpArrayI.cxx,v 1.5 2006/05/24 13:58:29 ivana Exp $
// Category: basic
-// ------------------------
+
+//-----------------------------------------------------------------------------
// Class AliMpArrayI
// ------------------------
// Helper class for sorted integer array
// Author:Ivana Hrivnacova; IPN Orsay
+//-----------------------------------------------------------------------------
#include "AliMpArrayI.h"
-#include "AliMpIntPair.h"
#include "AliLog.h"
#include <Riostream.h>
#include <stdlib.h>
+#include <limits.h>
/// \cond CLASSIMP
ClassImp(AliMpArrayI)
const Int_t AliMpArrayI::fgkDefaultSize = 100;
//_____________________________________________________________________________
-AliMpArrayI::AliMpArrayI()
+AliMpArrayI::AliMpArrayI(Bool_t sort)
: TObject(),
+ fSort(sort),
fNofValues(0),
- fValues(fgkDefaultSize)
+ fValues(fgkDefaultSize),
+ fLimits(INT_MIN,INT_MAX)
{
/// Standard & default constructor
//_____________________________________________________________________________
AliMpArrayI::AliMpArrayI(TRootIOCtor* /*ioCtor*/)
: TObject(),
+ fSort(),
fNofValues(),
- fValues()
+ fValues(),
+ fLimits()
{
/// IO constructor
}
//
//_____________________________________________________________________________
-Bool_t AliMpArrayI::Add(Int_t value)
+Bool_t AliMpArrayI::Add(Int_t value, Bool_t warn)
{
/// Add object with its key to the map and arrays
// Resize array if needed
- if ( fValues.GetSize() == fNofValues ) {
- fValues.Set(2*fValues.GetSize());
- AliWarningStream() << "Resized array." << endl;
+ if ( fValues.GetSize() == fNofValues )
+ {
+ fValues.Set(2*fValues.GetSize());
+ if ( warn )
+ {
+ AliWarningStream() << "Resized array." << endl;
+ }
}
- // Find the position for the new value
- Int_t pos = GetPosition(value);
-
- // Move elements
- for ( Int_t i=fNofValues; i>=pos; i-- )
- fValues.AddAt(fValues.At(i), i+1);
-
+ // The position for the new value
+ Int_t pos;
+ if ( fSort ) {
+ pos = GetPosition(value);
+
+ // Move elements
+ for ( Int_t i=fNofValues; i>=pos; i-- )
+ fValues.AddAt(fValues.At(i), i+1);
+ }
+ else
+ pos = fNofValues;
+
// Add the new value in freed space
fValues.AddAt(value, pos);
++fNofValues;
+ // Update linits
+ if ( value < fLimits.GetFirst() ) fLimits.SetFirst(value);
+ if ( value > fLimits.GetSecond() ) fLimits.SetSecond(value);
+
return true;
}
//_____________________________________________________________________________
Bool_t AliMpArrayI::Remove(Int_t value)
{
-/// Add object with its key to the map and arrays
+/// Remove value from the array
// Find the position for the new value
Int_t pos = GetPosition(value);
return true;
}
+//_____________________________________________________________________________
+Bool_t AliMpArrayI::Revert()
+{
+/// Revert the order of elements
+
+ if ( fSort ) {
+ AliErrorStream() << "Cannot revert sorted array." << endl;
+ return false;
+ }
+
+ Int_t size = GetSize();
+ TArrayI newArray(size);
+ Int_t idx = 0 ;
+ for ( Int_t i = size-1 ; i >= 0 ; i--) {
+ Int_t value = GetValue(i);
+ newArray.AddAt(value,idx++);
+ }
+
+ for (Int_t i = 0; i < size ; i++) {
+ fValues[i]=newArray.At(i);
+ }
+ return true;
+}
+
+//_____________________________________________________________________________
+void AliMpArrayI::Reset()
+{
+/// Reset the array
+
+ fValues.Set(fgkDefaultSize);
+ fNofValues = 0;
+ fLimits = AliMpIntPair(INT_MIN,INT_MAX);
+}
+
//_____________________________________________________________________________
void AliMpArrayI::SetSize(Int_t size)
{
if ( ! fNofValues ) return false;
- if ( fValues.At(0) > value || fValues.At(fNofValues-1) < value )
+ if ( value < fLimits.GetFirst() || value > fLimits.GetSecond() )
return false;
for ( Int_t i=0; i<fNofValues; i++ )