Ejemplo n.º 1
0
//set_intersection
int set_intersection(Set *seti, const Set *set1, const Set *set2)
{
    ListElmt *member;
    void *data;

    //Initialize the set for the intersection.
    set_init(seti, set1->match, NULL);

    //insert the members present in both sets.
    for (member = list_head(set1); member != NULL; member = list_next(member))
    {
        if (set_is_member(set2, list_data(member)))
        {
            data = list_data(member);
            if (list_ins_next(seti, list_tail(seti), data) != 0)
            {
                set_destroy(seti);
                return -1;
            }
        }
    }
    return 0;
}
Ejemplo n.º 2
0
int set_difference(Set *setd, const Set *set1, const Set *set2)
{
    ListElmt *member;
    void *data;

    set_init(setd, set1->match, NULL);

    for (member = list_head(set1); member != NULL; member = list_next(member))
    {
        if (!set_is_member(set2, list_data(member)))
        {
            data = list_data(member);

            if (list_ins_next(setd, list_tail(setd), data) != 0)
            {
                set_destroy(setd);
                return -1;
            }
        }
    }

    return 0;
}
Ejemplo n.º 3
0
int set_intersection(Set *seti, const Set *set1, const Set *set2) {
    ListElem *member;
    void *data;

    //init union
    set_init(seti, set1->match, NULL);

    //insert members of the first set
    for (member = list_head(set1); member != NULL; member = list_next(member)) {

        if (set_is_member(set2, list_data(member))) {
            data = list_data(member);

            if (list_ins_next(seti, list_tail(seti), data) != 0) {

                set_destroy(seti);
                return -1;
            }
        }
    }

    return 0;
};
Ejemplo n.º 4
0
/* graph_is_adjacent */
int graph_is_adjacent(const Graph *graph, const void *data1,
		             const void *data2) {
	ListElmt *element, *prev;

	/* Locate the adjacency list of the first vertex. */
	prev = NULL;

	for (element = list_head(&graph->adjlists); element != NULL;
	     element = list_next(element)) {
		if (graph->match(data1, ((AdjList *)list_data(element))->vertex))
			break;

		prev = element;
	}

	/* Return if the first vertex was not found. */
	if (element == NULL)
		return 0;

	/* Return whether the second vertex is in the adjacency list of the first. */
	return set_is_member(&((AdjList *)list_data(element))->adjacent, data2);

}
Ejemplo n.º 5
0
int set_union(Set *setu, const Set *set1, const Set *set2)
{
	ListElmt *member;
	void *data;

	set_init(setu, set1->match, NULL);

	/* Insert all members of the first set */
	for (member = list_head(set1); member != NULL; member = list_next(member))
	{
		data = list_data(member);
		if (list_ins_next(setu, list_tail(setu), data) != 0)
		{
			set_destroy(setu);
			return -1;
		}
	}

	/* Insert members of the second set not already present */
	for (member = list_head(set2); member != NULL; member = list_next(member))
	{
		if (set_is_member(set1, list_data(member)))
		{
			continue;
		}
		else
		{
			data = list_data(member);
			if (list_ins_next(setu, list_tail(setu), data) != 0)
			{
				set_destroy(setu);
				return -1;
			}
		}
	}
	return 0;
}
Ejemplo n.º 6
0
int set_union(Set *setu, const Set *set1, const Set *set2)
{
	ListElmt *member;
	void *data;

	set_init(setu, set1->match, NULL);

	for(member = list_head(set1); member != NULL; member = list_next(member))
	{
		data = list_data(member);
		if(list_ins_next(setu, list_tail(setu), data) != 0)
		{
			set_destroy(setu);
			return -1;
		}
	}

	for(member = list_head(set2); member != NULL; member = list_next(member))
	{
		if(set_is_member(set1, list_data(member)))
		{
			continue;
		}
		else
		{
			data = list_data(member);
			if(list_ins_next(setu, list_tail(setu), data) != 0)
			{
				set_destroy(setu);
				return -1;
			}
		}
	}

	return 0;
}
Ejemplo n.º 7
0
//set_difference
int set_difference(set *setd, const Set *set1, const Set *set2)
{
    ListElmt *member;
    void *data;

    //initialize the set for the difference
    set_init(setd, set1->match, NULL);

    //Insert the members from set1 not set2
    for (member = list_head(set1); member != NULL; member = list_next(member))
    {
        if (!set_is_member(set2, list_data(member)))
        {
            data = list_data(member);

            if (list_ins_next(setd, list_tail(setd), data) != 0)
            {
                set_destroy(setd);
                return -1;
            }
        }
    }
    return 0;
}
Ejemplo n.º 8
0
int set_insert(Set *set, const void *data)
{
	if (set_is_member(set, data))
		return 1;
	return list_ins_next(set, list_tail(set), data);
}
Ejemplo n.º 9
0
int graph_rem_vertex(Graph *graph, void **data) {

ListElmt           *element,
                   *temp,
                   *prev;

AdjList            *adjlist;

int                found;

/*****************************************************************************
*                                                                            *
*  Traverse each adjacency list and the vertices it contains.                *
*                                                                            *
*****************************************************************************/

prev = NULL;
found = 0;

for (element = list_head(&graph->adjlists); element != NULL; element =
   list_next(element)) {

   /**************************************************************************
   *                                                                         *
   *  Do not allow removal of the vertex if it is in an adjacency list.      *
   *                                                                         *
   **************************************************************************/

   if (set_is_member(&((AdjList *)list_data(element))->adjacent, *data))
      return -1;

   /**************************************************************************
   *                                                                         *
   *  Keep a pointer to the vertex to be removed.                            *
   *                                                                         *
   **************************************************************************/

   if (graph->match(*data, ((AdjList *)list_data(element))->vertex)) {
 
      temp = element;
      found = 1;

   }

   /**************************************************************************
   *                                                                         *
   *  Keep a pointer to the vertex before the vertex to be removed.          *
   *                                                                         *
   **************************************************************************/

   if (!found)
      prev = element;

}
 
/*****************************************************************************
*                                                                            *
*  Return if the vertex was not found.                                       *
*                                                                            *
*****************************************************************************/

if (!found)
   return -1;

/*****************************************************************************
*                                                                            *
*  Do not allow removal of the vertex if its adjacency list is not empty.    *
*                                                                            *
*****************************************************************************/

if (set_size(&((AdjList *)list_data(temp))->adjacent) > 0)
   return -1;

/*****************************************************************************
*                                                                            *
*  Remove the vertex.                                                        *
*                                                                            *
*****************************************************************************/

if (list_rem_next(&graph->adjlists, prev, (void **)&adjlist) != 0)
   return -1;

/*****************************************************************************
*                                                                            *
*  Free the storage allocated by the abstract data type.                     *
*                                                                            *
*****************************************************************************/

*data = adjlist->vertex;
free(adjlist);

/*****************************************************************************
*                                                                            *
*  Adjust the vertex count to account for the removed vertex.                *
*                                                                            *
*****************************************************************************/

graph->vcount--;

return 0;

}
Ejemplo n.º 10
0
static void test_insert(){

  Set *set = (Set *)malloc(sizeof(Set));
  set_init(set, equals, NULL);

  int a = 10;
  int b = 20;
  int c = 30;

  assert(0 == set_is_member(set, (void *)&a));

  int n;

  n = set_insert(set, (void *)&a);

  assert(1 == set_is_member(set, (void *)&a));

  n = set_insert(set, (void *)&a);
  assert(1 == n);
  assert(1 == set_size(set));

  n = set_insert(set, (void *)&b);

  assert(2 == set_size(set));

  set_remove(set, (void *)&c);
  assert(2 == set_size(set));

  int *x;
  x = &b;
  set_remove(set, (void **)&x);
  assert(1 == set_size(set));


/*
  set_enset(set, &a);
  assert(1 == set_size(set));
  assert(&a == set_peek(set));

  set_enset(set, &b);
  assert(2 == set_size(set));
  assert(&a == set_peek(set));

  set_enset(set, &c);
  assert(3 == set_size(set));
  assert(&a == set_peek(set));

  int *d = (int *)malloc(sizeof(int));
  set_deset(set, (void **) &d);
  assert(2 == set_size(set));
  assert(&a == d);

  set_deset(set, (void **) &d);
  assert(1 == set_size(set));
  assert(&b == d);

  set_deset(set, (void **) &d);
  assert(0 == set_size(set));
  assert(&c == d);

  set_deset(set, (void **) &d);
  assert(0 == set_size(set));
  assert(&c == d);
*/

}
int set_union(Set * setu, const Set * set1, const Set * set2)
{

    ListElmt *member;

    void *data;

    /*****************************************************************************
    *  Initialize the set for the union.                                         *
    *****************************************************************************/

    set_init(setu, set1->match, NULL);

    /*****************************************************************************
    *  Insert the members of the first set.                                      *
    *****************************************************************************/

    for (member = list_head(set1); member != NULL;
            member = list_next(member)) {

        data = list_data(member);

        if (list_ins_next(setu, list_tail(setu), data) != 0) {

            set_destroy(setu);
            return -1;

        }

    }

    /*****************************************************************************
    *  Insert the members of the second set.                                     *
    *****************************************************************************/

    for (member = list_head(set2); member != NULL;
            member = list_next(member)) {

        if (set_is_member(set1, list_data(member))) {

            /***********************************************************************
            *  Do not allow the insertion of duplicates.                           *
            ***********************************************************************/

            continue;

        }

        else {

            data = list_data(member);

            if (list_ins_next(setu, list_tail(setu), data) != 0) {

                set_destroy(setu);
                return -1;

            }

        }

    }

    return 0;

}
Ejemplo n.º 12
0
int set_insert(Set *st, const void *data){
	if(set_is_member(st, data)){
		return 1;
	}
	return list_push(st, data);
}