📄 array_of.hpp
字号:
/*+-------------------------------------------------------------------
Ben Landon
CSCI E235
Final Project
Array_of - A parameterized array class.
I'm using this instead of the STL vector class because I only need
this class to do some relatively simple things, and this is easier
to view in a debugger than any STL template.
*/
#include <string.h> // for memcpy
/*+-------------------------------------------------------------------
ArrayException
This exception class can be thrown if calling code attempts
to access an out of bounds element.
*/
class ArrayException
{
public:
ArrayException (void) { }
~ArrayException () {}
};
/*+-------------------------------------------------------------------
Array_of - A parameterized array class.
*/
template<class T> class Array_of
{
private:
T* m_rep;
unsigned int m_num_elements;
unsigned int m_rep_size;
void maybe_grow_rep (void);
static int min_size;
public:
Array_of (unsigned int initial_size);
Array_of (void);
~Array_of ();
void set (T& element, unsigned int index);
T& get (unsigned int index);
const T& get (unsigned int index) const;
T get_copy (unsigned int index) const;
void append (T& element);
void clear (void);
unsigned int num_elements (void) const { return m_num_elements; }
};
template<class T> Array_of<T>::Array_of<T> (unsigned int initial_size)
: m_rep(NULL),
m_num_elements(0),
m_rep_size(0)
{
if (initial_size == 0)
initial_size = Array_of<T>::min_size;
m_rep = new T[initial_size];
m_rep_size = initial_size;
m_num_elements = 0;
}
template <class T> Array_of<T>::Array_of<T> (void)
: m_rep(NULL), m_num_elements(0), m_rep_size(0)
{
unsigned int initial_size = 4;
m_rep = new T[initial_size];
m_rep_size = initial_size;
m_num_elements = 0;
}
template<class T> Array_of<T>::~Array_of<T> ()
{
if (m_rep)
{
delete [] m_rep;
m_rep = NULL;
}
m_rep_size = 0;
m_num_elements = 0;
}
template<class T> void Array_of<T>::set (T& element, unsigned int index)
{
if (index > (m_num_elements - 1))
throw ArrayException();
m_rep[index] = element;
}
template<class T> T& Array_of<T>::get (unsigned int index)
{
if (index > (m_num_elements - 1))
throw ArrayException();
return m_rep[index];
}
template<class T> const T& Array_of<T>::get (unsigned int index) const
{
if (index > (m_num_elements - 1))
throw ArrayException();
return m_rep[index];
}
template<class T> T Array_of<T>::get_copy (unsigned int index) const
{
if (index > (m_num_elements - 1))
throw ArrayException();
return m_rep[index];
}
template<class T> void Array_of<T>::append (T& element)
{
maybe_grow_rep();
m_rep[m_num_elements++] = element;
}
/*+-------------------------------------------------------------------
Array_of::clear
This method effectively empties the array, but it DOES NOT
destroy any of the elements previously in the Array_of object.
If the elements in the Array_of have to be explicitly destroyed,
then it is up to the calling code to do this before invoking
the clear method.
This method sets the number of elements to zero and uses
memset to zero the elements of the array representation.
This method DOES NOT shrink the size of the array representation,
even if it is huge.
*/
template<class T> void Array_of<T>::clear (void)
{
// If m_num_elements is zero, then don't do
// anything.
if (m_num_elements != 0)
{
m_num_elements = 0;
memset(m_rep, 0, m_rep_size * sizeof(T));
// m_rep_size is unchanged
}
}
/*+-------------------------------------------------------------------
Array_of::maybe_grow_rep
This is a private method of Array_of, and it is invoked
at the beginning of every operation that adds another
element to the array. This method simply checks to see
if there is enough room in the array representation for
another element, if not, it doubles the size of the
rep and copies the elements over to the new array.
WARNING: This method doesn't do any checking to make
sure that the memory allocation succeeds.
Maybe I should change this so instead of always
doubling the size of the array rep, it would double
up to a certain threshold, and then start adding
a constant amount each time it needed to grow.
*/
template<class T> void Array_of<T>::maybe_grow_rep (void)
{
if (m_num_elements >= m_rep_size)
{
size_t new_size = 2 * m_rep_size;
if (new_size <= 0)
new_size = Array_of<T>::min_size;
T* new_rep = new T[new_size];
memcpy(new_rep, m_rep, m_num_elements * sizeof(T));
m_rep_size = new_size;
T* old_rep = m_rep;
m_rep = new_rep;
if (old_rep)
{
delete [] old_rep;
old_rep = NULL;
}
}
}
template<class T> int Array_of<T>::min_size = 4;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -