source: trunk/MagicSoft/Mars/mbase/MArrayB.h@ 8813

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