Debian Codegraph @

vendor/mruby-include/mruby/array.h

raw
1 /**
2 ** @file mruby/array.h - Array class
3 **
4 ** See Copyright Notice in mruby.h
5 */
6
7 #ifndef MRUBY_ARRAY_H
8 #define MRUBY_ARRAY_H
9
10 #include "common.h"
11
12 /*
13 * Array class
14 */
15 MRB_BEGIN_DECL
16
17 typedef struct mrb_shared_array {
18 int refcnt;
19 mrb_ssize len;
20 mrb_value *ptr;
21 } mrb_shared_array;
22
23 #if defined(MRB_32BIT) && defined(MRB_NO_BOXING) && !defined(MRB_USE_FLOAT32) && !defined(MRB_ARY_NO_EMBED)
24 # define MRB_ARY_NO_EMBED
25 #endif
26
27 #ifdef MRB_ARY_NO_EMBED
28 # define MRB_ARY_EMBED_LEN_MAX 0
29 #else
30 # define MRB_ARY_EMBED_LEN_MAX ((mrb_int)(sizeof(void*)*3/sizeof(mrb_value)))
31 mrb_static_assert(MRB_ARY_EMBED_LEN_MAX > 0, "MRB_ARY_EMBED_LEN_MAX > 0");
32 #endif
33
34 struct RArray {
35 MRB_OBJECT_HEADER;
36 union {
37 struct {
38 mrb_ssize len;
39 union {
40 mrb_ssize capa;
41 mrb_shared_array *shared;
42 } aux;
43 mrb_value *ptr;
44 } heap;
45 #ifndef MRB_ARY_NO_EMBED
46 mrb_value ary[MRB_ARY_EMBED_LEN_MAX];
47 #endif
48 } as;
49 };
50
51 #define mrb_ary_ptr(v) ((struct RArray*)(mrb_ptr(v)))
52 #define mrb_ary_value(p) mrb_obj_value((void*)(p))
53 #define RARRAY(v) ((struct RArray*)(mrb_ptr(v)))
54
55 #ifdef MRB_ARY_NO_EMBED
56 #define ARY_EMBED_P(a) 0
57 #define ARY_UNSET_EMBED_FLAG(a) (void)0
58 #define ARY_EMBED_LEN(a) 0
59 #define ARY_SET_EMBED_LEN(a,len) (void)0
60 #define ARY_EMBED_PTR(a) 0
61 #else
62 #define MRB_ARY_EMBED_MASK 7
63 #define ARY_EMBED_P(a) ((a)->flags & MRB_ARY_EMBED_MASK)
64 #define ARY_UNSET_EMBED_FLAG(a) ((a)->flags &= ~(MRB_ARY_EMBED_MASK))
65 #define ARY_EMBED_LEN(a) ((mrb_int)(((a)->flags & MRB_ARY_EMBED_MASK) - 1))
66 #define ARY_SET_EMBED_LEN(a,len) ((a)->flags = ((a)->flags&~MRB_ARY_EMBED_MASK) | ((uint32_t)(len) + 1))
67 #define ARY_EMBED_PTR(a) ((a)->as.ary)
68 #endif
69
70 #define ARY_LEN(a) (ARY_EMBED_P(a)?ARY_EMBED_LEN(a):(mrb_int)(a)->as.heap.len)
71 #define ARY_PTR(a) (ARY_EMBED_P(a)?ARY_EMBED_PTR(a):(a)->as.heap.ptr)
72 #define RARRAY_LEN(a) ARY_LEN(RARRAY(a))
73 #define RARRAY_PTR(a) ARY_PTR(RARRAY(a))
74 #define ARY_SET_LEN(a,n) do {\
75 if (ARY_EMBED_P(a)) {\
76 mrb_assert((n) <= MRB_ARY_EMBED_LEN_MAX); \
77 ARY_SET_EMBED_LEN(a,n);\
78 }\
79 else\
80 (a)->as.heap.len = (n);\
81 } while (0)
82 #define ARY_CAPA(a) (ARY_EMBED_P(a)?MRB_ARY_EMBED_LEN_MAX:(a)->as.heap.aux.capa)
83 #define MRB_ARY_SHARED 256
84 #define ARY_SHARED_P(a) ((a)->flags & MRB_ARY_SHARED)
85 #define ARY_SET_SHARED_FLAG(a) ((a)->flags |= MRB_ARY_SHARED)
86 #define ARY_UNSET_SHARED_FLAG(a) ((a)->flags &= ~MRB_ARY_SHARED)
87
88 MRB_API void mrb_ary_modify(mrb_state*, struct RArray*);
89 MRB_API mrb_value mrb_ary_new_capa(mrb_state*, mrb_int);
90
91 /*
92 * Initializes a new array.
93 *
94 * Equivalent to:
95 *
96 * Array.new
97 *
98 * @param mrb The mruby state reference.
99 * @return The initialized array.
100 */
101 MRB_API mrb_value mrb_ary_new(mrb_state *mrb);
102
103 /*
104 * Initializes a new array with initial values
105 *
106 * Equivalent to:
107 *
108 * Array[value1, value2, ...]
109 *
110 * @param mrb The mruby state reference.
111 * @param size The number of values.
112 * @param vals The actual values.
113 * @return The initialized array.
114 */
115 MRB_API mrb_value mrb_ary_new_from_values(mrb_state *mrb, mrb_int size, const mrb_value *vals);
116
117 /*
118 * Initializes a new array with two initial values
119 *
120 * Equivalent to:
121 *
122 * Array[car, cdr]
123 *
124 * @param mrb The mruby state reference.
125 * @param car The first value.
126 * @param cdr The second value.
127 * @return The initialized array.
128 */
129 MRB_API mrb_value mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr);
130
131 /*
132 * Concatenate two arrays. The target array will be modified
133 *
134 * Equivalent to:
135 * ary.concat(other)
136 *
137 * @param mrb The mruby state reference.
138 * @param self The target array.
139 * @param other The array that will be concatenated to self.
140 */
141 MRB_API void mrb_ary_concat(mrb_state *mrb, mrb_value self, mrb_value other);
142
143 /*
144 * Create an array from the input. It tries calling to_a on the
145 * value. If value does not respond to that, it creates a new
146 * array with just this value.
147 *
148 * @param mrb The mruby state reference.
149 * @param value The value to change into an array.
150 * @return An array representation of value.
151 */
152 MRB_API mrb_value mrb_ary_splat(mrb_state *mrb, mrb_value value);
153
154 /*
155 * Pushes value into array.
156 *
157 * Equivalent to:
158 *
159 * ary << value
160 *
161 * @param mrb The mruby state reference.
162 * @param ary The array in which the value will be pushed
163 * @param value The value to be pushed into array
164 */
165 MRB_API void mrb_ary_push(mrb_state *mrb, mrb_value array, mrb_value value);
166
167 /*
168 * Pops the last element from the array.
169 *
170 * Equivalent to:
171 *
172 * ary.pop
173 *
174 * @param mrb The mruby state reference.
175 * @param ary The array from which the value will be popped.
176 * @return The popped value.
177 */
178 MRB_API mrb_value mrb_ary_pop(mrb_state *mrb, mrb_value ary);
179
180 /*
181 * Sets a value on an array at the given index
182 *
183 * Equivalent to:
184 *
185 * ary[n] = val
186 *
187 * @param mrb The mruby state reference.
188 * @param ary The target array.
189 * @param n The array index being referenced.
190 * @param val The value being set.
191 */
192 MRB_API void mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val);
193
194 /*
195 * Replace the array with another array
196 *
197 * Equivalent to:
198 *
199 * ary.replace(other)
200 *
201 * @param mrb The mruby state reference
202 * @param self The target array.
203 * @param other The array to replace it with.
204 */
205 MRB_API void mrb_ary_replace(mrb_state *mrb, mrb_value self, mrb_value other);
206
207 /*
208 * Unshift an element into the array
209 *
210 * Equivalent to:
211 *
212 * ary.unshift(item)
213 *
214 * @param mrb The mruby state reference.
215 * @param self The target array.
216 * @param item The item to unshift.
217 */
218 MRB_API mrb_value mrb_ary_unshift(mrb_state *mrb, mrb_value self, mrb_value item);
219
220 /*
221 * Get nth element in the array
222 *
223 * Equivalent to:
224 *
225 * ary[offset]
226 *
227 * @param ary The target array.
228 * @param offset The element position (negative counts from the tail).
229 */
230 MRB_API mrb_value mrb_ary_entry(mrb_value ary, mrb_int offset);
231 #define mrb_ary_ref(mrb, ary, n) mrb_ary_entry(ary, n)
232
233 /*
234 * Replace subsequence of an array.
235 *
236 * Equivalent to:
237 *
238 * ary[head, len] = rpl
239 *
240 * @param mrb The mruby state reference.
241 * @param self The array from which the value will be partiality replaced.
242 * @param head Beginning position of a replacement subsequence.
243 * @param len Length of a replacement subsequence.
244 * @param rpl The array of replacement elements.
245 * It is possible to pass `mrb_undef_value()` instead of an empty array.
246 * @return The receiver array.
247 */
248 MRB_API mrb_value mrb_ary_splice(mrb_state *mrb, mrb_value self, mrb_int head, mrb_int len, mrb_value rpl);
249
250 /*
251 * Shifts the first element from the array.
252 *
253 * Equivalent to:
254 *
255 * ary.shift
256 *
257 * @param mrb The mruby state reference.
258 * @param self The array from which the value will be shifted.
259 * @return The shifted value.
260 */
261 MRB_API mrb_value mrb_ary_shift(mrb_state *mrb, mrb_value self);
262
263 /*
264 * Removes all elements from the array
265 *
266 * Equivalent to:
267 *
268 * ary.clear
269 *
270 * @param mrb The mruby state reference.
271 * @param self The target array.
272 * @return self
273 */
274 MRB_API mrb_value mrb_ary_clear(mrb_state *mrb, mrb_value self);
275
276 /*
277 * Join the array elements together in a string
278 *
279 * Equivalent to:
280 *
281 * ary.join(sep="")
282 *
283 * @param mrb The mruby state reference.
284 * @param ary The target array
285 * @param sep The separator, can be NULL
286 */
287 MRB_API mrb_value mrb_ary_join(mrb_state *mrb, mrb_value ary, mrb_value sep);
288
289 /*
290 * Update the capacity of the array
291 *
292 * @param mrb The mruby state reference.
293 * @param ary The target array.
294 * @param new_len The new capacity of the array
295 */
296 MRB_API mrb_value mrb_ary_resize(mrb_state *mrb, mrb_value ary, mrb_int new_len);
297
298
299 MRB_END_DECL
300
301 #endif /* MRUBY_ARRAY_H */