Module: CA
- Defined in:
- ext/ruby_carray.c
Class Method Summary collapse
-
.sort_addr(*args) ⇒ Object
(Sort) Returns index table for index sort.
Class Method Details
.sort_addr(*args) ⇒ Object
(Sort) Returns index table for index sort
idx = CA.sort_addr(a, b, c) ### priority a > b > c
a[idx]
b[idx]
c[idx]
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'ext/carray_sort_addr.c', line 162
static VALUE
rb_ca_s_sort_addr (int argc, VALUE *argv, VALUE self)
{
volatile VALUE out;
CArray *co;
struct cmp_base *base;
struct cmp_data *data;
ca_size_t elements;
ca_size_t *q;
int j;
ca_size_t i;
if ( argc <= 0 ) {
rb_raise(rb_eArgError, "no arg given");
}
rb_check_carray_object(argv[0]);
elements = NUM2SIZE(rb_ca_elements(argv[0]));
for (j=0; j<argc; j++) {
rb_check_carray_object(argv[j]);
if ( elements != NUM2SIZE(rb_ca_elements(argv[j])) ) {
rb_raise(rb_eArgError, "elements mismatch");
}
}
base = malloc_with_check(sizeof(struct cmp_base));
base->n = argc;
base->ca = malloc_with_check(sizeof(CArray *)*base->n);
for (j=0; j<argc; j++) {
CArray *ca;
Data_Get_Struct(argv[j], CArray, ca);
base->ca[j] = ca;
ca_attach(ca);
}
data = malloc_with_check(sizeof(struct cmp_data)*elements);
for (i=0; i<elements; i++) {
data[i].i = i;
data[i].base = base;
}
#ifdef HAVE_MERGESORT
mergesort(data, elements, sizeof(struct cmp_data),
(int (*)(const void*,const void*)) qcmp_func);
#else
qsort(data, elements, sizeof(struct cmp_data),
(int (*)(const void*,const void*)) qcmp_func);
#endif
out = rb_ca_template_with_type(argv[0], INT2NUM(CA_SIZE), INT2NUM(0));
Data_Get_Struct(out, CArray, co);
q = (ca_size_t *) co->ptr;
for (i=0; i<elements; i++) {
*q = data[i].i;
q++;
}
for (j=0; j<argc; j++) {
ca_detach(base->ca[j]);
}
free(data);
free(base->ca);
free(base);
return out;
}
|