summaryrefslogtreecommitdiffstats
path: root/general/Matrix.h
diff options
context:
space:
mode:
Diffstat (limited to 'general/Matrix.h')
-rw-r--r--general/Matrix.h213
1 files changed, 213 insertions, 0 deletions
diff --git a/general/Matrix.h b/general/Matrix.h
new file mode 100644
index 00000000..d4a696b2
--- /dev/null
+++ b/general/Matrix.h
@@ -0,0 +1,213 @@
+/////////////////////////////////////////////////////////////////////////////
+// Name: general/Matrix.h
+// Purpose: A 2D vector class
+// Author: Ben Boeckel
+// Modified by: Ben Boeckel
+// Created: Sun Apr 8 12:51:20 2007
+// Copyright: ©2007 Nerdy Productions
+// Licence:
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program. If not, see <http://www.gnu.org/licenses/>.
+/////////////////////////////////////////////////////////////////////////////
+
+#ifndef __MATRIX__
+#define __MATRIX__
+
+#include <QFile>
+#include <QVector>
+#include "Ini.h"
+#include "Point.h"
+
+namespace PokeGen
+{
+ template<class T> class MatrixBase
+ {
+ public:
+ MatrixBase() :
+ width(0),
+ height(0)
+ {
+ }
+ MatrixBase(const unsigned w, const unsigned h, const T& d = T()) :
+ width(w),
+ height(h),
+ matrix(w, QVector<T>(h, d))
+ {
+ }
+
+ void AddRow(const T& d = T())
+ {
+ for (typename QVector< QVector<T> >::Iterator i = matrix.begin(); i != matrix.end(); ++i)
+ i->append(d);
+ ++height;
+ }
+ void AddCol(const T& d = T())
+ {
+ matrix.append(QVector<T>(height, d));
+ ++width;
+ }
+ bool InsertRow(const unsigned pos, const T& d = T())
+ {
+ if (height < pos)
+ return false;
+ for (typename QVector< QVector<T> >::Iterator i = matrix.begin(); i != matrix.end(); ++i)
+ i->insert(i->begin() + pos, d);
+ ++height;
+ return true;
+ }
+ bool InsertCol(const unsigned pos, const T& d = T())
+ {
+ if (width < pos)
+ return false;
+ matrix.insert(matrix.begin() + pos, QVector<T>(height, d));
+ ++width;
+ return true;
+ }
+ bool DeleteRow(const unsigned pos)
+ {
+ if (height <= pos)
+ return false;
+ for (typename QVector< QVector<T> >::Iterator i = matrix.begin(); i != matrix.end(); ++i)
+ i->erase(i->begin() + pos);
+ --height;
+ return true;
+ }
+ bool DeleteCol(const unsigned pos)
+ {
+ if (width <= pos)
+ return false;
+ matrix.erase(matrix.begin() + pos);
+ --width;
+ return true;
+ }
+ void Clear()
+ {
+ matrix.clear();
+ }
+
+ bool Set(const unsigned row, const unsigned col, const T& s)
+ {
+ if ((width <= col) || (height <= col))
+ return false;
+ (matrix.begin() + col)->assign(row, s);
+ return true;
+ }
+ void Resize(const unsigned w, const unsigned h, const T& d = T())
+ {
+ Clear();
+ width = w;
+ height = h;
+ matrix = QVector< QVector<T> >(w, QVector<T>(h, d));
+ }
+
+ T& Get(const unsigned row, const unsigned col) const
+ {
+ if ((width <= col) || (height <= col))
+ return T();
+ return matrix[col][row];
+ }
+ const QVector<T> GetRow(const unsigned row) const
+ {
+ QVector<T> r;
+ for (typename QVector< QVector<T> >::Iterator i = matrix.begin(); i != matrix.end(); ++i)
+ r.append(i->at(row));
+ return r;
+ }
+ const QVector<T> GetCol(const unsigned col) const
+ {
+ return matrix[col];
+ }
+ unsigned GetHeight() const
+ {
+ return height;
+ }
+ unsigned GetWidth() const
+ {
+ return width;
+ }
+ Point GetSize() const
+ {
+ return Point(width, height);
+ }
+
+ T& operator()(const Point& p)
+ {
+ return matrix.value[p.GetX()][p.GetY()];
+ }
+ const T& operator()(const Point& p) const
+ {
+ return matrix.value[p.GetX()][p.GetY()];
+ }
+ T& operator()(const unsigned col, const unsigned row)
+ {
+ return matrix[col][row];
+ }
+ const T& operator()(const unsigned col, const unsigned row) const
+ {
+ return matrix[col][row];
+ }
+ protected:
+ unsigned width;
+ unsigned height;
+ QVector< QVector<T> > matrix;
+ };
+
+ template<class T> class Matrix : public MatrixBase<T>
+ {
+ public:
+ Matrix() :
+ MatrixBase<T>()
+ {
+ }
+ Matrix(const unsigned w, const unsigned h, const T& d = T()) :
+ MatrixBase<T>(w, h, d)
+ {
+ }
+ Matrix(Ini& ini)
+ {
+ ImportIni(ini);
+ }
+
+ void ImportIni(Ini& ini)
+ {
+ ini.GetValue("width", MatrixBase<T>::width, 0);
+ ini.GetValue("height", MatrixBase<T>::height, 0);
+ if (!(MatrixBase<T>::width && MatrixBase<T>::height))
+ return;
+ MatrixBase<T>::matrix.resize(MatrixBase<T>::width, QVector<T>(MatrixBase<T>::height, 0));
+ for (unsigned i = 0; i < MatrixBase<T>::width; ++i)
+ {
+ for (unsigned j = 0; j < MatrixBase<T>::height; ++j)
+ {
+ int k;
+ ini.GetValue(QString("Element-%1-%2").arg(i).arg(j), k, 0);
+ MatrixBase<T>::matrix[i][j] = k;
+ }
+ }
+ }
+ void ExportIni(QFile& fout, const QString& val) const
+ {
+ // Declare the elements
+ Ini exMatrix(val);
+ exMatrix.AddField("height", MatrixBase<T>::height);
+ exMatrix.AddField("width", MatrixBase<T>::width);
+ for (int i = 0; i < MatrixBase<T>::width; ++i)
+ {
+ for (unsigned j = 0; j < MatrixBase<T>::height; ++j)
+ exMatrix.AddField(QString("Element-%1-%2").arg(i).arg(j), MatrixBase<T>::matrix[i][j]);
+ }
+ }
+ };
+}
+
+#endif