Array class

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Array class

Post by GroundUpEngine »

I felt like sharing this, I just made a array class for my Engine and it's as close as I code get to stl vector. I did this to familiarise myself with templates and such. Feel free to make suggestions, or borrow ideas from it. I lost the file data from a power cut, but I managed to write it again in a couple of minutes so I think I got templates down ;)

Code: Select all

/********************************************************
*  Array.hpp
*  GroundUpEngine
*
*  Created by Hebron Sawyers
*  Last Updated on 29/05/10
*  Copyright 2009 by Hebron Sawyers. All rights reserved.
*
*********************************************************/

#ifndef __ARRAY_HPP
#define __ARRAY_HPP

namespace gue
{
	template<class T>
	class Array {
	  private:
		/// Class Private Variables ///
		T *Elements;
		unsigned int Array_Size;

	  public:
		/// De/Constructor ///
		Array(unsigned int size = 0);
		~Array();

		/// Array Functions ///
		unsigned int size();
		void Resize(unsigned int size);
		void push_back(T val);
		void erase(unsigned int place);
		void clear();

		/// Array Operators ///
		T& operator[](unsigned int place);
		T* operator*();
	};

	/// ---------------------------------------- De/Constructor ---------------------------------------- ///
	template<class T>
	Array<T>::Array(unsigned int size)
	{
		Elements = new T[size];
		Array_Size = size;
	}

	template<class T>
	Array<T>::~Array()
	{
		delete[] Elements;
	}

	/// ---------------------------------------- Array Functions ---------------------------------------- ///
	template<class T>
	unsigned int Array<T>::size()
	{
		return Array_Size;
	}

	template<class T>
	void Array<T>::Resize(unsigned int size)
	{
		T *temp = new T[size];

		int min = 0;
		if(size < Array_Size) min = size;
		else min = Array_Size;

		for(int i = 0; i < min; i++) {
			temp[i] = Elements[i];
		}

		Array_Size = size;

		delete[] Elements;
		Elements = temp;
	}

	template<class T>
	void Array<T>::push_back(T val)
	{
	    Resize(Array_Size+1);

	    Elements[Array_Size-1] = val;
	}

	template<class T>
	void Array<T>::erase(unsigned int place)
	{
		T *temp = new T[Array_Size-1];

		unsigned int curr = 0;
		for(unsigned int i = 0; i < Array_Size; i++) {
			if(place != i+1) {
				temp[curr] = Elements[i];
				curr++;
			}
		}

		Array_Size -= 1;

		delete[] Elements;
		Elements = temp;
	}

	template<class T>
	void Array<T>::clear()
	{
	    Array_Size = 0;

	    delete[] Elements;
	}

	/// ---------------------------------------- Array Operators ---------------------------------------- ///
	template<class T>
	T& Array<T>::operator[](unsigned int place)
	{
		return Elements[place];
	}

	template<class T>
	T* Array<T>::operator*()
	{
	    return Elements;
	}

} // namespace gue

#endif // __ARRAY_HPP
Last edited by GroundUpEngine on Sat May 29, 2010 9:32 am, edited 1 time in total.
User avatar
cypher1554R
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1124
Joined: Sun Jun 22, 2008 5:06 pm

Re: Array class

Post by cypher1554R »

sometimes you have to reinvent the wheel to get insight on how it works :)

much appreciated share. thanks!
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Array class

Post by GroundUpEngine »

Agreed hehe, np 8-)
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: Array class

Post by X Abstract X »

Hey awesome, are you going to use it too or was it just for knowledge? I think your push_back function might be really inefficient though. I believe the way the STL does it is by doubling the current capacity when the need for more capacity arises.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Array class

Post by GroundUpEngine »

X Abstract X wrote:Hey awesome, are you going to use it too or was it just for knowledge? I think your push_back function might be really inefficient though. I believe the way the STL does it is by doubling the current capacity when the need for more capacity arises.
Both, hmm ok well I was under the impression that push_back() makes room for one more element hehe ;)
STL is kinda iffy on embedded systems IIRC, so if I ever do anything like that I could benefit from my own array class (: (:
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Array class

Post by programmerinprogress »

Agree with everyone else, nice post ;)

I should do more stuff like this, to get myself up to speed after a large break from hobbyist programming.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Array class

Post by GroundUpEngine »

Made some changes ->
  • added clear() function
    some places had delete Elements instead of delete[] Elements
programmerinprogress wrote:Agree with everyone else, nice post ;)

I should do more stuff like this, to get myself up to speed after a large break from hobbyist programming.
Thanks! It's good to break away from bigger projects and just practise techniques sometimes ;)
Post Reply