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 pathFixedVector.hpp
More file actions
148 lines (124 loc) · 4.02 KB
/
FixedVector.hpp
File metadata and controls
148 lines (124 loc) · 4.02 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
#pragma once
#include <stdexcept>
// Declaration
template <typename T>
class FixedVector {
private:
size_t size_; // number of elements in the data structure
const size_t capacity_; // length of the array
T* array_; // pointer to dynamically allocated array
public:
// Constructors
FixedVector(size_t arraysize = 0); // Also serves as default constructor
FixedVector(const FixedVector& input ); // Copy constructor
~FixedVector();
// 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();
// Overloaded Operators
FixedVector& operator=(const FixedVector& rhs); //Copy assignment
};
// Implementation
// Constructor with initial capacity argument
template <typename T>
FixedVector<T>::FixedVector(size_t arraysize) : size_(0), capacity_(arraysize) {
array_ = new T[capacity_];
}
template <typename T>
size_t FixedVector<T>::size() {
return size_;
}
template <typename T>
bool FixedVector<T>::empty() {
return (size_ == 0);
}
template <typename T>
void FixedVector<T>::clear() {
size_ = 0;
}
// Getter
template <typename T>
T& FixedVector<T>::at(size_t index) {
if (index >= size_) {
throw std::range_error( "index out of bounds" );
}
return array_[index];
}
// Getter
template <typename T>
void FixedVector<T>::push_back(const T& value) {
insert( size_, value ); // delegate to insert() leveraging error checking
}
// Overloaded Array-Access Operator
template <typename T>
T& FixedVector<T>::operator[](size_t index) {
return array_[index]; // Note: intentionally not checking array bounds
}
// Setter
template <typename T>
void FixedVector<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 FixedVector<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 (size_t 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 FixedVector<T>::insert(size_t beforeIndex, const T &value) {
if( size_ >= capacity_) {
throw std::range_error("insufficient capacity to add another element");
}
if( beforeIndex > size_ ) {
beforeIndex = size_; // insert at the back
}
// 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_++;
}
// Copy Constructor
template <typename T>
FixedVector<T>::FixedVector(const FixedVector<T>& input) : size_(input.size_), capacity_(input.capacity_) {
array_ = new T[capacity_];
// Copy each element from the input vector to this vector
for (size_t i = 0; i < size_; i++) {
array_[i] = input.array_[i];
}
}
// Overloaded Assignment Operator
template <typename T>
FixedVector<T>& FixedVector<T>::operator=(const FixedVector<T>& rhs) {
if (this != &rhs) {
// Being fixed size, the already allocated array can be reused
// Capacity is not adjusted. If capacity_ < rhs.capacity, then some
// rhs elements may not be copied.
for (size_ = 0; size_ < rhs.size_ && size_ < capacity_ ; ++size_ ) {
array_[size_] = rhs.array_[size_];
}
}
return *this;
}
// Destructor
template <typename T>
FixedVector<T>::~FixedVector() {
delete[] array_;
}