| 1 | // ----------------------------------------------------------------------------- |
|---|
| 2 | // Include-Dateien |
|---|
| 3 | // ----------------------------------------------------------------------------- |
|---|
| 4 | |
|---|
| 5 | #include <cstring> |
|---|
| 6 | #include <fstream> |
|---|
| 7 | #include <iostream> |
|---|
| 8 | |
|---|
| 9 | #include "a3_matrix.hxx" |
|---|
| 10 | |
|---|
| 11 | using std::cout; |
|---|
| 12 | |
|---|
| 13 | // ----------------------------------------------------------------------------- |
|---|
| 14 | void A3Matrix::Init ( int xlen, |
|---|
| 15 | int ylen, |
|---|
| 16 | int del ) |
|---|
| 17 | // ----------------------------------------------------------------------------- |
|---|
| 18 | { |
|---|
| 19 | width = xlen; |
|---|
| 20 | height = ylen; |
|---|
| 21 | free = !!del; |
|---|
| 22 | matrix = new vp [width * height]; |
|---|
| 23 | |
|---|
| 24 | memset(matrix,0,width * height * sizeof(vp)); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | // ----------------------------------------------------------------------------- |
|---|
| 28 | A3Matrix::A3Matrix ( int len, |
|---|
| 29 | int del ) |
|---|
| 30 | // ----------------------------------------------------------------------------- |
|---|
| 31 | { |
|---|
| 32 | Init(len,len,del); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | // ----------------------------------------------------------------------------- |
|---|
| 36 | A3Matrix::A3Matrix ( int xlen, |
|---|
| 37 | int ylen, |
|---|
| 38 | int del ) |
|---|
| 39 | // ----------------------------------------------------------------------------- |
|---|
| 40 | { |
|---|
| 41 | Init(xlen,ylen,del); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | // ----------------------------------------------------------------------------- |
|---|
| 45 | A3Matrix::A3Matrix ( A3Matrix &other ) |
|---|
| 46 | // ----------------------------------------------------------------------------- |
|---|
| 47 | { |
|---|
| 48 | Init(other.width,other.height,other.free); |
|---|
| 49 | |
|---|
| 50 | memcpy(matrix,other.matrix,width * height * sizeof(vp)); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | // ----------------------------------------------------------------------------- |
|---|
| 54 | A3Matrix::~A3Matrix ( void ) |
|---|
| 55 | // ----------------------------------------------------------------------------- |
|---|
| 56 | { |
|---|
| 57 | if (free) Clear(); |
|---|
| 58 | |
|---|
| 59 | delete matrix; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | // ----------------------------------------------------------------------------- |
|---|
| 63 | int A3Matrix::Set ( int xpos, |
|---|
| 64 | int ypos, |
|---|
| 65 | vp val ) |
|---|
| 66 | // ----------------------------------------------------------------------------- |
|---|
| 67 | { |
|---|
| 68 | int error = 0; |
|---|
| 69 | |
|---|
| 70 | if ((xpos < 0) || |
|---|
| 71 | (xpos >= width) || |
|---|
| 72 | (ypos < 0) || |
|---|
| 73 | (ypos >= height)) error = 1; |
|---|
| 74 | else |
|---|
| 75 | { |
|---|
| 76 | int p = ypos * width + xpos; |
|---|
| 77 | |
|---|
| 78 | if (free && matrix[p]) delete matrix[p]; |
|---|
| 79 | |
|---|
| 80 | matrix[p] = val; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | return error; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | // ----------------------------------------------------------------------------- |
|---|
| 87 | vp A3Matrix::Get ( int xpos, |
|---|
| 88 | int ypos ) |
|---|
| 89 | // ----------------------------------------------------------------------------- |
|---|
| 90 | { |
|---|
| 91 | vp val = NULL; |
|---|
| 92 | |
|---|
| 93 | if ((xpos >= 0) && |
|---|
| 94 | (xpos < width) && |
|---|
| 95 | (ypos >= 0) && |
|---|
| 96 | (ypos < height)) |
|---|
| 97 | { |
|---|
| 98 | int p = ypos * width + xpos; |
|---|
| 99 | |
|---|
| 100 | val = matrix[p]; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | return val; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | // ----------------------------------------------------------------------------- |
|---|
| 107 | void A3Matrix::Clear ( void ) |
|---|
| 108 | // ----------------------------------------------------------------------------- |
|---|
| 109 | { |
|---|
| 110 | int y = height; |
|---|
| 111 | |
|---|
| 112 | while (y--) |
|---|
| 113 | { |
|---|
| 114 | int x = width, |
|---|
| 115 | l = y * width; |
|---|
| 116 | |
|---|
| 117 | while (x--) |
|---|
| 118 | { |
|---|
| 119 | int p = l + x; |
|---|
| 120 | |
|---|
| 121 | if (free && matrix[p]) delete matrix[p]; |
|---|
| 122 | |
|---|
| 123 | matrix[p] = NULL; |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | // ----------------------------------------------------------------------------- |
|---|
| 129 | void A3Matrix::Dump ( dumpfunc edump ) |
|---|
| 130 | // ----------------------------------------------------------------------------- |
|---|
| 131 | { |
|---|
| 132 | int y = 0; |
|---|
| 133 | |
|---|
| 134 | while (y < height) |
|---|
| 135 | { |
|---|
| 136 | int x = 0, |
|---|
| 137 | l = y * width; |
|---|
| 138 | |
|---|
| 139 | while (x < width) |
|---|
| 140 | { |
|---|
| 141 | int p = l + x; |
|---|
| 142 | |
|---|
| 143 | if (edump) edump(matrix[p]); |
|---|
| 144 | else cout << " " << (long)matrix[p]; |
|---|
| 145 | |
|---|
| 146 | x++; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | cout << "\n"; |
|---|
| 150 | |
|---|
| 151 | y++; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|