📄 tarray.cc
字号:
//// Project : threadpool// File : TArray.hh// Author : Ronald Kriemann// Purpose : class for a dyn. array of templates//// Copyright (C) 2003 - Ronald Kriemann//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA////// constructor with default size//template <class T> TArray<T>::TArray ( t_size size ) : _size( size ){ if ( _size < 0 ) _size = 0; if ( _size > 0 ) _data = new T[_size]; else _data = NULL;}//// set a new size but keep the old elements//template <class T> void TArray<T>::set_size ( t_size n ){ if ( n == _size ) return; if ( n < 0 ) n = 0; // // create new array and copy old data // (to be done: n <= 0) // t_size i; T * tmp = NULL; T * p1, * p2; if (n > 0) tmp = new T[n]; p1 = tmp; p2 = _data; if ( _size > n ) { for ( i = 0; i < n; i++ ) *p1++ = *p2++; }// if else { for ( i = 0; i < _size; i++ ) *p1++ = *p2++; // the rest of the new data is undefinded }// else delete[] _data; _data = tmp; _size = n;}//// copy operator//template <class T> TArray<T> & TArray<T>::operator = ( const TArray<T> & vec ){ t_size i; if (vec._size == _size) { T * p1 = _data; T * p2 = vec._data; for (i = 0; i < _size; i++) *p1++ = *p2++; }// if else { _size = vec._size; // set new size but no need for keeping old elements if (_data != NULL) delete[] _data; if (_size > 0) _data = new T[_size]; else _data = NULL; if (_size > 0) { T * p1 = _data; T * p2 = vec._data; for (i = 0; i < _size; i++) *p1++ = *p2++; }// if }// else return (*this);}template <class T>TArray<T> &TArray<T>::copy ( T * data, t_size size ){ if ( size < 0 ) size = 0; set_size( size ); for ( uint i = 0; i < size; i++ ) _data[i] = data[i]; return *this;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -