summaryrefslogtreecommitdiff
path: root/src/ringlist.cpp
blob: 9efbbc4dda3ac8bfedf452dfecfe7e9e1b4a1587 (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
101
102
103
104
105
106
//
// C++ Implementation: ringlist
//
// Description: 
//
//
// Author: Mike Buland <eichlan@yf-soft.com>, (C) 2005
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include <stdlib.h>

#include "ringlist.h"

RingList::RingList( int nInitSize )
 : List()
{
	nFirstIndex = 0;
	nRealLength = nInitSize;
	nDataLength = 0;
	apData = new void*[nInitSize];
	pPushBuf = NULL;
}

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

void *RingList::getAt( int nIndex )
{
	if( nIndex < 0 || nIndex >= nDataLength )
	{
		return NULL;
	}
	
	return apData[(nFirstIndex+nIndex)%nRealLength];
}

void RingList::append( void *pData )
{
	int nIndex = (nFirstIndex+nDataLength)%nRealLength;
	
	pPushBuf = apData[nIndex];
	apData[nIndex] = pData;
	
	if( nDataLength == nRealLength )
	{
		nFirstIndex = (nFirstIndex+1)%nRealLength;
	}
	else
	{
		nDataLength++;
		// We really didn't need it this time...
		pPushBuf = NULL;
	}
}

void RingList::insertBefore( void *pData, int nPos )
{
	// Not implemented right now, don't even try it!
}

int RingList::getSize()
{
	return nDataLength;
}

bool RingList::isEmpty()
{
	return nDataLength==0;
}

void RingList::deleteAt( int nIndex )
{
	// Also not implemented yet
}

void RingList::empty()
{
	nFirstIndex = 0;
	nDataLength = 0;
}

void RingList::setSize( int nNewSize )
{
	if( apData )
	{
		delete[] apData;
	}
	nFirstIndex = 0;
	nRealLength = nNewSize;
	nDataLength = 0;
	apData = new void*[nNewSize];
}

void RingList::setAt( int nIndex, void *pData )
{
	apData[(nIndex+nFirstIndex)%nRealLength] = pData;
}

void *RingList::getPushBuf()
{
	return pPushBuf;
}