adding vec.h

This commit is contained in:
Jidong Xiao
2023-09-25 23:46:06 -04:00
parent 0dd92b6415
commit fc361430a1
2 changed files with 120 additions and 1 deletions

View File

@@ -29,7 +29,7 @@ manipulate the underlying representation, and discuss memory management.
## 8.2 Templated Class Declarations and Member Function Definitions
- In terms of the layout of the code in vec.h (pages 5 & 6 of the handout), the biggest difference is that this is a
- In terms of the layout of the code in [vec.h](vec.h), the biggest difference is that this is a
templated class. The keyword template and the template type name must appear before the class declaration:
```cpp

View File

@@ -0,0 +1,119 @@
#ifndef Vec_h_
#define Vec_h_
// Simple implementation of the vector class, revised from Koenig and Moo. This
// 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
// call would violate this condition, the data is copied to a larger array.
template <class T> class Vec {
public:
// TYPEDEFS
typedef unsigned int size_type;
// CONSTRUCTORS, ASSIGNMNENT OPERATOR, & DESTRUCTOR
Vec() { this->create(); }
Vec(size_type n, const T& t = T()) { this->create(n, t); }
Vec(const Vec& v) { copy(v); }
Vec& operator=(const Vec& v);
~Vec() { delete [] m_data; }
// MEMBER FUNCTIONS AND OTHER OPERATORS
T& operator[] (size_type i) { return m_data[i]; }
const T& operator[] (size_type i) const { return m_data[i]; }
void push_back(const T& t);
void resize(size_type n, const T& fill_in_value = T());
void clear() { delete [] m_data; create(); }
bool empty() const { return m_size == 0; }
size_type size() const { return m_size; }
private:
// PRIVATE MEMBER FUNCTIONS
void create();
void create(size_type n, const T& val);
void copy(const Vec<T>& v);
// REPRESENTATION
T* m_data; // Pointer to first location in the allocated array
size_type m_size; // Number of elements stored in the vector
size_type m_alloc; // Number of array locations allocated, m_size <= m_alloc
};
// Create an empty vector (null pointers everywhere).
template <class T> void Vec<T>::create() {
m_data = NULL;
m_size = m_alloc = 0; // No memory allocated yet
}
// 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) {
m_data = new T[n];
m_size = m_alloc = n;
for (size_type i = 0; i < m_size; i++) {
m_data[i] = val;
}
}
// Assign one vector to another, avoiding duplicate copying.
template <class T> Vec<T>& Vec<T>::operator=(const Vec<T>& v) {
if (this != &v) {
delete [] m_data;
this -> copy(v);
}
return *this;
}
// Create the vector as a copy of the given vector.
template <class T> void Vec<T>::copy(const Vec<T>& v) {
}
// Add an element to the end, resize if necesssary.
template <class T> void Vec<T>::push_back(const T& val) {
if (m_size == m_alloc) {
// Allocate a larger array, and copy the old values
}
// Add the value at the last location and increment the bound
m_data[m_size] = val;
++ m_size;
}
// 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.
// 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) {
}
#endif