ENERGY MONITOR 0.2
Loading...
Searching...
No Matches
dataTable.h
Go to the documentation of this file.
1#pragma once
2
3/*
4SINCE CLASSES ARE TEMPLATED
5IMPLEMENTATION AND PROTOTYPES ARE IN HEADER FOR NO-ERROR COMPILING
6
7*/
8
9#include <vector>
10#include <string>
11#include <iostream>
12using namespace std;
13//ALLOCATE EXTRA MEMORY TO DAMPEN THE DELAY OF VECTOR EXPANSION
14#define ROW_VECTOR_OVERSIZE 10
15#define COLUMN_VECTOR_OVERSIZE 1
16
25/*
26 DYNAMIC DATA HOLDER THAT KEEPS DATA IN RECTANGULAR 2D arrays for now
27*/
28template <typename HELD_DATA>
30
31 private:
32 unsigned int _cols_num = 0;
33 unsigned int _rows_num = 0;
34 vector<vector<HELD_DATA>> _table;
35
36
37 public:
38 unsigned int get_row_num(){
39 return _rows_num;
40 }
41 unsigned int get_col_num(){
42 return _cols_num;
43 }
44
46 void insertData(HELD_DATA d, unsigned int row , unsigned int col){
47 if(row >= _rows_num){
48 _rows_num = row + 1;
49 }
50 if(col >= _cols_num){
51 _cols_num = col + 1;
52 }
53
54 if(col >= _table.size())_table.resize(col + COLUMN_VECTOR_OVERSIZE);
55 if(row >= _table.at(col).size())_table.at(col).resize(row + ROW_VECTOR_OVERSIZE);
56 //WE CHECK BOUNDARIES FIRST THEN INSERT OUR DATA
57 _table.at(col).at(row) = d;
58 }
59
64 HELD_DATA getData(int row , int col)const {
65 return _table.at(col).at(row);
66 }
67
68
74 void eraseRow(int start_row, int rows_num = 1){
75 for(int i = 0; i < _table.size(); i++){
76 _table.at(i).erase(_table.at(i).begin() + start_row , _table.at(i).begin() + start_row + rows_num);
77 }
78 _rows_num-= rows_num;
79 }
80
86 void eraseColumn(int start_col, int cols_num = 1){
87 _table.erase(_table.begin() + start_col, _table.begin() + start_col + cols_num);
88 _cols_num-= cols_num;
89 }
90
91
95 void refresh(){
96 for(unsigned int i = 0; i < _cols_num; i++){
97 if(_table.at(i).size() <= _rows_num){
98 _table.at(i).resize(_rows_num);
99 }
100 }
101 //SHRINK THE TABLE TOO TO COMPENSATE FOR OVERSIZING
102 _table.shrink_to_fit();
103 for(unsigned int i = 0; i < _table.size(); i++){
104 _table.at(i).shrink_to_fit();
105 }
106
107
108 }
109
114 bool extractRow(unsigned int rowNumber, std::vector<HELD_DATA> &returnArray){
115 if(rowNumber > this->_rows_num){
116 return false;
117 }else{
118 for(unsigned int idx = 0; idx < _cols_num; idx++){
119 returnArray.push_back(getData(rowNumber, idx));
120 }
121 return true;
122 }
123 }
124
129 bool extractColumn(unsigned int columnNumber, std::vector<HELD_DATA> &returnArray){
130 if(columnNumber > this->_cols_num){
131 return false;
132 }else{
133 returnArray = _table.at(columnNumber);
134 }
135 return true;
136 }
137
142 bool insertRow(unsigned int rowNumber,const std::vector<HELD_DATA> &putArray){
143 for(unsigned int idx = 0; idx < putArray->size(); idx++){
144 this->insertData(putArray->at(idx), rowNumber , idx);
145 }
146 return true;
147 }
148
153 bool insertColumn(unsigned int columnNumber,const std::vector<HELD_DATA> &putArray){
154 if(columnNumber >= this->_table.size()){
155 this->_table.resize(columnNumber + 1);
156 _cols_num = columnNumber + 1;
157 }
158 if(putArray.size() >= this->_rows_num){
159 _rows_num = putArray.size();
160 }
161 _table.at(columnNumber) = putArray;
162 return true;
163 }
164
172 bool sub_table(dataTable &subTable, int rowStart, int rowEnd, int columnStart, int columnEnd){
173 if((rowStart >= this->_rows_num) || (rowEnd >= this->_rows_num) || (columnStart >= this->_cols_num) || (columnEnd >= this->_cols_num)){
174 return false;
175 }else{
176 for(int i = columnStart; i <= columnEnd; i++){
177 if(i < subTable._table.size()){
178 subTable._table.at(i) = ( std::vector<HELD_DATA>(this->_table.at(i).begin() + rowStart ,this->_table.at(i).begin() + rowEnd + 1));
179 }else{
180 subTable._table.push_back( std::vector<HELD_DATA>(this->_table.at(i).begin() + rowStart ,this->_table.at(i).begin() + rowEnd + 1));
181 }
182 }
183
184 subTable._rows_num = abs(rowEnd - rowStart + 1);
185 subTable._cols_num = columnEnd - columnStart + 1;
186 return true;
187 }
188 }
189};
190
191
192
193
bool insertColumn(unsigned int columnNumber, const std::vector< HELD_DATA > &putArray)
inserts a column in the table replacing existing if they exist or adds them
Definition dataTable.h:153
bool sub_table(dataTable &subTable, int rowStart, int rowEnd, int columnStart, int columnEnd)
EXTRACT A SUBSET OF THE BIGGER TABLE.
Definition dataTable.h:172
bool insertRow(unsigned int rowNumber, const std::vector< HELD_DATA > &putArray)
inserts a row in the table replacing existing if they exist or adds them
Definition dataTable.h:142
HELD_DATA getData(int row, int col) const
return the data at some address
Definition dataTable.h:64
bool extractRow(unsigned int rowNumber, std::vector< HELD_DATA > &returnArray)
Extracts a row from the dataTable in a vector.
Definition dataTable.h:114
void insertData(HELD_DATA d, unsigned int row, unsigned int col)
inserts a value to a cell of dataTable instance with that address
Definition dataTable.h:46
void eraseColumn(int start_col, int cols_num=1)
entirely deletes colunms and frees thier memory while keeping order
Definition dataTable.h:86
void eraseRow(int start_row, int rows_num=1)
entirely deletes the rows and frees their memory
Definition dataTable.h:74
unsigned int get_col_num()
Definition dataTable.h:41
unsigned int get_row_num()
Definition dataTable.h:38
bool extractColumn(unsigned int columnNumber, std::vector< HELD_DATA > &returnArray)
Extracts a row from the dataTable in a vector.
Definition dataTable.h:129
void refresh()
refreshes the data Table for any mistake probability or bad handling
Definition dataTable.h:95
#define COLUMN_VECTOR_OVERSIZE
Definition dataTable.h:15
#define ROW_VECTOR_OVERSIZE
Definition dataTable.h:14