|
|
@@ -33,41 +33,39 @@ OTHER DEALINGS IN THE SOFTWARE.
|
|
|
Does not grow in size.
|
|
|
Does not initialize memory.
|
|
|
*/
|
|
|
-template <typename T, int size>
|
|
|
+template <typename T, uint SIZE>
|
|
|
class Array
|
|
|
{
|
|
|
|
|
|
public:
|
|
|
|
|
|
Array();
|
|
|
- Array(const Array<T, size>& array);
|
|
|
+ Array(const Array<T, SIZE>& array);
|
|
|
~Array();
|
|
|
|
|
|
- T& operator[](int index);
|
|
|
- const T& operator[](int index) const;
|
|
|
+ T& operator[](uint index);
|
|
|
+ const T& operator[](uint index) const;
|
|
|
|
|
|
- bool IsEmpty() const;
|
|
|
- int GetSize() const;
|
|
|
- int GetCapacity() const;
|
|
|
+ bool empty() const;
|
|
|
+ uint size() const;
|
|
|
+ uint get_capacity() const;
|
|
|
|
|
|
- int Append(const T& element);
|
|
|
- int Insert(int index, const T& element);
|
|
|
- void Remove(int index);
|
|
|
- void Clear();
|
|
|
+ uint push_back(const T& element);
|
|
|
+ void pop_back();
|
|
|
|
|
|
- int Find(const T& element) const;
|
|
|
+ void clear();
|
|
|
|
|
|
- const Array<T, size>& operator=(const Array<T, size>& b);
|
|
|
+ const Array<T, SIZE>& operator=(const Array<T, SIZE>& b);
|
|
|
|
|
|
- T* GetBegin();
|
|
|
- const T* GetBegin() const;
|
|
|
- T* GetEnd();
|
|
|
- const T* GetEnd() const;
|
|
|
+ T* begin();
|
|
|
+ const T* begin() const;
|
|
|
+ T* end();
|
|
|
+ const T* end() const;
|
|
|
|
|
|
private:
|
|
|
|
|
|
- int mCount; // The number of elements
|
|
|
- T mArray[size];
|
|
|
+ uint mCount; // The number of elements
|
|
|
+ T mArray[SIZE];
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
@@ -75,10 +73,10 @@ private:
|
|
|
@note
|
|
|
Does not initialize array memory.
|
|
|
*/
|
|
|
-template <typename T, int size>
|
|
|
-inline Array<T, size>::Array()
|
|
|
+template <typename T, uint SIZE>
|
|
|
+inline Array<T, SIZE>::Array()
|
|
|
{
|
|
|
- assert(size > 0);
|
|
|
+ assert(SIZE > 0);
|
|
|
|
|
|
mCount = 0;
|
|
|
|
|
|
@@ -88,8 +86,8 @@ inline Array<T, size>::Array()
|
|
|
/**
|
|
|
Copy constructor.
|
|
|
*/
|
|
|
-template <typename T, int size>
|
|
|
-inline Array<T, size>::Array(const Array<T, size>& array)
|
|
|
+template <typename T, uint SIZE>
|
|
|
+inline Array<T, SIZE>::Array(const Array<T, SIZE>& array)
|
|
|
{
|
|
|
*this = array;
|
|
|
}
|
|
|
@@ -97,22 +95,22 @@ inline Array<T, size>::Array(const Array<T, size>& array)
|
|
|
/**
|
|
|
Destructor.
|
|
|
*/
|
|
|
-template <typename T, int size>
|
|
|
-inline Array<T, size>::~Array()
|
|
|
+template <typename T, uint SIZE>
|
|
|
+inline Array<T, SIZE>::~Array()
|
|
|
{
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
Random access.
|
|
|
@note
|
|
|
- The index has to be smaller than GetSize()
|
|
|
+ The index has to be smaller than size()
|
|
|
@param index
|
|
|
The index
|
|
|
@return
|
|
|
The element at the given index
|
|
|
*/
|
|
|
-template <typename T, int size>
|
|
|
-inline T& Array<T, size>::operator[](int index)
|
|
|
+template <typename T, uint SIZE>
|
|
|
+inline T& Array<T, SIZE>::operator[](uint index)
|
|
|
{
|
|
|
assert(index >= 0);
|
|
|
assert(index < mCount);
|
|
|
@@ -123,14 +121,14 @@ inline T& Array<T, size>::operator[](int index)
|
|
|
/**
|
|
|
Random access.
|
|
|
@note
|
|
|
- The index has to be smaller than GetSize()
|
|
|
+ The index has to be smaller than size()
|
|
|
@param index
|
|
|
The index
|
|
|
@return
|
|
|
The element at the given index
|
|
|
*/
|
|
|
-template <typename T, int size>
|
|
|
-inline const T& Array<T, size>::operator[](int index) const
|
|
|
+template <typename T, uint SIZE>
|
|
|
+inline const T& Array<T, SIZE>::operator[](uint index) const
|
|
|
{
|
|
|
assert(index >= 0);
|
|
|
assert(index < mCount);
|
|
|
@@ -143,8 +141,8 @@ inline const T& Array<T, size>::operator[](int index) const
|
|
|
@return
|
|
|
True if empty, false otherwise
|
|
|
*/
|
|
|
-template <typename T, int size>
|
|
|
-inline bool Array<T, size>::IsEmpty() const
|
|
|
+template <typename T, uint SIZE>
|
|
|
+inline bool Array<T, SIZE>::empty() const
|
|
|
{
|
|
|
return mCount == 0;
|
|
|
}
|
|
|
@@ -154,8 +152,8 @@ inline bool Array<T, size>::IsEmpty() const
|
|
|
@return
|
|
|
The number of elements
|
|
|
*/
|
|
|
-template <typename T, int size>
|
|
|
-inline int Array<T, size>::GetSize() const
|
|
|
+template <typename T, uint SIZE>
|
|
|
+inline uint Array<T, SIZE>::size() const
|
|
|
{
|
|
|
return mCount;
|
|
|
}
|
|
|
@@ -165,10 +163,10 @@ inline int Array<T, size>::GetSize() const
|
|
|
@return
|
|
|
The maximum number of elements
|
|
|
*/
|
|
|
-template <typename T, int size>
|
|
|
-inline int Array<T, size>::GetCapacity() const
|
|
|
+template <typename T, uint SIZE>
|
|
|
+inline uint Array<T, SIZE>::get_capacity() const
|
|
|
{
|
|
|
- return size;
|
|
|
+ return SIZE;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -178,53 +176,14 @@ inline int Array<T, size>::GetCapacity() const
|
|
|
@return
|
|
|
The index of the element or -1 if full
|
|
|
*/
|
|
|
-template <typename T, int size>
|
|
|
-inline int Array<T, size>::Append(const T& element)
|
|
|
+template <typename T, uint SIZE>
|
|
|
+inline uint Array<T, SIZE>::push_back(const T& element)
|
|
|
{
|
|
|
- assert(mCount < size);
|
|
|
+ assert(mCount < SIZE);
|
|
|
|
|
|
- if (mCount < size)
|
|
|
- {
|
|
|
- mArray[mCount] = element;
|
|
|
-
|
|
|
- return mCount++;
|
|
|
- }
|
|
|
+ mArray[mCount] = element;
|
|
|
|
|
|
- return -1;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- Inserts an element in the array at a given index.
|
|
|
- Returns a value equal to index if success or -1 if full.
|
|
|
-@note
|
|
|
- Insert operation involves data moving.
|
|
|
-@param index
|
|
|
- The index where to insert the element
|
|
|
-@param element
|
|
|
- The element
|
|
|
-@return
|
|
|
- A value equal to index if success or -1 if full
|
|
|
-*/
|
|
|
-template <typename T, int size>
|
|
|
-inline int Array<T, size>::Insert(int index, const T& element)
|
|
|
-{
|
|
|
- assert(index >= 0);
|
|
|
- assert(index <= mCount);
|
|
|
- assert(mCount < size);
|
|
|
-
|
|
|
- if (mCount < size)
|
|
|
- {
|
|
|
- for (int i = mCount; i > index; i--)
|
|
|
- {
|
|
|
- mArray[i] = mArray[i-1];
|
|
|
- }
|
|
|
-
|
|
|
- mArray[index] = element;
|
|
|
-
|
|
|
- return mCount++;
|
|
|
- }
|
|
|
-
|
|
|
- return -1;
|
|
|
+ return mCount++;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -232,18 +191,12 @@ inline int Array<T, size>::Insert(int index, const T& element)
|
|
|
@param index
|
|
|
The index of the element to remove
|
|
|
*/
|
|
|
-template <typename T, int size>
|
|
|
-inline void Array<T, size>::Remove(int index)
|
|
|
+template <typename T, uint SIZE>
|
|
|
+inline void Array<T, SIZE>::pop_back()
|
|
|
{
|
|
|
- assert(index >= 0);
|
|
|
- assert(index < mCount);
|
|
|
+ assert(mCount > 0);
|
|
|
|
|
|
mCount--;
|
|
|
-
|
|
|
- for (int i = index; i < mCount; i++)
|
|
|
- {
|
|
|
- mArray[i] = mArray[i+1];
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -252,40 +205,21 @@ inline void Array<T, size>::Remove(int index)
|
|
|
Does not free memory, it only zeroes
|
|
|
the number of elements in the array.
|
|
|
*/
|
|
|
-template <typename T, int size>
|
|
|
-inline void Array<T, size>::Clear()
|
|
|
+template <typename T, uint SIZE>
|
|
|
+inline void Array<T, SIZE>::clear()
|
|
|
{
|
|
|
mCount = 0;
|
|
|
}
|
|
|
|
|
|
-/**
|
|
|
- Returns the index of a given element or -1 if not found.
|
|
|
-@return
|
|
|
- The index of the element of -1 if not found.
|
|
|
-*/
|
|
|
-template <typename T, int size>
|
|
|
-inline int Array<T, size>::Find(const T& element) const
|
|
|
-{
|
|
|
- for (int i = 0; i < mCount; i++)
|
|
|
- {
|
|
|
- if (mArray[i] == element)
|
|
|
- {
|
|
|
- return i;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return -1;
|
|
|
-}
|
|
|
-
|
|
|
/**
|
|
|
Copies the content of the other list into this.
|
|
|
@return
|
|
|
The reference to list after copying
|
|
|
*/
|
|
|
-template <typename T, int size>
|
|
|
-inline const Array<T, size>& Array<T, size>::operator=(const Array<T, size>& b)
|
|
|
+template <typename T, uint SIZE>
|
|
|
+inline const Array<T, SIZE>& Array<T, SIZE>::operator=(const Array<T, SIZE>& b)
|
|
|
{
|
|
|
- for (int i = 0; i < b.mCount; i++)
|
|
|
+ for (uint i = 0; i < b.mCount; i++)
|
|
|
{
|
|
|
mArray[i] = b.mArray[i];
|
|
|
}
|
|
|
@@ -296,29 +230,29 @@ inline const Array<T, size>& Array<T, size>::operator=(const Array<T, size>& b)
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-template <typename T, int size>
|
|
|
-inline const T* Array<T, size>::GetBegin() const
|
|
|
+template <typename T, uint SIZE>
|
|
|
+inline const T* Array<T, SIZE>::begin() const
|
|
|
{
|
|
|
return mArray;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-template <typename T, int size>
|
|
|
-inline T* Array<T, size>::GetBegin()
|
|
|
+template <typename T, uint SIZE>
|
|
|
+inline T* Array<T, SIZE>::begin()
|
|
|
{
|
|
|
return mArray;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-template <typename T, int size>
|
|
|
-inline const T* Array<T, size>::GetEnd() const
|
|
|
+template <typename T, uint SIZE>
|
|
|
+inline const T* Array<T, SIZE>::end() const
|
|
|
{
|
|
|
return mArray + (mCount - 1);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-template <typename T, int size>
|
|
|
-inline T* Array<T, size>::GetEnd()
|
|
|
+template <typename T, uint SIZE>
|
|
|
+inline T* Array<T, SIZE>::end()
|
|
|
{
|
|
|
return mArray + (mCount - 1);
|
|
|
}
|