summaryrefslogtreecommitdiff
path: root/src/array.h
blob: ba6ce16d06835c3fec15ab84904edaccbdcf8d19 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#ifndef ARRAY_H
#define ARRAY_H
#include "first.h"

#include "buffer.h"

struct data_unset; /* declaration */

struct data_methods {
	struct data_unset *(*copy)(const struct data_unset *src); \
	void (*free)(struct data_unset *p); \
	void (*insert_dup)(struct data_unset *dst, struct data_unset *src);
};

typedef enum { TYPE_STRING, TYPE_ARRAY, TYPE_INTEGER, TYPE_CONFIG, TYPE_OTHER } data_type_t;
#define DATA_UNSET \
	buffer key; \
	const struct data_methods *fn; /* function table */ \
	data_type_t type

typedef struct data_unset {
	DATA_UNSET;
} data_unset;

typedef struct {
	data_unset **data;
	data_unset **sorted;

	uint32_t used; /* <= INT32_MAX */
	uint32_t size;
} array;

typedef struct {
	DATA_UNSET;
	int ext; /*(fits in space due to alignment in 64-bit; extends 32-bit)*/
	buffer value;
} data_string;

__attribute_returns_nonnull__
data_string *array_data_string_init(void);

typedef struct {
	DATA_UNSET;

	array value;
} data_array;

__attribute_returns_nonnull__
data_array *array_data_array_init(void);

typedef struct {
	DATA_UNSET;

	int value;
} data_integer;

__attribute_returns_nonnull__
data_integer *array_data_integer_init(void);

__attribute_returns_nonnull__
array *array_init(uint32_t n);

__attribute_cold__
void array_copy_array(array *dst, const array *src);

__attribute_cold__
void array_free_data(array *a);

void array_free(array *a);

__attribute_hot__
void array_reset_data_strings(array *a);

__attribute_cold__
void array_insert_unique(array *a, data_unset *entry);

__attribute_cold__
data_unset *array_pop(array *a); /* only works on "simple" lists with autogenerated keys */

__attribute_cold__
__attribute_pure__
int array_is_vlist(const array *a);

__attribute_cold__
__attribute_pure__
int array_is_kvany(const array *a);

__attribute_cold__
__attribute_pure__
int array_is_kvarray(const array *a);

__attribute_cold__
__attribute_pure__
int array_is_kvstring(const array *a);

__attribute_pure__
data_unset *array_get_element_klen_ext(const array *a, int ext, const char *key, uint32_t klen);

__attribute_pure__
const data_unset *array_get_element_klen(const array *a, const char *key, uint32_t klen);

__attribute_cold__
__attribute_pure__
data_unset *array_get_data_unset(const array *a, const char *key, uint32_t klen);

__attribute_cold__
data_unset *array_extract_element_klen(array *a, const char *key, uint32_t klen); /* removes found entry from array */

__attribute_returns_nonnull__
int * array_get_int_ptr(array *a, const char *k, uint32_t klen);

__attribute_returns_nonnull__
buffer * array_get_buf_ptr_ext(array *a, int ext, const char *k, uint32_t klen);

__attribute_returns_nonnull__
buffer * array_get_buf_ptr(array *a, const char *k, uint32_t klen);

void array_insert_value(array *a, const char *v, uint32_t vlen);

static inline void array_set_key_value(array * const a, const char * const k, const uint32_t klen, const char * const v, const uint32_t vlen);

static inline void array_set_key_value(array * const a, const char * const k, const uint32_t klen, const char * const v, const uint32_t vlen) {
    buffer_copy_string_len(array_get_buf_ptr(a, k, klen), v, vlen);
}

__attribute_cold__
void array_replace(array *a, data_unset *entry);

__attribute_pure__
data_unset * array_match_key_prefix_klen (const array * const a, const char * const s, const uint32_t slen);

__attribute_pure__
data_unset * array_match_key_prefix_nc_klen (const array * const a, const char * const s, const uint32_t slen);

__attribute_pure__
data_unset * array_match_key_prefix (const array * const a, const buffer * const b);

__attribute_pure__
data_unset * array_match_key_prefix_nc (const array * const a, const buffer * const b);

__attribute_pure__
const buffer * array_match_value_prefix (const array * const a, const buffer * const b);

__attribute_pure__
const buffer * array_match_value_prefix_nc (const array * const a, const buffer * const b);

__attribute_pure__
data_unset * array_match_key_suffix (const array * const a, const buffer * const b);

__attribute_pure__
data_unset * array_match_key_suffix_nc (const array * const a, const buffer * const b);

__attribute_pure__
const buffer * array_match_value_suffix (const array * const a, const buffer * const b);

__attribute_pure__
const buffer * array_match_value_suffix_nc (const array * const a, const buffer * const b);

__attribute_pure__
data_unset * array_match_path_or_ext (const array * const a, const buffer * const b);

#endif