summaryrefslogtreecommitdiffstats
path: root/pokemod/Matrix.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'pokemod/Matrix.cpp')
-rw-r--r--pokemod/Matrix.cpp235
1 files changed, 235 insertions, 0 deletions
diff --git a/pokemod/Matrix.cpp b/pokemod/Matrix.cpp
new file mode 100644
index 00000000..96550a25
--- /dev/null
+++ b/pokemod/Matrix.cpp
@@ -0,0 +1,235 @@
+/////////////////////////////////////////////////////////////////////////////
+// Name: Matrix.cpp
+// Purpose: A 2D vector class
+// Author: Ben Boeckel
+// Modified by: Ben Boeckel
+// Created: Sun Apr 8 12:51:20 2007
+// Copyright: ©2007 Ben Boeckel and 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 2 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, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+/////////////////////////////////////////////////////////////////////////////
+
+#include "Matrix.h"
+
+template<class T>
+PokeMod::Matrix<T>::Matrix(unsigned w, unsigned h, T &d)
+{
+ matrix.resize(w, std::vector<T>(h, d));
+ width = w;
+ height = h;
+}
+
+template<class T>
+PokeMod::Matrix<T>::Matrix(XmlElement &x)
+{
+ ImportXml(xml);
+}
+
+template<class T>
+void PokeMod::Matrix<T>::AddRow(T &d)
+{
+ for (std::vector<T> *i = matrix.begin(); i != matrix.end(); ++i)
+ i->push_back(d);
+ ++height;
+}
+
+template<class T>
+void PokeMod::Matrix<T>::ImportXml(XmlElement &xml)
+{
+ LogImportStart("Matrix");
+ String curName;
+ width = 0;
+ height = 0;
+ bool haveHeight = false;
+ bool haveWidth = false;
+ xml.ClearCounter();
+ for (XmlElement *child = xml.NextElement(); child; child = xml.NextElement())
+ {
+ curName = child->GetName();
+ if (curName == "height")
+ {
+ child->GetValue(height);
+ if (height)
+ haveHeight = true;
+ }
+ else if (curName == "width")
+ {
+ if (haveHeight)
+ {
+ child->GetValue(width);
+ if (width)
+ haveWidth = true;
+ matrix.resize(w, std::vector<T>(h, T()));
+ }
+ else
+ Log("Matrix Import: Elements out of order", PM_DEBUG_ERROR);
+ }
+ else if (curName == "col")
+ {
+ if (height && width)
+ {
+ unsigned colNum;
+ unsigned rowNum;
+ child->GetValue(colNum);
+ if (colNum < width)
+ {
+ child->ClearCounter();
+ for (XmlElement *item = child->NextElement(); item; item = child->NextElement())
+ {
+ item->GetValue(rowNum);
+ if ((item->GetName() == "item") && (rowNum < height))
+ matrix[colNum][rowNum].ImportXml(*item);
+ else
+ Log("Matrix Import: Item out of bounds", PM_DEBUG_ERROR);
+ }
+ }
+ else
+ Log("Matrix Import: Column out of bounds", PM_DEBUG_ERROR);
+ }
+ else
+ Log("Matrix Import: Elements out of order", PM_DEBUG_ERROR);
+ }
+ else
+ LogUnknownXml("Matrix", curName);
+ }
+ LogImportOver("Matrix", id, name);
+}
+
+template<class T>
+PokeMod::XmlElement PokeMod::Matrix<T>::ExportXml(const String &val)
+{
+ Log(String("Matrix Export: Starting %s", val.c_str()), PM_DEBUG_INFO);
+ // Declare the elements
+ XmlElement exMatrix(val, 0, true);
+ exMatrix.AddElement("height", height);
+ exMatrix.AddElement("width", width);
+ for (int i = 0; i < width; ++i)
+ {
+ XmlElement col("column", i, true);
+ for (int j = 0; j < height; ++j)
+ {
+ XmlElement item("item", j, true);
+ item.AddElement(matrix[i][j].ExportXml());
+ }
+ exMatrix.AddElement(col);
+ }
+ Log(String("Matrix Export: Finished %s", val.c_str()), PM_DEBUG_INFO);
+ return exMatrix;
+}
+
+template<class T>
+void PokeMod::Matrix<T>::AddCol(T &d)
+{
+ matrix.push_back(std::vector<T>(GetHeight(), d));
+ ++width;
+}
+
+template<class T>
+bool PokeMod::Matrix<T>::InsertRow(unsigned pos, T &d)
+{
+ if (height < pos)
+ return false;
+ for (std::vector<T> *i = matrix.begin(); i != matrix.end(); ++i)
+ i->insert(pos, d);
+ ++height;
+ return true;
+}
+
+template<class T>
+bool PokeMod::Matrix<T>::InsertCol(unsigned pos, T &d)
+{
+ if (width < pos)
+ return false;
+ matrix.insert(pos, std::vector<T>(GetHeight(), d));
+ ++width;
+ return true;
+}
+
+template<class T>
+bool PokeMod::Matrix<T>::DeleteRow(unsigned pos)
+{
+ if (height <= pos)
+ return false;
+ for (std::vector<T> *i = matrix.begin(); i != matrix.end(); ++i)
+ i->erase(i->begin() + pos);
+ --height;
+ return true;
+}
+
+template<class T>
+bool PokeMod::Matrix<T>::DeleteCol(unsigned pos)
+{
+ if (width <= pos)
+ return false;
+ matrix.erase(matrix.begin() + pos);
+ --width;
+ return true;
+}
+
+template<class T>
+bool PokeMod::Matrix<T>::Set(unsigned row, unsigned col, T &s)
+{
+ if ((width <= col) || (height <= col))
+ return false;
+ (matrix.begin() + col)->assign(row, s);
+ return true;
+}
+
+template<class T>
+T PokeMod::Matrix<T>::Get(unsigned row, unsigned col)
+{
+ if ((width <= col) || (height <= col))
+ return T();
+ return matrix[col][row];
+}
+
+template<class T>
+std::vector<T> PokeMod::Matrix<T>::GetRow(unsigned row)
+{
+ std::vector<T> r;
+ for (std::vector<T> *i = matrix.begin(); i != matrix.end(); ++i)
+ r.push_back(i->at(row));
+ return r;
+}
+
+template<class T>
+std::vector<T> PokeMod::Matrix<T>::GetCol(unsigned col)
+{
+ return matrix[col];
+}
+
+template<class T>
+unsigned PokeMod::Matrix<T>::GetHeight()
+{
+ return height;
+}
+
+template<class T>
+unsigned PokeMod::Matrix<T>::GetWidth()
+{
+ return width;
+}
+
+template<class T>
+T PokeMod::Matrix<T>::operator[](Point &p)
+{
+ return matrix[p.GetY()][p.GetX()];
+}
+
+template<class T>
+std::vector<T> PokeMod::Matrix<T>::operator[](int col)
+{
+ return matrix[col];
+}