6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'ext/just_all_the_same/just_all_the_same.c', line 6
static VALUE
all_same_p(VALUE ary, VALUE target)
{
long size = RARRAY_LEN(ary);
if (size == 0) return Qtrue;
VALUE *forward_p = RARRAY_PTR(ary);
if (forward_p[0] != target) return Qfalse;
VALUE *backward_p;
long s = size; // size of foward_ary + backward_aray
long v = size / 2; // splitting index
/* split algorithm */
while (v >= SWITCH_TO_LINER / 2)
{
backward_p = forward_p + v;
/* check last of array nil? if size is not odd? */
if (s%2 && forward_p[s-1] != target) return Qfalse;
/* check quick same back and fowerd */
if (memcmp(forward_p, backward_p, sizeof(VALUE) * v)) return Qfalse;
s = v;
v /= 2;
}
/* liner algorithm */
for (long i = 1; i < s; i++) if (forward_p[i] != target) return Qfalse;
return Qtrue;
}
|