-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray2D.hpp
52 lines (47 loc) · 1.13 KB
/
array2D.hpp
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
// Author: Kristi Daigh
// Project: MLEM2 Rule Induction
// Date: 11/20/2019
/** Header file for Array2D template.
@file array2D.hpp
This 2D array handles only arrays where
the number of columns is known in advance. */
#include <stdexcept>
#include <iostream>
#include <vector>
using namespace std;
template<class T>
class Array2D {
public:
Array2D(const int cols, size_t offset = 0)
: m_cols(cols), m_offset(offset) {
m_array = new vector<T>[m_cols];
}
~Array2D(){
delete[] m_array;
}
void add(int col, T& item){
if(col < m_cols){
m_array[col].push_back(item);
}
}
T at(int row, int col){
try {
return m_array[col].at(row - m_offset);
} catch (out_of_range){
throw out_of_range("Invalid array coordinates.");
}
}
size_t getNumCols() const {
return m_cols;
}
size_t getNumRows() const {
return m_array[m_cols - 1].size();
}
vector<T> getCol(int col) const{
return m_array[col];
}
vector<T> * m_array;
private:
const int m_cols;
size_t m_offset;
};