?? array.h
字號:
/* 2008 (c) Dorival M. Pedroso */#ifndef MPM_ARRAY_H#define MPM_ARRAY_H// STL#include <algorithm> // for std::find, std::min_element, and std::max_element// MechSys#include "fatal.h"#include "fmtnum.h"template<typename Value_T>class Array{public: // Constructors Array () : _values(NULL), _fn(&_8s) { Resize(0, 2); } ///< Constructor Array (size_t Size) : _values(NULL), _fn(&_8s) { Resize(Size); } ///< Alternative constructor Array (Array<Value_T> const & Other); ///< Copy constructor (needed when using Array< Array<...> >) /** Destructor. */ ~Array() { if (_values!=NULL) delete [] _values; } // Methods void SetFN (FmtNum & FN) { _fn = &FN; } ///< Set the FmtNum, a structure to aid format output of numbers FmtNum const & FN () const { return (*_fn); } ///< Return the FmtNum, a structure to aid format output of numbers size_t Size () const { return _size; } ///< Returns the size Value_T * GetPtr () { return _values; } ///< Returns a pointer to the values (write) Value_T const * GetPtr () const { return _values; } ///< Returns a pointer to the values (read) void Resize (size_t Size, double SzFactor=1.2); ///< Resize the array void Push (Value_T const & Value, double SzFactor=1.2); ///< Add a new entry increasing the size if necessary void Remove (size_t i, size_t Length=1); ///< Remove item i from the array long Find (Value_T const & Value) const; ///< Find a value: returns -1 if not found, otherwise, returns the index of the element found long Min () const; ///< Find the minimum value: returns the index of the minimum element long Max () const; ///< Find the maximum value: returns the index of the maximum element // Operators Value_T & operator[] (size_t i); ///< Access operator (write) Value_T const & operator[] (size_t i) const; ///< Access operator (read) void operator= (Array<Value_T> const & R); ///< Assignment operator (needed when using Array< Array<...> >) void operator+= (Array<Value_T> const & R); ///< Plus-assignment operator void operator-= (Array<Value_T> const & R); ///< Minus-assignment operator void operator= (Value_T const & V); ///< Set all values equal to Vprivate: // Variables size_t _size; ///< Current number of components size_t _space; ///< Available space Value_T * _values; ///< Space to hold all values FmtNum * _fn; ///< Structure to aid format output of numbers}; // class Array/////////////////////////////////////////////////////////////////////////////////////////// Implementation /////// Constructorstemplate<typename Value_T>inline Array<Value_T>::Array(Array<Value_T> const & Other) : _values(NULL){ Resize(Other.Size()); for (size_t i=0; i<_size; ++i) _values[i] = Other[i];}// Methodstemplate<typename Value_T>inline void Array<Value_T>::Resize(size_t Size, double SzFactor){#ifndef NDEBUG // Check if (Size<0) throw new Fatal("Array::Resize size==%d must be positive", Size);#endif // Clear previous memory if (_values!=NULL) delete [] _values; // Allocate new memory _size = Size; _space = static_cast<size_t>((_size+1)*SzFactor); _values = new Value_T [_space];}template<typename Value_T>inline void Array<Value_T>::Push(Value_T const & Value, double SzFactor){ if (_size==_space) { size_t oldsz = _size; Value_T * tmp = new Value_T [oldsz]; for (size_t i=0; i<oldsz; ++i) tmp[i] = _values[i]; Resize(oldsz+1, SzFactor); for (size_t i=0; i<oldsz; ++i) _values[i] = tmp[i]; delete [] tmp; } else _size++; _values[_size-1] = Value;}template<typename Value_T>inline void Array<Value_T>::Remove(size_t i, size_t Length){ size_t oldsz = _size; Value_T * tmp = new Value_T [oldsz]; for (size_t j=0; j<oldsz; ++j) tmp[j] = _values[j]; Resize(_size-Length); size_t k = 0; for (size_t j=0; j<i; ++j) { _values[k]=tmp[j]; k++; } for (size_t j=i+Length; j<oldsz; ++j) { _values[k]=tmp[j]; k++; } delete [] tmp;}template<typename Value_T>inline long Array<Value_T>::Find(Value_T const & Value) const{ Value_T * res = std::find(_values, _values+_size, Value); if (res==_values+_size) return -1; else return res-_values;}template<typename Value_T>inline long Array<Value_T>::Min() const{ Value_T * res = std::min_element(_values, _values+_size); return res-_values;}template<typename Value_T>inline long Array<Value_T>::Max() const{ Value_T * res = std::max_element(_values, _values+_size); return res-_values;}// Operatorstemplate<typename Value_T>inline Value_T & Array<Value_T>::operator[] (size_t i){#ifndef NDEBUG if (i<0 || i>=_size) throw new Fatal("Array::operator[] (write) Subscript==%d (size==%d) is out of range.", i, _size);#endif return _values[i];}template<typename Value_T>inline Value_T const & Array<Value_T>::operator[] (size_t i) const{#ifndef NDEBUG if (i<0 || i>=_size) throw new Fatal("Array::operator[] (read) Subscript==%d (size==%d) is out of range.", i, _size);#endif return _values[i];}template<typename Value_T>inline void Array<Value_T>::operator= (Array<Value_T> const & R){#ifndef DNDEBUG if (&R==this) throw new Fatal("Array::operator= The right-hand-size of this operation (LHS = RHS) must not be equal to the LHS.");#endif // Reallocate if they are different (LHS != RHS) if (_size!=R.Size()) Resize(R.Size()); // Copy values for (size_t i=0; i<_size; ++i) _values[i] = R[i];}template<typename Value_T>inline void Array<Value_T>::operator+= (Array<Value_T> const & R){#ifndef DNDEBUG if (_size!=R.Size()) throw new Fatal("Array::operator+= The number of components of the LHS (%d) must be equal to the number of components of the RHS (%d).",_size,R.Size());#endif // Add values for (int i=0; i<_size; ++i) _values[i] += R[i];}template<typename Value_T>inline void Array<Value_T>::operator-= (Array<Value_T> const & R){#ifndef DNDEBUG if (_size!=R.Size()) throw new Fatal("Array::operator-= The number of components of the LHS (%d) must be equal to the number of components of the RHS (%d).",_size,R.Size());#endif // Subtract values for (int i=0; i<_size; ++i) _values[i] -= R[i];}template<typename Value_T>inline void Array<Value_T>::operator= (Value_T const & V){ // Set all values equal to V for (size_t i=0; i<_size; ++i) _values[i] = V;}/** Outputs an array. */template<typename Value_T>std::ostream & operator<< (std::ostream & os, const Array<Value_T> & V){ for (size_t i=0; i<V.Size(); ++i) os << V.FN()<< V[i]; os << std::endl; return os;}#endif // MPM_ARRAY_H/* 2008 (c) Dorival M. Pedroso */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -