remove typedef, m_alloc to capacity

This commit is contained in:
Jidong Xiao
2024-02-05 23:19:56 -05:00
parent 60e1736d11
commit 6659fcdf9b
2 changed files with 28 additions and 29 deletions

View File

@@ -9,7 +9,7 @@ int main() {
// --------------------------------------------------- // ---------------------------------------------------
// initialize v1 with 10 values... the multiples of 5 // initialize v1 with 10 values... the multiples of 5
Vec<int> v1( 10, 0 ); Vec<int> v1( 10, 0 );
Vec<int>::size_type i; int i;
for ( i = 0; i < v1.size(); i++) { for ( i = 0; i < v1.size(); i++) {
v1[i] = 5 * i; v1[i] = 5 * i;
} }
@@ -84,7 +84,7 @@ int main() {
for ( i = 0; i<5; ++i ) for ( i = 0; i<5; ++i )
z.push_back( sqrt( double(10*(i+1)) )); z.push_back( sqrt( double(10*(i+1)) ));
cout << "Contents of vector z: "; cout << "Contents of vector z: ";
for ( Vec<double>::size_type j = 0; j < z.size(); j++ ) for (int j = 0; j < z.size(); j++ )
cout << " " << z[j]; cout << " " << z[j];
cout << endl; cout << endl;

View File

@@ -2,7 +2,7 @@
#define Vec_h_ #define Vec_h_
// Simple implementation of the vector class, revised from Koenig and Moo. This // Simple implementation of the vector class, revised from Koenig and Moo. This
// class is implemented using a dynamically allocated array (of templated type T). // class is implemented using a dynamically allocated array (of templated type T).
// We ensure that that m_size is always <= m_alloc and when a push_back or resize // We ensure that that m_size is always <= capacity and when a push_back or resize
// call would violate this condition, the data is copied to a larger array. // call would violate this condition, the data is copied to a larger array.
template <class T> class Vec { template <class T> class Vec {
@@ -11,24 +11,23 @@ public:
// TYPEDEFS // TYPEDEFS
typedef T* iterator; typedef T* iterator;
typedef const T* const_iterator; typedef const T* const_iterator;
typedef unsigned int size_type;
// CONSTRUCTORS, ASSIGNMNENT OPERATOR, & DESTRUCTOR // CONSTRUCTORS, ASSIGNMNENT OPERATOR, & DESTRUCTOR
Vec() { this->create(); } Vec() { this->create(); }
Vec(size_type n, const T& t = T()) { this->create(n, t); } Vec(int n, const T& t = T()) { this->create(n, t); }
Vec(const Vec& v) { copy(v); } Vec(const Vec& v) { copy(v); }
Vec& operator=(const Vec& v); Vec& operator=(const Vec& v);
~Vec() { delete [] m_data; } ~Vec() { delete [] m_data; }
// MEMBER FUNCTIONS AND OTHER OPERATORS // MEMBER FUNCTIONS AND OTHER OPERATORS
T& operator[] (size_type i) { return m_data[i]; } T& operator[] (int i) { return m_data[i]; }
const T& operator[] (size_type i) const { return m_data[i]; } const T& operator[] (int i) const { return m_data[i]; }
void push_back(const T& t); void push_back(const T& t);
iterator erase(iterator p); iterator erase(iterator p);
void resize(size_type n, const T& fill_in_value = T()); void resize(int n, const T& fill_in_value = T());
void clear() { delete [] m_data; create(); } void clear() { delete [] m_data; create(); }
bool empty() const { return m_size == 0; } bool empty() const { return m_size == 0; }
size_type size() const { return m_size; } int size() const { return m_size; }
// ITERATOR OPERATIONS // ITERATOR OPERATIONS
iterator begin() { return m_data; } iterator begin() { return m_data; }
@@ -39,25 +38,25 @@ public:
private: private:
// PRIVATE MEMBER FUNCTIONS // PRIVATE MEMBER FUNCTIONS
void create(); void create();
void create(size_type n, const T& val); void create(int n, const T& val);
void copy(const Vec<T>& v); void copy(const Vec<T>& v);
// REPRESENTATION // REPRESENTATION
T* m_data; // Pointer to first location in the allocated array T* m_data; // Pointer to first location in the allocated array
size_type m_size; // Number of elements stored in the vector int m_size; // Number of elements stored in the vector
size_type m_alloc; // Number of array locations allocated, m_size <= m_alloc int capacity; // Number of array locations allocated, m_size <= capacity
}; };
// Create an empty vector (null pointers everywhere). // Create an empty vector (null pointers everywhere).
template <class T> void Vec<T>::create() { template <class T> void Vec<T>::create() {
m_data = NULL; m_data = NULL;
m_size = m_alloc = 0; // No memory allocated yet m_size = capacity = 0; // No memory allocated yet
} }
// Create a vector with size n, each location having the given value // Create a vector with size n, each location having the given value
template <class T> void Vec<T>::create(size_type n, const T& val) { template <class T> void Vec<T>::create(int n, const T& val) {
m_data = new T[n]; m_data = new T[n];
m_size = m_alloc = n; m_size = capacity = n;
for (T* p = m_data; p != m_data + m_size; ++p) for (T* p = m_data; p != m_data + m_size; ++p)
*p = val; *p = val;
} }
@@ -73,27 +72,27 @@ template <class T> Vec<T>& Vec<T>::operator=(const Vec<T>& v) {
// Create the vector as a copy of the given vector. // Create the vector as a copy of the given vector.
template <class T> void Vec<T>::copy(const Vec<T>& v) { template <class T> void Vec<T>::copy(const Vec<T>& v) {
this->m_alloc = v.m_alloc; this->capacity = v.capacity;
this->m_size = v.m_size; this->m_size = v.m_size;
this->m_data = new T[this->m_alloc]; this->m_data = new T[this->capacity];
// Copy the data // Copy the data
for (size_type i = 0; i < this->m_size; ++i) for (int i = 0; i < this->m_size; ++i)
this -> m_data[ i ] = v.m_data[ i ]; this -> m_data[ i ] = v.m_data[ i ];
} }
// Add an element to the end, resize if necesssary. // Add an element to the end, resize if necesssary.
template <class T> void Vec<T>::push_back(const T& val) { template <class T> void Vec<T>::push_back(const T& val) {
if (m_size == m_alloc) { if (m_size == capacity) {
// Allocate a larger array, and copy the old values // Allocate a larger array, and copy the old values
// Calculate the new allocation. Make sure it is at least one. // Calculate the new allocation. Make sure it is at least one.
m_alloc *= 2; capacity *= 2;
if (m_alloc < 1) m_alloc = 1; if (capacity < 1) capacity = 1;
// Allocate and copy the old array // Allocate and copy the old array
T* new_data = new T[ m_alloc ]; T* new_data = new T[capacity];
for (size_type i=0; i<m_size; ++i) for (int i=0; i<m_size; ++i)
new_data[i] = m_data[i]; new_data[i] = m_data[i];
// Delete the old array and reset the pointers // Delete the old array and reset the pointers
@@ -119,22 +118,22 @@ template <class T> typename Vec<T>::iterator Vec<T>::erase(iterator p) {
// If n is less than or equal to the current size, just change the size. If n is // If n is less than or equal to the current size, just change the size. If n is
// greater than the current size, the new slots must be filled in with the given value. // greater than the current size, the new slots must be filled in with the given value.
// Re-allocation should occur only if necessary. push_back should not be used. // Re-allocation should occur only if necessary. push_back should not be used.
template <class T> void Vec<T>::resize(size_type n, const T& fill_in_value) { template <class T> void Vec<T>::resize(int n, const T& fill_in_value) {
if (n <= m_size) if (n <= m_size)
m_size = n; m_size = n;
else { else {
// If necessary, allocate new space and copy the old values // If necessary, allocate new space and copy the old values
if (n > m_alloc) { if (n > capacity) {
m_alloc = n; capacity = n;
T* new_data = new T[m_alloc]; T* new_data = new T[capacity];
for (size_type i=0; i<m_size; ++i) for (int i=0; i<m_size; ++i)
new_data[i] = m_data[i]; new_data[i] = m_data[i];
delete [] m_data; delete [] m_data;
m_data = new_data; m_data = new_data;
} }
// Now fill in the remaining values and assign the final size. // Now fill in the remaining values and assign the final size.
for (size_type i = m_size; i<n; ++i) for (int i = m_size; i<n; ++i)
m_data[i] = fill_in_value; m_data[i] = fill_in_value;
m_size = n; m_size = n;
} }