set.h File Reference

A set data structure. More...

#include "int_sizes.h"

Go to the source code of this file.

Functions

struct set * set_create (int buckets)
 Create a new set.
void set_clear (struct set *s)
 Remove all entries from a set.
void set_delete (struct set *s)
 Delete a set.
int set_size (struct set *s)
 Count the entries in a set.
int set_insert (struct set *s, const void *element)
 Insert a element to the set.
int set_push (struct set *h, const void *element)
 Insert a element to the set.
int set_lookup (struct set *s, void *element)
 Look up a element in the set.
int set_remove (struct set *s, const void *element)
 Remove a element.
void * set_pop (struct set *s)
 Remove an arbitrary element from the set.
void set_first_element (struct set *s)
 Begin iteration over all the elements.
void * set_next_element (struct set *s)
 Continue iteration over all elements.

Detailed Description

A set data structure.

Arbitrary objects that are equal (the same location in memory) appear only once in the set. For example, as a set of filenames:

struct set *s;
s = set_create(0);
set_push(s,pathname);
set_push(s,pathname_b);
set_push(s,pathname);
assert(set_size(s) == 2);
path = set_pop(s);
assert(set_size(s) == 1);

To list all of the elements in a set, use set_first_element and set_next_element like this:

void *element;
set_first_element(s);
while(element = set_next_element(s)) {
	printf("set contains: %x\n", element);
}

Function Documentation

struct set* set_create ( int  buckets  )  [read]

Create a new set.

Parameters:
buckets The number of elements in the set. If zero, a default element will be used. Increases dynamically as needed.
Returns:
A pointer to a new set.
void set_clear ( struct set *  s  ) 

Remove all entries from a set.

Note that this function will not free all of the objects contained within the set.

Parameters:
s The set to delete.
void set_delete ( struct set *  s  ) 

Delete a set.

Note that this function will not free all of the objects contained within the set.

Parameters:
s The set to delete.
int set_size ( struct set *  s  ) 

Count the entries in a set.

Returns:
The number of entries in the set.
Parameters:
s A pointer to a set.
int set_insert ( struct set *  s,
const void *  element 
)

Insert a element to the set.

This call will return 0 if element was already in the set. You must call set_remove to remove it. Also note that you cannot insert a null element into the set.

Parameters:
s A pointer to a set.
element A pointer to store.
Returns:
One if the insert succeeded, 0 otherwise.
int set_push ( struct set *  h,
const void *  element 
)

Insert a element to the set.

This is equivalent to set_insert

int set_lookup ( struct set *  s,
void *  element 
)

Look up a element in the set.

Parameters:
s A pointer to a set.
element A pointer to search for.
Returns:
If found, 1, otherwise 0.
int set_remove ( struct set *  s,
const void *  element 
)

Remove a element.

Parameters:
s A pointer to a set.
element A pointer to remove.
Returns:
If found 1, otherwise 0.
void* set_pop ( struct set *  s  ) 

Remove an arbitrary element from the set.

Parameters:
s A pointer to a set.
Returns:
The pointer removed.
void set_first_element ( struct set *  s  ) 

Begin iteration over all the elements.

This function begins a new iteration over a set, allowing you to visit every element in the set. Next, invoke set_next_element to retrieve each element in order.

Parameters:
s A pointer to a set.
void* set_next_element ( struct set *  s  ) 

Continue iteration over all elements.

This function returns the next element in the iteration.

Parameters:
s A pointer to a set.
Returns:
zero if there are no more elements to visit, the next element otherwise.

Generated on 10 Oct 2013 for cctools by  doxygen 1.6.1