forked from CSUF-CPSC-131-Fall2019/Data-Structures-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtendableVector.hpp
More file actions
166 lines (142 loc) · 4.31 KB
/
ExtendableVector.hpp
File metadata and controls
166 lines (142 loc) · 4.31 KB
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
#pragma once
#include <stdexcept>
// Declaration
template <typename T>
class ExtendableVector {
private:
static const size_t DEFAULT_CAPACITY = 100;
size_t size_;
size_t capacity_;
T* array_;
public:
// Constructors
ExtendableVector(size_t arraysize = DEFAULT_CAPACITY);
ExtendableVector(const ExtendableVector& input);
ExtendableVector& operator=(const ExtendableVector& rhs); // Assignment operator
~ExtendableVector(); // destructor
// Getters / Setters
T& at(size_t index );
T& operator[](size_t index );
void push_back(const T& value );
void set(size_t index, const T& value );
void erase(size_t index );
void insert(size_t beforeIndex, const T& value);
size_t size();
bool empty();
void clear();
private:
void reserve(size_t); // helper function to change capacity
};
// Constructor with initial capacity argument
template <typename T>
ExtendableVector<T>::ExtendableVector(size_t arraysize) {
size_ = 0;
capacity_ = arraysize;
array_ = new T[capacity_];
}
template <typename T>
size_t ExtendableVector<T>::size() {
return size_;
}
template <typename T>
bool ExtendableVector<T>::empty() {
return (size_ == 0);
}
template <typename T>
void ExtendableVector<T>::clear() {
size_ = 0;
}
// Getter
template <typename T>
T& ExtendableVector<T>::at(size_t index) {
if (index >= size_) {
throw std::range_error("index out of bounds");
}
return array_[index];
}
// Getter
template <typename T>
void ExtendableVector<T>::push_back(const T &value) {
if (size_ >= capacity_) // If at max capacity, double the capacity
reserve (2 * capacity_);
array_[size_] = value;
size_++;
}
// Overloaded Array-Access Operator
template <typename T>
T& ExtendableVector<T>::operator[](size_t index) {
return array_[index]; // Note: array bounds intentionally not checking
}
// Setter
template <typename T>
void ExtendableVector<T>::set(size_t index, const T &value) {
at( index ) = value; // delegate to at() leveraging error checking
}
// Removes element from position. Elements from higher positions are shifted back to fill gap.
// Vector size decrements
template <typename T>
void ExtendableVector<T>::erase(size_t index) {
if (index >= size_) {
throw std::range_error( "index out of bounds" );
}
// move elements to close the gap from the left and working right
for (int j = index+1; j < size_; j++) // shift elements to the left
array_[j-1] = array_[j];
size_--;
}
// Copies x to element at position. Items at that position and higher are shifted over to make room. Vector size increments.
template <typename T>
void ExtendableVector<T>::insert(size_t beforeIndex, const T &value) {
if ( beforeIndex > size_ ) {
throw std::range_error( "index out of bounds" );
}
if (size_ >= capacity_) // If at max capacity, double the capacity
reserve (2 * capacity_);
// move elements to create space starting from the right and working left
for (size_t j = size_; j > beforeIndex; j--)
array_[j] = array_[j-1]; // shift elements to the right
array_[beforeIndex] = value; // put in empty slot
size_++;
}
template <typename T>
void ExtendableVector<T>::reserve(size_t newCapacity) {
if (newCapacity > capacity_) {
T* newArray = new T[newCapacity];
// Copy values to new array
for (size_t i = 0; i < size_; i++) {
newArray[i] = array_[i];
}
delete[] array_;
array_ = newArray;
capacity_ = newCapacity;
}
}
// Copy Constructor
template <typename T>
ExtendableVector<T>::ExtendableVector(const ExtendableVector<T>& input) {
size_ = input.size_;
capacity_ = input.capacity_;
array_ = new T[capacity_];
for (size_t i = 0; i < size_; i++) {
array_[i] = input.array_[i];
}
}
// Overloaded Assignment Operator
template <typename T>
ExtendableVector<T>& ExtendableVector<T>::operator=(const ExtendableVector<T>& rhs) {
if (this != &rhs) {
delete[] array_;
size_ = rhs.size_;
capacity_ = rhs.capacity_;
array_ = new T[size_];
for (size_t i = 0; i < size_; i++) {
array_[i] = rhs.array_[i];
}
}
return *this;
}
// Deconstructor
template <typename T>
ExtendableVector<T>::~ExtendableVector() {
delete[] array_;
}