Logo ROOT   6.14/05
Reference Guide
TArrayI.cxx
Go to the documentation of this file.
1 // @(#)root/cont:$Id$
2 // Author: Rene Brun 06/03/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class TArrayI
13 \ingroup Containers
14 Array of integers (32 bits per element).
15 */
16 
17 #include "TArrayI.h"
18 #include "TBuffer.h"
19 
20 
22 
23 ////////////////////////////////////////////////////////////////////////////////
24 /// Default TArrayI ctor.
25 
27 {
28  fArray = 0;
29 }
30 
31 ////////////////////////////////////////////////////////////////////////////////
32 /// Create TArrayI object and set array size to n integers.
33 
35 {
36  fArray = 0;
37  if (n > 0) Set(n);
38 }
39 
40 ////////////////////////////////////////////////////////////////////////////////
41 /// Create TArrayI object and initialize it with values of array.
42 
43 TArrayI::TArrayI(Int_t n, const Int_t *array)
44 {
45  fArray = 0;
46  Set(n, array);
47 }
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Copy constructor.
51 
52 TArrayI::TArrayI(const TArrayI &array) : TArray(array)
53 {
54  fArray = 0;
55  Set(array.fN, array.fArray);
56 }
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// TArrayI assignment operator.
60 
62 {
63  if (this != &rhs)
64  Set(rhs.fN, rhs.fArray);
65  return *this;
66 }
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 /// Delete TArrayI object.
70 
72 {
73  delete [] fArray;
74  fArray = 0;
75 }
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// Adopt array arr into TArrayI, i.e. don't copy arr but use it directly
79 /// in TArrayI. User may not delete arr, TArrayI dtor will do it.
80 
82 {
83  if (fArray)
84  delete [] fArray;
85 
86  fN = n;
87  fArray = arr;
88 }
89 
90 ////////////////////////////////////////////////////////////////////////////////
91 /// Add Int_t c at position i. Check for out of bounds.
92 
94 {
95  if (!BoundsOk("TArrayI::AddAt", i)) return;
96  fArray[i] = c;
97 }
98 
99 ////////////////////////////////////////////////////////////////////////////////
100 /// Set size of this array to n ints.
101 /// A new array is created, the old contents copied to the new array,
102 /// then the old array is deleted.
103 /// This function should not be called if the array was declared via Adopt.
104 
106 {
107  if (n < 0) return;
108  if (n != fN) {
109  Int_t *temp = fArray;
110  if (n != 0) {
111  fArray = new Int_t[n];
112  if (n < fN) memcpy(fArray,temp, n*sizeof(Int_t));
113  else {
114  memcpy(fArray,temp,fN*sizeof(Int_t));
115  memset(&fArray[fN],0,(n-fN)*sizeof(Int_t));
116  }
117  } else {
118  fArray = 0;
119  }
120  if (fN) delete [] temp;
121  fN = n;
122  }
123 }
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 /// Set size of this array to n ints and set the contents.
127 /// This function should not be called if the array was declared via Adopt.
128 
129 void TArrayI::Set(Int_t n, const Int_t *array)
130 {
131  if (fArray && fN != n) {
132  delete [] fArray;
133  fArray = 0;
134  }
135  fN = n;
136  if (fN == 0) return;
137  if (array == 0) return;
138  if (!fArray) fArray = new Int_t[fN];
139  memmove(fArray, array, n*sizeof(Int_t));
140 }
141 
142 ////////////////////////////////////////////////////////////////////////////////
143 /// Stream a TArrayI object.
144 
145 void TArrayI::Streamer(TBuffer &b)
146 {
147  if (b.IsReading()) {
148  Int_t n;
149  b >> n;
150  Set(n);
151  b.ReadFastArray(fArray,n);
152  } else {
153  b << fN;
154  b.WriteFastArray(fArray, fN);
155  }
156 }
157 
Abstract array base class.
Definition: TArray.h:31
Bool_t IsReading() const
Definition: TBuffer.h:83
void Adopt(Int_t n, Int_t *array)
Adopt array arr into TArrayI, i.e.
Definition: TArrayI.cxx:81
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
TArrayI()
Default TArrayI ctor.
Definition: TArrayI.cxx:26
int Int_t
Definition: RtypesCore.h:41
Array of integers (32 bits per element).
Definition: TArrayI.h:27
Bool_t BoundsOk(const char *where, Int_t at) const
Definition: TArray.h:77
void Set(Int_t n)
Set size of this array to n ints.
Definition: TArrayI.cxx:105
void AddAt(Int_t c, Int_t i)
Add Int_t c at position i. Check for out of bounds.
Definition: TArrayI.cxx:93
Int_t fN
Definition: TArray.h:38
virtual ~TArrayI()
Delete TArrayI object.
Definition: TArrayI.cxx:71
virtual void ReadFastArray(Bool_t *b, Int_t n)=0
virtual void WriteFastArray(const Bool_t *b, Int_t n)=0
#define ClassImp(name)
Definition: Rtypes.h:359
Int_t * fArray
Definition: TArrayI.h:30
TArrayI & operator=(const TArrayI &rhs)
TArrayI assignment operator.
Definition: TArrayI.cxx:61
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
#define c(i)
Definition: RSha256.hxx:101
const Int_t n
Definition: legend1.C:16