summaryrefslogtreecommitdiff
path: root/src/mem/gems_common/Vector.hh
blob: ddba7a2fc42c7f6a1c46f3e9d9a3f5c1ab9d5acb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * Copyright (c) 1999-2005 Mark D. Hill and David A. Wood
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Description: The Vector class is a generic container which acts
 * much like an array.  The Vector class handles dynamic sizing and
 * resizing as well as performing bounds checking on each access.  An
 * "insertAtBottom" operation is supported to allow adding elements to
 * the Vector much like you would add new elements to a linked list or
 * queue.
 */

#ifndef VECTOR_H
#define VECTOR_H

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

template <class TYPE>
class Vector
{
public:
  Vector();
  explicit Vector(int initial_size);  // Construct with an initial max size
  ~Vector();
  const TYPE& ref(int index) const;   // Get an element of the vector
  TYPE& ref(int index);               // Get an element of the vector
  void clear();                       // remove all elements of the vector
  void sortVector();                  // sort all elements using < operator
  int size() const { return m_size; }
  void setSize(int new_size);         // Increase size, reallocates memory as needed
  void expand(int num) { setSize(m_size+num); } // Increase size by num
  void increaseSize(int new_size, const TYPE& reset);    // and adds num of slots at the bottom set to reset value
  void insertAtTop(const TYPE& element);  // Increase size by one and set last element
    // FIXME - WARNING: insertAtTop is currently O(n) and needs to be fixed
  void insertAtBottom(const TYPE& element);  // Increase size by one and set last element
  TYPE sum() const;  // Uses the += operator to sum all the elements of the vector
  void deletePointers(); // Walks the Vector calling delete on all
                         // elements and sets them to NULL, can only
                         // be used when the TYPE is a pointer type.
  void removeFromTop(int num);  // removes elements from top
  void print(std::ostream& out) const;


  // Array Reference operator overloading
  const TYPE& operator[](int index) const { return ref(index); }
  TYPE& operator[](int index) { return ref(index); }

  // Public copy constructor and assignment operator
  Vector(const Vector& vec);
  Vector<TYPE>& operator=(const Vector& vec);
private:

  void grow(int new_max_size);  // Expands vector to new_max_size

  // Data members
  TYPE* m_vec;           // Array to hold the elements
  int m_size;            // Number of elements in use
  int m_max_size;        // Size of allocated array
};

template <class TYPE>
std::ostream& operator<<(std::ostream& out, const Vector<TYPE>& vec);

// *********************

template <class TYPE>
Vector<TYPE>::Vector()
{
  m_size = 0;
  m_max_size = 0;
  m_vec = NULL;
}

template <class TYPE>
Vector<TYPE>::Vector(int initial_size)
{
  m_size = 0;
  m_max_size = initial_size;
  m_vec = NULL;
  grow(initial_size);
}

template <class TYPE>
Vector<TYPE>::~Vector()
{
  delete [] m_vec;
}

template <class TYPE>
const TYPE& Vector<TYPE>::ref(int index) const
{
#ifndef NO_VECTOR_BOUNDS_CHECKS
  assert(m_size != 0);
  assert(index < m_size);
  assert(index >= 0);
#endif
  return m_vec[index];
}

template <class TYPE>
TYPE& Vector<TYPE>::ref(int index)
{
#ifndef NO_VECTOR_BOUNDS_CHECKS
  assert(m_size != 0);
  assert(index < m_size);
  assert(index >= 0);
#endif
  return m_vec[index];
}


template <class TYPE>
void Vector<TYPE>::setSize(int new_size)
{
  // FIXME - this should also decrease or shrink the size of the array at some point.
  if (new_size > m_max_size) {
    grow(std::max((m_max_size+1)*2, new_size));
  }
  m_size = new_size;
#ifndef NO_VECTOR_BOUNDS_CHECKS
  assert(m_size <= m_max_size);
  assert(m_size >= 0);
#endif
}

template <class TYPE>
inline
void Vector<TYPE>::increaseSize(int new_size, const TYPE& reset)
{
  assert(new_size >= m_size);
  if (new_size >= m_max_size) {
    grow(std::max((m_max_size+1)*2, new_size));
  }
  int old_size = m_size;
  m_size = new_size;
  for (int j = old_size; j < m_size; j++) {
    ref(j) = reset;
  }

#ifndef NO_VECTOR_BOUNDS_CHECKS
    assert(m_size <= m_max_size);
    assert(m_size >= 0);
#endif
}

template <class TYPE>
inline
void Vector<TYPE>::clear()
{
  m_size = 0;
  m_max_size = 0;
  delete [] m_vec;
  m_vec = NULL;
}

template <class TYPE>
inline
void Vector<TYPE>::sortVector()
{
  std::sort(&m_vec[0], &m_vec[m_size]);
}

template <class TYPE>
inline
void Vector<TYPE>::insertAtTop(const TYPE& element)
{
  setSize(m_size+1);
  for (int i = m_size-1; i >= 1; i--) {
    ref(i) = ref(i-1);
  }
  ref(0) = element;
}

template <class TYPE>
inline
void Vector<TYPE>::removeFromTop(int num)
{
  if (num > m_size) {
    num = m_size;
  }
  for (int i = 0; i < m_size - num; i++) {
    m_vec[i] = m_vec[i+num];
  }
  m_size = m_size - num;

}

template <class TYPE>
void Vector<TYPE>::insertAtBottom(const TYPE& element)
{
  setSize(m_size+1);
  ref(m_size-1) = element;
}

template <class TYPE>
TYPE Vector<TYPE>::sum() const
{
  assert(m_size > 0);
  TYPE sum = ref(0);
  for(int i=1; i<m_size; i++) {
    sum += ref(i);
  }
  return sum;
}

template <class TYPE>
void Vector<TYPE>::deletePointers()
{
  assert(m_size >= 0);
  for(int i=0; i<m_size; i++) {
    // FIXME this function should be non-member function, otherwise this
    // prevent template instantiation for non-pointer types
    //
    // Also, there is warning of Switch.cc which use void* here
    delete ref(i);
    ref(i) = NULL;
  }
}

template <class TYPE>
void Vector<TYPE>::print(std::ostream& out) const
{
  out << "[ ";
  for(int i=0; i<m_size; i++) {
    if (i != 0) {
      out << " ";
    }
    out << ref(i);
  }
  out << " ]";
  out << std::flush;
}

// Copy constructor
template <class TYPE>
Vector<TYPE>::Vector(const Vector& vec)
{
  // Setup the new memory
  m_size = vec.m_size;
  m_max_size = vec.m_max_size;
  if (m_max_size != 0) {
    m_vec = new TYPE[m_max_size];
    assert(m_vec != NULL);
  } else {
    m_vec = NULL;
  }

  // Copy the elements of the array
  for(int i=0; i<m_size; i++) {
    m_vec[i] = vec.m_vec[i];  // Element copy
  }
}

template <class TYPE>
Vector<TYPE>& Vector<TYPE>::operator=(const Vector& vec)
{
  if (this == &vec) {
    //    assert(0);
  } else {
    // Free the old memory
    delete [] m_vec;

    // Setup the new memory
    m_size = vec.m_size;
    m_max_size = vec.m_max_size;

    if (m_max_size != 0) {
      m_vec = new TYPE[m_max_size];
      assert(m_vec != NULL);
    } else {
      m_vec = NULL;
    }

    // Copy the elements of the array
    for(int i=0; i<m_size; i++) {
      m_vec[i] = vec.m_vec[i];  // Element copy
    }
  }
  return *this;
}

template <class TYPE>
void Vector<TYPE>::grow(int new_max_size)
{
  TYPE* temp_vec;
  m_max_size = new_max_size;
  if (new_max_size != 0) {
    temp_vec = new TYPE[new_max_size];
    assert(temp_vec != NULL);
  } else {
    temp_vec = NULL;
  }

  // Copy the elements of the array
  for(int i=0; i<m_size; i++) {
    temp_vec[i] = m_vec[i];  // Element copy
  }
  delete [] m_vec;
  m_vec = temp_vec;
}

template <class TYPE>
std::ostream& operator<<(std::ostream& out, const Vector<TYPE>& vec)
{
  vec.print(out);
  return out;
}

#endif //VECTOR_H