summaryrefslogtreecommitdiff
path: root/src/old/arraylist.h
blob: 0fda34ac8e9351193376b8b1c78e8ff75c0b514b (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
/** \file arraylist.h
 * Describes the ArrayList class.
 *@author Mike Buland
 */
#ifndef ARRAY_LIST_H
#define ARRAY_LIST_H

#include "list.h"

/** A simple list which uses an array.  This is a great choice if you won't do
  * a lot of adding and deleting and need a fast random access list.  Otherwise
  * use the LinkedList.
  *@author Mike Buland
  */
class ArrayList : public List
{
public:
	/** Creates an arraylist with some pre-defined specs spelled out.
	  *@param initSize the inital number of elements to allocate.
	  *@param growByFactor How much to increase the size of the array by
	  * each time we run out of room.
	  */
	ArrayList( int initSize=100, int growByFactor=10 );
	/**
	 * Destroy the ArrayList
	 */
	virtual ~ArrayList();

	void *getAt( int nIndex );
	void append( void *pData );
	void insertBefore( void *pData, int nPos = 0 );
	int getSize(  );
	bool isEmpty(  );
	void deleteAt( int nIndex );
	void empty();
	void setSize( int nNewSize );
	void setAt( int nIndex, void *pData );

private:
	/**
	 * Checks to see if the system needs to be resized, if it does, this will
	 * automatically resize based on your parameters.
	 */
	void checkResize();

	/**
	 * Resize the system to a specified size.  If it is larger, then all data
	 * will be retained, if smaller the elements at the end will be cut off.
	 *@param newSize The number of elements to include after resizing.
	 */
	void resizeTo( int newSize );

	/**
	 * Actual master array of pointers.  This is done to follow the List specs.
	 * All data transactions are performed with pointers or compatable
	 * primitive data-types.
	 */
	void **apData;

	/**
	 * The number of filled in elements in the array.  This is the practical
	 * real size of the ArrayList for all userspace applications.
	 */
	int nSize;

	/**
	 * The number of elements allocated in memory.  Not all of these have to be
	 * filled in, and it is usually larger than nSize so that adding and
	 * deleting elements is fast and easy.
	 */
	int nCapacity;

	/**
	 * The amount to grow by whenever the array needs resizing.
	 */
	int nGrowByFactor;
};

#endif