summaryrefslogtreecommitdiff
path: root/tools/swig/test/Farray.cxx
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2014-03-12 11:19:40 -0600
committerCharles Harris <charlesr.harris@gmail.com>2014-03-12 11:26:48 -0600
commita38888c18cd2a20de0eb0578b3fa8660cda79582 (patch)
tree4f0590684328a013544de84b1577f9322db4cbac /tools/swig/test/Farray.cxx
parent4fd4850d6b8bb9a8837e19b7ef2b38d0cd67fdd1 (diff)
downloadnumpy-a38888c18cd2a20de0eb0578b3fa8660cda79582.tar.gz
MAINT: Move doc/swig to tools/swig.
Also update MANIFEST.in and documentation to reflect the move. The discussion of this change is at #2384. Closes #2384. Closes #4374.
Diffstat (limited to 'tools/swig/test/Farray.cxx')
-rw-r--r--tools/swig/test/Farray.cxx122
1 files changed, 122 insertions, 0 deletions
diff --git a/tools/swig/test/Farray.cxx b/tools/swig/test/Farray.cxx
new file mode 100644
index 000000000..3983c333b
--- /dev/null
+++ b/tools/swig/test/Farray.cxx
@@ -0,0 +1,122 @@
+#include "Farray.h"
+#include <sstream>
+
+// Size constructor
+Farray::Farray(int nrows, int ncols) :
+ _nrows(nrows), _ncols(ncols), _buffer(0)
+{
+ allocateMemory();
+}
+
+// Copy constructor
+Farray::Farray(const Farray & source) :
+ _nrows(source._nrows), _ncols(source._ncols)
+{
+ allocateMemory();
+ *this = source;
+}
+
+// Destructor
+Farray::~Farray()
+{
+ delete [] _buffer;
+}
+
+// Assignment operator
+Farray & Farray::operator=(const Farray & source)
+{
+ int nrows = _nrows < source._nrows ? _nrows : source._nrows;
+ int ncols = _ncols < source._ncols ? _ncols : source._ncols;
+ for (int i=0; i < nrows; ++i)
+ {
+ for (int j=0; j < ncols; ++j)
+ {
+ (*this)(i,j) = source(i,j);
+ }
+ }
+ return *this;
+}
+
+// Equals operator
+bool Farray::operator==(const Farray & other) const
+{
+ if (_nrows != other._nrows) return false;
+ if (_ncols != other._ncols) return false;
+ for (int i=0; i < _nrows; ++i)
+ {
+ for (int j=0; j < _ncols; ++j)
+ {
+ if ((*this)(i,j) != other(i,j)) return false;
+ }
+ }
+ return true;
+}
+
+// Length accessors
+int Farray::nrows() const
+{
+ return _nrows;
+}
+
+int Farray::ncols() const
+{
+ return _ncols;
+}
+
+// Set item accessor
+long & Farray::operator()(int i, int j)
+{
+ if (i < 0 || i > _nrows) throw std::out_of_range("Farray row index out of range");
+ if (j < 0 || j > _ncols) throw std::out_of_range("Farray col index out of range");
+ return _buffer[offset(i,j)];
+}
+
+// Get item accessor
+const long & Farray::operator()(int i, int j) const
+{
+ if (i < 0 || i > _nrows) throw std::out_of_range("Farray row index out of range");
+ if (j < 0 || j > _ncols) throw std::out_of_range("Farray col index out of range");
+ return _buffer[offset(i,j)];
+}
+
+// String output
+std::string Farray::asString() const
+{
+ std::stringstream result;
+ result << "[ ";
+ for (int i=0; i < _nrows; ++i)
+ {
+ if (i > 0) result << " ";
+ result << "[";
+ for (int j=0; j < _ncols; ++j)
+ {
+ result << " " << (*this)(i,j);
+ if (j < _ncols-1) result << ",";
+ }
+ result << " ]";
+ if (i < _nrows-1) result << "," << std::endl;
+ }
+ result << " ]" << std::endl;
+ return result.str();
+}
+
+// Get view
+void Farray::view(int* nrows, int* ncols, long** data) const
+{
+ *nrows = _nrows;
+ *ncols = _ncols;
+ *data = _buffer;
+}
+
+// Private methods
+void Farray::allocateMemory()
+{
+ if (_nrows <= 0) throw std::invalid_argument("Farray nrows <= 0");
+ if (_ncols <= 0) throw std::invalid_argument("Farray ncols <= 0");
+ _buffer = new long[_nrows*_ncols];
+}
+
+inline int Farray::offset(int i, int j) const
+{
+ return i + j * _nrows;
+}