Skip to content

Instantly share code, notes, and snippets.

@Sarverott
Created December 19, 2021 17:56
Show Gist options
  • Save Sarverott/b929be360a089827d4eeef03026e3d1a to your computer and use it in GitHub Desktop.
Save Sarverott/b929be360a089827d4eeef03026e3d1a to your computer and use it in GitHub Desktop.
broken version
#include <iostream>
using namespace std;
template <typename customType> class MatrixCell{
public:
MatrixCell* NEXT=NULL;
customType* value=NULL;
MatrixCell(){
this->value=new customType;
}
MatrixCell(customType value){
this->value=new customType;
*(this->value)=value;
}
MatrixCell(customType* value){
this->value=value;
}
MatrixCell* getLast(){
if(this->NEXT==NULL){
return this;
}else{
return this->NEXT->getLast();
}
}
int count(int counted=1){
if(this->NEXT==NULL){
return counted;
}else{
return this->NEXT->count(++counted);
}
}
customType get(){
return *(this->value);
}
void set(customType value){
this->value=new customType;
*(this->value)=value;
}
void set(customType* value){
this->value=new customType;
*(this->value)=*(value);
}
void point(customType& value){
this->value=value;
}
void point(customType* value){
this->value=value;
}
MatrixCell* cp(){
MatrixCell* tmp=new MatrixCell(this->get());
tmp->NEXT=this->NEXT->cp();
return tmp;
}
MatrixCell* getByIndex(int index){
if(index==0){
return this;
}else if(index>0&&this->NEXT!=NULL){
return this->NEXT->getByIndex(--index);
}else{
throw "WRONG_INDEX";
}
}
MatrixCell operator[](int index){
return this->getByIndex(index);
}
MatrixCell operator++(){
if(this->value!=NULL)*(this->value)++;
if(this->NEXT!=NULL)*(this->NEXT)++;
}
MatrixCell operator--(){
if(this->value!=NULL)*(this->value)--;
if(this->NEXT!=NULL)*(this->NEXT)--;
}
};
template <typename customType> class MatrixRow{
public:
MatrixRow* NEXT=NULL;
MatrixCell<customType>* firstInRow=NULL;
customType filler;
MatrixRow(){}
MatrixRow(customType fillin){
this->filler=fillin;
}
int count(int counted=1){
if(this->NEXT==NULL){
return counted;
}else{
return this->NEXT->count(++counted);
}
}
void fillRow(int count=1){
if(this->firstInRow==NULL){
this->firstInRow=new MatrixCell<customType>(filler);
count--;
}
MatrixCell<customType>* hook=this->firstInRow->getLast();
for(int i=0;i<count;i++){
hook->NEXT=new MatrixCell<customType>(filler);
}
}
int countColumns(){
int i=0;
int j=this->count();
int lengthMax=0;
for(
int
i=0,
j=this->count(),
lengthMax=0;
i<j;
i++
){
int current=0;
if(this->firstInRow!=NULL){
current=this->firstInRow->count();
}
if(current>lengthMax){
i=-1;
lengthMax=current;
}else if(current<lengthMax){
this->fillRow(lengthMax-current);
}
}
return lengthMax;
}
MatrixRow* cp(){
MatrixRow* tmp=new MatrixRow();
tmp->firstInRow=this->firstInRow->cp();
tmp->NEXT=this->NEXT->cp();
return tmp;
}
MatrixRow* getByIndex(int index){
if(index==0){
return this;
}else if(index>0&&this->NEXT!=NULL){
return this->NEXT->getByIndex(--index);
}else if(this->NEXT!=NULL){
return this;
}else{
throw "WRONG_INDEX";
}
}
MatrixRow operator[](int id){
return *(this->getByIndex(id)->firstInRow);
}
MatrixRow operator++(){
if(this->firstInRow!=NULL)*(this->firstInRow)++;
if(this->NEXT!=NULL)*(this->NEXT)++;
}
MatrixRow operator--(){
if(this->firstInRow!=NULL)*(this->firstInRow)--;
if(this->NEXT!=NULL)*(this->NEXT)--;
}
};
template <typename customType> class Matrix{
public:
customType filler;
MatrixRow<customType>* firstRowHook=NULL;
Matrix(){}
Matrix(int width, int height, customType filler){
this->setupMatrix(width,height,filler);
}
void setupMatrix(int width, int height, customType filler){
this->filler=filler;
this->firstRowHook=new MatrixRow<customType>(filler);
MatrixRow<customType>* hook=this->firstRowHook;
hook->fillRow(width);
for(int i=1;i<height;i++){
hook->NEXT=new MatrixRow<customType>(filler);
hook=hook->NEXT;
hook->fillRow(width);
}
}
int getHeight(){
return this->firstRowHook->count();
}
int getWidth(){
return this->firstRowHook->countColumns();
}
int size(){
return this->getWidth()*this->getHeight();
}
Matrix& operator++(){
*(this->firstRowHook)++;
}
Matrix& operator--(){
*(this->firstRowHook)--;
}
MatrixRow<customType>* rowHook(){
return this->firstRowHook;
}
};
void printMatrix(Matrix<int>* data){
cout<<"matrix "
<<data->getWidth()
<<'x'
<<data->getHeight()
<<" (size:"
<<data->size()
<<")\n\n";
for(int i=0;i<data->getHeight();i++){
cout<<"[\t";
for(int j=0;j<data->getWidth();j++){
cout<<*(
data
->firstRowHook
->getByIndex(i)
->firstInRow
->getByIndex(j)
->value
)<<"\t";
}
cout<<"]\n";
}
cout<<"\n";
}
Matrix<int>* inputMatrix(){
cout<<"matrix insertion\n";
cout<<"set width:";
int w;
cin>>w;
cout<<"set height:";
int h;
cin>>h;
Matrix<int>* data=new Matrix<int>(w,h,0);
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
cout<<"cell["<<i<<"]["<<j<<"]: 44";
cin>>*(
data
->firstRowHook
->getByIndex(i)
->firstInRow
->getByIndex(j)
->value
);
}
}
return data;
}
int main()
{
Matrix<int>* data=inputMatrix();
cout << "\n\n##########\n#~ PRINTING\n############\n\n";
printMatrix(data);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment