182 lines
3.5 KiB
C++
182 lines
3.5 KiB
C++
|
|
// vector.h
|
|
// implements c++ vector object for HOS
|
|
// Author: Josh Holtrop
|
|
// Date: 05/30/05
|
|
// Modified: 05/30/05
|
|
|
|
|
|
#ifndef __HOS_VECTOR__
|
|
#define __HOS_VECTOR__ __HOS_VECTOR__
|
|
|
|
#include "hos_defines.h"
|
|
|
|
template<typename type>
|
|
class vector
|
|
{
|
|
protected:
|
|
/* Pointers to vector elements */
|
|
type **myData;
|
|
|
|
/* How many elements are present */
|
|
unsigned int mySize;
|
|
|
|
/* How many elements there are pointers for */
|
|
unsigned int myAllocated;
|
|
|
|
/* Causes the vector to double in its allocated size */
|
|
void grow();
|
|
|
|
public:
|
|
/* Constructors/Destructor */
|
|
vector<type>();
|
|
vector<type>(unsigned int size);
|
|
vector<type>(const vector<type> & v1);
|
|
~vector<type>();
|
|
|
|
/* Assignment operator */
|
|
vector<type> & operator=(const vector<type> & v1);
|
|
|
|
/* Returns the size of the vector */
|
|
unsigned int size() const;
|
|
|
|
/* Add an element to the end of the vector */
|
|
vector<type> & add(type elem);
|
|
|
|
/* Remove an element from the vector */
|
|
vector<type> & remove(unsigned int index);
|
|
|
|
/* Insert an element into a position in the vector */
|
|
vector<type> & insert(type elem, unsigned int position);
|
|
|
|
/* Direct access operators */
|
|
const type & operator[](unsigned int index) const;
|
|
type & operator[](unsigned int index);
|
|
|
|
/* Returns pointer to data */
|
|
type **data();
|
|
};
|
|
|
|
template<typename type>
|
|
vector<type>::vector<type>()
|
|
{
|
|
myData = NULL;
|
|
mySize = 0;
|
|
myAllocated = 0;
|
|
}
|
|
|
|
template<typename type>
|
|
vector<type>::vector<type>(unsigned int size)
|
|
{
|
|
myData = new (type *)[size];
|
|
mySize = 0;
|
|
myAllocated = size;
|
|
}
|
|
|
|
template<typename type>
|
|
vector<type>::vector<type>(const vector<type> & v1)
|
|
{
|
|
mySize = v1.mySize;
|
|
myAllocated = v1.myAllocated;
|
|
myData = new (type *)[myAllocated];
|
|
for (u32_t i = 0; i < mySize; i++)
|
|
myData[i] = new type(*v1.myData[i]);
|
|
}
|
|
|
|
template<typename type>
|
|
vector<type>::~vector<type>()
|
|
{
|
|
for (unsigned int i = 0; i < mySize; i++)
|
|
delete myData[i];
|
|
delete[] myData;
|
|
}
|
|
|
|
template<typename type>
|
|
vector<type> & vector<type>::operator=(const vector<type> & v1)
|
|
{
|
|
mySize = v1.mySize;
|
|
myAllocated = v1.myAllocated;
|
|
myData = new (type *)[myAllocated];
|
|
for (u32_t i = 0; i < mySize; i++)
|
|
myData[i] = new type(*v1.myData[i]);
|
|
return *this;
|
|
}
|
|
|
|
template<typename type>
|
|
u32_t vector<type>::size() const
|
|
{
|
|
return mySize;
|
|
}
|
|
|
|
template<typename type>
|
|
const type & vector<type>::operator[](unsigned int index) const
|
|
{
|
|
return *myData[index];
|
|
}
|
|
|
|
template<typename type>
|
|
type & vector<type>::operator[](unsigned int index)
|
|
{
|
|
return *myData[index];
|
|
}
|
|
|
|
template<typename type>
|
|
vector<type> & vector<type>::add(type elem)
|
|
{
|
|
while (mySize >= myAllocated)
|
|
grow();
|
|
myData[mySize++] = new type(elem);
|
|
return *this;
|
|
}
|
|
|
|
template<typename type>
|
|
void vector<type>::grow()
|
|
{
|
|
myAllocated <<= 1;
|
|
if (myAllocated == 0)
|
|
myAllocated = 1;
|
|
type **data_new = new (type *)[myAllocated];
|
|
for (unsigned int i = 0; i < mySize; i++)
|
|
data_new[i] = myData[i];
|
|
if (myData)
|
|
delete[] myData;
|
|
myData = data_new;
|
|
}
|
|
|
|
template<typename type>
|
|
vector<type> & vector<type>::remove(unsigned int index)
|
|
{
|
|
if (index < mySize)
|
|
{
|
|
delete myData[index];
|
|
for (unsigned int i = index; i < (mySize - 1); i++)
|
|
myData[i] = myData[i+1];
|
|
mySize--;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
template<typename type>
|
|
vector<type> & vector<type>::insert(type elem, unsigned int position)
|
|
{
|
|
if (position <= mySize)
|
|
{
|
|
if (mySize >= myAllocated)
|
|
grow();
|
|
for (unsigned int i = mySize; i > position; i--)
|
|
myData[i] = myData[i-1];
|
|
myData[position] = new type(elem);
|
|
mySize++;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
|
|
template<typename type>
|
|
type **vector<type>::data()
|
|
{
|
|
return myData;
|
|
}
|
|
|
|
#endif
|