source: branches/removing_cpp11_features/mbase/MArrayS.h@ 18621

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