| 1 | #ifndef MARS_MArrayI | 
|---|
| 2 | #define MARS_MArrayI | 
|---|
| 3 |  | 
|---|
| 4 | #ifndef MARS_MArray | 
|---|
| 5 | #include "MArray.h" | 
|---|
| 6 | #endif | 
|---|
| 7 |  | 
|---|
| 8 | #include <string.h> | 
|---|
| 9 |  | 
|---|
| 10 | class MArrayI : public MArray | 
|---|
| 11 | { | 
|---|
| 12 | private: | 
|---|
| 13 | Int_t *fArray; //[fN] Array of fN chars | 
|---|
| 14 |  | 
|---|
| 15 | public: | 
|---|
| 16 |  | 
|---|
| 17 | MArrayI() | 
|---|
| 18 | { | 
|---|
| 19 | fN     = 0; | 
|---|
| 20 | fArray = NULL; | 
|---|
| 21 | } | 
|---|
| 22 |  | 
|---|
| 23 | MArrayI(UInt_t n) | 
|---|
| 24 | { | 
|---|
| 25 | fN     = 0; | 
|---|
| 26 | fArray = NULL; | 
|---|
| 27 | Set(n); | 
|---|
| 28 | } | 
|---|
| 29 |  | 
|---|
| 30 | MArrayI(UInt_t n, Int_t *array) | 
|---|
| 31 | { | 
|---|
| 32 | // Create TArrayC object and initialize it with values of array. | 
|---|
| 33 | fN     = 0; | 
|---|
| 34 | fArray = NULL; | 
|---|
| 35 | Set(n, array); | 
|---|
| 36 | } | 
|---|
| 37 |  | 
|---|
| 38 | MArrayI(const MArrayI &array) : MArray() | 
|---|
| 39 | { | 
|---|
| 40 | // Copy constructor. | 
|---|
| 41 | fArray = NULL; | 
|---|
| 42 | Set(array.fN, array.fArray); | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | UInt_t GetSize() const | 
|---|
| 46 | { | 
|---|
| 47 | return fN; | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | MArrayI &operator=(const MArrayI &rhs) | 
|---|
| 51 | { | 
|---|
| 52 | // TArrayC assignment operator. | 
|---|
| 53 | if (this != &rhs) | 
|---|
| 54 | Set(rhs.fN, rhs.fArray); | 
|---|
| 55 | return *this; | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | virtual ~MArrayI() | 
|---|
| 59 | { | 
|---|
| 60 | // Delete TArrayC object. | 
|---|
| 61 | delete [] fArray; | 
|---|
| 62 | fArray = NULL; | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | void Adopt(UInt_t n, Int_t *array) | 
|---|
| 66 | { | 
|---|
| 67 | // Adopt array arr into TArrayC, i.e. don't copy arr but use it directly | 
|---|
| 68 | // in TArrayC. User may not delete arr, TArrayC dtor will do it. | 
|---|
| 69 | if (fArray) | 
|---|
| 70 | delete [] fArray; | 
|---|
| 71 |  | 
|---|
| 72 | fN     = n; | 
|---|
| 73 | fArray = array; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | void AddAt(Int_t c, UInt_t i) | 
|---|
| 77 | { | 
|---|
| 78 | // Add char c at position i. Check for out of bounds. | 
|---|
| 79 | fArray[i] = c; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | Int_t     At(UInt_t i) const | 
|---|
| 83 | { | 
|---|
| 84 | return fArray[i]; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | Int_t    *GetArray() const | 
|---|
| 88 | { | 
|---|
| 89 | return fArray; | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | void Reset() | 
|---|
| 93 | { | 
|---|
| 94 | memset(fArray, 0, fN*sizeof(Int_t)); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | void Set(UInt_t n) | 
|---|
| 98 | { | 
|---|
| 99 | // Set size of this array to n chars. | 
|---|
| 100 | // A new array is created, the old contents copied to the new array, | 
|---|
| 101 | // then the old array is deleted. | 
|---|
| 102 |  | 
|---|
| 103 | if (n==fN) | 
|---|
| 104 | return; | 
|---|
| 105 |  | 
|---|
| 106 | Int_t *temp = fArray; | 
|---|
| 107 | if (n == 0) | 
|---|
| 108 | fArray = NULL; | 
|---|
| 109 | else | 
|---|
| 110 | { | 
|---|
| 111 | fArray = new Int_t[n]; | 
|---|
| 112 | if (n < fN) | 
|---|
| 113 | memcpy(fArray, temp, n*sizeof(Int_t)); | 
|---|
| 114 | else | 
|---|
| 115 | { | 
|---|
| 116 | memcpy(fArray, temp, fN*sizeof(Int_t)); | 
|---|
| 117 | memset(&fArray[fN], 0, (n-fN)*sizeof(Int_t)); | 
|---|
| 118 | } | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | if (fN) | 
|---|
| 122 | delete [] temp; | 
|---|
| 123 |  | 
|---|
| 124 | fN = n; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | void Set(UInt_t n, Int_t *array) | 
|---|
| 128 | { | 
|---|
| 129 | // Set size of this array to n chars and set the contents. | 
|---|
| 130 | if (!array) | 
|---|
| 131 | return; | 
|---|
| 132 |  | 
|---|
| 133 | if (fArray && fN != n) | 
|---|
| 134 | { | 
|---|
| 135 | delete [] fArray; | 
|---|
| 136 | fArray = 0; | 
|---|
| 137 | } | 
|---|
| 138 | fN = n; | 
|---|
| 139 |  | 
|---|
| 140 | if (fN == 0) | 
|---|
| 141 | return; | 
|---|
| 142 |  | 
|---|
| 143 | if (!fArray) | 
|---|
| 144 | fArray = new Int_t[fN]; | 
|---|
| 145 |  | 
|---|
| 146 | memcpy(fArray, array, n*sizeof(Int_t)); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | Int_t &operator[](UInt_t i) | 
|---|
| 150 | { | 
|---|
| 151 | return fArray[i]; | 
|---|
| 152 | } | 
|---|
| 153 | const Int_t &operator[](UInt_t i) const | 
|---|
| 154 | { | 
|---|
| 155 | return fArray[i]; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | ClassDef(MArrayI, 1)  //Array of Int_t | 
|---|
| 159 | }; | 
|---|
| 160 |  | 
|---|
| 161 | #endif | 
|---|