142 lines
4.6 KiB
C++
Executable File
142 lines
4.6 KiB
C++
Executable File
// **********
|
|
// ARRAYS.CPP
|
|
// **********
|
|
|
|
#include <iostream.h>
|
|
#include <assert.h>
|
|
|
|
#include "arrays.h"
|
|
|
|
//=============================================================================
|
|
template <class T> void Array<T>::Init(const Array<T> &t) {
|
|
Allocate(t.size);
|
|
assert(size == t.size);
|
|
for (int i = 0; i < size; i++)
|
|
data[i] = t.data[i];
|
|
}
|
|
//=============================================================================
|
|
template <class T> void Array<T>::Allocate(int as) {
|
|
// if previously allocated, delete old dynamic array
|
|
if (size) delete[] data;
|
|
size = as;
|
|
data = new T[size];
|
|
assert(data);
|
|
}
|
|
//=============================================================================
|
|
template <class T> Array<T>::~Array() {
|
|
delete[] data;
|
|
}
|
|
//=============================================================================
|
|
template <class T> T &Array<T>::operator[](int i) const {
|
|
if (i < 0) { cout<<"ERROR in []: "<<i<<"< 0"<<endl; exit(-1); }
|
|
if (i >= size) { cout<<"ERROR in []: "<<i<<" >= "<<size<<endl; exit(-1); }
|
|
assert(i >= 0);
|
|
assert(i < size);
|
|
return data[i];
|
|
}
|
|
//=============================================================================
|
|
template <class T> T &Array<T>::Data(int i) {
|
|
if (i < 0) { cout<<"ERROR in Data: "<<i<<"< 0"<<endl; exit(-1); }
|
|
if (i >= size) { cout<<"ERROR in Data: "<<i<<" >= "<<size<<endl; exit(-1); }
|
|
assert(i >= 0);
|
|
assert(i < size);
|
|
return data[i];
|
|
}
|
|
//=============================================================================
|
|
template <class T> Array<T> &Array<T>::operator = (const Array<T> &t) {
|
|
if (!size) // if the object in not yet allocated, do it and then assign
|
|
Allocate(t.size);
|
|
assert(size == t.size);
|
|
for (int i = 0; i < size; i++)
|
|
data[i] = t.data[i];
|
|
return *this;
|
|
}
|
|
//=============================================================================
|
|
template <class T> int Array<T>::Size() const {
|
|
return size;
|
|
}
|
|
//=============================================================================
|
|
template <class T> ostream &operator<<(ostream &s, const Array<T> &t) {
|
|
for (int i =0; i < t.size; i++)
|
|
s<<t.data[i]<<" ";
|
|
return s;
|
|
}
|
|
//=============================================================================
|
|
template <class T> void Array<T>::Set(T t) {
|
|
for (int i =0; i < size; i++)
|
|
data[i] = t;
|
|
}
|
|
//=============================================================================
|
|
//=============================================================================
|
|
// HeapSort data[0..size-1] DESCENDING
|
|
template <class T> void SortableArray<T>::Sort() {
|
|
// build the heap
|
|
for (int i = Size()-1; i >= 0; i--)
|
|
Adjust(i, Size()-1);
|
|
|
|
for (int i = Size()-1; i >= 1; i--) {
|
|
// swap data
|
|
T temp1 = Data(0);
|
|
Data(0) = Data(i);
|
|
Data(i) = temp1;
|
|
|
|
Adjust(0, i-1);
|
|
}
|
|
}
|
|
//=============================================================================
|
|
template <class T> void SortableArray<T>::Adjust(int root, int last) {
|
|
if (2*root <= last) {
|
|
int child = 2*root;
|
|
if ((child+1) <= last) {
|
|
if (Data(child+1) < Data(child))
|
|
child++;
|
|
}
|
|
if (Data(child) < Data(root)) {
|
|
T temp = Data(root);
|
|
Data(root) = Data(child);
|
|
Data(child) = temp;
|
|
|
|
Adjust(child, last);
|
|
}
|
|
}
|
|
}
|
|
//=============================================================================
|
|
//=============================================================================
|
|
// HeapSort data[0..size-1] DESCENDING
|
|
template <class T, class C> void CompSortableArray<T, C>::Sort() {
|
|
// build the heap
|
|
int sz = Size();
|
|
for (int i = sz-1; i >= 0; i--) {
|
|
Adjust(i, sz-1);
|
|
}
|
|
|
|
for (i = sz-1; i >= 1; i--) {
|
|
// do the swap
|
|
T temp1 = Data(0);
|
|
Data(0) = Data(i);
|
|
Data(i) = temp1;
|
|
Adjust(0, i-1);
|
|
}
|
|
}
|
|
//=============================================================================
|
|
template <class T, class C> void CompSortableArray<T, C>::Adjust(int root,
|
|
int last) {
|
|
if (2*root <= last) {
|
|
int child = 2*root;
|
|
if ((child+1) <= last) {
|
|
if (comp_ptr->Compare(Data(child+1), Data(child)) == -1) {
|
|
child++;
|
|
}
|
|
}
|
|
if (comp_ptr->Compare(Data(child), Data(root)) == -1) {
|
|
T temp = Data(root);
|
|
Data(root) = Data(child);
|
|
Data(child) = temp;
|
|
|
|
Adjust(child, last);
|
|
}
|
|
}
|
|
}
|
|
//=============================================================================
|
|
|