data in buffer
This commit is contained in:
parent
3c49920613
commit
0081e8f89e
25
matrix.c
25
matrix.c
@ -5,31 +5,40 @@
|
|||||||
/*typedef struct {
|
/*typedef struct {
|
||||||
unsigned int rows; //Zeilen
|
unsigned int rows; //Zeilen
|
||||||
unsigned int cols; //Spalten
|
unsigned int cols; //Spalten
|
||||||
MatrixType *data; //Zeiger auf Speicherbereich Reihen*Spalten
|
MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten
|
||||||
} Matrix;*/
|
} Matrix;*/
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols) {
|
Matrix createMatrix(unsigned int rows, unsigned int cols) {
|
||||||
MatrixType *data =
|
MatrixType *buffer =
|
||||||
malloc(rows * cols * sizeof(MatrixType)); // Speicher reservieren, malloc
|
malloc(rows * cols * sizeof(MatrixType)); // Speicher reservieren, malloc
|
||||||
// liefert Zeiger auf Speicher
|
// liefert Zeiger auf Speicher
|
||||||
Matrix newMatrix = {rows, cols, data}; // neue Matrix nach struct
|
Matrix newMatrix = {rows, cols, buffer}; // neue Matrix nach struct
|
||||||
return newMatrix;
|
return newMatrix;
|
||||||
}
|
}
|
||||||
void clearMatrix(Matrix *matrix) {
|
void clearMatrix(Matrix *matrix) {
|
||||||
matrix->data = UNDEFINED_MATRIX_VALUE;
|
matrix->buffer = UNDEFINED_MATRIX_VALUE;
|
||||||
matrix->rows = UNDEFINED_MATRIX_VALUE;
|
matrix->rows = UNDEFINED_MATRIX_VALUE;
|
||||||
matrix->cols = UNDEFINED_MATRIX_VALUE;
|
matrix->cols = UNDEFINED_MATRIX_VALUE;
|
||||||
free((*matrix).data); // Speicher freigeben
|
free((*matrix).buffer); // Speicher freigeben
|
||||||
}
|
}
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix,
|
void setMatrixAt(MatrixType value, Matrix matrix,
|
||||||
unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
||||||
unsigned int colIdx) {
|
unsigned int colIdx) {
|
||||||
matrix.data[rowIdx * matrix.cols + colIdx] =
|
|
||||||
|
matrix.buffer[rowIdx * matrix.cols + colIdx] =
|
||||||
value; // rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte
|
value; // rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte
|
||||||
// innerhalb der Zeile
|
// innerhalb der Zeile
|
||||||
}
|
}
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx,
|
MatrixType getMatrixAt(const Matrix matrix,
|
||||||
|
unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
||||||
unsigned int colIdx) {
|
unsigned int colIdx) {
|
||||||
return 0;
|
if (rowIdx >= matrix.rows ||
|
||||||
|
colIdx >= matrix.cols) { // Speichergröße nicht überschreiten
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
MatrixType value = matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||||
|
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
||||||
// broadcasting
|
// broadcasting
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user