~ubuntu-branches/ubuntu/precise/primrose/precise

« back to all changes in this revision

Viewing changes to minorGems/util/SimpleVector.h

  • Committer: Bazaar Package Importer
  • Author(s): Paul Wise
  • Date: 2009-04-06 19:26:56 UTC
  • Revision ID: james.westby@ubuntu.com-20090406192656-cri7503gebyvfl8t
Tags: upstream-5+dfsg1
ImportĀ upstreamĀ versionĀ 5+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Jason Rohrer
 
2
// SimpleVector.h
 
3
 
 
4
/**
 
5
*
 
6
*       Simple vector template class. Supports pushing at end and random-access deletions.
 
7
*       Dynamically sized.
 
8
*
 
9
*
 
10
*       Created 10-24-99
 
11
*       Mods:
 
12
*               Jason Rohrer    12-11-99        Added deleteAll function
 
13
*               Jason Rohrer    1-30-2000       Changed to return NULL if get called on non-existent element
 
14
*               Jason Rohrer    12-20-2000      Added a function for deleting a particular
 
15
*                                                                       element.
 
16
*               Jason Rohrer    12-14-2001      Added a function for getting the index of
 
17
*                                                                       a particular element.
 
18
*               Jason Rohrer    1-24-2003       Added a functions for getting an array or
 
19
*                                                                       string of all elements.
 
20
*               Jason Rohrer    7-26-2005       Added template <> to explicitly specialized
 
21
*                                                                       getElementString.
 
22
*               Jason Rohrer    1-9-2009        Added setElementString method.
 
23
*/
 
24
 
 
25
#include "minorGems/common.h"
 
26
 
 
27
 
 
28
 
 
29
#ifndef SIMPLEVECTOR_INCLUDED
 
30
#define SIMPLEVECTOR_INCLUDED
 
31
 
 
32
#include <string.h>             // for memory moving functions
 
33
 
 
34
 
 
35
const int defaultStartSize = 50;
 
36
 
 
37
template <class Type>
 
38
class SimpleVector {
 
39
        public:
 
40
        
 
41
                SimpleVector();         // create an empty vector
 
42
                SimpleVector(int sizeEstimate); // create an empty vector with a size estimate
 
43
                
 
44
                ~SimpleVector();
 
45
                
 
46
                
 
47
                void push_back(Type x);         // add x to the end of the vector
 
48
                
 
49
                Type *getElement(int index);            // get a ptr to element at index in vector
 
50
                
 
51
                
 
52
                int size();             // return the number of allocated elements in the vector
 
53
                
 
54
                bool deleteElement(int index);          // delete element at an index in vector
 
55
                
 
56
                /**
 
57
                 * Deletes a particular element.  Deletes the first element
 
58
                 * in vector == to inElement.
 
59
                 *
 
60
                 * @param inElement the element to delete.
 
61
                 *
 
62
                 * @return true iff an element was deleted.
 
63
                 */
 
64
                bool deleteElement( Type inElement );
 
65
 
 
66
 
 
67
 
 
68
                /**
 
69
                 * Gets the index of a particular element.  Gets the index of the
 
70
                 * first element in vector == to inElement.
 
71
                 *
 
72
                 * @param inElement the element to get the index of.
 
73
                 *
 
74
                 * @return the index if inElement, or -1 if inElement is not found.
 
75
                 */
 
76
                int getElementIndex( Type inElement );
 
77
 
 
78
                
 
79
                
 
80
                void deleteAll();               // delete all elements from vector
 
81
 
 
82
 
 
83
        
 
84
                /**
 
85
                 * Gets the elements as an array.
 
86
                 *
 
87
                 * @return the a new array containing all elements in this vector.
 
88
         *   Must be destroyed by caller, though elements themselves are
 
89
         *   not copied.
 
90
                 */
 
91
                Type *getElementArray();
 
92
 
 
93
 
 
94
        
 
95
        /**
 
96
                 * Gets the char elements as a \0-terminated string.
 
97
                 *
 
98
                 * @return a \0-terminated string containing all elements in this
 
99
         *   vector.
 
100
         *   Must be destroyed by caller.
 
101
                 */
 
102
                char *getElementString();
 
103
 
 
104
 
 
105
        /**
 
106
                 * Sets the char elements as a \0-terminated string.
 
107
                 *
 
108
                 * @param inString a \0-terminated string containing all elements to 
 
109
         *   this vector with.
 
110
         *   Must be destroyed by caller.
 
111
                 */
 
112
                void setElementString( char *inString );
 
113
 
 
114
        
 
115
        private:
 
116
                Type *elements;
 
117
                int numFilledElements;
 
118
                int maxSize;
 
119
                int minSize;            // number of allocated elements when vector is empty
 
120
                };
 
121
                
 
122
                
 
123
template <class Type>           
 
124
inline SimpleVector<Type>::SimpleVector() {
 
125
        elements = new Type[defaultStartSize];
 
126
        numFilledElements = 0;
 
127
        maxSize = defaultStartSize;
 
128
        minSize = defaultStartSize;
 
129
        }
 
130
 
 
131
template <class Type>
 
132
inline SimpleVector<Type>::SimpleVector(int sizeEstimate) {
 
133
        elements = new Type[sizeEstimate];
 
134
        numFilledElements = 0;
 
135
        maxSize = sizeEstimate;
 
136
        minSize = sizeEstimate;
 
137
        }
 
138
        
 
139
template <class Type>   
 
140
inline SimpleVector<Type>::~SimpleVector() {
 
141
        delete [] elements;
 
142
        }       
 
143
 
 
144
template <class Type>
 
145
inline int SimpleVector<Type>::size() {
 
146
        return numFilledElements;
 
147
        }
 
148
 
 
149
template <class Type>
 
150
inline Type *SimpleVector<Type>::getElement(int index) {
 
151
        if( index < numFilledElements && index >=0 ) {
 
152
                return &(elements[index]);
 
153
                }
 
154
        else return NULL;
 
155
        }
 
156
        
 
157
 
 
158
template <class Type>
 
159
inline bool SimpleVector<Type>::deleteElement(int index) {
 
160
        if( index < numFilledElements) {        // if index valid for this vector
 
161
                
 
162
                if( index != numFilledElements - 1)  {  // this spot somewhere in middle
 
163
                
 
164
                        // move memory towards front by one spot
 
165
                        int sizeOfElement = sizeof(Type);
 
166
                
 
167
                        int numBytesToMove = sizeOfElement*(numFilledElements - (index+1));
 
168
                
 
169
                        Type *destPtr = &(elements[index]);
 
170
                        Type *srcPtr = &(elements[index+1]);
 
171
                
 
172
                        memmove((void *)destPtr, (void *)srcPtr, numBytesToMove);
 
173
                        }
 
174
                        
 
175
                numFilledElements--;    // one less element in vector
 
176
                return true;
 
177
                }
 
178
        else {                          // index not valid for this vector
 
179
                return false;
 
180
                }
 
181
        }
 
182
 
 
183
 
 
184
template <class Type>
 
185
inline bool SimpleVector<Type>::deleteElement( Type inElement ) {
 
186
        int index = getElementIndex( inElement );
 
187
        if( index != -1 ) {
 
188
                return deleteElement( index );
 
189
                }
 
190
        else {
 
191
                return false;
 
192
                }
 
193
        }
 
194
 
 
195
 
 
196
 
 
197
template <class Type>
 
198
inline int SimpleVector<Type>::getElementIndex( Type inElement ) {
 
199
        // walk through vector, looking for first match.
 
200
        for( int i=0; i<numFilledElements; i++ ) {
 
201
                if( elements[i] == inElement ) {
 
202
                        return i;
 
203
                        }
 
204
                }
 
205
        
 
206
        // no element found
 
207
        return -1;
 
208
        }
 
209
 
 
210
 
 
211
 
 
212
template <class Type>
 
213
inline void SimpleVector<Type>::deleteAll() {
 
214
        numFilledElements = 0;
 
215
        if( maxSize > minSize ) {               // free memory if vector has grown
 
216
                delete [] elements;
 
217
                elements = new Type[minSize];   // reallocate an empty vector
 
218
                maxSize = minSize;
 
219
                }
 
220
        }
 
221
 
 
222
 
 
223
template <class Type>
 
224
inline void SimpleVector<Type>::push_back(Type x)       {
 
225
        if( numFilledElements < maxSize) {      // still room in vector
 
226
                elements[numFilledElements] = x;
 
227
                numFilledElements++;
 
228
                }
 
229
        else {                                  // need to allocate more space for vector
 
230
                int newMaxSize = maxSize << 1;          // double size
 
231
                
 
232
                Type *newAlloc = new Type[newMaxSize];
 
233
                int sizeOfElement = sizeof(Type);
 
234
                int numBytesToMove = sizeOfElement*(numFilledElements);
 
235
                
 
236
                // move into new space
 
237
                memcpy((void *)newAlloc, (void *) elements, numBytesToMove);
 
238
                
 
239
                // delete old space
 
240
                delete [] elements;
 
241
                
 
242
                elements = newAlloc;
 
243
                maxSize = newMaxSize;   
 
244
                
 
245
                elements[numFilledElements] = x;
 
246
                numFilledElements++;    
 
247
                }
 
248
        }
 
249
 
 
250
 
 
251
 
 
252
template <class Type>
 
253
inline Type *SimpleVector<Type>::getElementArray() {
 
254
    Type *newAlloc = new Type[ numFilledElements ];
 
255
    int sizeOfElement = sizeof( Type );
 
256
    int numBytesToCopy = sizeOfElement * numFilledElements;
 
257
                
 
258
    // copy into new space
 
259
    memcpy( (void *)newAlloc, (void *)elements, numBytesToCopy );
 
260
 
 
261
    return newAlloc;
 
262
    }
 
263
 
 
264
 
 
265
 
 
266
template <>
 
267
inline char *SimpleVector<char>::getElementString() {
 
268
    char *newAlloc = new char[ numFilledElements + 1 ];
 
269
    int sizeOfElement = sizeof( char );
 
270
    int numBytesToCopy = sizeOfElement * numFilledElements;
 
271
                
 
272
    // copy into new space
 
273
    memcpy( (void *)newAlloc, (void *)elements, numBytesToCopy );
 
274
 
 
275
    newAlloc[ numFilledElements ] = '\0';
 
276
    
 
277
    return newAlloc;
 
278
    }
 
279
 
 
280
 
 
281
 
 
282
template <>
 
283
inline void SimpleVector<char>::setElementString( char *inString ) {
 
284
    // slow but correct
 
285
    
 
286
    deleteAll();
 
287
    
 
288
    int numChars = strlen( inString );
 
289
    for( int i=0; i<numChars; i++ ) {
 
290
        push_back( inString[i] );
 
291
        }
 
292
    }
 
293
 
 
294
 
 
295
 
 
296
#endif