source: trunk/MagicSoft/Mars/mbase/MArrayS.h@ 9141

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