Cross-platform C SDK logo

Cross-platform C SDK

Binary search trees (pointers)

❮ Back
Next ❯
This page has been automatically translated using the Google Translate API services. We are working on improving texts. Thank you for your understanding and patience.

Sets of pointers. For general information, see Binary search trees.


Functions

SetPt(type)*setpt_create (...)
voidsetpt_destroy (...)
uint32_tsetpt_size (...)
type*setpt_get (...)
const type*setpt_get_const (...)
bool_tsetpt_insert (...)
bool_tsetpt_delete (...)
type*setpt_first (...)
const type*setpt_first_const (...)
type*setpt_last (...)
const type*setpt_last_const (...)
type*setpt_next (...)
const type*setpt_next_const (...)
type*setpt_prev (...)
const type*setpt_prev_const (...)
voidsetpt_foreach (...)
voidsetpt_foreach_const (...)
voidsetpt_fornext (...)
voidsetpt_fornext_const (...)
voidsetpt_forback (...)
voidsetpt_forback_const (...)
voidsetpt_forprev (...)
voidsetpt_forprev_const (...)
❮ Back
Next ❯

setpt_create ()

Create an empty pointer set.

SetPt(type)*
setpt_create(FPtr_compare func_compare,
             type);
func_compare

Function to compare two elements. Sort and search.

type

Object type.

Return

The new set.


setpt_destroy ()

Destroy a set and all its elements.

void
setpt_destroy(SetPt(type) **set,
              FPtr_destroy func_destroy,
              type);
set

The set. Will be set to NULL after destruction.

func_destroy

Function to destroy an element of the set Destructors. If it is NULL only the set will be destroyed, but not its elements.

type

Object type.


setpt_size ()

Get the number of set elements.

uint32_t
setpt_size(const SetPt(type) *set,
           type);
set

The set.

type

Object type.

Return

Number of items.


setpt_get ()

Search for an item in O(logn). It is equivalent to arrpt_bsearch. The internal set iterator will be fixed in it.

type*
setpt_get(SetPt(type) *set,
          type *key,
          type);
1
2
3
4
Product key;
Product *pr;
key.id = 453;
pr = setpt_get(setpt, &key, Product);
set

The set.

key

Search key. It is a pointer to an object where only the relevant fields of the search must be initialized.

type

Object type.

Return

Pointer to the searched item if it exists, or NULL if not.

Remarks

Iterators.


setpt_get_const ()

Const version of setpt_get.

const type*
setpt_get_const(const SetPt(type) *set,
                const type *key,
                type);
set

The set.

key

Search key.

type

Object type.

Return

Element.


setpt_insert ()

Insert a new item in the set.

bool_t
setpt_insert(SetPt(type) *set,
             type *value,
             type);
1
2
3
4
5
6
Product *pr = product_create(...);
if (setpt_insert(setpt, pr, Product) == FALSE)
{
    error("Already exists");
    product_destroy(&pr);
}
set

The set.

value

Pointer to the element to insert.

type

Object type.

Return

TRUE if the item has been inserted. FALSE if another element with the same key already exists.

Remarks

Inserting or deleting elements invalidates the internal set iterator Iterators. You must initialize it with setpt_first.


setpt_delete ()

Remove an item from the set.

bool_t
setpt_delete(SetPt(type) *set,
             type *key,
             FPtr_destroy func_destroy,
             type);
1
2
3
4
Product key;
key.id = 345;
if (setpt_delete(setpt, &key, product_destroy, Product) == FALSE)
    error("Doesn't exists");
set

The set.

key

Key to delete. It is a pointer to an object where only the relevant fields of the search must be initialized.

func_destroy

Element destructor. Can be NULL. See setpt_destroy.

type

Object type.

Return

TRUE if the item has been deleted, or FALSE if there is no item with that key.

Remarks

Inserting or deleting elements invalidates the internal set iterator Iterators. You must initialize it with setpt_first.


setpt_first ()

Get the first element of the set and initialize the internal iterator.

type*
setpt_first(SetPt(type) *set,
            type);
set

The set.

type

Object type.

Return

Pointer to the first element or NULL if the set is empty.

Remarks

Iterators.


setpt_first_const ()

Const version of setpt_first.

const type*
setpt_first_const(const SetPt(type) *set,
                  type);
set

The set.

type

Object type.

Return

Element.


setpt_last ()

Get the last element of the set and initialize the internal iterator.

type*
setpt_last(SetPt(type) *set,
           type);
set

The set.

type

Object type.

Return

Pointer to the last item or NULL if the set is empty.

Remarks

Iterators.


setpt_last_const ()

Const version of setpt_last.

const type*
setpt_last_const(const SetPt(type) *set,
                 type);
set

The set.

type

Object type.

Return

Element.


setpt_next ()

Get the next set item, after increasing the internal iterator.

type*
setpt_next(SetPt(type) *set,
           type);
set

The set.

type

Object type.

Return

Pointer to the next item or NULL if the iterator has reached the last.

Remarks

Use setpt_first to initialize the internal iterator Iterators.


setpt_next_const ()

Const version of setpt_next.

const type*
setpt_next_const(const SetPt(type) *set,
                 type);
set

The set.

type

Object type.

Return

Element.


setpt_prev ()

Gets the previous element of the set, after decrementing the internal iterator.

type*
setpt_prev(SetPt(type) *set,
           type);
set

The set.

type

Object type.

Return

Pointer to the previous item or NULL if the iterator has reached the first.

Remarks

Use setpt_last to initialize the internal iterator on reversed loops Iterators.


setpt_prev_const ()

Const version of setpt_prev.

const type*
setpt_prev_const(const SetPt(type) *set,
                 type);
set

The set.

type

Object type.

Return

Element.


setpt_foreach ()

Loop over all the elements of the set. Use setpt_fornext to close the loop.

void
setpt_foreach(type *elem,
              SetPt(type) *set,
              type);
1
2
3
setpt_foreach(product, set, Product)
    bstd_printf("Position:%d, Id:%d\n", product_i, product->id);
setpt_fornext(product, set, Product)
elem

Name of the variable 'element' within the loop. Adding the suffix '_i' we get the index.

set

The set.

type

Object type.


setpt_foreach_const ()

Const version of setpt_foreach.

void
setpt_foreach_const(const type *elem,
                    const SetPt(type) *set,
                    type);
elem

Element.

set

The set.

type

Object type.


setpt_fornext ()

Close the loop opened by setpt_foreach, increasing the internal iterator.

void
setpt_fornext(type *elem,
              SetPt(type) *set,
              type);
elem

Name of the variable 'element'. It must be the same as setpt_foreach.

set

The set.

type

Object type.


setpt_fornext_const ()

Const version of setpt_fornext.

void
setpt_fornext_const(const type *elem,
                    const SetPt(type) *set,
                    type);
elem

Element.

set

The set.

type

Object type.


setpt_forback ()

Loop over all the elements of the set in reverse order. Use setpt_forprev to close the loop.

void
setpt_forback(type *elem,
              SetPt(type) *set,
              type);
1
2
3
4
// Now in reverse order
setpt_forback(product, set, Product)
    bstd_printf("Position:%d, Id:%d\n", product_i, product->id);
setpt_forprev(product, set, Product)
elem

Name of the variable 'element' within the loop. Adding the suffix '_i' we get the index.

set

The set.

type

Object type.


setpt_forback_const ()

Const version of setpt_forback.

void
setpt_forback_const(const type *elem,
                    const SetPt(type) *set,
                    type);
elem

Element.

set

The set.

type

Object type.


setpt_forprev ()

Close the loop opened by setpt_forback, decreasing the internal iterator.

void
setpt_forprev(type *elem,
              SetPt(type) *set,
              type);
elem

Name of the variable 'element'. It must be the same as setpt_foreach_rev.

set

The set.

type

Object type.


setpt_forprev_const ()

Const version of setpt_forprev.

void
setpt_forprev_const(const type *elem,
                    const SetPt(type) *set,
                    type);
elem

Element.

set

The set.

type

Object type.

❮ Back
Next ❯