#include <andrewos.h>
#include <class.h>

#include <extlist.ih>
#include <ordkeyset.eh>

boolean orderedlistkeyset__InitializeObject(classid, self)
struct classheader *classid;
struct orderedlistkeyset *self;
{
    return TRUE;
}

void orderedlistkeyset__FinalizeObject(classid, self)
struct classheader *classid;
struct orderedlistkeyset *self;
{
}

struct findworkinfo {
    void *key;
    boolean (*compareFunc)();
    long top;
    long bottom;
};

long FindItemWork(item, ind, info)
struct listkeysetitem *item;
long ind;
struct findworkinfo *info;
{
    long diff;

    if ((info->compareFunc == NULL
	  && (diff = (((long) item->key) - ((long) info->key))) == 0)
	 || (info->compareFunc != NULL
	     && (diff = (*info->compareFunc)(item->key, info->key)) == 0)) {
	return -2;
    }
    if (diff > 0) {
	/* Move left */
	info->top = ind;
    }
    else {
	/* Move Right */
	info->bottom = ind;
    }
    if (info->top == info->bottom + 1) {
	return -1;
    }
    return (info->top + info->bottom) / 2;
}    

long orderedlistkeyset__FindItem(self, key, previous)
struct orderedlistkeyset *self;
void *key;
long *previous;
{
    struct findworkinfo info;
    long ind; 

    info.key = key;
    info.compareFunc = orderedlistkeyset_GetCompareFunction(self);
    info.bottom = -1;

    if ((info.top = orderedlistkeyset_NumberOfElements(self)) > 0) {
	ind = extendedlist_Traverse(orderedlistkeyset_GetInternalList(self), (info.top + info.bottom) / 2, FindItemWork, &info);
    }

    if (ind < 0)  {
	*previous = info.bottom;
	return -1;
    }
    else {
	return ind;
    }
}


void orderedlistkeyset__InsertItem(self, item, previous)
struct orderedlistkeyset *self;
struct listkeysetitem *item;
long previous;
{
    extendedlist_AddNthElement(orderedlistkeyset_GetInternalList(self), previous + 1, item);
}

struct foreachinfo {
    boolean (*proc)();
    long end;
    boolean stopped;
    void *rock;
};

static long ForEachWork(item, ind, info)
struct listkeysetitem *item;
long ind;
struct foreachinfo *info;
{
    if ((*info->proc)(item->key, item->value, info->rock)) {
	info->stopped = TRUE;
	return -1;
    }
    else {
	ind++;
	return (ind < info->end) ? ind : -1;
    }
}

void *orderedlistkeyset__GetNthValue(self, pos)
struct orderedlistkeyset *self;
long pos;
{
    struct listkeysetitem *item;

    item = (struct listkeysetitem *) extendedlist_GetNth(orderedlistkeyset_GetInternalList(self), pos);

    if (item != NULL) {
	return item->value;
    }
    else {
	return NULL;
    }
}

void *orderedlistkeyset__GetNthKey(self, pos)
struct orderedlistkeyset *self;
long pos;
{
    struct listkeysetitem *item;

    item = (struct listkeysetitem *) extendedlist_GetNth(orderedlistkeyset_GetInternalList(self), pos);

    if (item != NULL) {
	return item->key;
    }
    else {
	return NULL;
    }
}

long orderedlistkeyset__ForEachInRange(self, startInd, endInd,  stopProc, rock)
struct orderedlistkeyset *self;
long startInd;
long endInd;
boolerror (*stopProc)(/* void *key, void *value, void *rock */);
void *rock;
{
    struct foreachinfo info;
    long ind;

    info.proc = stopProc;
    info.end = endInd;
    info.stopped = FALSE;
    info.rock = rock;

    ind = extendedlist_Traverse(orderedlistkeyset_GetInternalList(self), startInd, ForEachWork, &info);

    return (info.stopped) ? ind : -1;
}
