Changeset 32114 in vbox
- Timestamp:
- Aug 31, 2010 8:59:15 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/include/vector.h
r31663 r32114 28 28 #define VECTOR_ELEMENT_SIZE sizeof(VECTOR_TYPE) 29 29 /** The unit by which the vector capacity is increased */ 30 #define VECTOR_ALLOC_UNIT VECTOR_ELEMENT_SIZE *1630 #define VECTOR_ALLOC_UNIT 16 31 31 32 32 #ifndef VECTOR_TYPE … … 65 65 } VECTOR_TYPENAME; 66 66 67 /** Non-constant iterator over the vector */ 68 typedef VECTOR_TYPE *VECTOR_PUBLIC(iterator); 69 67 70 /*** Private methods ***/ 68 71 … … 78 81 size_t cNewCap = pSelf->mCapacity - pSelf->mBegin + VECTOR_ALLOC_UNIT; 79 82 size_t cOffEnd = pSelf->mEnd - pSelf->mBegin; 80 void *pRealloc = VECTOR_ALLOCATOR(pSelf->mBegin, cNewCap); 83 void *pRealloc = VECTOR_ALLOCATOR(pSelf->mBegin, 84 cNewCap * VECTOR_ELEMENT_SIZE); 81 85 if (!pRealloc) 82 86 return 0; 83 87 pSelf->mBegin = (VECTOR_TYPE *)pRealloc; 84 88 pSelf->mEnd = pSelf->mBegin + cOffEnd; 85 pSelf->mCapacity = pSelf->mBegin + cNewCap / VECTOR_ELEMENT_SIZE;89 pSelf->mCapacity = pSelf->mBegin + cNewCap; 86 90 memset(pSelf->mEnd, 0, pSelf->mCapacity - pSelf->mEnd); 87 91 return 1; … … 144 148 } 145 149 146 /*** Iterators ***/ 147 148 /** Non-constant iterator over the vector */ 149 typedef VECTOR_TYPE *VECTOR_PUBLIC(iterator); 150 /** Get the special "begin" iterator for a vector */ 151 static inline const VECTOR_PUBLIC(iterator) *VECTOR_PUBLIC(begin) 152 (VECTOR_TYPENAME *pSelf) 153 { 154 return &pSelf->mBegin; 155 } 156 157 /** Get the special "end" iterator for a vector */ 158 static inline const VECTOR_PUBLIC(iterator) *VECTOR_PUBLIC(end) 159 (VECTOR_TYPENAME *pSelf) 160 { 161 return &pSelf->mEnd; 162 } 163 164 /** A structure with pointers to class operations to save too much use of long 165 * identifiers in source code. Use like: 166 * <vector_type>_op_table *pOps = &<vector_type>_ops; 167 * and then 168 * pOps->init(&my_vector); 169 */ 170 typedef const struct VECTOR_INTERNAL(op_table) 171 { 172 int (*init) (VECTOR_TYPENAME *pSelf); 173 void (*cleanup) (VECTOR_TYPENAME *pSelf); 174 int (*push_back) (VECTOR_TYPENAME *pSelf, VECTOR_TYPE *pElement); 175 void (*clear) (VECTOR_TYPENAME *pSelf); 176 size_t (*size) (VECTOR_TYPENAME *pSelf); 177 const VECTOR_PUBLIC(iterator) * 178 (*begin) (VECTOR_TYPENAME *pSelf); 179 const VECTOR_PUBLIC(iterator) * 180 (*end) (VECTOR_TYPENAME *pSelf); 181 } VECTOR_PUBLIC(op_table); 182 183 static VECTOR_PUBLIC(op_table) VECTOR_PUBLIC(ops) = 184 { 185 VECTOR_PUBLIC(init), 186 VECTOR_PUBLIC(cleanup), 187 VECTOR_PUBLIC(push_back), 188 VECTOR_PUBLIC(clear), 189 VECTOR_PUBLIC(size), 190 VECTOR_PUBLIC(begin), 191 VECTOR_PUBLIC(end) 192 }; 193 194 /*** Iterator methods ***/ 150 195 151 196 /** Initialise a newly allocated iterator */ … … 166 211 { 167 212 ++*pSelf; 168 }169 170 /** Get the special "begin" iterator for a vector */171 static inline const VECTOR_PUBLIC(iterator) *VECTOR_PUBLIC(begin)172 (VECTOR_TYPENAME *pSelf)173 {174 return &pSelf->mBegin;175 }176 177 /** Get the special "end" iterator for a vector */178 static inline const VECTOR_PUBLIC(iterator) *VECTOR_PUBLIC(end)179 (VECTOR_TYPENAME *pSelf)180 {181 return &pSelf->mEnd;182 213 } 183 214 … … 196 227 return *pFirst == *pSecond; 197 228 } 229 230 /** A structure with pointers to iterator operations to save too much use of 231 * long identifiers in source code. Use like: 232 * <vector_type>_iter_op_table *pOps = &<vector_type>_iter_ops; 233 * and then 234 * pOps->init(&my_iter, &other_iter); 235 */ 236 typedef const struct VECTOR_INTERNAL(iter_op_table) 237 { 238 void (*init) (VECTOR_PUBLIC(iterator) *pSelf, 239 const VECTOR_PUBLIC(iterator) *pOther); 240 VECTOR_TYPE *(*target) (VECTOR_PUBLIC(iterator) *pSelf); 241 void (*incr) (VECTOR_PUBLIC(iterator) *pSelf); 242 int (*lt) (const VECTOR_PUBLIC(iterator) *pFirst, 243 const VECTOR_PUBLIC(iterator) *pSecond); 244 int (*eq) (const VECTOR_PUBLIC(iterator) *pFirst, 245 const VECTOR_PUBLIC(iterator) *pSecond); 246 } VECTOR_PUBLIC(iter_op_table); 247 248 static VECTOR_PUBLIC(iter_op_table) VECTOR_PUBLIC(iter_ops) = 249 { 250 VECTOR_PUBLIC(iter_init), 251 VECTOR_PUBLIC(iter_target), 252 VECTOR_PUBLIC(iter_incr), 253 VECTOR_PUBLIC(iter_lt), 254 VECTOR_PUBLIC(iter_eq) 255 }; 198 256 199 257 /* We need to undefine anything we have defined (and for convenience we also
Note:
See TracChangeset
for help on using the changeset viewer.