1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#ifndef ARRAY_H
#define ARRAY_H
#include <assert.h>
#include <memory.h>
template <class T, int LB, int UB>
class Array
{
public:
Array ();
Aray (const Array <T,LB, UB> &);
~Array ();
T & At (int);
T At (int) const;
Array <T, LB, UB> & Copy (const Array <T,LB, UB> &);
void Sort ();
Array <T, LB, UB> & operator = (const Array <T, LB, UB> &);
T & operator [] (int);
T operator [] (int) const;
private:
T Data [UB - LB + 1];
template <class T, int LB, int UB>
inline T & Array <T, LB, UB>::At (int i)
{
return (*this) [i];
}
template <class T, int LB, int UB>
inline T Array <T, LB, UB>::At (int i) const
{
return operator [] (i);
}
template <class T, int LB, int UB>
inline Array <T, LB, UB> & Array <T, LB, UB>::Copy (const Array <T, LB, UB> & A)
{
return operator = (A);
}
template <class T, int LB, int UB>
Array <T, LB, UB>::Array ()
{
}
template <class T, int LB, int UB>
Array <T, LB, UB>::Array (const Array <T, LB, UB> & A)
{
int i;
for (i = 0; i < (UB - LB); i++)
Data [i] = A.Data [i];
// memcpy (Data, A.Data, NumElements * sizeof (T));
}
template <class T, int LB, int UB>
Array <T, LB, UB>::~Array ()
{
}
template <class T, int LB, int UB>
void Array <T, LB, UB>::Sort ()
{
int i;
int Num;
bool Sorted;
T Temp;
Num = NumElements - 1;
do {
Sorted = true;
for (i = 0; i < Num; i++)
if (Data [i] > Data [i + 1])
{
Temp = Data [i];
Data [i] = Data [i + 1];
Data [i + 1] = Temp;
Sorted = false;
}
} while (!Sorted);
}
template <class T, int LB, int UB>
Array <T, LB, UB> & Array <T, LB, UB>::operator = (const Array <T, LB, UB> & A)
{
if (NumElements != A.NumElements)
{
delete [] Data;
NumElements = A.NumElements;
Data = new T [NumElements];
}
else;
for (int i = 0; i < NumElements; i++)
Data [i] = A.Data [i];
return *this;
}
template <class T, int LB, int UB>
T & Array <T, LB, UB>::operator [] (int i)
{
assert ((i >= LB) && (i <= UB));
return Data [i - LB];
}
template <class T, int LB, int UB>
T Array <T, LB, UB>::operator [] (int i) const
{
assert ((i >= 0) && (i < NumElements));
return Data [i - LB];
}
#endif
|