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