[270] | 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 |
|
---|
[331] | 10 | #include "common.h"
|
---|
[270] | 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.
|
---|
[331] | 57 | * @return The initialized array.
|
---|
[270] | 58 | */
|
---|
| 59 | MRB_API mrb_value mrb_ary_new(mrb_state *mrb);
|
---|
| 60 |
|
---|
[331] | 61 | /*
|
---|
| 62 | * Initializes a new array with initial values
|
---|
| 63 | *
|
---|
| 64 | * Equivalent to:
|
---|
| 65 | *
|
---|
| 66 | * Array[value1, value2, ...]
|
---|
| 67 | *
|
---|
| 68 | * @param mrb The mruby state reference.
|
---|
| 69 | * @param size The numer of values.
|
---|
| 70 | * @param vals The actual values.
|
---|
| 71 | * @return The initialized array.
|
---|
| 72 | */
|
---|
[270] | 73 | MRB_API mrb_value mrb_ary_new_from_values(mrb_state *mrb, mrb_int size, const mrb_value *vals);
|
---|
[331] | 74 |
|
---|
| 75 | /*
|
---|
| 76 | * Initializes a new array with two initial values
|
---|
| 77 | *
|
---|
| 78 | * Equivalent to:
|
---|
| 79 | *
|
---|
| 80 | * Array[car, cdr]
|
---|
| 81 | *
|
---|
| 82 | * @param mrb The mruby state reference.
|
---|
| 83 | * @param car The first value.
|
---|
| 84 | * @param cdr The second value.
|
---|
| 85 | * @return The initialized array.
|
---|
| 86 | */
|
---|
[270] | 87 | MRB_API mrb_value mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr);
|
---|
| 88 |
|
---|
| 89 | /*
|
---|
[331] | 90 | * Concatenate two arrays. The target array will be modified
|
---|
| 91 | *
|
---|
| 92 | * Equivalent to:
|
---|
| 93 | * ary.concat(other)
|
---|
| 94 | *
|
---|
| 95 | * @param mrb The mruby state reference.
|
---|
| 96 | * @param self The target array.
|
---|
| 97 | * @param other The array that will be concatenated to self.
|
---|
| 98 | */
|
---|
| 99 | MRB_API void mrb_ary_concat(mrb_state *mrb, mrb_value self, mrb_value other);
|
---|
| 100 |
|
---|
| 101 | /*
|
---|
| 102 | * Create an array from the input. It tries calling to_a on the
|
---|
| 103 | * value. If value does not respond to that, it creates a new
|
---|
| 104 | * array with just this value.
|
---|
| 105 | *
|
---|
| 106 | * @param mrb The mruby state reference.
|
---|
| 107 | * @param value The value to change into an array.
|
---|
| 108 | * @return An array representation of value.
|
---|
| 109 | */
|
---|
| 110 | MRB_API mrb_value mrb_ary_splat(mrb_state *mrb, mrb_value value);
|
---|
| 111 |
|
---|
| 112 | /*
|
---|
[270] | 113 | * Pushes value into array.
|
---|
| 114 | *
|
---|
| 115 | * Equivalent to:
|
---|
| 116 | *
|
---|
| 117 | * ary << value
|
---|
| 118 | *
|
---|
| 119 | * @param mrb The mruby state reference.
|
---|
| 120 | * @param ary The array in which the value will be pushed
|
---|
| 121 | * @param value The value to be pushed into array
|
---|
| 122 | */
|
---|
| 123 | MRB_API void mrb_ary_push(mrb_state *mrb, mrb_value array, mrb_value value);
|
---|
| 124 |
|
---|
| 125 | /*
|
---|
| 126 | * Pops the last element from the array.
|
---|
| 127 | *
|
---|
| 128 | * Equivalent to:
|
---|
| 129 | *
|
---|
| 130 | * ary.pop
|
---|
| 131 | *
|
---|
| 132 | * @param mrb The mruby state reference.
|
---|
[331] | 133 | * @param ary The array from which the value will be popped.
|
---|
| 134 | * @return The popped value.
|
---|
[270] | 135 | */
|
---|
| 136 | MRB_API mrb_value mrb_ary_pop(mrb_state *mrb, mrb_value ary);
|
---|
| 137 |
|
---|
| 138 | /*
|
---|
| 139 | * Returns a reference to an element of the array on the given index.
|
---|
| 140 | *
|
---|
| 141 | * Equivalent to:
|
---|
| 142 | *
|
---|
| 143 | * ary[n]
|
---|
| 144 | *
|
---|
| 145 | * @param mrb The mruby state reference.
|
---|
| 146 | * @param ary The target array.
|
---|
| 147 | * @param n The array index being referenced
|
---|
| 148 | * @return The referenced value.
|
---|
| 149 | */
|
---|
| 150 | MRB_API mrb_value mrb_ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n);
|
---|
| 151 |
|
---|
| 152 | /*
|
---|
| 153 | * Sets a value on an array at the given index
|
---|
| 154 | *
|
---|
| 155 | * Equivalent to:
|
---|
| 156 | *
|
---|
| 157 | * ary[n] = val
|
---|
| 158 | *
|
---|
| 159 | * @param mrb The mruby state reference.
|
---|
| 160 | * @param ary The target array.
|
---|
| 161 | * @param n The array index being referenced.
|
---|
| 162 | * @param val The value being setted.
|
---|
| 163 | */
|
---|
| 164 | MRB_API void mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val);
|
---|
| 165 |
|
---|
[331] | 166 | /*
|
---|
| 167 | * Replace the array with another array
|
---|
| 168 | *
|
---|
| 169 | * Equivalent to:
|
---|
| 170 | *
|
---|
| 171 | * ary.replace(other)
|
---|
| 172 | *
|
---|
| 173 | * @param mrb The mruby state reference
|
---|
| 174 | * @param self The target array.
|
---|
| 175 | * @param other The array to replace it with.
|
---|
| 176 | */
|
---|
| 177 | MRB_API void mrb_ary_replace(mrb_state *mrb, mrb_value self, mrb_value other);
|
---|
[270] | 178 | MRB_API mrb_value mrb_check_array_type(mrb_state *mrb, mrb_value self);
|
---|
[331] | 179 |
|
---|
| 180 | /*
|
---|
| 181 | * Unshift an element into an array
|
---|
| 182 | *
|
---|
| 183 | * Equivalent to:
|
---|
| 184 | *
|
---|
| 185 | * ary.unshift(item)
|
---|
| 186 | *
|
---|
| 187 | * @param mrb The mruby state reference.
|
---|
| 188 | * @param self The target array.
|
---|
| 189 | * @param item The item to unshift.
|
---|
| 190 | */
|
---|
[270] | 191 | MRB_API mrb_value mrb_ary_unshift(mrb_state *mrb, mrb_value self, mrb_value item);
|
---|
| 192 | MRB_API mrb_value mrb_ary_entry(mrb_value ary, mrb_int offset);
|
---|
[331] | 193 |
|
---|
| 194 | /*
|
---|
| 195 | * Shifts the first element from the array.
|
---|
| 196 | *
|
---|
| 197 | * Equivalent to:
|
---|
| 198 | *
|
---|
| 199 | * ary.shift
|
---|
| 200 | *
|
---|
| 201 | * @param mrb The mruby state reference.
|
---|
| 202 | * @param self The array from which the value will be shifted.
|
---|
| 203 | * @return The shifted value.
|
---|
| 204 | */
|
---|
[270] | 205 | MRB_API mrb_value mrb_ary_shift(mrb_state *mrb, mrb_value self);
|
---|
[331] | 206 |
|
---|
| 207 | /*
|
---|
| 208 | * Removes all elements from this array
|
---|
| 209 | *
|
---|
| 210 | * Equivalent to:
|
---|
| 211 | *
|
---|
| 212 | * ary.clear
|
---|
| 213 | *
|
---|
| 214 | * @param mrb The mruby state reference.
|
---|
| 215 | * @param self The target array.
|
---|
| 216 | * @return self
|
---|
| 217 | */
|
---|
[270] | 218 | MRB_API mrb_value mrb_ary_clear(mrb_state *mrb, mrb_value self);
|
---|
[331] | 219 |
|
---|
| 220 | /*
|
---|
| 221 | * Join the array elements together in a string
|
---|
| 222 | *
|
---|
| 223 | * Equivalent to:
|
---|
| 224 | *
|
---|
| 225 | * ary.join(sep="")
|
---|
| 226 | *
|
---|
| 227 | * @param mrb The mruby state reference.
|
---|
| 228 | * @param ary The target array
|
---|
| 229 | * @param sep The separater, can be NULL
|
---|
| 230 | */
|
---|
[270] | 231 | MRB_API mrb_value mrb_ary_join(mrb_state *mrb, mrb_value ary, mrb_value sep);
|
---|
| 232 |
|
---|
[331] | 233 | /*
|
---|
| 234 | * Update the capacity of the array
|
---|
| 235 | *
|
---|
| 236 | * @param mrb The mruby state reference.
|
---|
| 237 | * @param ary The target array.
|
---|
| 238 | * @param new_len The new capacity of the array
|
---|
| 239 | */
|
---|
| 240 | MRB_API mrb_value mrb_ary_resize(mrb_state *mrb, mrb_value ary, mrb_int new_len);
|
---|
| 241 |
|
---|
[270] | 242 | static inline mrb_int
|
---|
| 243 | mrb_ary_len(mrb_state *mrb, mrb_value ary)
|
---|
| 244 | {
|
---|
| 245 | (void)mrb;
|
---|
| 246 | mrb_assert(mrb_array_p(ary));
|
---|
| 247 | return RARRAY_LEN(ary);
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[331] | 250 | static inline mrb_value
|
---|
| 251 | ary_elt(mrb_value ary, mrb_int offset)
|
---|
| 252 | {
|
---|
| 253 | if (offset < 0 || RARRAY_LEN(ary) <= offset) {
|
---|
| 254 | return mrb_nil_value();
|
---|
| 255 | }
|
---|
| 256 | return RARRAY_PTR(ary)[offset];
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[270] | 259 | MRB_END_DECL
|
---|
| 260 |
|
---|
| 261 | #endif /* MRUBY_ARRAY_H */
|
---|