source: trunk/Mars/mbase/MArrayI.h@ 12629

Last change on this file since 12629 was 9234, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 3.4 KB
Line 
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
10class MArrayI : public MArray
11{
12private:
13 Int_t *fArray; //[fN] Array of fN chars
14
15public:
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 void Add(Int_t c)
78 {
79 Set(fN+1);
80 fArray[fN-1] = c;
81 }
82
83 Int_t Find(Int_t c) const
84 {
85 for (Int_t *ptr=fArray; ptr<fArray+fN; ptr++)
86 if (*ptr==c)
87 return ptr-fArray;
88
89 return -1;
90 }
91
92 void AddUniq(Int_t c)
93 {
94 if (Find(c)<0)
95 Add(c);
96 }
97
98 Int_t At(UInt_t i) const
99 {
100 return fArray[i];
101 }
102
103 Int_t *GetArray() const
104 {
105 return fArray;
106 }
107
108 void Reset()
109 {
110 memset(fArray, 0, fN*sizeof(Int_t));
111 }
112
113 void Reset(Int_t v)
114 {
115 for (Int_t *i=fArray; i<fArray+fN; i++)
116 *i = v;
117 }
118
119 void Set(UInt_t n)
120 {
121 // Set size of this array to n chars.
122 // A new array is created, the old contents copied to the new array,
123 // then the old array is deleted.
124
125 if (n==fN)
126 return;
127
128 Int_t *temp = fArray;
129 if (n == 0)
130 fArray = NULL;
131 else
132 {
133 fArray = new Int_t[n];
134 if (n < fN)
135 memcpy(fArray, temp, n*sizeof(Int_t));
136 else
137 {
138 memcpy(fArray, temp, fN*sizeof(Int_t));
139 memset(&fArray[fN], 0, (n-fN)*sizeof(Int_t));
140 }
141 }
142
143 if (fN)
144 delete [] temp;
145
146 fN = n;
147 }
148
149 void Set(UInt_t n, Int_t *array)
150 {
151 // Set size of this array to n chars and set the contents.
152 if (!array)
153 return;
154
155 if (fArray && fN != n)
156 {
157 delete [] fArray;
158 fArray = 0;
159 }
160 fN = n;
161
162 if (fN == 0)
163 return;
164
165 if (!fArray)
166 fArray = new Int_t[fN];
167
168 memcpy(fArray, array, n*sizeof(Int_t));
169 }
170
171 Int_t &operator[](UInt_t i)
172 {
173 return fArray[i];
174 }
175 const Int_t &operator[](UInt_t i) const
176 {
177 return fArray[i];
178 }
179
180 void ReSort(Bool_t down=kFALSE);
181
182 ClassDef(MArrayI, 1) //Array of Int_t
183};
184
185#endif
Note: See TracBrowser for help on using the repository browser.