Skip to content
This repository
Newer
Older
100644 192 lines (170 sloc) 5.23 kb
ae059217 » Laurent Sansonetti
2010-02-07 completed Array refactoring, cleaned out a few things
1 /*
8511bcb8 » Laurent Sansonetti
2010-04-01 added xmalloc_ptrs() function and use it when it's necessary
2 * MacRuby Array.
ae059217 » Laurent Sansonetti
2010-02-07 completed Array refactoring, cleaned out a few things
3 *
4 * This file is covered by the Ruby license. See COPYING for more details.
7d7d3e80 » ferrous26
2012-04-22 Change ownership to The MacRuby Team and update copyrights
5 *
6 * Copyright (C) 2012, The MacRuby Team. All rights reserved.
95957256 » Laurent Sansonetti
2011-01-15 update copyrights to 2011
7 * Copyright (C) 2011, Apple Inc. All rights reserved.
ae059217 » Laurent Sansonetti
2010-02-07 completed Array refactoring, cleaned out a few things
8 */
9
10 #ifndef __ARRAY_H_
11 #define __ARRAY_H_
12
13 #if defined(__cplusplus)
14 extern "C" {
15 #endif
16
548a941c » Watson1978
2012-06-20 fix the struct declarations
17 typedef struct RArray {
ae059217 » Laurent Sansonetti
2010-02-07 completed Array refactoring, cleaned out a few things
18 struct RBasic basic;
19 size_t beg;
20 size_t len;
21 size_t cap;
22 VALUE *elements;
23 } rb_ary_t;
24
532eb6e1 » Watson1978
2011-01-02 will throw an exception when was passed very large index into rary_st…
25 #define ARY_MAX_SIZE (LONG_MAX / (int)sizeof(VALUE))
ae059217 » Laurent Sansonetti
2010-02-07 completed Array refactoring, cleaned out a few things
26 #define RARY(x) ((rb_ary_t *)x)
27
28 static inline bool
29 rb_klass_is_rary(VALUE klass)
30 {
31 do {
32 if (klass == rb_cRubyArray) {
33 return true;
34 }
35 if (klass == rb_cNSArray) {
36 return false;
37 }
38 klass = RCLASS_SUPER(klass);
39 }
40 while (klass != 0);
41 return false;
42 }
43
44 #define IS_RARY(x) (rb_klass_is_rary(*(VALUE *)x))
45
46 static inline void
47 rary_modify(VALUE ary)
48 {
f39c792e » Laurent Sansonetti
2011-01-28 r{ary,str,hash}_modify: check for the unstrust flag properly
49 const long mask = RBASIC(ary)->flags;
50 if ((mask & FL_FREEZE) == FL_FREEZE) {
51 rb_raise(rb_eRuntimeError, "can't modify frozen/immutable array");
ae059217 » Laurent Sansonetti
2010-02-07 completed Array refactoring, cleaned out a few things
52 }
f39c792e » Laurent Sansonetti
2011-01-28 r{ary,str,hash}_modify: check for the unstrust flag properly
53 if ((mask & FL_UNTRUSTED) != FL_UNTRUSTED) {
54 if (rb_safe_level() >= 4) {
55 rb_raise(rb_eSecurityError, "Insecure: can't modify array");
56 }
ae059217 » Laurent Sansonetti
2010-02-07 completed Array refactoring, cleaned out a few things
57 }
58 }
59
60 static inline VALUE
61 rary_elt(VALUE ary, size_t idx)
62 {
63 //assert(idx < RARY(ary)->len);
64 return RARY(ary)->elements[RARY(ary)->beg + idx];
65 }
66
67 static inline void
68 rary_elt_set(VALUE ary, size_t idx, VALUE item)
69 {
70 //assert(idx < ary->len);
71 GC_WB(&RARY(ary)->elements[RARY(ary)->beg + idx], item);
72 }
73
74 static inline VALUE *
75 rary_ptr(VALUE ary)
76 {
77 return &RARY(ary)->elements[RARY(ary)->beg];
78 }
79
80 static inline VALUE
81 rary_entry(VALUE ary, long offset)
82 {
83 const long n = RARY(ary)->len;
84 if (n == 0) {
85 return Qnil;
86 }
87 if (offset < 0) {
88 offset += n;
89 }
90 if (offset < 0 || n <= offset) {
91 return Qnil;
92 }
93 return rary_elt(ary, offset);
94 }
95
002ed5d8 » Laurent Sansonetti
2010-05-24 now compile some of the VM primitives into bitcode that will be added…
96 void rary_reserve(VALUE ary, size_t newlen);
97
98 static inline void
99 rary_store(VALUE ary, long idx, VALUE item)
100 {
101 if (idx < 0) {
102 const long len = RARY(ary)->len;
103 idx += len;
104 if (idx < 0) {
105 rb_raise(rb_eIndexError, "index %ld out of array",
106 idx - len);
107 }
108 }
532eb6e1 » Watson1978
2011-01-02 will throw an exception when was passed very large index into rary_st…
109 else if (idx >= ARY_MAX_SIZE) {
110 rb_raise(rb_eIndexError, "index %ld too big", idx);
111 }
112
c5041c0f » Watson1978
2011-01-02 Array#[]= will throw an exception when Array has been frozen.
113 rary_modify(ary);
002ed5d8 » Laurent Sansonetti
2010-05-24 now compile some of the VM primitives into bitcode that will be added…
114 size_t uidx = (size_t)idx;
115 if (uidx >= RARY(ary)->len) {
116 rary_reserve(ary, uidx + 1);
117 size_t i;
118 for (i = RARY(ary)->len; i < uidx + 1; i++) {
119 rary_elt_set(ary, i, Qnil);
120 }
121 RARY(ary)->len = uidx + 1;
122 }
123 rary_elt_set(ary, uidx, item);
124 }
125
126 static inline void
127 rary_push(VALUE ary, VALUE item)
128 {
129 rary_reserve(ary, RARY(ary)->len + 1);
130 rary_elt_set(ary, RARY(ary)->len, item);
131 RARY(ary)->len++;
132 }
133
ae059217 » Laurent Sansonetti
2010-02-07 completed Array refactoring, cleaned out a few things
134 static inline VALUE
135 to_ary(VALUE ary)
136 {
137 return rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
138 }
139
140 VALUE rary_dup(VALUE ary, SEL sel);
141 VALUE rary_clear(VALUE ary, SEL sel);
142 VALUE rary_reverse_bang(VALUE ary, SEL sel);
143 VALUE rary_includes(VALUE ary, SEL sel, VALUE item);
144 VALUE rary_delete(VALUE ary, SEL sel, VALUE item);
145 VALUE rary_delete_at(VALUE ary, SEL sel, VALUE pos);
146 VALUE rary_pop(VALUE ary, SEL sel, int argc, VALUE *argv);
147 VALUE rary_shift(VALUE ary, SEL sel, int argc, VALUE *argv);
148 VALUE rary_aref(VALUE ary, SEL sel, int argc, VALUE *argv);
149 VALUE rary_plus(VALUE x, SEL sel, VALUE y);
150 VALUE rary_push_m(VALUE ary, SEL sel, VALUE item);
151 VALUE rary_concat_m(VALUE x, SEL sel, VALUE y);
152 VALUE rary_last(VALUE ary, SEL sel, int argc, VALUE *argv);
153 VALUE rary_unshift(VALUE ary, SEL sel, int argc, VALUE *argv);
154 VALUE rary_each(VALUE ary, SEL sel);
155 VALUE rary_sort(VALUE ary, SEL sel);
156 VALUE rary_sort_bang(VALUE ary, SEL sel);
157 VALUE rary_subseq(VALUE ary, long beg, long len);
158 void rary_insert(VALUE ary, long idx, VALUE val);
a4c25b87 » Laurent Sansonetti
2010-12-17 improved rb_eql() for performance, makes faster hash lookup / keys co…
159 bool rary_eql_fast(rb_ary_t *ary1, rb_ary_t *ary2);
ae059217 » Laurent Sansonetti
2010-02-07 completed Array refactoring, cleaned out a few things
160
161 // Shared implementations.
162 VALUE rary_join(VALUE ary, SEL sel, int argc, VALUE *argv);
163 VALUE rary_zip(VALUE ary, SEL sel, int argc, VALUE *argv);
164 VALUE rary_transpose(VALUE ary, SEL sel);
165 VALUE rary_fill(VALUE ary, SEL sel, int argc, VALUE *argv);
166 VALUE rary_cmp(VALUE ary1, SEL sel, VALUE ary2);
167 VALUE rary_assoc(VALUE ary, SEL sel, VALUE key);
168 VALUE rary_rassoc(VALUE ary, SEL sel, VALUE value);
169 VALUE rary_flatten(VALUE ary, SEL sel, int argc, VALUE *argv);
170 VALUE rary_flatten_bang(VALUE ary, SEL sel, int argc, VALUE *argv);
171 VALUE rary_product(VALUE ary, SEL sel, int argc, VALUE *argv);
172 VALUE rary_combination(VALUE ary, SEL sel, VALUE num);
173 VALUE rary_permutation(VALUE ary, SEL sel, int argc, VALUE *argv);
174 VALUE rary_cycle(VALUE ary, SEL sel, int argc, VALUE *argv);
175 VALUE rary_sample(VALUE ary, SEL sel, int argc, VALUE *argv);
176 VALUE rary_diff(VALUE ary1, SEL sel, VALUE ary2);
177 VALUE rary_and(VALUE ary1, SEL sel, VALUE ary2);
178 VALUE rary_or(VALUE ary1, SEL sel, VALUE ary2);
281300f1 » Watson1978
2012-07-05 move the implementation about #roatation from Array to NSArray, in or…
179 VALUE rary_rotate(VALUE ary, SEL sel, int argc, VALUE *argv);
180 VALUE rary_rotate_bang(VALUE ary, SEL sel, int argc, VALUE *argv);
ae059217 » Laurent Sansonetti
2010-02-07 completed Array refactoring, cleaned out a few things
181
30d0432a » Watson1978
2012-07-04 add the block support with NSArray#uniq
182 VALUE rb_ary_make_hash(VALUE ary1, VALUE ary2);
183 VALUE rb_ary_make_hash_by(VALUE ary);
184
01ec3c8b » Laurent Sansonetti
2010-12-17 implement a better RubyHash hashing function for arrays, as the CF on…
185 unsigned long rb_ary_hash(VALUE ary);
f03ba947 » lrz
2011-05-27 String() and Array() will respectively convert NSStrings and NSArrays…
186 VALUE rb_f_array(VALUE, SEL, VALUE);
01ec3c8b » Laurent Sansonetti
2010-12-17 implement a better RubyHash hashing function for arrays, as the CF on…
187
ae059217 » Laurent Sansonetti
2010-02-07 completed Array refactoring, cleaned out a few things
188 #if defined(__cplusplus)
189 } // extern "C"
190 #endif
191
192 #endif // __ARRAY_H_
Something went wrong with that request. Please try again.