File: Array.h

package info (click to toggle)
klustakwik 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch, wheezy
  • size: 168 kB
  • ctags: 119
  • sloc: cpp: 812; ansic: 103; makefile: 21
file content (130 lines) | stat: -rw-r--r-- 2,571 bytes parent folder | download
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// template <class T> class Array;

template <class T>
class Array {
public:
	Array(const T a[], int n) {
		if (n<1) {
			cerr << "Illegal Array m_Size " << n << "\n";
			abort();
		}
		m_Size = n;
		m_Data = new T[m_Size];
			if (!m_Data) {
			cerr << "Could not allocate memory for array of size "
				 << n << "\n";
			abort();
		}
		for (int i=0; i<m_Size; i++) m_Data[i] = a[i];
	}
	Array() { // default constructor, makes an empty array
        m_Data = 0;
        m_Size = 0;
    };
	void SetSize(int n) {
		if (m_Data != 0) delete[] m_Data; // delete old data if there is any
		if (n<1) {
			cerr << "Illegal Array m_Size " << n << "\n";
			abort();
		}
		m_Size = n;
		m_Data = new T[m_Size];
		if (!m_Data) {
			cerr << "Could not allocate memory for array of size "
				 << n << "\n";
			abort();
		}
	}
	Array(int n) {
		if (n<1) {
			cerr << "Illegal Array m_Size " << n << "\n";
			abort();
		}
		m_Size = n;
		m_Data = new T[m_Size];
		if (!m_Data) {
			cerr << "Could not allocate memory for array of size "
				 << n << "\n";
			abort();
		}
	}
	~Array() {
		delete []m_Data;
	}
	int size() const {
		return m_Size;
	}
	T& operator[](int i) const {
		if (i<0 || i>=m_Size) {
			cerr << "Array index " << i << " out of bounds!\n";
			abort();
		}
		return (m_Data[i]);
	}

	Array<T>& operator=(const Array<T> &a) {
		if (m_Size != a.m_Size) {
			cerr << "Copying arrays of different sizes " << m_Size << " and " << a.m_Size << ".\n";
			abort();
		}

		for (int i=0; i<m_Size; i++) {
			m_Data[i] = a.m_Data[i];
		}

		return (*this);
	}

//private:
	T *m_Data;
	int m_Size;
};

template <class T>
class Array2 {
public:
	Array2(int n1, int n2) {
		if (n1<1 || n2<1) {
			cerr << "Illegal 2D Array m_Size " << n1 << "x" << n2 << "\n";
			abort();
		}
		m_Size1 = n1;
		m_Size2 = n2;
		m_Data2 = new Array<T> *[n1];
		if (!m_Data2) {
			cerr << "Could not allocate memory for 2D array of size "
				 << n1 << "x" << n2 << "\n";
			abort();
		}
		for(int i=0; i<n1; i++) {
			m_Data2[i] = new Array<T>(n2);
				if (!m_Data2[i]) {
					cerr << "Could not allocate memory for 2D array of size "
						 << n1 << "x" << n2 << "\n";
				abort();
			}
		}
	}
	~Array2() {
		for(int i=0; i<m_Size1; i++) {
			delete (m_Data2[i]);
		}
		delete[] m_Data2;
	}
	int size1() const {
		return m_Size1;
	}
	int size2() const {
		return m_Size2;
	}
	Array<T>& operator[](int i) const {
		if (i<0 || i>=m_Size1) {
			cerr << "2D Array index " << i << " out of bounds!\n";
			abort();
		}
		return (*(m_Data2[i]));
	}
private:
	Array<T> **m_Data2;
	int m_Size1, m_Size2;
};