data_structures
Data Structures | Macros | Typedefs | Functions
array.h File Reference
#include <errno.h>
#include <sys/types.h>
#include "types.h"
#include "queue.h"
#include "utils.h"
+ Include dependency graph for array.h:

Go to the source code of this file.

Data Structures

struct  s_array
 A resizable array. More...
 

Macros

#define ARRAY_INIT_CAPACITY   4
 The initial capacity of an array. More...
 

Typedefs

typedef struct s_array t_array
 

Functions

void array_delete (t_array *array)
 Deletes this array and free all its items and the associated data. More...
 
void * array_get (const t_array *array, size_t index)
 Returns the item at the specified position in this list. More...
 
void array_set (t_array *array, size_t index, const void *content)
 Replaces the element at the specified position in this array with a copy of the specified element. More...
 
void array_append (t_array *array, const void *content)
 Appends a copy the specified element to the end of this array. More...
 
void * array_pop (t_array *array)
 Removes and returns the element at the end of this array. More...
 
void array_swap (t_array *array, size_t ind1, size_t ind2)
 Swaps elements at the two specified positions in the array. More...
 
size_t array_size (const t_array *array)
 Returns the number of elements in this array. More...
 
t_arrayarray_new (const t_type *type)
 Initializes a new empty array. More...
 
t_arrayarray_zeros (const t_type *type, size_t size)
 Initializes a new array of the specified size, filled with zeros. More...
 
void array_remove (t_array *array, size_t index)
 Deletes the item at the specified index. More...
 
void array_insert (t_array *array, size_t index, const void *content)
 Inserts a copy of the specified item at the specified index. More...
 
void array_insertion_sort (t_array *array)
 Sorts this array using insertion sort. More...
 
void array_merge_sort (t_array *array)
 Sorts this array using merge sort. More...
 
void array_quick_sort (t_array *array)
 Sorts this array using quick sort. More...
 
int array_sorted (const t_array *array)
 Checks if an array is sorted in ascending order. More...
 
t_arrayarray_copy (const t_array *array)
 Copies the array and all it contents. More...
 
t_queuearray_to_queue (const t_array *array)
 Returns a queue that contains copies of the element in this array. More...
 
ssize_t array_indexof (const t_array *array, const void *val)
 Returns the index at which the specified value is found in the array, or -1 if the value isn't the array. More...
 

Macro Definition Documentation

◆ ARRAY_INIT_CAPACITY

#define ARRAY_INIT_CAPACITY   4

The initial capacity of an array.

Remarks
Change this if you know that the array will contain many more items on average.

Definition at line 24 of file array.h.

Function Documentation

◆ array_append()

void array_append ( t_array array,
const void *  content 
)

Appends a copy the specified element to the end of this array.

Parameters
contentThe item to be copied

Definition at line 17 of file array_append.c.

◆ array_copy()

t_array* array_copy ( const t_array array)

Copies the array and all it contents.

Returns
The copy of the array.

Definition at line 16 of file array_copy.c.

◆ array_delete()

void array_delete ( t_array array)

Deletes this array and free all its items and the associated data.

Does nothing if the argument is NULL.

Definition at line 16 of file array_delete.c.

◆ array_get()

void* array_get ( const t_array array,
size_t  index 
)

Returns the item at the specified position in this list.

Returns
The item or NULL if the index is out of range.
Parameters
indexThe index (0-based counting)
Exceptions
EINVALIndex is out of range

Definition at line 16 of file array_get.c.

◆ array_indexof()

ssize_t array_indexof ( const t_array array,
const void *  val 
)

Returns the index at which the specified value is found in the array, or -1 if the value isn't the array.

Parameters
valThe value
Returns
the index at which the specified value is found in the array, or -1 if the value isn't the array.
Note
For this function to work, the datatype in this array must be comparable (i.e. implement the cmp function).

Definition at line 16 of file array_indexof.c.

◆ array_insert()

void array_insert ( t_array array,
size_t  index,
const void *  content 
)

Inserts a copy of the specified item at the specified index.

Parameters
indexThe index at which to insert
contentThe item to be copied
Note
This function has linear complexity in the number of elements in the worst-case.

Definition at line 17 of file array_insert.c.

◆ array_insertion_sort()

void array_insertion_sort ( t_array array)

Sorts this array using insertion sort.

Note
For this function to work, the datatype in this array must be comparable (i.e. implement the cmp function).

Definition at line 28 of file array_insertion_sort.c.

◆ array_merge_sort()

void array_merge_sort ( t_array array)

Sorts this array using merge sort.

Returns
0 on success, 1 on failure. In case of an error, errno is set accordingly.
Note
For this function to work, the datatype in this array must be comparable (i.e. implement the cmp function).

Definition at line 73 of file array_merge_sort.c.

◆ array_new()

t_array* array_new ( const t_type type)

Initializes a new empty array.

Parameters
typeThe type of items that this array can hold
Returns
The new array

Definition at line 16 of file array_new.c.

◆ array_pop()

void* array_pop ( t_array array)

Removes and returns the element at the end of this array.

Returns
The item at the end of this array, or NULL if the array is empty.

Definition at line 17 of file array_pop.c.

◆ array_quick_sort()

void array_quick_sort ( t_array array)

Sorts this array using quick sort.

Note
For this function to work, the datatype in this array must be comparable (i.e. implement the cmp function).

Definition at line 50 of file array_quick_sort.c.

◆ array_remove()

void array_remove ( t_array array,
size_t  index 
)

Deletes the item at the specified index.

Parameters
indexThe index
Note
This function has linear complexity in the number of elements in the worst-case.

Definition at line 17 of file array_remove.c.

◆ array_set()

void array_set ( t_array array,
size_t  index,
const void *  content 
)

Replaces the element at the specified position in this array with a copy of the specified element.

Parameters
indexThe index (0-based counting)
contentThe item to be copied.
Exceptions
ENOMEMMemory allocation error
EINVALIndex is out of range

Definition at line 16 of file array_set.c.

◆ array_size()

size_t array_size ( const t_array array)

Returns the number of elements in this array.

Returns
The number of elements in this array

Definition at line 16 of file array_size.c.

◆ array_sorted()

int array_sorted ( const t_array array)

Checks if an array is sorted in ascending order.

Returns
1 if the array is sorted, 0 if the array is not sorted.
Note
For this function to work, the datatype in this array must be comparable (i.e. implement the cmp function).

Definition at line 16 of file array_sorted.c.

◆ array_swap()

void array_swap ( t_array array,
size_t  ind1,
size_t  ind2 
)

Swaps elements at the two specified positions in the array.

Parameters
ind1The index of the first element
ind2The index of the second element

Definition at line 16 of file array_swap.c.

◆ array_to_queue()

t_queue* array_to_queue ( const t_array array)

Returns a queue that contains copies of the element in this array.

Returns
The queue representing the array.

Definition at line 16 of file array_to_queue.c.

◆ array_zeros()

t_array* array_zeros ( const t_type type,
size_t  size 
)

Initializes a new array of the specified size, filled with zeros.

Parameters
typeThe type of items that this array can hold
sizeThe size
Returns
The new array of the specified size, filled with zeros

Definition at line 16 of file array_zeros.c.