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

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