Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial support for sort

git-svn-id: https://svn.parrot.org/parrot/trunk@7319 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information...
commit f35b03c94753b17a3550dd85aedbcec78b945154 1 parent ab2d681
@rubys rubys authored
View
28 classes/nci.pmc
@@ -137,6 +137,34 @@ Returns the function pointer as an integer.
return((INTVAL)PMC_data(SELF));
}
+/*
+
+=item C<INTVAL get_bool()>
+
+Returns the boolean value of the pointer.
+
+=cut
+
+*/
+
+ INTVAL get_bool () {
+ return(0 != (INTVAL)PMC_data(SELF));
+ }
+
+/*
+
+=item C<PMC* get_attr_str(STRING *name)>
+
+Return attribute named C<name>.
+
+=cut
+
+*/
+
+ PMC* get_attr_str(STRING* idx) {
+ return VTABLE_getprop(INTERP, SELF, idx);
+ }
+
}
/*
View
3  dynclasses/pybuiltin.pmc
@@ -160,8 +160,11 @@ enter_nci_method should create these as locals to begin with.
iter = Parrot_Hash_get_iter(INTERP, stash);
n = VTABLE_elements(INTERP, stash);
for (j = 0; j < n; ++j) {
+ PMC *nameprop = pmc_new(interpreter, dynclass_PyString);
key = VTABLE_shift_string(INTERP, iter);
item = VTABLE_get_pmc_keyed_str(INTERP, stash, key);
+ VTABLE_set_string_native(interpreter, nameprop, key);
+ VTABLE_setprop(interpreter, item, NAME, nameprop);
scratchpad_store_by_name(INTERP, pad, 0, key, item);
}
View
60 dynclasses/pyclass.pmc
@@ -24,6 +24,7 @@ methods you would expect to see on python objects).
/* cache of classes referenced */
static INTVAL dynclass_PyType;
static STRING *CLASS;
+static STRING *CMP;
static STRING *INT;
static STRING *ITER;
static STRING *NAME;
@@ -66,8 +67,8 @@ Parrot_PyClass_runops_fromc(Parrot_Interp interpreter, PMC *sub)
}
static PMC *
-Parrot_PyClass_run_meth_fromc_P(Parrot_Interp interpreter, PMC *sub, PMC *obj,
- STRING *meth)
+Parrot_PyClass_run_meth_fromc_P(Parrot_Interp interpreter, PMC *sub,
+ PMC *obj, STRING *meth)
{
/* TODO: optimize for func->vtable->base_type == enum_class_NCI */
@@ -90,6 +91,33 @@ Parrot_PyClass_run_meth_fromc_P(Parrot_Interp interpreter, PMC *sub, PMC *obj,
return ret;
}
+static PMC *
+Parrot_PyClass_run_meth_fromc_P_P(Parrot_Interp interpreter, PMC *sub,
+ PMC *obj, STRING *meth, PMC* arg1)
+{
+ /* TODO: optimize for func->vtable->base_type == enum_class_NCI */
+
+ struct parrot_regs_t *bp;
+ struct Parrot_Context ctx;
+ PMC *ret;
+
+ save_context(interpreter, &ctx);
+
+ REG_STR(0) = meth;
+ REG_INT(0) = 1;
+ REG_INT(1) = REG_INT(2) = 0;
+ REG_INT(3) = 2;
+ REG_PMC(5) = obj;
+ REG_PMC(6) = arg1;
+ interpreter->ctx.current_object = REG_PMC(2) = obj;
+
+ bp = Parrot_PyClass_runops_fromc(interpreter, sub);
+
+ ret = BP_REG_PMC(bp,5);
+ restore_context(interpreter, &ctx);
+ return ret;
+}
+
pmclass PyClass dynpmc group python_group {
/*
@@ -107,6 +135,7 @@ they will be used frequently here.
if (pass) {
dynclass_PyType = Parrot_PMC_typenum(INTERP, "PyType");
CLASS = const_string(INTERP, "__class__");
+ CMP = const_string(INTERP, "__cmp__");
INT = const_string(INTERP, "__int__");
ITER = const_string(INTERP, "__iter__");
NAME = const_string(INTERP, "__name__");
@@ -201,6 +230,33 @@ Return the class of this object.
/*
+=item C<INTVAL cmp(PMC *value)>
+
+Returns the result of comparing the integer with C<*value>.
+
+=cut
+
+TODO: optimize for NCI, PyProxyClass
+
+*/
+
+ INTVAL cmp(PMC* value) {
+ PMC *cmpsub, *ret;
+
+ if (SELF == value) return 0;
+
+ cmpsub = VTABLE_find_method(INTERP, SELF, CMP);
+ if (!cmpsub || !VTABLE_defined(INTERP, cmpsub))
+ return -1;
+
+ ret = Parrot_PyClass_run_meth_fromc_P_P(INTERP, cmpsub,
+ SELF, CMP, value);
+
+ return VTABLE_get_integer(INTERP, ret);
+ }
+
+/*
+
=item C<INTVAL get_integer()>
Returns the integer value of the object.
View
14 dynclasses/pyfunc.pmc
@@ -130,6 +130,20 @@ TODO: error checking (e.g., missing, duplicate or extra arguments)
/*
+=item C<PMC* get_attr_str(STRING *name)>
+
+Return attribute named C<name>.
+
+=cut
+
+*/
+
+ PMC* get_attr_str(STRING* idx) {
+ return VTABLE_getprop(INTERP, SELF, idx);
+ }
+
+/*
+
=item C<void* invoke(void* next)>
Invoke a function. Defaults are filled in first.
View
81 dynclasses/pylist.pmc
@@ -27,6 +27,70 @@ static INTVAL dynclass_PyList;
static INTVAL dynclass_PyNone;
static INTVAL dynclass_PyString;
+struct parrot_regs_t *
+Parrot_PyClass_runops_fromc(Parrot_Interp interpreter, PMC *sub);
+
+static void quicksort(Interp* interpreter, List *list, PMC *cmpsub,
+ INTVAL left, INTVAL right)
+{
+ while (right > left) {
+ INTVAL cursor = left;
+ INTVAL index = (left + right) / 2;
+ PMC *pivot = *(PMC**)list_get(interpreter, list, index, enum_type_PMC);
+ PMC *temp = *(PMC**)list_get(interpreter, list, right, enum_type_PMC);
+ list_assign(interpreter, list, index, temp, enum_type_PMC);
+
+ index = right;
+ while (cursor != index) {
+ INTVAL cmp;
+ temp = *(PMC**)list_get(interpreter, list, cursor, enum_type_PMC);
+
+ if (!cmpsub)
+ cmp = mmd_dispatch_i_pp(interpreter, temp, pivot, MMD_CMP);
+ else {
+ struct parrot_regs_t *bp;
+ REG_INT(0) = 1;
+ REG_INT(1) = REG_INT(2) = 0;
+ REG_INT(3) = 2;
+ REG_PMC(5) = temp;
+ REG_PMC(6) = pivot;
+ bp = Parrot_PyClass_runops_fromc(interpreter, cmpsub);
+ cmp = VTABLE_get_integer(interpreter, BP_REG_PMC(bp,5));
+ }
+
+ if (cmp <= 0) {
+ if (cursor > index) {
+ INTVAL swap = index;
+ index = cursor;
+ cursor = swap;
+ list_assign(interpreter, list, cursor, temp, enum_type_PMC);
+ }
+ cursor++;
+ }
+ else {
+ if (cursor < index) {
+ INTVAL swap = index;
+ index = cursor;
+ cursor = swap;
+ list_assign(interpreter, list, cursor, temp, enum_type_PMC);
+ }
+ cursor--;
+ }
+ }
+
+ list_assign(interpreter, list, index, pivot, enum_type_PMC);
+
+ if (index*2 < left + right) {
+ quicksort(interpreter, list, cmpsub, left, index-1);
+ left = index+1;
+ }
+ else {
+ quicksort(interpreter, list, cmpsub, index+1, right);
+ right = index-1;
+ }
+ }
+}
+
pmclass PyList extends PyObject need_ext does array dynpmc group python_group {
/*
@@ -469,6 +533,23 @@ Sets the string value of the PMC at element C<key> to C<value>.
/*
+=item C<void* "sort"(PMC* cmp_func)>
+
+Sort this array, optionally using the provided cmp_func
+
+=cut
+
+*/
+
+METHOD void sort(PMC *self, PMC *cmp_func) {
+ if (REG_INT(3) < 2 || !VTABLE_get_bool(INTERP, cmp_func))
+ cmp_func = NULL;
+ INTVAL n = VTABLE_elements(INTERP, self);
+ quicksort(INTERP, (List *) PMC_data(self), cmp_func, 0, n-1);
+}
+
+/*
+
=back
=cut
View
14 dynclasses/pynone.pmc
@@ -43,6 +43,20 @@ Class initialization. Caches constant strings that will be used later.
/*
+=item C<INTVAL get_bool()>
+
+Returns the boolean value of the integer.
+
+=cut
+
+*/
+
+ INTVAL get_bool () {
+ return 0;
+ }
+
+/*
+
=item C<STRING* get_string ()>
Return "None"
Please sign in to comment.
Something went wrong with that request. Please try again.