xref: /php-src/ext/dom/lexbor/lexbor/core/array.h (revision bffab33a)
1 /*
2  * Copyright (C) 2018 Alexander Borisov
3  *
4  * Author: Alexander Borisov <borisov@lexbor.com>
5  */
6 
7 #ifndef LEXBOR_ARRAY_H
8 #define LEXBOR_ARRAY_H
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 #include "lexbor/core/base.h"
15 
16 
17 typedef struct {
18     void   **list;
19     size_t size;
20     size_t length;
21 }
22 lexbor_array_t;
23 
24 
25 LXB_API lexbor_array_t *
26 lexbor_array_create(void);
27 
28 LXB_API lxb_status_t
29 lexbor_array_init(lexbor_array_t *array, size_t size);
30 
31 LXB_API void
32 lexbor_array_clean(lexbor_array_t *array);
33 
34 LXB_API lexbor_array_t *
35 lexbor_array_destroy(lexbor_array_t *array, bool self_destroy);
36 
37 
38 LXB_API void **
39 lexbor_array_expand(lexbor_array_t *array, size_t up_to);
40 
41 
42 LXB_API lxb_status_t
43 lexbor_array_push(lexbor_array_t *array, void *value);
44 
45 LXB_API void *
46 lexbor_array_pop(lexbor_array_t *array);
47 
48 LXB_API lxb_status_t
49 lexbor_array_insert(lexbor_array_t *array, size_t idx, void *value);
50 
51 LXB_API lxb_status_t
52 lexbor_array_set(lexbor_array_t *array, size_t idx, void *value);
53 
54 LXB_API void
55 lexbor_array_delete(lexbor_array_t *array, size_t begin, size_t length);
56 
57 
58 /*
59  * Inline functions
60  */
61 lxb_inline void *
lexbor_array_get(lexbor_array_t * array,size_t idx)62 lexbor_array_get(lexbor_array_t *array, size_t idx)
63 {
64     if (idx >= array->length) {
65         return NULL;
66     }
67 
68     return array->list[idx];
69 }
70 
71 lxb_inline size_t
lexbor_array_length(lexbor_array_t * array)72 lexbor_array_length(lexbor_array_t *array)
73 {
74     return array->length;
75 }
76 
77 lxb_inline size_t
lexbor_array_size(lexbor_array_t * array)78 lexbor_array_size(lexbor_array_t *array)
79 {
80     return array->size;
81 }
82 
83 /*
84  * No inline functions for ABI.
85  */
86 LXB_API void *
87 lexbor_array_get_noi(lexbor_array_t *array, size_t idx);
88 
89 LXB_API size_t
90 lexbor_array_length_noi(lexbor_array_t *array);
91 
92 LXB_API size_t
93 lexbor_array_size_noi(lexbor_array_t *array);
94 
95 
96 #ifdef __cplusplus
97 } /* extern "C" */
98 #endif
99 
100 #endif /* LEXBOR_ARRAY_H */
101