data_structures
array_insert.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* array_insert.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/19 00:18:09 by unite #+# #+# */
10 /* Updated: 2020/09/07 21:51:10 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "array.h"
15 #include "array_utils.h"
16 
17 void array_insert(t_array *array, size_t index, const void *content)
18 {
19  size_t i;
20 
21  if (index >= array->size)
22  ds_exit_set(EINVAL);
23  if (array->size++ == array->capacity)
24  array_grow(array);
25  i = array->size;
26  while (i-- > index)
27  array->arr[i] = array->arr[i - 1];
28  array->arr[index] = array->type->copy(content);
29 }
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
s_type::copy
void *(* copy)(const void *)
A function pointer used to copy the data type.
Definition: types.h:50
array.h
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
s_array
A resizable array.
Definition: array.h:47
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
ds_exit_set
void ds_exit_set(int err)
Set errno to the specified value, print the error message, and exit the process.
Definition: ds_exit_set.c:22
array_utils.h