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 | MArrayI &operator=(const MArrayI &rhs)
|
---|
46 | {
|
---|
47 | // TArrayC assignment operator.
|
---|
48 | if (this != &rhs)
|
---|
49 | Set(rhs.fN, rhs.fArray);
|
---|
50 | return *this;
|
---|
51 | }
|
---|
52 |
|
---|
53 | virtual ~MArrayI()
|
---|
54 | {
|
---|
55 | // Delete TArrayC object.
|
---|
56 | delete [] fArray;
|
---|
57 | fArray = NULL;
|
---|
58 | }
|
---|
59 |
|
---|
60 | void Adopt(UInt_t n, Int_t *array)
|
---|
61 | {
|
---|
62 | // Adopt array arr into TArrayC, i.e. don't copy arr but use it directly
|
---|
63 | // in TArrayC. User may not delete arr, TArrayC dtor will do it.
|
---|
64 | if (fArray)
|
---|
65 | delete [] fArray;
|
---|
66 |
|
---|
67 | fN = n;
|
---|
68 | fArray = array;
|
---|
69 | }
|
---|
70 |
|
---|
71 | void AddAt(Int_t c, UInt_t i)
|
---|
72 | {
|
---|
73 | // Add char c at position i. Check for out of bounds.
|
---|
74 | fArray[i] = c;
|
---|
75 | }
|
---|
76 |
|
---|
77 | Int_t At(UInt_t i) const
|
---|
78 | {
|
---|
79 | return fArray[i];
|
---|
80 | }
|
---|
81 |
|
---|
82 | Int_t *GetArray() const
|
---|
83 | {
|
---|
84 | return fArray;
|
---|
85 | }
|
---|
86 |
|
---|
87 | void Reset()
|
---|
88 | {
|
---|
89 | memset(fArray, 0, fN*sizeof(Int_t));
|
---|
90 | }
|
---|
91 |
|
---|
92 | void Reset(Int_t v)
|
---|
93 | {
|
---|
94 | for (Int_t *i=fArray; i<fArray+fN; i++)
|
---|
95 | *i = v;
|
---|
96 | }
|
---|
97 |
|
---|
98 | void Set(UInt_t n)
|
---|
99 | {
|
---|
100 | // Set size of this array to n chars.
|
---|
101 | // A new array is created, the old contents copied to the new array,
|
---|
102 | // then the old array is deleted.
|
---|
103 |
|
---|
104 | if (n==fN)
|
---|
105 | return;
|
---|
106 |
|
---|
107 | Int_t *temp = fArray;
|
---|
108 | if (n == 0)
|
---|
109 | fArray = NULL;
|
---|
110 | else
|
---|
111 | {
|
---|
112 | fArray = new Int_t[n];
|
---|
113 | if (n < fN)
|
---|
114 | memcpy(fArray, temp, n*sizeof(Int_t));
|
---|
115 | else
|
---|
116 | {
|
---|
117 | memcpy(fArray, temp, fN*sizeof(Int_t));
|
---|
118 | memset(&fArray[fN], 0, (n-fN)*sizeof(Int_t));
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (fN)
|
---|
123 | delete [] temp;
|
---|
124 |
|
---|
125 | fN = n;
|
---|
126 | }
|
---|
127 |
|
---|
128 | void Set(UInt_t n, Int_t *array)
|
---|
129 | {
|
---|
130 | // Set size of this array to n chars and set the contents.
|
---|
131 | if (!array)
|
---|
132 | return;
|
---|
133 |
|
---|
134 | if (fArray && fN != n)
|
---|
135 | {
|
---|
136 | delete [] fArray;
|
---|
137 | fArray = 0;
|
---|
138 | }
|
---|
139 | fN = n;
|
---|
140 |
|
---|
141 | if (fN == 0)
|
---|
142 | return;
|
---|
143 |
|
---|
144 | if (!fArray)
|
---|
145 | fArray = new Int_t[fN];
|
---|
146 |
|
---|
147 | memcpy(fArray, array, n*sizeof(Int_t));
|
---|
148 | }
|
---|
149 |
|
---|
150 | Int_t &operator[](UInt_t i)
|
---|
151 | {
|
---|
152 | return fArray[i];
|
---|
153 | }
|
---|
154 | const Int_t &operator[](UInt_t i) const
|
---|
155 | {
|
---|
156 | return fArray[i];
|
---|
157 | }
|
---|
158 |
|
---|
159 | ClassDef(MArrayI, 1) //Array of Int_t
|
---|
160 | };
|
---|
161 |
|
---|
162 | #endif
|
---|