Generic class libraries for 1D array and 2D array: Array and Matrix. 1次元および2次元配列用の汎用クラスライブラリ: Array と Matrix.

Dependents:   F746_SD_WavPlayer CW_Decoder_using_FFT_on_F446 F446_MySoundMachine F446_ADF_Nlms ... more

Files at this revision

API Documentation at this revision

Comitter:
MikamiUitOpen
Date:
Sun May 22 06:14:18 2016 +0000
Child:
1:54b07f0d5ba1
Commit message:
1

Changed in this revision

Array.hpp Show annotated file Show diff for this revision Revisions of this file
Matrix.hpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Array.hpp	Sun May 22 06:14:18 2016 +0000
@@ -0,0 +1,121 @@
+//-----------------------------------------------------------------------
+//  Generic Array class
+//      If you define "#define DEBUG_ARRAY_CHECK",
+//      range check of index is available.
+//
+//  2016/05/22, Copyright (c) 2016 MIKAMI, Naoki
+//-----------------------------------------------------------------------
+
+#include "mbed.h"
+#include <new>          // for new, delete, and set_new_handler()
+
+#ifndef MIKAMI_ARRAY_HPP
+#define MIKAMI_ARRAY_HPP
+
+namespace Mikami
+{
+    template <class T> class Array
+    {
+    public:
+        explicit Array(int i = 1) { ArrayNew(i); }  // default constructor
+        Array(const Array<T>& a) { Copy(a); }       // copy constructor
+        inline Array(int i, T initialValue);        // constructor with initialization
+        ~Array() { delete[] v_; }                   // destructor
+        inline void Fill(T val);                    // fill with same value
+        void SetSize(int i);                        // setting size
+        int Length() const { return size_; }        // get size of array
+        Array<T>& operator=(const Array<T>& a);     // assignment
+        inline T& operator[](int i);                // assignment by element
+        inline const T& operator[](int n) const;    // get element
+        operator const T* () const { return v_; }   // type conversion
+        operator T* () const { return v_; }         // type conversion
+
+    private:
+        T    *v_;
+        int  size_;                                 // size of array
+
+        void Range(int pos) const;                  // range checking for Array
+        void Copy(const Array<T>& v_src);           // copy of object
+        inline void ArrayNew(const int i);          // routine for constructor
+        static void MemoryAssignError();            // error message
+    };
+
+//-----------------------------------------------------------------------
+// implementation of generic array class
+//-----------------------------------------------------------------------
+
+    // constructor with initialization
+    template <class T> inline Array<T>::Array(int i, T initialValue)
+    {
+        ArrayNew(i);
+        Fill(initialValue);
+    }
+
+    template <class T> void Array<T>::SetSize(int i)
+    {
+        delete[] v_;
+        v_ = new T[size_ = i];
+    }
+
+    // fill with same value
+    template <class T> void Array<T>::Fill(T val)
+    {
+        for (int n=0; n<size_; n++) v_[n] = val;        
+    }
+                                     
+    template <class T> Array<T>& Array<T>::operator=(const Array<T>& a)
+    {
+        if (this != &a)                     // prohibition of self-assignment
+        {
+            delete [] v_;
+            Copy(a);
+        }
+        return *this;
+    }
+
+    template <class T> inline T& Array<T>::operator[](int i)
+    {
+    #ifdef DEBUG_ARRAY_CHECK
+        Range(i);                               // out of bound ?
+    #endif
+        return v_[i];
+    }
+
+    template <class T> inline const T& Array<T>::operator[](int i) const
+    {
+    #ifdef DEBUG_ARRAY_CHECK
+        Range(i);                               // out of bounds ?
+    #endif
+        return v_[i];
+    }
+
+    template <class T> void Array<T>::Range(int pos) const
+    {
+        if ((pos < 0) || (pos >= size_))
+        {
+            fprintf(stderr, "\r\nOut of range\r\n");
+            while (true);
+        }            
+    }
+
+    template <class T> void Array<T>::Copy(const Array<T>& v_src)
+    {
+        v_ = new T[size_ = v_src.size_];
+        for (int i=0; i<size_; i++) v_[i] = v_src.v_[i];
+    }
+
+    // routine for constructor
+    template <class T> void Array<T>::ArrayNew(int i)
+    {
+        set_new_handler(Array<T>::MemoryAssignError);
+        v_ = new T[size_ = i];
+    }
+
+    // Message of "Can't allocate to memory!"
+    template <class T> void Array<T>::MemoryAssignError()
+    {
+        fprintf(stderr, "Can't allocate to memory!\r\n");
+        while(true);
+    }
+}
+#endif  // MIKAMI_ARRAY_HPP
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Matrix.hpp	Sun May 22 06:14:18 2016 +0000
@@ -0,0 +1,58 @@
+//------------------------------------------------------------------------------
+//  Generic 2-dimensional array class ---- Matrix
+//
+//  2016/05/22, Copyright (c) 2016 MIKAMI, Naoki
+//------------------------------------------------------------------------------
+
+#ifndef MIKAMI_MATRIX_HPP
+#define MIKAMI_MATRIX_HPP
+
+#include "Array.hpp"
+
+namespace Mikami
+{
+    template <class T> class Matrix
+    {
+    public:
+        explicit Matrix(int rows = 1, int cols = 1);
+        Matrix(int rows, int cols, T initialValue);
+        void Fill(T val);
+        void SetSize(int rows, int cols);                           // setting size
+        Array<T>& operator[](int i) { return vv_[i]; }              // assign by element
+        const Array<T>& operator[](int i) const { return vv_[i]; }  //get element
+        int Rows() const { return vv_.Length(); }
+        int Cols() const { return vv_[0].Length(); }
+    private:
+        Array< Array<T> > vv_;
+    };
+
+//-----------------------------------------------------------------------
+// implementation of generic 2-dimensional array class
+//-----------------------------------------------------------------------
+    template <class T> Matrix<T>::Matrix(int rows, int cols): vv_(rows)
+    {
+        Array<T> a(cols);
+        for (int i=0; i<rows; i++) vv_[i] = a;
+    }
+
+    template <class T> Matrix<T>::Matrix(int rows, int cols, T initialValue)
+            : vv_(rows)
+    {
+        Array<T> a(cols, initialValue);
+        for (int i=0; i<rows; i++) vv_[i] = a;
+    }
+
+    template <class T> void Matrix<T>::Fill(T val)
+    {
+        for (int n=0; n<Rows(); n++) vv_[n].Fill(val);
+    }
+
+    template <class T> void Matrix<T>::SetSize(int rows, int cols)
+    {
+        Array<T> a(cols);
+        vv_.SetSize(rows);
+        for (int i=0; i<rows; i++) vv_[i] = a;
+    }
+}
+#endif  // MIKAMI_MATRIX_HPP
+