source: trunk/MagicSoft/Mars/mbase/MArrayI.h@ 8654

Last change on this file since 8654 was 8398, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 3.1 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 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 Reset(Int_t v)
98 {
99 for (Int_t *i=fArray; i<fArray+fN; i++)
100 *i = v;
101 }
102
103 void Set(UInt_t n)
104 {
105 // Set size of this array to n chars.
106 // A new array is created, the old contents copied to the new array,
107 // then the old array is deleted.
108
109 if (n==fN)
110 return;
111
112 Int_t *temp = fArray;
113 if (n == 0)
114 fArray = NULL;
115 else
116 {
117 fArray = new Int_t[n];
118 if (n < fN)
119 memcpy(fArray, temp, n*sizeof(Int_t));
120 else
121 {
122 memcpy(fArray, temp, fN*sizeof(Int_t));
123 memset(&fArray[fN], 0, (n-fN)*sizeof(Int_t));
124 }
125 }
126
127 if (fN)
128 delete [] temp;
129
130 fN = n;
131 }
132
133 void Set(UInt_t n, Int_t *array)
134 {
135 // Set size of this array to n chars and set the contents.
136 if (!array)
137 return;
138
139 if (fArray && fN != n)
140 {
141 delete [] fArray;
142 fArray = 0;
143 }
144 fN = n;
145
146 if (fN == 0)
147 return;
148
149 if (!fArray)
150 fArray = new Int_t[fN];
151
152 memcpy(fArray, array, n*sizeof(Int_t));
153 }
154
155 Int_t &operator[](UInt_t i)
156 {
157 return fArray[i];
158 }
159 const Int_t &operator[](UInt_t i) const
160 {
161 return fArray[i];
162 }
163
164 ClassDef(MArrayI, 1) //Array of Int_t
165};
166
167#endif
Note: See TracBrowser for help on using the repository browser.