#include "StdAfx.h"

PacketsBuffer::PacketsBuffer( int capacity_par )
{
	packets = 0;
	Resize( capacity_par );
}

PacketsBuffer::~PacketsBuffer()
{
	if( packets )
	{
		for( int i=0 ; i<capacity ; i++ )
			delete packets[i];
		delete[] packets;
	}
}

void PacketsBuffer::Clear()
{
	for( int i=0 ; i<capacity ; i++ )
		packets[i] = 0;
}

void PacketsBuffer::Resize( int new_capacity )
{
	if( packets )
		delete[] packets;
	
	capacity = new_capacity;
	packets = new PacketPointer[capacity];

	Clear();
}

bool PacketsBuffer::Push( Packet * packet )
 // return false if full
{
	assert( packet );

	// find empty entry
	int index = 0;
	while( index < capacity && packets[index] != 0 )
		index++;
	if( index == capacity )
		return false;
	else
		packets[index] = packet;
	return true;
}

Packet * PacketsBuffer::Pop()
// 0 if empty
{
	PacketPointer answer = packets[0];

	for( int i=0 ; i<capacity-1 && packets[i] != 0 ; i++ )
	{
		packets[i] = packets[i+1];
	}
	packets[capacity-1] = 0;

	return answer;
}

int PacketsBuffer::GetLoad() const
{
	int counter = 0;
	while( counter < capacity && packets[counter] != 0 )
		counter++;
	return counter;
}
