data_structures
array.h
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* array.h :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/16 17:32:17 by unite #+# #+# */
10 /* Updated: 2020/09/07 23:34:07 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #ifndef ARRAY_H
15 
16 # define ARRAY_H
17 
24 # define ARRAY_INIT_CAPACITY 4
25 
26 # include <errno.h>
27 # include <sys/types.h>
28 # include "types.h"
29 # include "queue.h"
30 # include "utils.h"
31 
47 typedef struct s_array
48 {
49  size_t size;
50  size_t capacity;
51  void **arr;
52  const t_type *type;
53 } t_array;
54 
60 void array_delete(t_array *array);
61 
69 void *array_get(const t_array *array, size_t index);
70 
80 void array_set(t_array *array, size_t index, const void *content);
81 
87 void array_append(t_array *array, const void *content);
88 
94 void *array_pop(t_array *array);
95 
102 void array_swap(t_array *array, size_t ind1, size_t ind2);
103 
109 size_t array_size(const t_array *array);
110 
117 t_array *array_new(const t_type *type);
118 
126 t_array *array_zeros(const t_type *type, size_t size);
127 
135 void array_remove(t_array *array, size_t index);
136 
145 void array_insert(t_array *array, size_t index, const void *content);
146 
153 void array_insertion_sort(t_array *array);
154 
163 void array_merge_sort(t_array *array);
164 
171 void array_quick_sort(t_array *array);
172 
180 int array_sorted(const t_array *array);
181 
187 t_array *array_copy(const t_array *array);
188 
194 t_queue *array_to_queue(const t_array *array);
195 
206 ssize_t array_indexof(const t_array *array, const void *val);
207 
208 #endif
array_to_queue
t_queue * array_to_queue(const t_array *array)
Returns a queue that contains copies of the element in this array.
Definition: array_to_queue.c:16
array_insertion_sort
void array_insertion_sort(t_array *array)
Sorts this array using insertion sort.
Definition: array_insertion_sort.c:28
array_remove
void array_remove(t_array *array, size_t index)
Deletes the item at the specified index.
Definition: array_remove.c:17
types.h
array_zeros
t_array * array_zeros(const t_type *type, size_t size)
Initializes a new array of the specified size, filled with zeros.
Definition: array_zeros.c:16
array_new
t_array * array_new(const t_type *type)
Initializes a new empty array.
Definition: array_new.c:16
array_quick_sort
void array_quick_sort(t_array *array)
Sorts this array using quick sort.
Definition: array_quick_sort.c:50
array_delete
void array_delete(t_array *array)
Deletes this array and free all its items and the associated data.
Definition: array_delete.c:16
utils.h
array_swap
void array_swap(t_array *array, size_t ind1, size_t ind2)
Swaps elements at the two specified positions in the array.
Definition: array_swap.c:16
array_sorted
int array_sorted(const t_array *array)
Checks if an array is sorted in ascending order.
Definition: array_sorted.c:16
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.
Definition: array_set.c:16
s_list
Doubly-linked list of generic items.
Definition: list.h:54
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 ar...
Definition: array_indexof.c:16
queue.h
s_type
A full representation of a data type, used to achieve polymorphism in the implementation of data stru...
Definition: types.h:47
s_array::type
const t_type * type
The type of items in this array.
Definition: array.h:52
s_array::arr
void ** arr
The data.
Definition: array.h:51
array_copy
t_array * array_copy(const t_array *array)
Copies the array and all it contents.
Definition: array_copy.c:16
array_append
void array_append(t_array *array, const void *content)
Appends a copy the specified element to the end of this array.
Definition: array_append.c:17
s_array
A resizable array.
Definition: array.h:47
array_get
void * array_get(const t_array *array, size_t index)
Returns the item at the specified position in this list.
Definition: array_get.c:16
s_array::capacity
size_t capacity
The capacity of this array.
Definition: array.h:50
s_array::size
size_t size
The number of items in this array.
Definition: array.h:49
array_size
size_t array_size(const t_array *array)
Returns the number of elements in this array.
Definition: array_size.c:16
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.
Definition: array_insert.c:17
array_merge_sort
void array_merge_sort(t_array *array)
Sorts this array using merge sort.
Definition: array_merge_sort.c:73
array_pop
void * array_pop(t_array *array)
Removes and returns the element at the end of this array.
Definition: array_pop.c:17