DNWA/11/wywolania/Data/stide_v1.1/Utils/arrays.h
Jakub Stefko ab1d7e2546 ...
2021-01-28 18:33:55 +01:00

110 lines
4.7 KiB
C++
Executable File

// ********
// ARRAYS.H
// ********
#ifndef ARRAYS_H
#define ARRAYS_H
//#define PC
#include <iostream.h>
#include "errors.h"
// this is a template for all classes which use an array of objects
// it is dynamically allocated
template <class T> class Array {
public:
Array() {Init();}
Array(const Array<T> &t) {Init(t);} // the copy constructor
Array(int asize) {Init(); Allocate(asize);} // creates an array of size "asize"
~Array(); // the destructor deletes all internal
// objects
void Allocate(int asize); // allocates asize objects
// if data was already allocated,
// deletes and re-allocates
T &operator[](int i) const;
Array<T> &operator = (const Array<T> &t);
// copies one array to another,
// requires that the assignment
// operator be defined for array
// elements
void Set(T t); // sets all elements to t
int Size() const; // returns the size of the array
friend ostream &operator<<(ostream &s, const Array<T> &t);
protected:
T &Data(int i); // method derived class can use for
// accessing data
void Init() {size = 0; data = NULL;}// default intialisor
void Init(const Array<T> &t); // implements copy constructor
private:
int size; // the size of the array
T *data; // ptr to the array of objects
};
// this is a template for sortable arrays of objects, i.e. the objects provide
// a less than comparison operator, which is used in the Sort method to perform
// a heap sort
template <class T> class SortableArray : public Array<T> {
public:
SortableArray() {Init();}
SortableArray(const SortableArray<T> &t) {Init(t);}
SortableArray(int asize) {Allocate(asize);}
void Sort(); // performs a heapsort on the data,
// using the < operator
protected:
void Adjust(int root, int last); // for the heap sort
};
// this is a template for sortable arrays of objects, but the comparison
// operator is provided by another class C
template <class T, class C> class CompSortableArray : public Array<T> {
public:
CompSortableArray() {Init();}
CompSortableArray(int asize, C *c_ptr) {Allocate(asize); comp_ptr = c_ptr;}
void Sort(); // performs a heapsort on the data,
// using comp_ptr->Compare
protected:
C *comp_ptr; // a ptr to the object with the Compare
// method
void Adjust(int root, int last);
};
// this is a template for a multidimensional array of one type of object
// when declaring this one must specify the number of dimensions first,
// followed by the size for each array dimension
/*
template <class T> class MultiArray {
public:
MultiArray() {Init();}
MultiArray(const MultiArray<T> &t) {Init(t);} // the copy constructor
MultiArray(int dims, int x, ...); // a variable number of parameters
{Init(); Allocate(xsize, ysize);}
~Array2D(); // the destructor deletes all internal
// objects
void Allocate(int xsize, ...); // allocates x, y, ... size array
// if data was already allocated,
// deletes and re-allocates
T Data(int x, int y); // returns object in x,y location
Array2D<T> &operator = (const Array2D<T> &t);
// copies one array to another,
// requires that the assignment
// operator be defined for array
// elements
void Set(T t); // sets all elements to t
int XSize(); // returns the x size of the array
int YSize(); // returns the y size of the array
friend ostream &operator<<(ostream &s, const Array2D<T> &t);
protected:
T &Data(int i); // method derived class can use for
// accessing data
void Init() {size = 0; data = NULL;}// default intialisor
void Init(const Array<T> &t); // implements copy constructor
protected:
Array<Array<T> > data;
};
*/
#endif