Cross-platform C SDK logo

Cross-platform C SDK

Arrays of 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.

Structure that stores pointers in contiguous memory positions. For object arrays use Arrays.


Functions

ArrPt(type)*arrpt_create (...)
voidarrpt_destroy (...)
voidarrpt_clear (...)
ArrPt(type)*arrpt_read (...)
voidarrpt_write (...)
uint32_tarrpt_size (...)
type*arrpt_get (...)
type*arrpt_last (...)
type**arrpt_all (...)
type**arrpt_grow (...)
voidarrpt_append (...)
voidarrpt_prepend (...)
voidarrpt_insert (...)
voidarrpt_delete (...)
voidarrpt_pop (...)
voidarrpt_sort (...)
voidarrpt_sort_ex (...)
uint32_tarrpt_find (...)
type*arrpt_search (...)
type*arrpt_bsearch (...)
voidarrpt_foreach (...)
voidarrpt_foreach_rev (...)
voidarrpt_end (void)

arrpt_create ()

Create an empty array of pointers.

ArrPt(type)*
arrpt_create(type);
type

Object type.

Return

The new array.


arrpt_destroy ()

Destroy an array and all its elements.

void
arrpt_destroy(ArrPt(type)**,
              FPtr_destroy func_destroy,
              type);

The array. It will be set to NULL after destruction.

func_destroy

Function to destroy an element Destructors. If NULL only the array will be destroyed, but not its elements.

type

Object type.


arrpt_clear ()

Delete the contents of the array, without destroying the container that will be left with zero elements.

void
arrpt_clear(ArrPt(type) *array,
            FPtr_destroy func_destroy,
            type);
array

The array.

func_destroy

Destructor function. See arrpt_destroy.

type

Object type.


arrpt_read ()

Create an array by reading its contents from a Streams (de-serialization).

ArrPt(type)*
arrpt_read(Stream *stream,
           FPtr_read func_read,
           type);
stream

A read stream.

func_read

Constructor to create an object from the data obtained from a stream. Serialization.

type

Object type.

Return

The array readed.


arrpt_write ()

Write an array in a Streams (serialization).

void
arrpt_write(Stream *stream,
            const ArrPt(type) *array,
            FPtr_write func_write,
            type);
stream

A write stream.

array

The array.

func_write

Function that writes the content of an element in a stream Serialization.

type

Object type.


arrpt_size ()

Get the number of elements in an array.

uint32_t
arrpt_size(const ArrPt(type) *array,
           type);
array

The array.

type

Object type.

Return

Number of elements.


arrpt_get ()

Get a pointer to the item in pos position.

type*
arrpt_get(const ArrPt(type) *array,
          const uint32_t pos,
          type);
array

The array.

pos

Item position or index.

type

Object type.

Return

Item Pointer.


arrpt_last ()

Get a pointer to the last element of the array.

type*
arrpt_last(const ArrPt(type) *array,
           type);
array

The array.

type

Object type.

Return

Item Pointer.


arrpt_all ()

Get a pointer to the internal memory of the array, which gives access to all the elements.

type**
arrpt_all(const ArrPt(type) *array,
          type);
array

The array.

type

Object type.

Return

Base pointer. Increasing it one by one we will iterate over the elements.

Remarks

Use arrpt_foreach to iterate over all elements in a more secure and elegant way.


arrpt_grow ()

Append n elements, not initialized, at the end of the array.

type**
arrpt_grow(ArrPt(type) *array,
           const uint32_t n,
           type);
array

The array.

n

Number of items to add.

type

Object type.

Return

Pointer to the first item added.


arrpt_append ()

Adds a pointer to the end of the array.

void
arrpt_append(ArrPt(type) *array,
             type *value,
             type);
array

The array.

value

Pointer to the item to append.

type

Object type.


arrpt_prepend ()

Insert a pointer at the beginning of the array. The rest of the elements will be shifted to the right.

void
arrpt_prepend(ArrPt(type) *array,
              type *value,
              type);
array

The array.

value

Pointer to the element to insert.

type

Object type.


arrpt_insert ()

Insert a pointer in an arbitrary array position.

void
arrpt_insert(ArrPt(type) *array,
             const uint32_t pos,
             type *value,
             type);
array

The array.

pos

Position where it will be inserted. The current item in pos and following will be shifted to the right.

value

Pointer to the element to insert.

type

Object type.


arrpt_delete ()

Remove a pointer from the array.

void
arrpt_delete(ArrPt(type) *array,
             const uint32_t pos,
             FPtr_destroy func_destroy,
             type);
array

The array.

pos

Position of the item to be deleted. The current item in pos+1 and following will be shifted to the left.

func_destroy

Element destructor. See arrpt_destroy.

type

Object type.


arrpt_pop ()

Remove the last pointer from the array.

void
arrpt_pop(ArrPt(type) *array,
          FPtr_destroy func_destroy,
          type);
array

The array.

func_destroy

Element destructor. See arrpt_destroy.

type

Object type.


arrpt_sort ()

Sort the array elements using Quicksort.

void
arrpt_sort(ArrPt(type) *array,
           FPtr_compare func_compare,
           type);
array

The array.

func_compare

Function to compare two elements. Sort and search.

type

Object type.


arrpt_sort_ex ()

Sort array elements using Quicksort and additional data.

void
arrpt_sort_ex(ArrPt(type) *array,
              FPtr_compare_ex func_compare,
              type,
              dtype);
array

The array.

func_compare

Function to compare two elements using an additional data.

type

Object type.

dtype

Type of data in the comparison function.


arrpt_find ()

Search for a specific pointer in the array.

uint32_t
arrpt_find(const ArrPt(type) *array,
           type *elem,
           type);
array

The array.

elem

Pointer to find.

type

Object type.

Return

The position of the pointer if it exists, or UINT32_MAX if not.


arrpt_search ()

Search for an element in the array linearly O(n).

type*
arrpt_search(ArrPt(type) *array,
             FPtr_compare func_compare,
             ktype key,
             uint32_t *pos,
             type,
             ktype);
array

The array.

func_compare

Comparison function. The first parameter is the element, the second the search key. Sort and search.

key

Search key. Pointer to a data type that may be different from the type of array element.

pos

Position of the element in the array (if it exists), or UINT32_MAX if it does not exist. Can be NULL.

type

Object type.

ktype

Key type.

Return

Pointer to the first element that matches the search criteria or NULL if none exists.


arrpt_bsearch ()

Search for an element in the array logarithmically O(logn).

type*
arrpt_bsearch(ArrPt(type) *array,
              FPtr_compare func_compare,
              ktype key,
              uint32_t *pos,
              type,
              ktype);
array

The array.

func_compare

Comparison function. The first parameter is the element, the second the search key. Sort and search.

key

Clave a buscar. Puntero a un tipo de dato que puede ser diferente al tipo de elemento del array.

pos

Position of the element in the array (if it exists), or UINT32_MAX if it does not exist. Can be NULL.

type

Object type.

ktype

Key type.

Return

Pointer to the first element that matches the search criteria or NULL if none exists.

Remarks

The array must be sorted according to the same criteria as the search. If not, the result is unpredictable.


arrpt_foreach ()

Iterate on all array elements. Uses arrpt_end to close the loop.

void
arrpt_foreach(type *elem,
              ArrPt(type) *array,
              type);
1
2
3
arrpt_foreach(product, array, Product)
    bstd_printf("Index:%d, Id:%d\n", product_i, product->id);
arrpt_end()
elem

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

array

The array.

type

Object type.


arrpt_foreach_rev ()

Iterate on all array elements backward, from the last to the first. Uses arrpt_end to close the loop.

void
arrpt_foreach_rev(type *elem,
                  ArrPt(type) *array,
                  type);
1
2
3
4
// Now in reverse order
arrpt_foreach_rev(product, array, Product)
    bstd_printf("Index:%d, Id:%d\n", product_i, product->id);
arrpt_end()
elem

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

array

The array.

type

Object type.


arrpt_end ()

Close the loop opened by arrpt_foreach or arrpt_foreach_rev.

void
arrpt_end(void);
❮ Back
Next ❯