Blob Blame Raw

#include "private.h"


void heliPairInit(HeliPair *p) {
	p->key = p->value = NULL;
	p->freeKey = p->freeValue = NULL;
}

void heliPairDestroy(HeliPair *p) {
	HeliPair pp;
	memcpy(&pp, p, sizeof(pp));
	heliPairInit(p);
	if (pp.key && pp.freeKey) pp.freeKey(pp.key);
	if (pp.value && pp.freeValue) pp.freeValue(pp.value);
}

void heliArrayInit(HeliArray *a) {
	a->items = NULL;
	a->count = a->allocated = 0;
}

void heliArrayDestroy(HeliArray *a) {
	heliArrayClear(a);
	free(a->items);
	heliArrayInit(a);
}

HeliPair* heliArrayGet(HeliArray *a, int i)
	{ return i >= 0 && i < a->count ? &a->items[i] : NULL; }

void* heliArrayGetKey(HeliArray *a, int i) {
	HeliPair *item = heliArrayGet(a, i);
	return item ? item->key : NULL;
}

void* heliArrayGetValue(HeliArray *a, int i) {
	HeliPair *item = heliArrayGet(a, i);
	return item ? item->value : NULL;
}

HeliPair* heliArrayInsertPair(HeliArray *a, int i, void *k, HeliFreeCallback fk, void *v, HeliFreeCallback fv) {
	if (i < 0 || i > a->count)
		i = a->count;
	if (a->allocated < a->count + 1) {
		a->allocated += a->allocated/4 + 32;
		a->items = realloc(a->items, a->allocated*sizeof(a->items[0]));
		memset(&a->items[a->count], 0, (a->allocated - a->count)*sizeof(a->items[0]));
	}
	if (i < a->count) {
		memmove(&a->items[i+1], &a->items[i], (a->count-i)*sizeof(a->items[0]));
		memset(&a->items[i], 0, sizeof(a->items[0]));
	}
	++a->count;
	
	HeliPair *item = &a->items[i];
	item->key = k;
	item->value = v;
	item->freeKey = fk;
	item->freeValue = fv;
	return item;
}

HeliPair* heliArrayInsert(HeliArray *a, int i, void *v, HeliFreeCallback fv)
	{ return heliArrayInsertPair(a, i, NULL, NULL, v, fv); }

void heliArrayRemove(HeliArray *a, int i) {
	if (i < 0 || i >= a->count) return;
	HeliPair item;
	memcpy(&item, &a->items[i], sizeof(item));
	memmove(&a->items[i], &a->items[i+1], (a->count-i-1)*sizeof(a->items[0]));
	--a->count;
	memset(&a->items[a->count], 0, sizeof(a->items[0]));
	heliPairDestroy(&item);
}

void heliArrayClear(HeliArray *a) {
	while(a->count > 0)
		heliArrayRemove(a, 0);
}


HeliPair* heliMapFind(HeliArray *a, const void *k, HeliCompareCallback cmp, int *gtOrEqIndex) {
	int i0 = 0;
	int i1 = a->count - 1;
	int c = i0 < a->count ? cmp(a->items[i0].key, k) : -1;
	if (c >= 0) {
		if (gtOrEqIndex) *gtOrEqIndex = i0;
		return c ? NULL : &a->items[i0];
	}
	if (i1 > i0) c = cmp(a->items[i1].key, k);
	if (c < 0) {
		if (gtOrEqIndex) *gtOrEqIndex = a->count;
		return NULL;
	}
	if (c == 0) {
		if (gtOrEqIndex) *gtOrEqIndex = i1;
		return &a->items[i1];
	}
	
	while(i0 + 1 < i1) {
		int i = (i0 + i1)/2;
		c = cmp(a->items[i].key, k);
		if (c == 0) {
			if (gtOrEqIndex) *gtOrEqIndex = i;
			return &a->items[i];
		}
		if (c < 0) i0 = i; else i1 = i;
	}
	if (gtOrEqIndex) *gtOrEqIndex = i1;
	return NULL;
}

HeliPair* heliMapGet(HeliArray *a, const void *k, HeliCompareCallback cmp)
	{ return heliMapFind(a, k, cmp, NULL); }

HeliPair* heliMapAdd(
	HeliArray *a, const void *k, HeliCompareCallback cmp,
	HeliCloneCallback ck, HeliFreeCallback fk,
	void *v, HeliFreeCallback fv )
{
	int i;
	HeliPair *item = heliMapFind(a, k, cmp, &i);
	if (item) {
		if (item->value != v) {
			void *vv = item->value;
			HeliFreeCallback ffvv = item->freeValue;
			item->value = v;
			item->freeValue = fv;
			if (vv && ffvv) ffvv(vv);
		}
	} else {
		item = heliArrayInsertPair(a, i, ck(k), fk, v, fv);
	}
	return item;
}

int heliMapRemove(HeliArray *a, const void *k, HeliCompareCallback cmp) {
	int i;
	if (heliMapFind(a, k, cmp, &i)) {
		heliArrayRemove(a, i);
		return TRUE;
	}
	return FALSE;
}

static int uintCompare(size_t a, size_t b)
	{ return a < b ? -1 : (a > b ? 1: 0); }
static size_t uintClone(size_t a)
	{ return a; }
HeliPair* heliUIntFind(HeliArray *a, size_t k, int *gtOrEqIndex)
	{ return heliMapFind(a, (const void*)k, (HeliCompareCallback)&uintCompare, gtOrEqIndex); }
HeliPair* heliUIntGet(HeliArray *a, size_t k)
	{ return heliUIntFind(a, k, NULL); }
HeliPair* heliUIntAdd(HeliArray *a, size_t k, void *v, HeliFreeCallback fv)
	{ return heliMapAdd(a, (const void*)k, (HeliCompareCallback)&uintCompare, (HeliCloneCallback)&uintClone, NULL, v, fv); }
int heliUIntRemove(HeliArray *a, size_t k)
	{ return heliMapRemove(a, (const void*)k, (HeliCompareCallback)&uintCompare); }

HeliPair* heliStringmapFind(HeliArray *a, const char *k, int *gtOrEqIndex)
	{ return heliMapFind(a, k, (HeliCompareCallback)&strcmp, gtOrEqIndex); }
HeliPair* heliStringmapGet(HeliArray *a, const char *k)
	{ return heliStringmapFind(a, k, NULL); }
HeliPair* heliStringmapAdd(HeliArray *a, const char *k, void *v, HeliFreeCallback fv)
	{ return heliMapAdd(a, k, (HeliCompareCallback)&strcmp, (HeliCloneCallback)&heliStringCopy, &free, v, fv); }
int heliStringmapRemove(HeliArray *a, const char *k)
	{ return heliMapRemove(a, k, (HeliCompareCallback)&strcmp); }