-
Notifications
You must be signed in to change notification settings - Fork 6
/
GUISpreadsheet.cpp
103 lines (84 loc) · 2.02 KB
/
GUISpreadsheet.cpp
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
//
// GUISpreadSheet.cpp
// GUI_Showcase
//
// Created by Nathan Daly on 2/19/13.
// Copyright (c) 2013 Lions Entertainment. All rights reserved.
//
#include "GUISpreadsheet.h"
#include <iostream>
#include <algorithm>
namespace GUI {
template <typename T>
class Matrix2D {
public:
Matrix2D(int rows_, int cols_)
:rows(rows_), cols(cols_)
{
T *data = new T[rows*cols];
arr = new T*[rows];
for (int i = 0; i < rows; ++i) {
arr[i] = &data[i*cols];
}
}
Matrix2D(const Matrix2D &rhs)
:rows(rhs.rows_), cols(rhs.cols_)
{
T *data = new T[rows*cols];
arr = new T*[rows];
for (int i = 0; i < rows; ++i) {
arr[i] = &data[i*cols];
T *rhs_data = rhs[i];
for (int j = 0; j < cols; ++j) {
data[i+j] = rhs_data[j];
}
}
}
~Matrix2D() {
delete [] arr[0];
delete [] arr;
}
Matrix2D& operator=(const Matrix2D &rhs)
{
Matrix2D temp(rhs);
std::swap(arr, rhs.arr);
rows = rhs.rows;
cols = rhs.cols;
}
T& operator()(int i, int j) {
if (i >= rows || j >= cols) {
throw Error("out of bounds Matrix access.");
}
return arr[i][j];
}
void grow(int rows_, int cols_) {
if (rows_ == rows && cols_ == cols) {
return;
}
if (rows_ < rows || cols_ < cols) {
throw Error("new size smaller than current size.");
}
Matrix2D temp(rows_, cols_);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
temp(i,j) = arr[i][j];
}
}
*this = temp;
}
private:
T **arr;
int rows, cols;
};
class GUICell;
class CellsView : public View {
public:
CellsView(int w_, int h_);
private:
Matrix2D<GUICell*> cells;
};
CellsView::CellsView(int w_, int h_)
:View(w_,h_), cells(1,1)
{
}
} // namespace GUI