[cig-commits] r13249 - in cs/cigma/trunk: . src tests tests/libcigma

luis at geodynamics.org luis at geodynamics.org
Tue Nov 4 01:48:58 PST 2008


Author: luis
Date: 2008-11-04 01:48:56 -0800 (Tue, 04 Nov 2008)
New Revision: 13249

Added:
   cs/cigma/trunk/src/core_array.cpp
   cs/cigma/trunk/src/core_array.h
   cs/cigma/trunk/tests/libcigma/ArrayTest.cpp
   cs/cigma/trunk/tests/libcigma/ArrayTest.h
Modified:
   cs/cigma/trunk/Makefile.am
   cs/cigma/trunk/tests/test_registry.cpp
Log:
Core cigma::array object (refactored form {nc,eb}_array classes)

Modified: cs/cigma/trunk/Makefile.am
===================================================================
--- cs/cigma/trunk/Makefile.am	2008-11-04 09:48:53 UTC (rev 13248)
+++ cs/cigma/trunk/Makefile.am	2008-11-04 09:48:56 UTC (rev 13249)
@@ -96,6 +96,8 @@
 	src/Numeric.cpp \
 	src/DataPath.h \
 	src/DataPath.cpp \
+	src/core_array.h \
+	src/core_array.cpp \
 	src/Point.h \
 	src/Point.cpp \
 	src/Points.h \
@@ -300,6 +302,8 @@
 cigma_test_SOURCES = \
 	tests/simple/SimpleTest.h \
 	tests/simple/SimpleTest.cpp \
+	tests/libcigma/ArrayTest.h \
+	tests/libcigma/ArrayTest.cpp \
 	tests/libcigma/DataPathTest.h \
 	tests/libcigma/DataPathTest.cpp \
 	tests/libcigma/PointsTest.h \

Added: cs/cigma/trunk/src/core_array.cpp
===================================================================
--- cs/cigma/trunk/src/core_array.cpp	                        (rev 0)
+++ cs/cigma/trunk/src/core_array.cpp	2008-11-04 09:48:56 UTC (rev 13249)
@@ -0,0 +1,113 @@
+#include "core_array.h"
+#include <algorithm>    // std::copy
+using namespace cigma;
+
+template <typename T>
+array<T>::array()
+{
+    this->npts = 0;
+    this->ndim = 0;
+    this->_data = 0;
+}
+
+template <typename T>
+array<T>::array(int npts, int ndim)
+{
+    this->npts = npts;
+    this->ndim = ndim;
+    this->_data = 0;
+    if (size() > 0)
+    {
+        this->_data = new T[size()];
+    }
+}
+
+template <typename T>
+void array<T>::reinit(int npts, int ndim)
+{
+    if (_data != 0)
+    {
+        delete [] _data;
+    }
+
+    this->npts = npts;
+    this->ndim = ndim;
+    this->_data = 0;
+    if (size() > 0)
+    {
+        this->_data = new T[size()];
+    }
+}
+
+template <typename T>
+array<T>::array(const array<T>& other)
+{
+    this->npts = other.npts;
+    this->ndim = other.ndim;
+    this->_data = 0;
+    if (size() > 0)
+    {
+        _data = new T[size()];
+    }
+}
+
+template <typename T>
+array<T>& array<T>::operator=(const array<T>& other)
+{
+    if (this != &other)
+    {
+        T* new_data = new T[other.size()];
+        std::copy(other._data, other._data + other.size(), new_data);
+
+        if (this->_data != 0)
+        {
+            delete [] this->_data;
+        }
+
+        this->npts = other.npts;
+        this->ndim = other.ndim;
+        this->_data = new_data;
+    }
+    return *this;
+}
+
+template <typename T>
+array<T>::~array()
+{
+    this->npts = 0;
+    this->ndim = 0;
+    if (this->_data != 0)
+    {
+        delete [] this->_data;
+        this->_data = 0;
+    }
+}
+
+template <typename T>
+void array<T>::init_array(const T* xs)
+{
+    if (xs != 0)
+    {
+        for (int i = 0; i < size(); i++)
+        {
+            _data[i] = xs[i];
+        }
+    }
+}
+
+template <typename T>
+void array<T>::init_value(const T x)
+{
+    for (int i = 0; i < size(); i++)
+    {
+        _data[i] = x;
+    }
+}
+
+// ----------------------------------------------------------------------------
+
+template class cigma::array<int>;
+template class cigma::array<float>;
+template class cigma::array<double>;
+
+// ----------------------------------------------------------------------------

Added: cs/cigma/trunk/src/core_array.h
===================================================================
--- cs/cigma/trunk/src/core_array.h	                        (rev 0)
+++ cs/cigma/trunk/src/core_array.h	2008-11-04 09:48:56 UTC (rev 13249)
@@ -0,0 +1,100 @@
+#ifndef __CIGMA_ARRAY_H__
+#define __CIGMA_ARRAY_H__
+
+#include <cstddef>
+#include <utility>
+
+namespace cigma
+{
+    template <typename T> class array;
+}
+
+template <typename T>
+class cigma::array
+{
+public:
+    array();
+    array(int npts, int ndim);
+    void reinit(int m, int n);
+    array(const array& other);
+    array& operator=(const array& other);
+    ~array();
+
+    int n_points() const;
+    int n_dim() const;
+    std::size_t size() const;
+    std::pair<int,int> shape() const;
+
+    int index(int i, int j) const;
+    T data(int i, int j) const;
+    T operator()(int i, int j) const;
+
+    T& data(int i, int j);
+    T& operator()(int i, int j);
+
+    void init_array(const T* xs);
+    void init_value(const T x);
+
+public:
+    int npts;
+    int ndim;
+    T* _data;
+};
+
+
+template <typename T>
+inline int cigma::array<T>::n_points() const
+{
+    return npts;
+}
+
+template <typename T>
+inline int cigma::array<T>::n_dim() const
+{
+    return ndim;
+}
+
+template <typename T>
+inline size_t cigma::array<T>::size() const
+{
+    return npts * ndim;
+}
+
+template <typename T>
+inline std::pair<int,int> cigma::array<T>::shape() const
+{
+    return std::pair<int,int>(npts, ndim);
+}
+
+template <typename T>
+inline int cigma::array<T>::index(int i, int j) const
+{
+    return ndim*i + j;
+}
+
+template <typename T>
+inline T cigma::array<T>::data(int i, int j) const
+{
+    return _data[ndim*i + j];
+}
+
+template <typename T>
+inline T cigma::array<T>::operator()(int i, int j) const
+{
+    return _data[ndim*i + j];
+}
+
+template <typename T>
+inline T& cigma::array<T>::data(int i, int j)
+{
+    return _data[ndim*i + j];
+}
+
+template <typename T>
+inline T& cigma::array<T>::operator()(int i, int j)
+{
+    return _data[ndim*i + j];
+}
+
+
+#endif

Added: cs/cigma/trunk/tests/libcigma/ArrayTest.cpp
===================================================================
--- cs/cigma/trunk/tests/libcigma/ArrayTest.cpp	                        (rev 0)
+++ cs/cigma/trunk/tests/libcigma/ArrayTest.cpp	2008-11-04 09:48:56 UTC (rev 13249)
@@ -0,0 +1,20 @@
+#include "ArrayTest.h"
+using namespace libcigma;
+
+#include "core_array.h"
+using namespace cigma;
+
+void ArrayTest::test_something()
+{
+    cigma::array<double> a;
+    cigma::array<float> b(10,3);
+    cigma::array<int> c;
+
+
+    a = cigma::array<double>(100,2);
+    a.init_value(0);
+
+    c.reinit(22,8);
+    c.init_value(0);
+}
+

Added: cs/cigma/trunk/tests/libcigma/ArrayTest.h
===================================================================
--- cs/cigma/trunk/tests/libcigma/ArrayTest.h	                        (rev 0)
+++ cs/cigma/trunk/tests/libcigma/ArrayTest.h	2008-11-04 09:48:56 UTC (rev 13249)
@@ -0,0 +1,23 @@
+#ifndef __ARRAY_TEST_H__
+#define __ARRAY_TEST_H__
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+
+namespace libcigma
+{
+    class ArrayTest : public CPPUNIT_NS::TestFixture
+    {
+        CPPUNIT_TEST_SUITE(ArrayTest);
+        CPPUNIT_TEST(test_something);
+        CPPUNIT_TEST_SUITE_END();
+
+    public:
+        ArrayTest() {}
+        ~ArrayTest() {}
+        void test_something();
+    };
+};
+
+#endif

Modified: cs/cigma/trunk/tests/test_registry.cpp
===================================================================
--- cs/cigma/trunk/tests/test_registry.cpp	2008-11-04 09:48:53 UTC (rev 13248)
+++ cs/cigma/trunk/tests/test_registry.cpp	2008-11-04 09:48:56 UTC (rev 13249)
@@ -6,6 +6,9 @@
 #include <simple/SimpleTest.h>
 CPPUNIT_TEST_SUITE_REGISTRATION(SimpleTest);
 
+#include <libcigma/ArrayTest.h>
+CPPUNIT_TEST_SUITE_REGISTRATION(libcigma::ArrayTest);
+
 #include <libcigma/DataPathTest.h>
 CPPUNIT_TEST_SUITE_REGISTRATION(libcigma::DataPathTest);
 



More information about the CIG-COMMITS mailing list