Spectral analysis
Fork of Array_Matrix by
Array.hpp
- Committer:
- MikamiUitOpen
- Date:
- 2016-05-22
- Revision:
- 0:efe9b1f01090
- Child:
- 1:54b07f0d5ba1
File content as of revision 0:efe9b1f01090:
//----------------------------------------------------------------------- // 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