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

Revision:
3:d9dea7748b27
Parent:
2:a25dba17218c
Child:
4:d3aa1ddb57e1
--- a/Array.hpp	Mon Aug 15 07:11:16 2016 +0000
+++ b/Array.hpp	Tue Nov 19 05:44:54 2019 +0000
@@ -1,13 +1,13 @@
 //-----------------------------------------------------------------------
 //  Generic Array class
-//      If you define "#define DEBUG_ARRAY_CHECK",
-//      range check of index is available.
+//      Macro definition "DEBUG_ARRAY_CHECK" enables to check
+//      range of index.
 //
-//  2016/08/15, Copyright (c) 2016 MIKAMI, Naoki
+//  2019/11/19, Copyright (c) 2019 MIKAMI, Naoki
 //-----------------------------------------------------------------------
 
 #include "mbed.h"
-#include <new>          // for new, delete, and set_new_handler()
+#include <new>          // for Rev.122 or before revision of Mbed official library
 
 #ifndef MIKAMI_ARRAY_HPP
 #define MIKAMI_ARRAY_HPP
@@ -17,29 +17,28 @@
     template <class T> class Array
     {
     public:
-        explicit Array(int i = 1) { ArrayNew(i); }  // default constructor
+        explicit Array(int n = 1) { ArrayNew(n); }  // default constructor
         Array(const Array<T>& a) { Copy(a); }       // copy constructor
-        inline Array(int i, T initialValue);        // constructor with initialization
-        inline Array(int i, const T val[]);         // constructor with assignment built-in array
+        Array(int n, T initialValue);               // constructor with initialization
+        Array(int n, const T val[]);                // constructor with assignment built-in array
         ~Array() { delete[] v_; }                   // destructor
-        inline void Fill(T val);                    // fill with same value
-        inline void Assign(const T val[]);          // assign built-in array
-        void SetSize(int i);                        // setting size
+        void Fill(T val);                           // fill with same value
+        void Assign(const T val[]);                 // assign built-in array
+        void SetSize(int n);                        // 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
+        T& operator[](int n);                       // non-const [] operator
+        const T& operator[](int n) const;           // const [] operator
+        operator T* () const { return v_; }         // non-const type conversion
+        operator const T* () const { return v_; }   // const type conversion
 
     private:
         T    *v_;
-        int  size_;                                 // size of array
+        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
+        void Range(int pos) const;          // range checking for Array
+        void Copy(const Array<T>& v_src);   // copy of object
+        void ArrayNew(const int n);         // routine for constructor
     };
 
 //-----------------------------------------------------------------------
@@ -47,38 +46,38 @@
 //-----------------------------------------------------------------------
 
     // constructor with initialization
-    template <class T> Array<T>::Array(int i, T initialValue)
+    template <class T> inline Array<T>::Array(int n, T initialValue)
     {
-        ArrayNew(i);
+        ArrayNew(n);
         Fill(initialValue);
     }
                                      
     // constructor with assignment built-in array
-    template <class T> Array<T>::Array(int i, const T val[])
+    template <class T> inline Array<T>::Array(int n, const T val[])
     {
-        ArrayNew(i);
+        ArrayNew(n);
         Assign(val);
     }
 
-    template <class T> void Array<T>::SetSize(int i)
+    template <class T> inline void Array<T>::SetSize(int n)
     {
         delete[] v_;
-        v_ = new T[size_ = i];
+        v_ = new T[size_ = n];
     }
 
     // fill with same value
-    template <class T> void Array<T>::Fill(T val)
+    template <class T> inline void Array<T>::Fill(T val)
     {
         for (int n=0; n<size_; n++) v_[n] = val;        
     }
 
     // assign built-in array
-    template <class T> void Array<T>::Assign(const T val[])
+    template <class T> inline void Array<T>::Assign(const T val[])
     {
         for (int n=0; n<size_; n++) v_[n] = val[n];        
     }          
 
-    template <class T> Array<T>& Array<T>::operator=(const Array<T>& a)
+    template <class T> inline Array<T>& Array<T>::operator=(const Array<T>& a)
     {
         if (this != &a) // prohibition of self-assignment
         {
@@ -88,49 +87,38 @@
         return *this;
     }
 
-    template <class T> inline T& Array<T>::operator[](int i)
+    template <class T> inline T& Array<T>::operator[](int n)
     {
     #ifdef DEBUG_ARRAY_CHECK
-        Range(i);       // out of bound ?
+        Range(n);       // out of bound ?
     #endif
-        return v_[i];
+        return v_[n];
     }
 
-    template <class T> inline const T& Array<T>::operator[](int i) const
+    template <class T> inline const T& Array<T>::operator[](int n) const
     {
     #ifdef DEBUG_ARRAY_CHECK
-        Range(i);       // out of bounds ?
+        Range(n);       // out of bounds ?
     #endif
-        return v_[i];
+        return v_[n];
     }
 
     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);
-        }            
+            mbed_assert_internal("Out of range", __FILE__, __LINE__);   // mbed_assert.h
     }
 
-    template <class T> void Array<T>::Copy(const Array<T>& v_src)
+    template <class T> inline void Array<T>::Copy(const Array<T>& v_src)
     {
         v_ = new T[size_ = v_src.size_];
         for (int n=0; n<size_; n++) v_[n] = v_src.v_[n];
     }
 
     // routine for constructor
-    template <class T> void Array<T>::ArrayNew(int i)
+    template <class T> inline void Array<T>::ArrayNew(int n)
     {
-        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);
+        v_ = new T[size_ = n];
     }
 }
-#endif  // MIKAMI_ARRAY_HPP
+#endif  // MIKAMI_ARRAY_HPP
\ No newline at end of file