/*****************************************************************************
 * Copyright (c) 2005  Daniel Lerch Hostalot <http://daniellerch.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 ****************************************************************************/



#include "set.h"
#include <stdlib.h>

void* set_remove (set_t* obj, void *element)
{
	void* elm;
	int i=0;

	list_begin(obj);
	while ((elm=list_next(obj))) {

		if (obj->cmpfunc) {
			if (obj->cmpfunc(elm, element)==0) return list_remove (obj, i);
		}
		else {
			if (elm==element) return list_remove (obj, i);
		}
		i++;
	}	
	return NULL;
}

void set_union (set_t *set_u, set_t* set_a, set_t* set_b)
{
	void* elm;

	list_begin (set_a);
	while ((elm=list_next(set_a))) set_add (set_u, elm);

	list_begin (set_b);
	while ((elm=list_next(set_b))) 
		if (!set_contains (set_u, elm)) set_add (set_u, elm);
}

void set_intersection (set_t *set_i, set_t* set_a, set_t* set_b)
{
	void* elm;

	list_begin (set_b);
	while ((elm=list_next(set_b))) 
		if (set_contains (set_a, elm)) set_add (set_i, elm);
}

void set_difference (set_t *set_d, set_t* set_a, set_t* set_b)
{
	void* elm;

	list_begin (set_a);
	while ((elm=list_next(set_a))) 
		if (!set_contains (set_b, elm)) set_add (set_d, elm);
}





