table.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright 2001, 2002 Adrian Thurston <thurston@cs.queensu.ca>
  3. */
  4. /* This file is part of Aapl.
  5. *
  6. * Aapl is free software; you can redistribute it and/or modify it under the
  7. * terms of the GNU Lesser General Public License as published by the Free
  8. * Software Foundation; either version 2.1 of the License, or (at your option)
  9. * any later version.
  10. *
  11. * Aapl is distributed in the hope that it will be useful, but WITHOUT ANY
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Aapl; if not, write to the Free Software Foundation, Inc., 59
  18. * Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #ifndef _AAPL_TABLE_H
  21. #define _AAPL_TABLE_H
  22. #ifdef AAPL_NAMESPACE
  23. namespace Aapl {
  24. #endif
  25. /**
  26. * \addtogroup vector
  27. * @{
  28. */
  29. /** \class Table
  30. * \brief Base class for dynamic arrays.
  31. *
  32. * Table is used as the common data storage class for vectors. It does not
  33. * provide any methods to operate on the data and as such it is not intended
  34. * to be used directly. It exists so that algorithms that operatate on dynamic
  35. * arrays can be written without knowing about the various vector classes that
  36. * my exist.
  37. */
  38. /*@}*/
  39. /* Table class. */
  40. template <class T> class Table
  41. {
  42. public:
  43. /* Default Constructor. */
  44. inline Table();
  45. /**
  46. * \brief Get the length of the vector.
  47. *
  48. * \returns the length of the vector.
  49. */
  50. long length() const
  51. { return tabLen; }
  52. /**
  53. * \brief Table data.
  54. *
  55. * The pointer to the elements in the vector. Modifying the vector may
  56. * cause this pointer to change.
  57. */
  58. T *data;
  59. /**
  60. * \brief Table length.
  61. *
  62. * The number of items of type T in the table.
  63. */
  64. long tabLen;
  65. /**
  66. * \brief Allocated length.
  67. *
  68. * The number of items for which there is room in the current allocation.
  69. */
  70. long allocLen;
  71. };
  72. /**
  73. * \brief Default constructor
  74. *
  75. * Initialize table data to empty.
  76. */
  77. template <class T> inline Table<T>::Table()
  78. :
  79. data(0),
  80. tabLen(0),
  81. allocLen(0)
  82. {
  83. }
  84. /* Default shared table header class. */
  85. struct STabHead
  86. {
  87. /**
  88. * \brief Table length.
  89. *
  90. * The number of items of type T in the table.
  91. */
  92. long tabLen;
  93. /**
  94. * \brief Allocated length.
  95. *
  96. * The number of items for which there is room in the current allocation.
  97. */
  98. long allocLen;
  99. /**
  100. * \brief Ref Count.
  101. *
  102. * The number of shared vectors referencing this data.
  103. */
  104. long refCount;
  105. };
  106. /**
  107. * \addtogroup vector
  108. * @{
  109. */
  110. /** \class STable
  111. * \brief Base class for implicitly shared dynamic arrays.
  112. *
  113. * STable is used as the common data storage class for shared vectors. It does
  114. * not provide any methods to operate on the data and as such it is not
  115. * intended to be used directly. It exists so that algorithms that operatate
  116. * on dynamic arrays can be written without knowing about the various shared
  117. * vector classes that my exist.
  118. */
  119. /*@}*/
  120. /* STable class. */
  121. template <class T> class STable
  122. {
  123. public:
  124. /* Default Constructor. */
  125. inline STable();
  126. /**
  127. * \brief Get the length of the shared vector.
  128. *
  129. * \returns the length of the shared vector.
  130. */
  131. long length() const
  132. { return data == 0 ? 0 : (((STabHead*)data) - 1)->tabLen; }
  133. /**
  134. * \brief Get header of the shared vector.
  135. *
  136. * \returns the header of the shared vector.
  137. */
  138. STabHead *header() const
  139. { return data == 0 ? 0 : (((STabHead*)data) - 1); }
  140. /**
  141. * \brief Table data.
  142. *
  143. * The pointer to the elements in the vector. The shared table header is
  144. * located just behind the data. Modifying the vector may cause this
  145. * pointer to change.
  146. */
  147. T *data;
  148. };
  149. /**
  150. * \brief Default constructor
  151. *
  152. * Initialize shared table data to empty.
  153. */
  154. template <class T> inline STable<T>::STable()
  155. :
  156. data(0)
  157. {
  158. }
  159. /* If needed is greater than existing, give twice needed. */
  160. #define EXPN_UP( existing, needed ) \
  161. needed > existing ? (needed<<1) : existing
  162. /* If needed is less than 1 quarter existing, give twice needed. */
  163. #define EXPN_DOWN( existing, needed ) \
  164. needed < (existing>>2) ? (needed<<1) : existing
  165. /**
  166. * \addtogroup vector
  167. * @{
  168. */
  169. /** \class ResizeExpn
  170. * \brief Exponential table resizer.
  171. *
  172. * ResizeExpn is the default table resizer. When an up resize is needed, space
  173. * is doubled. When a down resize is needed, space is halved. The result is
  174. * that when growing the vector in a linear fashion, the number of resizes of
  175. * the allocated space behaves logarithmically.
  176. *
  177. * If only up resizes are done, there will never be more than 2 times the
  178. * needed space allocated. If down resizes are done as well, there will never
  179. * be more than 4 times the needed space allocated. ResizeExpn uses this 50%
  180. * usage policy on up resizing and 25% usage policy on down resizing to
  181. * improve performance when repeatedly inserting and removing a small number
  182. * of elements relative to the size of the array. This scheme guarantees that
  183. * repetitive inserting and removing of a small number of elements will never
  184. * result in repetative reallocation.
  185. *
  186. * The sizes passed to the resizer from the vectors are in units of T.
  187. */
  188. /*@}*/
  189. /* Exponential resizer. */
  190. class ResizeExpn
  191. {
  192. protected:
  193. /**
  194. * \brief Determine the new table size when up resizing.
  195. *
  196. * If the existing size is insufficient for the space needed then allocate
  197. * twice the space needed. Otherwise use the existing size.
  198. *
  199. * \returns The new table size.
  200. */
  201. static inline long upResize( long existing, long needed )
  202. { return EXPN_UP( existing, needed ); }
  203. /**
  204. * \brief Determine the new table size when down resizing.
  205. *
  206. * If the space needed is less than one quarter of the existing size then
  207. * allocate twice the space needed. Otherwise use the exitsing size.
  208. *
  209. * \returns The new table size.
  210. */
  211. static inline long downResize( long existing, long needed )
  212. { return EXPN_DOWN( existing, needed ); }
  213. };
  214. #undef EXPN_UP
  215. #undef EXPN_DOWN
  216. #ifdef AAPL_NAMESPACE
  217. }
  218. #endif
  219. #endif /* _AAPL_TABLE_H */