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

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