sorting - Is there a partial sort command for tcl? -
as c++'s std::partial_sort
do. lsort not powerful enough.
there no built-in equivalent partial_sort
. think choices either implement hand in tcl, undermine whatever efficiency sought gain; or write extension exposes partial_sort
interpreter. wouldn't difficult -- tcl extensions pretty easy write. here's bit of code whipped should started:
#include <algorithm> #include "tcl.h" using namespace std; static int partialsortcommand(clientdata dummy, tcl_interp *interp, int objc, tcl_obj *const objv[]); extern int partialsort_init(tcl_interp *interp) { if (tcl_initstubs(interp, "8.0", 0) == null) { return tcl_error; } if (tcl_pkgprovide(interp, "partialsort", "1.0") != tcl_ok) { return tcl_error; } tcl_createobjcommand(interp, "partialsort", partialsortcommand, (clientdata) null, (tcl_cmddeleteproc *) null); return tcl_ok; } bool compareobjs(tcl_obj *a, tcl_obj *b) { int left, right; tcl_getintfromobj(0, a, &left); tcl_getintfromobj(0, b, &right); return left < right; } int partialsortcommand( clientdata dummy, tcl_interp *interp, int objc, tcl_obj *const objv[]) { if (objc != 5) { tcl_wrongnumargs(interp, 1, objv, "list start middle end"); return tcl_error; } tcl_obj **objs; int count; if (tcl_listobjgetelements(interp, objv[1], &count, &objs) != tcl_ok) { return tcl_error; } int start, middle, end; if (tcl_getintfromobj(interp, objv[2], &start) != tcl_ok) { return tcl_error; } if (tcl_getintfromobj(interp, objv[3], &middle) != tcl_ok) { return tcl_error; } if (tcl_getintfromobj(interp, objv[4], &end) != tcl_ok) { return tcl_error; } partial_sort(&objs[start], &objs[middle], &objs[end], compareobjs); tcl_setobjresult(interp, tcl_newlistobj(count, objs)); return tcl_ok; }
of course rough cut. handles lists of integers. doesn't in way of error checking. it's bit cavalier respect shared tcl_obj structures. going in right directory.
Comments
Post a Comment