2 * Copyright 2001, 2002 Adrian Thurston <thurston@complang.org>
5 /* This file is part of Aapl.
7 * Aapl is free software; you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free
9 * Software Foundation; either version 2.1 of the License, or (at your option)
12 * Aapl is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Aapl; if not, write to the Free Software Foundation, Inc., 59
19 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 * \brief Base class for dynamic arrays.
37 * Table is used as the common data storage class for vectors. It does not
38 * provide any methods to operate on the data and as such it is not intended
39 * to be used directly. It exists so that algorithms that operatate on dynamic
40 * arrays can be written without knowing about the various vector classes that
47 template <class T> class Table
50 /* Default Constructor. */
54 * \brief Get the length of the vector.
56 * \returns the length of the vector.
64 * The pointer to the elements in the vector. Modifying the vector may
65 * cause this pointer to change.
70 * \brief Table length.
72 * The number of items of type T in the table.
77 * \brief Allocated length.
79 * The number of items for which there is room in the current allocation.
85 * \brief Default constructor
87 * Initialize table data to empty.
89 template <class T> inline Table<T>::Table()
97 /* Default shared table header class. */
101 * \brief Table length.
103 * The number of items of type T in the table.
108 * \brief Allocated length.
110 * The number of items for which there is room in the current allocation.
117 * The number of shared vectors referencing this data.
128 * \brief Base class for implicitly shared dynamic arrays.
130 * STable is used as the common data storage class for shared vectors. It does
131 * not provide any methods to operate on the data and as such it is not
132 * intended to be used directly. It exists so that algorithms that operatate
133 * on dynamic arrays can be written without knowing about the various shared
134 * vector classes that my exist.
140 template <class T> class STable
143 /* Default Constructor. */
147 * \brief Get the length of the shared vector.
149 * \returns the length of the shared vector.
152 { return data == 0 ? 0 : (((STabHead*)data) - 1)->tabLen; }
155 * \brief Get header of the shared vector.
157 * \returns the header of the shared vector.
159 STabHead *header() const
160 { return data == 0 ? 0 : (((STabHead*)data) - 1); }
165 * The pointer to the elements in the vector. The shared table header is
166 * located just behind the data. Modifying the vector may cause this
173 * \brief Default constructor
175 * Initialize shared table data to empty.
177 template <class T> inline STable<T>::STable()
183 /* If needed is greater than existing, give twice needed. */
184 #define EXPN_UP( existing, needed ) \
185 needed > existing ? (needed<<1) : existing
187 /* If needed is less than 1 quarter existing, give twice needed. */
188 #define EXPN_DOWN( existing, needed ) \
189 needed < (existing>>2) ? (needed<<1) : existing
196 /** \class ResizeExpn
197 * \brief Exponential table resizer.
199 * ResizeExpn is the default table resizer. When an up resize is needed, space
200 * is doubled. When a down resize is needed, space is halved. The result is
201 * that when growing the vector in a linear fashion, the number of resizes of
202 * the allocated space behaves logarithmically.
204 * If only up resizes are done, there will never be more than 2 times the
205 * needed space allocated. If down resizes are done as well, there will never
206 * be more than 4 times the needed space allocated. ResizeExpn uses this 50%
207 * usage policy on up resizing and 25% usage policy on down resizing to
208 * improve performance when repeatedly inserting and removing a small number
209 * of elements relative to the size of the array. This scheme guarantees that
210 * repetitive inserting and removing of a small number of elements will never
211 * result in repetative reallocation.
213 * The sizes passed to the resizer from the vectors are in units of T.
218 /* Exponential resizer. */
223 * \brief Determine the new table size when up resizing.
225 * If the existing size is insufficient for the space needed then allocate
226 * twice the space needed. Otherwise use the existing size.
228 * \returns The new table size.
230 static inline long upResize( long existing, long needed )
231 { return EXPN_UP( existing, needed ); }
234 * \brief Determine the new table size when down resizing.
236 * If the space needed is less than one quarter of the existing size then
237 * allocate twice the space needed. Otherwise use the exitsing size.
239 * \returns The new table size.
241 static inline long downResize( long existing, long needed )
242 { return EXPN_DOWN( existing, needed ); }
248 #ifdef AAPL_NAMESPACE
252 #endif /* _AAPL_TABLE_H */