aboutsummaryrefslogtreecommitdiff
path: root/src/old/arraylist.cpp
blob: ef214267ec6dc3718fdf082ee78c8d07365b7eba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "arraylist.h"
#include <stdlib.h>
#include <string.h>

ArrayList::ArrayList( int initSize, int growByFactor )
{
	apData = new void *[initSize];
	nSize = 0;
	nCapacity = initSize;
	nGrowByFactor = growByFactor;
}

ArrayList::~ArrayList(  )
{
	delete[] apData;
}

void *ArrayList::getAt( int index )
{
	if( index < 0 || index > nSize )
		return NULL;

	return apData[index];
}

void ArrayList::append( void *data )
{
	insertBefore( data, nSize );
}

void ArrayList::insertBefore( void *data, int pos )
{
	if( pos < 0 || pos > nSize )
		return;

	checkResize();
	memmove( &apData[pos+1], &apData[pos], (nSize-pos)*sizeof(void*) );
	apData[pos] = data;
	nSize++;
}

int ArrayList::getSize(  )
{
	return nSize;
}

bool ArrayList::isEmpty(  )
{
	return nSize==0;
}

void ArrayList::deleteAt( int index )
{
	if( index < 0 || index >= nSize )
		return;

	memmove( &apData[index], &apData[index+1], (nSize-index-1)*sizeof(void *) );
	nSize--;
}

void ArrayList::empty()
{
	// Probably the easiest as far as things go.
	nSize = 0;
}

void ArrayList::resizeTo( int newSize )
{
	void **apNew = new void *[newSize];
	memmove( apNew, apData, nSize*sizeof(void *) );
	nCapacity = newSize;
	delete[] apData;
	apData = apNew;
}

void ArrayList::checkResize()
{
	if( nSize >= nCapacity )
	{
		resizeTo( nCapacity + nGrowByFactor );
	}
}

void ArrayList::setSize( int newSize )
{
	if( newSize < 0 )
		return;

	nSize = newSize;
	checkResize();
}

void ArrayList::setAt( int index, void *data )
{
	if( index < 0 || index >= nSize )
		return;

	apData[index] = data;
}