2 * Copyright (c) 1999-2005 Mark D. Hill and David A. Wood
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Description: The Vector class is a generic container which acts
31 * much like an array. The Vector class handles dynamic sizing and
32 * resizing as well as performing bounds checking on each access. An
33 * "insertAtBottom" operation is supported to allow adding elements to
34 * the Vector much like you would add new elements to a linked list or
41 #include "std-includes.hh"
48 explicit Vector(int initial_size); // Construct with an initial max size
50 const TYPE& ref(int index) const; // Get an element of the vector
51 TYPE& ref(int index); // Get an element of the vector
52 void clear(); // remove all elements of the vector
53 void sortVector(); // sort all elements using < operator
54 int size() const { return m_size; }
55 void setSize(int new_size); // Increase size, reallocates memory as needed
56 void expand(int num) { setSize(m_size+num); } // Increase size by num
57 void increaseSize(int new_size, const TYPE& reset); // and adds num of slots at the bottom set to reset value
58 void insertAtTop(const TYPE& element); // Increase size by one and set last element
59 // FIXME - WARNING: insertAtTop is currently O(n) and needs to be fixed
60 void insertAtBottom(const TYPE& element); // Increase size by one and set last element
61 TYPE sum() const; // Uses the += operator to sum all the elements of the vector
62 void deletePointers(); // Walks the Vector calling delete on all
63 // elements and sets them to NULL, can only
64 // be used when the TYPE is a pointer type.
65 void removeFromTop(int num); // removes elements from top
66 void print(ostream& out) const;
69 // Array Reference operator overloading
70 const TYPE& operator[](int index) const { return ref(index); }
71 TYPE& operator[](int index) { return ref(index); }
73 // Public copy constructor and assignment operator
74 Vector(const Vector& vec);
75 Vector<TYPE>& operator=(const Vector& vec);
78 void grow(int new_max_size); // Expands vector to new_max_size
81 TYPE* m_vec; // Array to hold the elements
82 int m_size; // Number of elements in use
83 int m_max_size; // Size of allocated array
87 ostream& operator<<(ostream& out, const Vector<TYPE>& vec);
89 // *********************
92 Vector<TYPE>::Vector()
100 Vector<TYPE>::Vector(int initial_size)
103 m_max_size = initial_size;
108 template <class TYPE>
109 Vector<TYPE>::~Vector()
114 template <class TYPE>
115 const TYPE& Vector<TYPE>::ref(int index) const
117 #ifndef NO_VECTOR_BOUNDS_CHECKS
119 assert(index < m_size);
125 template <class TYPE>
126 TYPE& Vector<TYPE>::ref(int index)
128 #ifndef NO_VECTOR_BOUNDS_CHECKS
130 assert(index < m_size);
137 template <class TYPE>
138 void Vector<TYPE>::setSize(int new_size)
140 // FIXME - this should also decrease or shrink the size of the array at some point.
141 if (new_size > m_max_size) {
142 grow(max((m_max_size+1)*2, new_size));
145 #ifndef NO_VECTOR_BOUNDS_CHECKS
146 assert(m_size <= m_max_size);
151 template <class TYPE>
153 void Vector<TYPE>::increaseSize(int new_size, const TYPE& reset)
155 assert(new_size >= m_size);
156 if (new_size >= m_max_size) {
157 grow(max((m_max_size+1)*2, new_size));
159 int old_size = m_size;
161 for (int j = old_size; j < m_size; j++) {
165 #ifndef NO_VECTOR_BOUNDS_CHECKS
166 assert(m_size <= m_max_size);
171 template <class TYPE>
173 void Vector<TYPE>::clear()
181 template <class TYPE>
183 void Vector<TYPE>::sortVector()
185 sort(&m_vec[0], &m_vec[m_size]);
188 template <class TYPE>
190 void Vector<TYPE>::insertAtTop(const TYPE& element)
193 for (int i = m_size-1; i >= 1; i--) {
199 template <class TYPE>
201 void Vector<TYPE>::removeFromTop(int num)
206 for (int i = 0; i < m_size - num; i++) {
207 m_vec[i] = m_vec[i+num];
209 m_size = m_size - num;
213 template <class TYPE>
214 void Vector<TYPE>::insertAtBottom(const TYPE& element)
217 ref(m_size-1) = element;
220 template <class TYPE>
221 TYPE Vector<TYPE>::sum() const
225 for(int i=1; i<m_size; i++) {
231 template <class TYPE>
232 void Vector<TYPE>::deletePointers()
235 for(int i=0; i<m_size; i++) {
236 // FIXME this function should be non-member function, otherwise this
237 // prevent template instantiation for non-pointer types
239 // Also, there is warning of Switch.cc which use void* here
245 template <class TYPE>
246 void Vector<TYPE>::print(ostream& out) const
249 for(int i=0; i<m_size; i++) {
260 template <class TYPE>
261 Vector<TYPE>::Vector(const Vector& vec)
263 // Setup the new memory
265 m_max_size = vec.m_max_size;
266 if (m_max_size != 0) {
267 m_vec = new TYPE[m_max_size];
268 assert(m_vec != NULL);
273 // Copy the elements of the array
274 for(int i=0; i<m_size; i++) {
275 m_vec[i] = vec.m_vec[i]; // Element copy
279 template <class TYPE>
280 Vector<TYPE>& Vector<TYPE>::operator=(const Vector& vec)
285 // Free the old memory
288 // Setup the new memory
290 m_max_size = vec.m_max_size;
292 if (m_max_size != 0) {
293 m_vec = new TYPE[m_max_size];
294 assert(m_vec != NULL);
299 // Copy the elements of the array
300 for(int i=0; i<m_size; i++) {
301 m_vec[i] = vec.m_vec[i]; // Element copy
307 template <class TYPE>
308 void Vector<TYPE>::grow(int new_max_size)
311 m_max_size = new_max_size;
312 if (new_max_size != 0) {
313 temp_vec = new TYPE[new_max_size];
314 assert(temp_vec != NULL);
319 // Copy the elements of the array
320 for(int i=0; i<m_size; i++) {
321 temp_vec[i] = m_vec[i]; // Element copy
327 template <class TYPE>
328 ostream& operator<<(ostream& out, const Vector<TYPE>& vec)