In the variable statement changed the name "curstate" to "cs" (the default
[ragel.git] / aapl / table.h
blobc1f2b7bdf7578851f9e4b794d38c13a3093395bb
1 /*
2 * Copyright 2001, 2002 Adrian Thurston <thurston@cs.queensu.ca>
3 */
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)
10 * any later version.
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
15 * more details.
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
22 #ifndef _AAPL_TABLE_H
23 #define _AAPL_TABLE_H
25 #ifdef AAPL_NAMESPACE
26 namespace Aapl {
27 #endif
29 /**
30 * \addtogroup vector
31 * @{
34 /** \class Table
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
41 * my exist.
44 /*@}*/
46 /* Table class. */
47 template <class T> class Table
49 public:
50 /* Default Constructor. */
51 inline Table();
53 /**
54 * \brief Get the length of the vector.
56 * \returns the length of the vector.
58 long length() const
59 { return tabLen; }
61 /**
62 * \brief Table data.
64 * The pointer to the elements in the vector. Modifying the vector may
65 * cause this pointer to change.
67 T *data;
69 /**
70 * \brief Table length.
72 * The number of items of type T in the table.
74 long tabLen;
76 /**
77 * \brief Allocated length.
79 * The number of items for which there is room in the current allocation.
81 long allocLen;
84 /**
85 * \brief Default constructor
87 * Initialize table data to empty.
89 template <class T> inline Table<T>::Table()
91 data(0),
92 tabLen(0),
93 allocLen(0)
97 /* Default shared table header class. */
98 struct STabHead
101 * \brief Table length.
103 * The number of items of type T in the table.
105 long tabLen;
108 * \brief Allocated length.
110 * The number of items for which there is room in the current allocation.
112 long allocLen;
115 * \brief Ref Count.
117 * The number of shared vectors referencing this data.
119 long refCount;
123 * \addtogroup vector
124 * @{
127 /** \class STable
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.
137 /*@}*/
139 /* STable class. */
140 template <class T> class STable
142 public:
143 /* Default Constructor. */
144 inline STable();
147 * \brief Get the length of the shared vector.
149 * \returns the length of the shared vector.
151 long length() const
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); }
163 * \brief Table data.
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
167 * pointer to change.
169 T *data;
173 * \brief Default constructor
175 * Initialize shared table data to empty.
177 template <class T> inline STable<T>::STable()
179 data(0)
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
192 * \addtogroup vector
193 * @{
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.
216 /*@}*/
218 /* Exponential resizer. */
219 class ResizeExpn
221 protected:
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 ); }
245 #undef EXPN_UP
246 #undef EXPN_DOWN
248 #ifdef AAPL_NAMESPACE
250 #endif
252 #endif /* _AAPL_TABLE_H */