1 | /*
|
---|
2 | ** 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 "mruby/common.h"
|
---|
11 |
|
---|
12 | /*
|
---|
13 | * Array class
|
---|
14 | */
|
---|
15 | MRB_BEGIN_DECL
|
---|
16 |
|
---|
17 |
|
---|
18 | typedef struct mrb_shared_array {
|
---|
19 | int refcnt;
|
---|
20 | mrb_int len;
|
---|
21 | mrb_value *ptr;
|
---|
22 | } mrb_shared_array;
|
---|
23 |
|
---|
24 | struct RArray {
|
---|
25 | MRB_OBJECT_HEADER;
|
---|
26 | mrb_int len;
|
---|
27 | union {
|
---|
28 | mrb_int capa;
|
---|
29 | mrb_shared_array *shared;
|
---|
30 | } aux;
|
---|
31 | mrb_value *ptr;
|
---|
32 | };
|
---|
33 |
|
---|
34 | #define mrb_ary_ptr(v) ((struct RArray*)(mrb_ptr(v)))
|
---|
35 | #define mrb_ary_value(p) mrb_obj_value((void*)(p))
|
---|
36 | #define RARRAY(v) ((struct RArray*)(mrb_ptr(v)))
|
---|
37 |
|
---|
38 | #define RARRAY_LEN(a) (RARRAY(a)->len)
|
---|
39 | #define RARRAY_PTR(a) ((const mrb_value*)RARRAY(a)->ptr)
|
---|
40 | #define MRB_ARY_SHARED 256
|
---|
41 | #define ARY_SHARED_P(a) ((a)->flags & MRB_ARY_SHARED)
|
---|
42 | #define ARY_SET_SHARED_FLAG(a) ((a)->flags |= MRB_ARY_SHARED)
|
---|
43 | #define ARY_UNSET_SHARED_FLAG(a) ((a)->flags &= ~MRB_ARY_SHARED)
|
---|
44 |
|
---|
45 | void mrb_ary_decref(mrb_state*, mrb_shared_array*);
|
---|
46 | MRB_API void mrb_ary_modify(mrb_state*, struct RArray*);
|
---|
47 | MRB_API mrb_value mrb_ary_new_capa(mrb_state*, mrb_int);
|
---|
48 |
|
---|
49 | /*
|
---|
50 | * Initializes a new array.
|
---|
51 | *
|
---|
52 | * Equivalent to:
|
---|
53 | *
|
---|
54 | * Array.new
|
---|
55 | *
|
---|
56 | * @param mrb The mruby state reference.
|
---|
57 | * @return The initialized array
|
---|
58 | */
|
---|
59 | MRB_API mrb_value mrb_ary_new(mrb_state *mrb);
|
---|
60 |
|
---|
61 | MRB_API mrb_value mrb_ary_new_from_values(mrb_state *mrb, mrb_int size, const mrb_value *vals);
|
---|
62 | MRB_API mrb_value mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr);
|
---|
63 | MRB_API void mrb_ary_concat(mrb_state*, mrb_value, mrb_value);
|
---|
64 | MRB_API mrb_value mrb_ary_splat(mrb_state*, mrb_value);
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * Pushes value into array.
|
---|
68 | *
|
---|
69 | * Equivalent to:
|
---|
70 | *
|
---|
71 | * ary << value
|
---|
72 | *
|
---|
73 | * @param mrb The mruby state reference.
|
---|
74 | * @param ary The array in which the value will be pushed
|
---|
75 | * @param value The value to be pushed into array
|
---|
76 | */
|
---|
77 | MRB_API void mrb_ary_push(mrb_state *mrb, mrb_value array, mrb_value value);
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * Pops the last element from the array.
|
---|
81 | *
|
---|
82 | * Equivalent to:
|
---|
83 | *
|
---|
84 | * ary.pop
|
---|
85 | *
|
---|
86 | * @param mrb The mruby state reference.
|
---|
87 | * @param ary The array from which the value will be poped.
|
---|
88 | * @return The poped value.
|
---|
89 | */
|
---|
90 | MRB_API mrb_value mrb_ary_pop(mrb_state *mrb, mrb_value ary);
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * Returns a reference to an element of the array on the given index.
|
---|
94 | *
|
---|
95 | * Equivalent to:
|
---|
96 | *
|
---|
97 | * ary[n]
|
---|
98 | *
|
---|
99 | * @param mrb The mruby state reference.
|
---|
100 | * @param ary The target array.
|
---|
101 | * @param n The array index being referenced
|
---|
102 | * @return The referenced value.
|
---|
103 | */
|
---|
104 | MRB_API mrb_value mrb_ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n);
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Sets a value on an array at the given index
|
---|
108 | *
|
---|
109 | * Equivalent to:
|
---|
110 | *
|
---|
111 | * ary[n] = val
|
---|
112 | *
|
---|
113 | * @param mrb The mruby state reference.
|
---|
114 | * @param ary The target array.
|
---|
115 | * @param n The array index being referenced.
|
---|
116 | * @param val The value being setted.
|
---|
117 | */
|
---|
118 | MRB_API void mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val);
|
---|
119 |
|
---|
120 | MRB_API void mrb_ary_replace(mrb_state *mrb, mrb_value a, mrb_value b);
|
---|
121 | MRB_API mrb_value mrb_check_array_type(mrb_state *mrb, mrb_value self);
|
---|
122 | MRB_API mrb_value mrb_ary_unshift(mrb_state *mrb, mrb_value self, mrb_value item);
|
---|
123 | MRB_API mrb_value mrb_ary_entry(mrb_value ary, mrb_int offset);
|
---|
124 | MRB_API mrb_value mrb_ary_shift(mrb_state *mrb, mrb_value self);
|
---|
125 | MRB_API mrb_value mrb_ary_clear(mrb_state *mrb, mrb_value self);
|
---|
126 | MRB_API mrb_value mrb_ary_join(mrb_state *mrb, mrb_value ary, mrb_value sep);
|
---|
127 | MRB_API mrb_value mrb_ary_resize(mrb_state *mrb, mrb_value ary, mrb_int len);
|
---|
128 |
|
---|
129 | static inline mrb_int
|
---|
130 | mrb_ary_len(mrb_state *mrb, mrb_value ary)
|
---|
131 | {
|
---|
132 | (void)mrb;
|
---|
133 | mrb_assert(mrb_array_p(ary));
|
---|
134 | return RARRAY_LEN(ary);
|
---|
135 | }
|
---|
136 |
|
---|
137 | MRB_END_DECL
|
---|
138 |
|
---|
139 | #endif /* MRUBY_ARRAY_H */
|
---|