source: branches/start/MagicSoft/Mars/mbase/MArrayB.h@ 9584

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