wxArrayofArray(s) Topic is solved

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
dirkmausf
In need of some credit
In need of some credit
Posts: 7
Joined: Tue May 15, 2007 9:05 pm
Location: Freiburg
Contact:

wxArrayofArray(s)

Post by dirkmausf » Fri Dec 31, 2010 1:02 pm

Hi all!

One day i came across the problem of needing a 2 dimansional array.

Here is my solution as a template class:

Code: Select all

// ArrayOfArray.h 
//
#ifndef _ACE_ARRAYOFARRAY_H
#define _ACE_ARRAYOFARRAY_H
#pragma once
#include "./array.h"

//This template is a 2 dimensional array.
//You should use container classes which implements GetAt(),
//SetAt(), GetCount() and Add() methods


template<class ARRAYTYPE, class CLASSTYPE>
class wxArrayOfArray : public wxObject
{
protected:

	 wxCArray<ARRAYTYPE*,ARRAYTYPE*> m_arrArrays;

public:

	wxArrayOfArray(void);
	~wxArrayOfArray(void);
	
	ARRAYTYPE* GetArray(int in_iArray);
	int AddArray(ARRAYTYPE* in_lpArray);
	void SetArrayAt(int in_iIndex, ARRAYTYPE* in_lpArray);
	CLASSTYPE* GetItem(int in_iArray, int in_iIndex);
	void SetItem(int in_iArray, int in_iIndex, CLASSTYPE* in_Item);
	void ClearArray(int in_iArray);
	void ClearAllArrays();
	int GetCount();
};


template<class ARRAYTYPE, class CLASSTYPE> 
wxArrayOfArray<ARRAYTYPE, CLASSTYPE>::wxArrayOfArray(void)
{
	m_arrArrays.RemoveAll(); 
}

template<class ARRAYTYPE, class CLASSTYPE> 
wxArrayOfArray<ARRAYTYPE, CLASSTYPE>::~wxArrayOfArray(void)
{   
	m_arrArrays.RemoveAll(); 
}

template<class ARRAYTYPE, class CLASSTYPE> ARRAYTYPE* 
wxArrayOfArray<ARRAYTYPE, CLASSTYPE>::GetArray(int in_iArray)							
{ 
	return m_arrArrays.GetAt(in_iArray); 
}

template<class ARRAYTYPE, class CLASSTYPE> 
int wxArrayOfArray<ARRAYTYPE, CLASSTYPE>::AddArray(ARRAYTYPE* in_lpArray)
{ 
	return m_arrArrays.Add(in_lpArray); 
}

template<class ARRAYTYPE, class CLASSTYPE> 
void wxArrayOfArray<ARRAYTYPE, CLASSTYPE>::SetArrayAt(int in_iIndex, ARRAYTYPE* in_lpArray)
{
	m_arrArrays.SetAt(in_iIndex, in_lpArray); 
}

template<class ARRAYTYPE, class CLASSTYPE> 
CLASSTYPE* wxArrayOfArray<ARRAYTYPE, CLASSTYPE>::GetItem(int in_iArray, int in_iIndex)
{

	ARRAYTYPE* in_lpArray = m_arrArrays.GetAt(in_iArray); 

	if (in_lpArray)
	{
		return (CLASSTYPE*)in_lpArray->GetAt(in_iIndex);
	}
}


template<class ARRAYTYPE, class CLASSTYPE> 
void wxArrayOfArray<ARRAYTYPE, CLASSTYPE>::SetItem(int in_iArray, int in_iIndex, CLASSTYPE* in_Item)
{
	ARRAYTYPE* in_lpArray = m_arrArrays.GetAt(in_iArray); 
	if (in_lpArray)
	{
		in_lpArray->SetAt(in_iIndex, in_Item);
	}
}

template<class ARRAYTYPE, class CLASSTYPE> 
void wxArrayOfArray<ARRAYTYPE, CLASSTYPE>::ClearArray(int in_iArray)
{
	ARRAYTYPE* in_lpArray = m_arrArrays.GetAt(in_iArray); 
	if (in_lpArray)
	{
		for (int i=0; i<in_lpArray->GetCount(); i++)
		{
			CLASSTYPE* lpClass = (CLASSTYPE*)in_lpArray->GetAt(i);
			if (lpClass) delete lpClass;
		}
		in_lpArray->RemoveAll();
	}
	// TODO: are you sure, that you don't want to delete in_lpArray and remove the item at in_iArray, too???
}	

template<class ARRAYTYPE, class CLASSTYPE> 
void wxArrayOfArray<ARRAYTYPE, CLASSTYPE>::ClearAllArrays() 
{	
	for (int iArray=0; iArray<m_arrArrays.GetCount(); iArray++)
	{
		ARRAYTYPE* in_lpArray = m_arrArrays.GetAt(iArray);
		
		if (in_lpArray)
		{
			for (int i=0; i<in_lpArray->GetCount(); i++)
			{
				CLASSTYPE* lpClass = (CLASSTYPE*)in_lpArray->GetAt(i);
				if (lpClass) delete lpClass;
			}
			
			in_lpArray->RemoveAll();
			delete in_lpArray;
		}
	}
	m_arrArrays.RemoveAll();	
}
															
template<class ARRAYTYPE, class CLASSTYPE> int wxArrayOfArray<ARRAYTYPE, CLASSTYPE>::GetCount()
{ 
	return m_arrArrays.GetCount(); 
}	

#endif



Greetings, Dirk

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Fri Dec 31, 2010 3:38 pm

Hi,


nice, but out of curiosity, why not simply use std::vector< std::vector< T > > ?
"Keyboard not detected. Press F1 to continue"
-- Windows

dirkmausf
In need of some credit
In need of some credit
Posts: 7
Joined: Tue May 15, 2007 9:05 pm
Location: Freiburg
Contact:

Post by dirkmausf » Sat Jan 01, 2011 12:15 pm

Hi all!

Thanks for your advise. You are totally right!

There is always a simpler way to do it.

Why messing around with templates when using templates anyway... this is a good point and I appreciate your response.

The advance in my class is: I don't necessarily have to use vectors. That's why this class is a template... Use your favorite container class. As long as they implement the necessary functions, they are exchangeable.

I really hate the way STL classes look like... and I like the way, wxWidgets encapsulated this great functionality.

I tried to mimic that looking, but maybe I failed...

Please keep in mind: If you don't like it, you dont have to use it... cool, eye? ;)

Greetings,

Dirk

Post Reply