#include <andrewos.h>
#include <class.h>
#include <newms.h>
#include <extlist.ih>
#include <lstkeyset.eh>

static void FreeItem(item, self)
struct listkeysetitem *item;
struct listkeyset *self;
{
    if (listkeyset_GetKeyFreeFunction(self) != NULL) {
	(*listkeyset_GetKeyFreeFunction(self))(item->key);
    }
    if (listkeyset_GetValueFreeFunction(self) != NULL) {
	(*listkeyset_GetValueFreeFunction(self))(item->value);
    }
}

boolean listkeyset__InitializeObject(classid, self)
struct classheader *classid;
struct listkeyset *self;
{
    self->list = extendedlist_New();

    return TRUE;
}

void listkeyset__FinalizeObject(classid, self)
struct classheader *classid;
struct listkeyset *self;
{
    extendedlist_ForEach(self->list, FreeItem, self);
    extendedlist_Destroy(self->list);
}

struct findprocinfo {
    void *key;
    boolean (*compareFunc)();
};

static boolean FindProc(item, info)
struct listkeysetitem *item;
struct findprocinfo *info;
{
    if (info->compareFunc == NULL) {
	return item->key == info->key;
    }
    else {
	return (*info->compareFunc)(item->key, info->key) == 0;
    }
}

long listkeyset__FindItem(self, key, findRock)
struct listkeyset *self;
void *key;
void **findRock;
{
    struct findprocinfo info;

    info.key = key;
    info.compareFunc = listkeyset_GetCompareFunction(self);

    return extendedlist_ForEach(self->list, FindProc, &info);

}

    

boolean listkeyset__Lookup(self, key, value)
struct listkeyset *self;
void *key;
void **value;
{
    long ind;
    void *findRock;

    ind = listkeyset_FindItem(self, key, &findRock);

    if (ind >= 0) {
	struct listkeysetitem *item = (struct listkeysetitem *) extendedlist_GetNth(self->list, ind);
	if (value != NULL) {
	    *value = item->value;
	}
	return TRUE;
    }

    return FALSE;
}

void listkeyset__InsertItem(self, item, findRock)
struct listkeyset *self;
struct listkeysetitem *item;
void *findRock;
{
    extendedlist_AddElement(self->list, item);
}

void listkeyset__Insert(self, key, value)
struct listkeyset *self;
char *key;
void *value;
{
    long ind;
    struct listkeysetitem *item;

    ind = listkeyset_FindItem(self, key, NULL);

    if (ind < 0) {
	item = (struct listkeysetitem *) malloc(sizeof(struct listkeysetitem));
	if (listkeyset_GetKeyCopyFunction(self) != NULL) {
	    key = (*listkeyset_GetKeyCopyFunction(self))(key);
	}
	item->key = key;
	listkeyset_InsertItem(self, item, NULL);
    }
    else {
	item = (struct listkeysetitem *) extendedlist_GetNth(self->list, ind);
	if (listkeyset_GetValueFreeFunction(self) != NULL) {
	    (*listkeyset_GetValueFreeFunction(self))(item->value);
	}
    }
    if (listkeyset_GetValueCopyFunction(self) != NULL) {
	value = (*listkeyset_GetValueCopyFunction(self))(value);
    }
    item->value = value;
    listkeyset_IncrementNumberOfElements(self);
}

boolean listkeyset__Remove(self, key, retKey, retValue)
struct listkeyset *self;
void *key;
void **retKey;
void **retValue;
{
    long ind;

    ind = listkeyset_FindItem(self, key, NULL);

    if (ind >= 0) {
	struct listkeysetitem *item = extendedlist_GetNth(self->list, ind);

	if (listkeyset_GetKeyFreeFunction(self) != NULL) {
	    (*listkeyset_GetKeyFreeFunction(self))(item->key);
	    item->key = NULL;
	}
	if (retKey != NULL) {
	    *retKey = item->key;
	}

	if (listkeyset_GetValueFreeFunction(self) != NULL) {
	    (*listkeyset_GetValueFreeFunction(self))(item->value);
	    item->value = NULL;
	}
	if (retValue != NULL) {
	    *retValue = item->value;
	}

	extendedlist_RemoveNthElement(self->list, ind);
	listkeyset_DecrementNumberOfElements(self);
	return TRUE;
    }
    return FALSE;
}

struct getlistinfo {
    boolean (*func)();
    void *rock;
    struct extendedlist *list;
};

static boolean GetListWork(item, info)
struct listkeysetitem *item;
struct getlistinfo *info;
{
    if (info->func == NULL || (*info->func)(item->key, item->value, info->rock)) {
	extendedlist_AddElement(info->list, item->value);
    }
    return FALSE;
}

struct extendedlist *listkeyset__GetList(self, include, rock)
struct listkeyset *self;
boolean (*include)(/* void *key, void *value. void *rock */);
void *rock;
{
    struct getlistinfo info;

    info.list = extendedlist_New();
    info.func = include;
    info.rock = rock;

    extendedlist_ForEach(self->list, GetListWork, &info);
    return info.list;
}

static boolean ForEachPruneWork(item, info)
struct listkeysetitem *item;
struct getlistinfo *info;
{
    return (*info->func)(item->key, item->value, info->rock);
}

void *listkeyset__ForEach(self, stopFunc, rock)
struct listkeyset *self;
boolean (*stopFunc)(/* void *key, void *value, void *rock */);
void *rock;
{
    struct getlistinfo info;
    long ind;

    info.func = stopFunc;
    info.rock = rock;

    ind = extendedlist_ForEach(self->list, ForEachPruneWork, &info);
    if (ind >= 0) {
	struct listkeysetitem *item;

	item = (struct listkeysetitem *) extendedlist_GetNth(self->list, ind);

	return item->key;
    }
    else {
	return NULL;
    }
}

void listkeyset__Prune(self, include, rock)
struct listkeyset *self;
boolean (*include)(/* void *key, void *value, void *rock */ );
void *rock;
{
    struct getlistinfo info;

    info.func = include;
    info.rock = rock;

    extendedlist_Prune(self->list, ForEachPruneWork, &info);
    listkeyset_SetNumberOfElements(self, extendedlist_NumberOfElements(self->list));
}

void listkeyset__Clear(self)
struct listkeyset *self;
{
    extendedlist_ForEach(self->list, FreeItem, self);
    extendedlist_Clear(self->list);

    super_Clear(self);
}
