Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 194 lines (171 sloc) 5.267 kb
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
1 /*
8511bcb added xmalloc_ptrs() function and use it when it's necessary
Laurent Sansonetti authored
2 * MacRuby Array.
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
3 *
4 * This file is covered by the Ruby license. See COPYING for more details.
7d7d3e8 @ferrous26 Change ownership to The MacRuby Team and update copyrights
ferrous26 authored
5 *
6 * Copyright (C) 2012, The MacRuby Team. All rights reserved.
9595725 update copyrights to 2011
Laurent Sansonetti authored
7 * Copyright (C) 2011, Apple Inc. All rights reserved.
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
8 */
9
10 #ifndef __ARRAY_H_
11 #define __ARRAY_H_
12
13 #if defined(__cplusplus)
14 extern "C" {
15 #endif
16
548a941 @Watson1978 fix the struct declarations
Watson1978 authored
17 typedef struct RArray {
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
18 struct RBasic basic;
19 size_t beg;
20 size_t len;
21 size_t cap;
22 VALUE *elements;
23 } rb_ary_t;
24
532eb6e @Watson1978 will throw an exception when was passed very large index into rary_st…
Watson1978 authored
25 #define ARY_MAX_SIZE (LONG_MAX / (int)sizeof(VALUE))
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
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 {
f39c792 r{ary,str,hash}_modify: check for the unstrust flag properly
Laurent Sansonetti authored
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");
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
52 }
f39c792 r{ary,str,hash}_modify: check for the unstrust flag properly
Laurent Sansonetti authored
53 if ((mask & FL_UNTRUSTED) != FL_UNTRUSTED) {
54 if (rb_safe_level() >= 4) {
55 rb_raise(rb_eSecurityError, "Insecure: can't modify array");
56 }
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
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
002ed5d now compile some of the VM primitives into bitcode that will be added…
Laurent Sansonetti authored
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 }
532eb6e @Watson1978 will throw an exception when was passed very large index into rary_st…
Watson1978 authored
109 else if (idx >= ARY_MAX_SIZE) {
110 rb_raise(rb_eIndexError, "index %ld too big", idx);
111 }
112
c5041c0 @Watson1978 Array#[]= will throw an exception when Array has been frozen.
Watson1978 authored
113 rary_modify(ary);
002ed5d now compile some of the VM primitives into bitcode that will be added…
Laurent Sansonetti authored
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
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
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);
a4c25b8 improved rb_eql() for performance, makes faster hash lookup / keys co…
Laurent Sansonetti authored
159 bool rary_eql_fast(rb_ary_t *ary1, rb_ary_t *ary2);
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
160
161 // Shared implementations.
162 VALUE rary_join(VALUE ary, SEL sel, int argc, VALUE *argv);
fdc829b @Watson1978 move the implementation about #hash from Array to NSArray, in order t…
Watson1978 authored
163 VALUE rary_hash(VALUE ary, SEL sel);
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
164 VALUE rary_zip(VALUE ary, SEL sel, int argc, VALUE *argv);
165 VALUE rary_transpose(VALUE ary, SEL sel);
166 VALUE rary_fill(VALUE ary, SEL sel, int argc, VALUE *argv);
167 VALUE rary_cmp(VALUE ary1, SEL sel, VALUE ary2);
168 VALUE rary_assoc(VALUE ary, SEL sel, VALUE key);
169 VALUE rary_rassoc(VALUE ary, SEL sel, VALUE value);
170 VALUE rary_flatten(VALUE ary, SEL sel, int argc, VALUE *argv);
171 VALUE rary_flatten_bang(VALUE ary, SEL sel, int argc, VALUE *argv);
172 VALUE rary_product(VALUE ary, SEL sel, int argc, VALUE *argv);
173 VALUE rary_combination(VALUE ary, SEL sel, VALUE num);
174 VALUE rary_permutation(VALUE ary, SEL sel, int argc, VALUE *argv);
175 VALUE rary_cycle(VALUE ary, SEL sel, int argc, VALUE *argv);
176 VALUE rary_sample(VALUE ary, SEL sel, int argc, VALUE *argv);
177 VALUE rary_diff(VALUE ary1, SEL sel, VALUE ary2);
178 VALUE rary_and(VALUE ary1, SEL sel, VALUE ary2);
179 VALUE rary_or(VALUE ary1, SEL sel, VALUE ary2);
281300f @Watson1978 move the implementation about #roatation from Array to NSArray, in or…
Watson1978 authored
180 VALUE rary_rotate(VALUE ary, SEL sel, int argc, VALUE *argv);
181 VALUE rary_rotate_bang(VALUE ary, SEL sel, int argc, VALUE *argv);
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
182
30d0432 @Watson1978 add the block support with NSArray#uniq
Watson1978 authored
183 VALUE rb_ary_make_hash(VALUE ary1, VALUE ary2);
184 VALUE rb_ary_make_hash_by(VALUE ary);
185
01ec3c8 implement a better RubyHash hashing function for arrays, as the CF on…
Laurent Sansonetti authored
186 unsigned long rb_ary_hash(VALUE ary);
f03ba94 @lrz String() and Array() will respectively convert NSStrings and NSArrays…
lrz authored
187 VALUE rb_f_array(VALUE, SEL, VALUE);
01ec3c8 implement a better RubyHash hashing function for arrays, as the CF on…
Laurent Sansonetti authored
188
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
189 #if defined(__cplusplus)
190 } // extern "C"
191 #endif
192
193 #endif // __ARRAY_H_
Something went wrong with that request. Please try again.