diff options
| author | Ben Boeckel <MathStuf@gmail.com> | 2008-02-23 06:58:42 +0000 |
|---|---|---|
| committer | Ben Boeckel <MathStuf@gmail.com> | 2008-02-23 06:58:42 +0000 |
| commit | da42e77ec57e3ecf0ecc2e1b8fe8b21a50ac58cd (patch) | |
| tree | 5098a835087cf1c81cf4d81d4d444703e1253c6e /general | |
| parent | d7bc310363c98994457e7882e7010e276169f00a (diff) | |
[FIX] Matrix edge cases around 0 x 0 dimension
[FIX] Build order in pokegen.pro
git-svn-id: https://pokegen.svn.sourceforge.net/svnroot/pokegen/trunk@73 6ecfd1a5-f3ed-3746-8530-beee90d26b22
Diffstat (limited to 'general')
| -rw-r--r-- | general/Matrix.h | 94 |
1 files changed, 42 insertions, 52 deletions
diff --git a/general/Matrix.h b/general/Matrix.h index 26f9d703..f1613c09 100644 --- a/general/Matrix.h +++ b/general/Matrix.h @@ -28,7 +28,7 @@ #include <QVector> #include <QVectorIterator> -#include "Ini.h" +#include "Exception.h" template<class T> class Matrix { @@ -43,6 +43,13 @@ template<class T> class Matrix height(h), matrix(h, QVector<T>(w, d)) { + if (!width ^ !height) + { + if (width) + addRow(d); + if (height) + addCol(d); + } } Matrix(const Matrix<T>& rhs) : width(rhs.getWidth()), @@ -56,87 +63,70 @@ template<class T> class Matrix } } - void load(const QString& fname) - { - Ini ini(fname); - ini.getValue("width", width, 0); - ini.getValue("height", height, 0); - if (!(width && height)) - return; - matrix.resize(width, QVector<T>(height, 0)); - for (int i = 0; i < width; ++i) - { - for (int j = 0; j < height; ++j) - { - T k; - ini.getValue(QString("Element-%1-%2").arg(i).arg(j), k, 0); - matrix[i][j] = k; - } - } - } - void save(const QString& fname, const QString& val) const - { - Ini ini; - ini.addField("height", height); - ini.addField("width", width); - for (int i = 0; i < width; ++i) - { - for (int j = 0; j < height; ++j) - ini.addField(QString("Element-%1-%2").arg(i).arg(j), matrix[i][j]); - } - ini.save(fname); - } - void addRow(const T& d = T()) { + width += !width; matrix.append(QVector<T>(width, d)); ++height; } void addCol(const T& d = T()) { + if (!height) + { + matrix.append(QVector<T>()); + ++height; + } for (QMutableVectorIterator< QVector<T> > i(matrix); i.hasNext(); ) i.next().append(d); ++width; } - void insertRow(const int pos, const T& d = T()) + void insertRow(const int pos, const T& d = T()) throw(BoundsException) { if (height < pos) - throw("Matrix: dimension out-of-bounds"); + throw(BoundsException("Matrix", "dimension")); + width += !width; matrix.insert(pos, QVector<T>(width, d)); ++height; } - void insertCol(const int pos, const T& d = T()) + void insertCol(const int pos, const T& d = T()) throw(BoundsException) { if (width < pos) - throw("Matrix: dimension out-of-bounds"); + throw(BoundsException("Matrix", "dimension")); + if (!height) + { + matrix.append(QVector<T>()); + ++height; + } for (QMutableVectorIterator< QVector<T> > i(matrix); i.hasNext(); ) i.next().insert(pos, d); ++width; } - void deleteRow(const int pos) + void deleteRow(const int pos) throw(BoundsException) { if (height <= pos) - throw("Matrix: dimension out-of-bounds"); + throw(BoundsException("Matrix", "dimension")); matrix.remove(pos); - --height; + if (!(--height)) + width = 0; } - void deleteCol(const int pos) + void deleteCol(const int pos) throw(BoundsException) { if (width <= pos) - throw("Matrix: dimension out-of-bounds"); + throw(BoundsException("Matrix", "dimension")); for (QMutableVectorIterator< QVector<T> > i(matrix); i.hasNext(); ) i.next().remove(pos); - --width; + if (!(--width)) + height = 0; } void clear() { matrix.clear(); } - void set(const int row, const int col, const T& s) + void set(const int row, const int col, const T& s) throw(BoundsException) { if ((height <= row) || (width <= col)) - throw("Matrix: dimension out-of-bounds"); + throw(BoundsException("Matrix", "dimension")); (*this)(row, col) = s; } void resize(const int h, const int w, const T& d = T()) @@ -147,22 +137,22 @@ template<class T> class Matrix matrix = QVector< QVector<T> >(h, QVector<T>(w, d)); } - const T& at(const int row, const int col) const + const T& at(const int row, const int col) const throw(BoundsException) { if ((height <= row) || (width <= col)) - throw("Matrix: dimension out-of-bounds"); + throw(BoundsException("Matrix", "dimension")); return matrix.at(row).at(col); } - const QVector<T> getRow(const int row) const + const QVector<T> getRow(const int row) const throw(BoundsException) { if (height <= row) - throw("Matrix: dimension out-of-bounds"); + throw(BoundsException("Matrix", "dimension")); return matrix.at(row); } - const QVector<T> getCol(const int col) const + const QVector<T> getCol(const int col) const throw(BoundsException) { if (width <= col) - throw("Matrix: dimension out-of-bounds"); + throw(BoundsException("Matrix", "dimension")); QVector<T> c; for (QVectorIterator< QVector<T> > i(matrix); i.hasNext(); ) c.append(i.next().at(col)); @@ -177,10 +167,10 @@ template<class T> class Matrix return width; } - T& operator()(const int row, const int col) + T& operator()(const int row, const int col) throw(BoundsException) { if ((height <= row) || (width <= col)) - throw("Matrix: dimension out-of-bounds"); + throw(BoundsException("Matrix", "dimension")); return (matrix[row])[col]; } const T& operator()(const int row, const int col) const |
