head	1.1;
access;
symbols;
locks
	baron:1.1; strict;
comment	@// @;


1.1
date	99.03.31.18.54.56;	author baron;	state Exp;
branches;
next	;


desc
@class to hold a ptr/key combo
@


1.1
log
@Initial revision
@
text
@#include <stdlib.h>
#include <string.h>
#include "dictionary.h"


Dictionary::Dictionary(int n)
{
	num_items = 0;
	allocated = 0;
	alloc_size = n;
	data = NULL;
	
	get_mem();
}

Dictionary::~Dictionary()
{
	if (data) free(data);
}

void Dictionary::get_mem(void)
{
	allocated += alloc_size;
	if (!data)
		data = (_data *)malloc(allocated * sizeof(_data));
		
	else
		data = (_data *)realloc(data, allocated * sizeof(_data));
}

void Dictionary::insert(int K, void *I)
{

	if (num_items >= allocated)
		get_mem();
	
	if (data)
	{
			data[num_items].key = K;
			data[num_items].item = I;
			
			num_items++;
	}

}

void Dictionary::remove(int K)
{
int i;

	if (data)
	{
		for( i = 0; i < num_items; i++)
		{ 
			if (data[i].key == K) //matched !
			{
				num_items--;
				memmove(&data[i], &data[i+1], (num_items - i) * sizeof(_data));
				break;	
			}
		}
	}
}


void * Dictionary::find(int K) const
{
int i;

	if (data)
	{
		for (i = 0; i < num_items; i++)
		{
			if (data[i].key == K) return data[i].item;
		}
	}	
	return NULL;
}
@
