1 | /***************************************************************************** |
2 | |
3 | DynArray.hpp |
4 | Copyright (c) 2005 Laurent de Soras |
5 | |
6 | --- Legal stuff --- |
7 | |
8 | This library is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU Lesser General Public |
10 | License as published by the Free Software Foundation; either |
11 | version 2.1 of the License, or (at your option) any later version. |
12 | |
13 | This library is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | Lesser General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Lesser General Public |
19 | License along with this library; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 | |
22 | *Tab=3***********************************************************************/ |
23 | |
24 | |
25 | |
26 | #if defined (DynArray_CURRENT_CODEHEADER) |
27 | #error Recursive inclusion of DynArray code header. |
28 | #endif |
29 | #define |
30 | |
31 | #if ! defined (DynArray_CODEHEADER_INCLUDED) |
32 | #define |
33 | |
34 | |
35 | |
36 | /*\\\ INCLUDE FILES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/ |
37 | |
38 | #include <cassert> |
39 | |
40 | |
41 | |
42 | /*\\\ PUBLIC \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/ |
43 | |
44 | |
45 | |
46 | template <class T> |
47 | DynArray <T>::DynArray () |
48 | : _data_ptr (0) |
49 | , _len (0) |
50 | { |
51 | // Nothing |
52 | } |
53 | |
54 | |
55 | |
56 | template <class T> |
57 | DynArray <T>::DynArray (long size) |
58 | : _data_ptr (0) |
59 | , _len (0) |
60 | { |
61 | assert (size >= 0); |
62 | if (size > 0) |
63 | { |
64 | _data_ptr = new DataType [size]; |
65 | _len = size; |
66 | } |
67 | } |
68 | |
69 | |
70 | |
71 | template <class T> |
72 | DynArray <T>::~DynArray () |
73 | { |
74 | delete [] _data_ptr; |
75 | _data_ptr = 0; |
76 | _len = 0; |
77 | } |
78 | |
79 | |
80 | |
81 | template <class T> |
82 | long DynArray <T>::size () const |
83 | { |
84 | return (_len); |
85 | } |
86 | |
87 | |
88 | |
89 | template <class T> |
90 | void DynArray <T>::resize (long size) |
91 | { |
92 | assert (size >= 0); |
93 | if (size > 0) |
94 | { |
95 | DataType * old_data_ptr = _data_ptr; |
96 | DataType * tmp_data_ptr = new DataType [size]; |
97 | |
98 | _data_ptr = tmp_data_ptr; |
99 | _len = size; |
100 | |
101 | delete [] old_data_ptr; |
102 | } |
103 | } |
104 | |
105 | |
106 | |
107 | template <class T> |
108 | const typename DynArray <T>::DataType & DynArray <T>::operator [] (long pos) const |
109 | { |
110 | assert (pos >= 0); |
111 | assert (pos < _len); |
112 | |
113 | return (_data_ptr [pos]); |
114 | } |
115 | |
116 | |
117 | |
118 | template <class T> |
119 | typename DynArray <T>::DataType & DynArray <T>::operator [] (long pos) |
120 | { |
121 | assert (pos >= 0); |
122 | assert (pos < _len); |
123 | |
124 | return (_data_ptr [pos]); |
125 | } |
126 | |
127 | |
128 | |
129 | /*\\\ PROTECTED \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/ |
130 | |
131 | |
132 | |
133 | /*\\\ PRIVATE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/ |
134 | |
135 | |
136 | |
137 | #endif // DynArray_CODEHEADER_INCLUDED |
138 | |
139 | #undef DynArray_CURRENT_CODEHEADER |
140 | |
141 | |
142 | |
143 | /*\\\ EOF \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/ |
144 | |