72 lines
1.8 KiB
C++
72 lines
1.8 KiB
C++
#ifndef NALOGA0801_SPARSEMATRIX2D_H
|
|
#define NALOGA0801_SPARSEMATRIX2D_H
|
|
|
|
#include <vector>
|
|
#include "Element.h"
|
|
|
|
template<typename T>
|
|
class SparseMatrix2D {
|
|
private:
|
|
std::vector<Element<T>> elements;
|
|
unsigned int sizeX, sizeY;
|
|
T defaultElement;
|
|
public:
|
|
SparseMatrix2D(unsigned int sizeX, unsigned int sizeY, T defaultElement);
|
|
|
|
void set(unsigned int x, unsigned int y, T value);
|
|
|
|
T at(unsigned int x, unsigned int y);
|
|
|
|
unsigned int getSizeX();
|
|
|
|
unsigned int getSizeY();
|
|
|
|
bool compareSize(SparseMatrix2D comp);
|
|
};
|
|
|
|
template<typename T>
|
|
SparseMatrix2D<T>::SparseMatrix2D(unsigned int sizeX, unsigned int sizeY, T defaultElement) :
|
|
sizeX(sizeX), sizeY(sizeY), defaultElement(defaultElement) {}
|
|
|
|
template<typename T>
|
|
void SparseMatrix2D<T>::set(unsigned int x, unsigned int y, T value) {
|
|
bool elementExiest = false;
|
|
for (int i = 0; i < elements.size(); ++i) {
|
|
if (elements[i].getX() == x and elements[i].getY() == y) {
|
|
elements[i].setValue(value);
|
|
elementExiest = true;
|
|
}
|
|
}
|
|
if (!elementExiest) {
|
|
Element<T> tmp(x, y, value);
|
|
elements.push_back(tmp);
|
|
}
|
|
}
|
|
|
|
template<typename T>
|
|
T SparseMatrix2D<T>::at(unsigned int x, unsigned int y) {
|
|
for (int i = 0; i < elements.size(); ++i) {
|
|
if (elements[i].getX() == x and elements[i].getY() == y) {
|
|
return elements[i].getValue();
|
|
}
|
|
}
|
|
return defaultElement;
|
|
}
|
|
|
|
template<typename T>
|
|
unsigned int SparseMatrix2D<T>::getSizeX() {
|
|
return sizeX;
|
|
}
|
|
|
|
template<typename T>
|
|
unsigned int SparseMatrix2D<T>::getSizeY() {
|
|
return sizeY;
|
|
}
|
|
|
|
template<typename T>
|
|
bool SparseMatrix2D<T>::compareSize(SparseMatrix2D comp) {
|
|
return sizeX == comp.getSizeX() and sizeY == comp.getSizeY();
|
|
}
|
|
|
|
#endif //NALOGA0801_SPARSEMATRIX2D_H
|