ROOT logo
/**************************************************************************
 * 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$
// $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 "AliLog.h"

#include <TClass.h>
#include <TString.h>
#include <Riostream.h>

#include <stdlib.h>
#include <limits.h>

using std::endl;
/// \cond CLASSIMP
ClassImp(AliMpArrayI)
/// \endcond

const Int_t AliMpArrayI::fgkDefaultSize = 100;

//_____________________________________________________________________________
AliMpArrayI::AliMpArrayI(Bool_t sort) 
  : TObject(),
    fSort(sort),
    fNofValues(0),
    fValues(fgkDefaultSize),
    fMinValue(INT_MAX),
    fMaxValue(INT_MIN)
{
/// Standard & default constructor

}

//_____________________________________________________________________________
AliMpArrayI::AliMpArrayI(TRootIOCtor* /*ioCtor*/) 
  : TObject(),
    fSort(),
    fNofValues(),
    fValues(),
    fMinValue(),
    fMaxValue()
{
/// IO constructor
}

//_____________________________________________________________________________
AliMpArrayI::~AliMpArrayI() 
{
/// Destructor 
}

//
// private methods
//

//_____________________________________________________________________________
Int_t  AliMpArrayI::GetPosition(Int_t value) const
{
/// Return the new positon where the value should be put

  for ( Int_t i=0; i<fNofValues; i++ ) {
    if ( fValues.At(i) > value ) return i;
  }  

  return fNofValues;
}  

//
// public methods
//

//_____________________________________________________________________________
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());
    if ( warn ) 
    {
      AliWarningStream() << "Resized array." << endl;
    }
  }
  
  // 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 < fMinValue )  fMinValue = value;
  if ( value > fMaxValue )  fMaxValue = value;;
  
  return true;
}

//_____________________________________________________________________________
Bool_t AliMpArrayI::Remove(Int_t value)
{
/// Remove value from the array
  
  // Find the position for the new value  
  Int_t pos = GetPosition(value); 
   
  // Return if value is not present
  if ( pos == fNofValues ) return false;

  // Move elements 
  for ( Int_t i=pos; i<fNofValues-1; i++ )
    fValues.AddAt(fValues.At(i+1), i);
    
  // Decrement number of values
  --fNofValues;
  
  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;
  fMinValue = INT_MAX;
  fMaxValue = INT_MIN;
} 

//_____________________________________________________________________________
void AliMpArrayI::SetSize(Int_t size)
{
/// Set given size to the array

  fValues.Set(size);
} 

//_____________________________________________________________________________
Int_t AliMpArrayI::GetSize() const
{
/// Return the map size

  return fNofValues;
}

//_____________________________________________________________________________
Int_t AliMpArrayI::GetValue(Int_t index) const
{
/// Return the index-th value 

  if ( index < 0 || index >= fNofValues ) {
    AliErrorStream() << "Index outside limits" << endl;
    return 0;
  }
  
  return fValues.At(index);
}

//_____________________________________________________________________________
Bool_t AliMpArrayI::HasValue(Int_t value) const
{
/// Return true if contains the given value

  if ( ! fNofValues ) return false;

  if ( value < fMinValue || value > fMaxValue ) 
    return false;

  for ( Int_t i=0; i<fNofValues; i++ )
    if ( fValues.At(i) == value ) return true;
    
  return false;  
}

 AliMpArrayI.cxx:1
 AliMpArrayI.cxx:2
 AliMpArrayI.cxx:3
 AliMpArrayI.cxx:4
 AliMpArrayI.cxx:5
 AliMpArrayI.cxx:6
 AliMpArrayI.cxx:7
 AliMpArrayI.cxx:8
 AliMpArrayI.cxx:9
 AliMpArrayI.cxx:10
 AliMpArrayI.cxx:11
 AliMpArrayI.cxx:12
 AliMpArrayI.cxx:13
 AliMpArrayI.cxx:14
 AliMpArrayI.cxx:15
 AliMpArrayI.cxx:16
 AliMpArrayI.cxx:17
 AliMpArrayI.cxx:18
 AliMpArrayI.cxx:19
 AliMpArrayI.cxx:20
 AliMpArrayI.cxx:21
 AliMpArrayI.cxx:22
 AliMpArrayI.cxx:23
 AliMpArrayI.cxx:24
 AliMpArrayI.cxx:25
 AliMpArrayI.cxx:26
 AliMpArrayI.cxx:27
 AliMpArrayI.cxx:28
 AliMpArrayI.cxx:29
 AliMpArrayI.cxx:30
 AliMpArrayI.cxx:31
 AliMpArrayI.cxx:32
 AliMpArrayI.cxx:33
 AliMpArrayI.cxx:34
 AliMpArrayI.cxx:35
 AliMpArrayI.cxx:36
 AliMpArrayI.cxx:37
 AliMpArrayI.cxx:38
 AliMpArrayI.cxx:39
 AliMpArrayI.cxx:40
 AliMpArrayI.cxx:41
 AliMpArrayI.cxx:42
 AliMpArrayI.cxx:43
 AliMpArrayI.cxx:44
 AliMpArrayI.cxx:45
 AliMpArrayI.cxx:46
 AliMpArrayI.cxx:47
 AliMpArrayI.cxx:48
 AliMpArrayI.cxx:49
 AliMpArrayI.cxx:50
 AliMpArrayI.cxx:51
 AliMpArrayI.cxx:52
 AliMpArrayI.cxx:53
 AliMpArrayI.cxx:54
 AliMpArrayI.cxx:55
 AliMpArrayI.cxx:56
 AliMpArrayI.cxx:57
 AliMpArrayI.cxx:58
 AliMpArrayI.cxx:59
 AliMpArrayI.cxx:60
 AliMpArrayI.cxx:61
 AliMpArrayI.cxx:62
 AliMpArrayI.cxx:63
 AliMpArrayI.cxx:64
 AliMpArrayI.cxx:65
 AliMpArrayI.cxx:66
 AliMpArrayI.cxx:67
 AliMpArrayI.cxx:68
 AliMpArrayI.cxx:69
 AliMpArrayI.cxx:70
 AliMpArrayI.cxx:71
 AliMpArrayI.cxx:72
 AliMpArrayI.cxx:73
 AliMpArrayI.cxx:74
 AliMpArrayI.cxx:75
 AliMpArrayI.cxx:76
 AliMpArrayI.cxx:77
 AliMpArrayI.cxx:78
 AliMpArrayI.cxx:79
 AliMpArrayI.cxx:80
 AliMpArrayI.cxx:81
 AliMpArrayI.cxx:82
 AliMpArrayI.cxx:83
 AliMpArrayI.cxx:84
 AliMpArrayI.cxx:85
 AliMpArrayI.cxx:86
 AliMpArrayI.cxx:87
 AliMpArrayI.cxx:88
 AliMpArrayI.cxx:89
 AliMpArrayI.cxx:90
 AliMpArrayI.cxx:91
 AliMpArrayI.cxx:92
 AliMpArrayI.cxx:93
 AliMpArrayI.cxx:94
 AliMpArrayI.cxx:95
 AliMpArrayI.cxx:96
 AliMpArrayI.cxx:97
 AliMpArrayI.cxx:98
 AliMpArrayI.cxx:99
 AliMpArrayI.cxx:100
 AliMpArrayI.cxx:101
 AliMpArrayI.cxx:102
 AliMpArrayI.cxx:103
 AliMpArrayI.cxx:104
 AliMpArrayI.cxx:105
 AliMpArrayI.cxx:106
 AliMpArrayI.cxx:107
 AliMpArrayI.cxx:108
 AliMpArrayI.cxx:109
 AliMpArrayI.cxx:110
 AliMpArrayI.cxx:111
 AliMpArrayI.cxx:112
 AliMpArrayI.cxx:113
 AliMpArrayI.cxx:114
 AliMpArrayI.cxx:115
 AliMpArrayI.cxx:116
 AliMpArrayI.cxx:117
 AliMpArrayI.cxx:118
 AliMpArrayI.cxx:119
 AliMpArrayI.cxx:120
 AliMpArrayI.cxx:121
 AliMpArrayI.cxx:122
 AliMpArrayI.cxx:123
 AliMpArrayI.cxx:124
 AliMpArrayI.cxx:125
 AliMpArrayI.cxx:126
 AliMpArrayI.cxx:127
 AliMpArrayI.cxx:128
 AliMpArrayI.cxx:129
 AliMpArrayI.cxx:130
 AliMpArrayI.cxx:131
 AliMpArrayI.cxx:132
 AliMpArrayI.cxx:133
 AliMpArrayI.cxx:134
 AliMpArrayI.cxx:135
 AliMpArrayI.cxx:136
 AliMpArrayI.cxx:137
 AliMpArrayI.cxx:138
 AliMpArrayI.cxx:139
 AliMpArrayI.cxx:140
 AliMpArrayI.cxx:141
 AliMpArrayI.cxx:142
 AliMpArrayI.cxx:143
 AliMpArrayI.cxx:144
 AliMpArrayI.cxx:145
 AliMpArrayI.cxx:146
 AliMpArrayI.cxx:147
 AliMpArrayI.cxx:148
 AliMpArrayI.cxx:149
 AliMpArrayI.cxx:150
 AliMpArrayI.cxx:151
 AliMpArrayI.cxx:152
 AliMpArrayI.cxx:153
 AliMpArrayI.cxx:154
 AliMpArrayI.cxx:155
 AliMpArrayI.cxx:156
 AliMpArrayI.cxx:157
 AliMpArrayI.cxx:158
 AliMpArrayI.cxx:159
 AliMpArrayI.cxx:160
 AliMpArrayI.cxx:161
 AliMpArrayI.cxx:162
 AliMpArrayI.cxx:163
 AliMpArrayI.cxx:164
 AliMpArrayI.cxx:165
 AliMpArrayI.cxx:166
 AliMpArrayI.cxx:167
 AliMpArrayI.cxx:168
 AliMpArrayI.cxx:169
 AliMpArrayI.cxx:170
 AliMpArrayI.cxx:171
 AliMpArrayI.cxx:172
 AliMpArrayI.cxx:173
 AliMpArrayI.cxx:174
 AliMpArrayI.cxx:175
 AliMpArrayI.cxx:176
 AliMpArrayI.cxx:177
 AliMpArrayI.cxx:178
 AliMpArrayI.cxx:179
 AliMpArrayI.cxx:180
 AliMpArrayI.cxx:181
 AliMpArrayI.cxx:182
 AliMpArrayI.cxx:183
 AliMpArrayI.cxx:184
 AliMpArrayI.cxx:185
 AliMpArrayI.cxx:186
 AliMpArrayI.cxx:187
 AliMpArrayI.cxx:188
 AliMpArrayI.cxx:189
 AliMpArrayI.cxx:190
 AliMpArrayI.cxx:191
 AliMpArrayI.cxx:192
 AliMpArrayI.cxx:193
 AliMpArrayI.cxx:194
 AliMpArrayI.cxx:195
 AliMpArrayI.cxx:196
 AliMpArrayI.cxx:197
 AliMpArrayI.cxx:198
 AliMpArrayI.cxx:199
 AliMpArrayI.cxx:200
 AliMpArrayI.cxx:201
 AliMpArrayI.cxx:202
 AliMpArrayI.cxx:203
 AliMpArrayI.cxx:204
 AliMpArrayI.cxx:205
 AliMpArrayI.cxx:206
 AliMpArrayI.cxx:207
 AliMpArrayI.cxx:208
 AliMpArrayI.cxx:209
 AliMpArrayI.cxx:210
 AliMpArrayI.cxx:211
 AliMpArrayI.cxx:212
 AliMpArrayI.cxx:213
 AliMpArrayI.cxx:214
 AliMpArrayI.cxx:215
 AliMpArrayI.cxx:216
 AliMpArrayI.cxx:217
 AliMpArrayI.cxx:218
 AliMpArrayI.cxx:219
 AliMpArrayI.cxx:220
 AliMpArrayI.cxx:221
 AliMpArrayI.cxx:222
 AliMpArrayI.cxx:223
 AliMpArrayI.cxx:224
 AliMpArrayI.cxx:225
 AliMpArrayI.cxx:226
 AliMpArrayI.cxx:227
 AliMpArrayI.cxx:228
 AliMpArrayI.cxx:229
 AliMpArrayI.cxx:230
 AliMpArrayI.cxx:231
 AliMpArrayI.cxx:232
 AliMpArrayI.cxx:233
 AliMpArrayI.cxx:234