-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfvector.hpp
More file actions
103 lines (103 loc) · 3.34 KB
/
Copy pathfvector.hpp
File metadata and controls
103 lines (103 loc) · 3.34 KB
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
#ifndef FVECTOR_HPP
#define FVECTOR_HPP
#include <vector>
template <typename T>
class fvector {
private:
// vector of vectors of template type. contains actual data
std::vector<std::vector<T>> columns;
std::vector<T> columnstoreturn;
int columnstocreate;
int rowstocreate;
int rowscreated;
T newrow;
public:
fvector() {
// With no shape argument, there is nothing to do
}
/* ins() inserts an item, or acts like add().
because of that, the code below is almost identical
to the add() function. To understand it, read the add() function
*/
void ins(T value, int x, int y) {
columnstocreate = (x + 1) - columns.size();
if (columnstocreate > 0) {
int columnscreated = 0;
while (columnscreated < columnstocreate) {
columnscreated += 1;
std::vector<T> newcolumn;
columns.push_back(newcolumn);
}
}
rowstocreate = (y + 1) - columns[x].size();
if (rowstocreate > 0) {
rowscreated = 0;
while (rowscreated < rowstocreate) {
rowscreated += 1;
columns[x].push_back(newrow);
}
}
if (rowstocreate > 0 || columnstocreate > 0) {
add(value, x, y);
} else {
std::vector<T> newcolumn;
columns.insert(columns.begin() + x, newcolumn);
columns[x].insert(columns[x].begin() + y, value);
}
}
// add() either replaces an existing indice, or creates a new indice
void add(T value, int x, int y) {
// x is 0 indexed, size is 1 indexed, the + 1 converts
columnstocreate = (x + 1) - columns.size();
// iterate until number of columns is fulfilled
if (columnstocreate != 0) {
int columnscreated = 0;
while (columnscreated < columnstocreate) {
columnscreated += 1;
std::vector<T> newcolumn;
columns.push_back(newcolumn);
}
}
// y is also zero indexed
rowstocreate = (y + 1) - columns[x].size();
// iterate until number of rows is fulfilled
if (rowstocreate != 0) {
rowscreated = 0;
while (rowscreated < rowstocreate) {
rowscreated += 1;
columns[x].push_back(newrow);
}
}
columns[x][y] = value;
}
void rem(int x, int y) {
if (y != -1 && x != -1) {
/* erase takes an iterator, and only iterator available
is .begin (or .end) adding y increments iterator till correct spot.
the [x] reference returns the std::vector<T> column that contains the
correct y coord
*/
columns[x].erase(columns[x].begin() + y);
} else if (y == -1) {
/* [x] is not needed here because it removes an entire x column, so
the iter returns the begin of the x array instead of y array, and is
incremented to correct x coord. the entire array is then removed
*/
columns.erase(columns.begin() + x);
}
}
std::vector<T> get(int x = -1, int y = -1) {
// clear the vector so it doesn't return past results
columnstoreturn.clear();
// if no negatives, return coords, otherwise if y is negative, return x column
if (y != -1 && x != -1) {
columnstoreturn.push_back(columns.at(x).at(y));
} else if (y == -1) {
for (T currow : columns[x]) {
columnstoreturn.push_back(currow);
}
}
return columnstoreturn;
}
};
#endif