add.c 535 Bytes
#include <search.h>

#include "hash.h"
#include "interface/hashable.h"
#include "interface/class.h"

static
inline
int
hashAddComp(const void * a, const void * b)
{
	return hashableGetHash((void*)b) - hashableGetHash((void*)a);
}

void *
hashAdd(Hash this, void * operand)
{
	void * found = tsearch(operand, &(this->root), hashAddComp);

	if (NULL == found) {
		return NULL;
	}

	if (operand != *(void**)found) {
		hashableHandleDouble(*(void**)found, operand);
		delete(operand);
	}

	return *(void**)found;
}

// vim: set ts=4 sw=4: