Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * array.h
 * vdcore
 *
 * Copyright (c) 2006 Denis Defreyne
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#ifndef __VDCORE_ARRAY_H__
#define __VDCORE_ARRAY_H__

#include <stdbool.h>
#include <stddef.h>
#include <unistd.h>
#include <vdcore/data.h>

/* array structure */
typedef struct vd_array_s vd_array_t;

/* comparator callback type */
typedef int (*vd_array_comparator_callback_t)(void *value_1, void *value_2);

/* enumerator callback type */
typedef void (*vd_array_enumerator_callback_t)(vd_data_t *a_data, ssize_t a_index);

/* predicate callback type */
typedef bool (*vd_array_predicate_callback_t)(vd_data_t *a_data);

/* map callback type */
typedef vd_data_t *(*vd_array_map_callback_t)(vd_data_t *a_data);

#pragma mark -

/* creates an array */
vd_array_t *vd_array_create(ssize_t a_initial_capacity);

/* creates a sorted array */
vd_array_t *vd_array_create_sorted(ssize_t a_initial_capacity, vd_array_comparator_callback_t a_callback);

/* deletes an array */
void vd_array_destroy(vd_array_t *a_array);

#pragma mark -

/* inserts element at end of a non-sorted array */
int vd_array_append(vd_array_t *a_array, vd_data_t *a_data);

/* inserts element at begin of a non-sorted array */
int vd_array_prepend(vd_array_t *a_array, vd_data_t *a_data);

/* inserts element in a sorted array */
int vd_array_insert(vd_array_t *a_array, vd_data_t *a_data);

/* inserts element at specified index of a non-sorted array */
int vd_array_insert_at_index(vd_array_t *a_array, vd_data_t *a_data, ssize_t a_index);

#pragma mark -

/* empties the array */
int vd_array_empty(vd_array_t *a_array);

/* deletes element at index */
int vd_array_delete_at_index(vd_array_t *a_array, ssize_t a_index);

/* deletes elements in range */
int vd_array_delete_in_range(vd_array_t *a_array, ssize_t a_index_min, ssize_t a_index_max);

/* removes NULL values from array */
int vd_array_compact(vd_array_t *a_array);

/* removes elements matching given predicate */
int vd_array_reject(vd_array_t *a_array, vd_array_predicate_callback_t a_callback);

/* removes elements not matching given predicate */
int vd_array_select(vd_array_t *a_array, vd_array_predicate_callback_t a_callback);

#pragma mark -

/* sets the array comparator */
void vd_array_set_comparator(vd_array_t *a_array, vd_array_comparator_callback_t a_callback);

/* fills the array with the specified data */
int vd_array_fill(vd_array_t *a_array, vd_data_t *a_data);

/* fills the range with the specified data */
int vd_array_fill_in_range(vd_array_t *a_array, ssize_t a_index_min, ssize_t a_index_max, vd_data_t *a_data);

/* reverses the array */
int vd_array_reverse(vd_array_t *a_array);

/* sorts the array */
int vd_array_sort(vd_array_t *a_array);

/* maps every element to a new element */
int vd_array_map(vd_array_t *a_array, vd_array_map_callback_t a_callback);

#pragma mark -

/* checks whether a stack is empty */
bool vd_array_is_empty(vd_array_t *a_array);

/* checks whether a stack is sorted */
bool vd_array_is_sorted(vd_array_t *a_array);

/* gets the number of used element slots */
ssize_t vd_array_get_size(vd_array_t *a_array);

/* gets the number of used and free element slots */
ssize_t vd_array_get_capacity(vd_array_t *a_array);

/* gets the number of free element slots */
ssize_t vd_array_get_free_size(vd_array_t *a_array);

/* gets the element at the specified index */
vd_data_t *vd_array_get_element_at_index(vd_array_t *a_array, ssize_t a_index);

/* gets the first element */
vd_data_t *vd_array_get_first(vd_array_t *a_array);

/* gets the last element */
vd_array_t *vd_array_get_last(vd_array_t *a_array);

/* enumerates all elements */
void vd_array_enumerate_each(vd_array_t *a_array, vd_array_enumerator_callback_t a_callback);

/* enumerates all elements in reverse order */
void vd_array_enumerate_each_reversed(vd_array_t *a_array, vd_array_enumerator_callback_t a_callback);

#endif