#include<stk.h>
#include<LINK/stkWrapper/LINK_STk.h>
#include<LINK/stkWrapper/Wrapper.h>
#include<LINK/algorithm/Algorithms.h>
#include<LINK/basic/Set.h>
#include<LINK/graph/Graph.h>
#include<LINK/graph/Edge.h>

#include <stdio.h>
#include <stdlib.h>

static PRIMITIVE IntToHexString(SCM i)
{
	if (!INTEGERP(i))
		err("int-to-hex-string: integer expected", i);
	int v = INTEGER(i);
        char str[80];
        sprintf(str, "%x", v);
	return STk_makestring(str);
}

static PRIMITIVE KruskalCmd(SCM g)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("kruskal: bad arg 1, graph expected", g);
        Set<Edge*> s = Kruskal(gr,0);
        Set<Edge*>* sp = new Set<Edge*>(s);

        return STk_make_CXXwrapper(Wrapper<Set<Edge*> >::type,
                Wrapper<Set<Edge*> >::name, (void *) sp, LINK_DYNAMIC);
}

static PRIMITIVE KruskalAnimCmd(SCM g, SCM view=0)
{
        Graph *gr;
        if (!(gr = getGraph(g)))
                Err("kruskal: bad arg 1, graph expected", g);
	char *name = getViewName(view);
	if (name) {
		int k = initAnimation();
		animationWindows(k, name);
	}
        Set<Edge*> s = Kruskal(gr,name);
        Set<Edge*>* sp = new Set<Edge*>(s);

        return STk_make_CXXwrapper(Wrapper<Set<Edge*> >::type,
                Wrapper<Set<Edge*> >::name, (void *) sp, LINK_DYNAMIC);
}

void gen_Kruskal_Wrapper()
{
//        STk_add_new_cpp_primitive("kruskal", tc_subr_1,
//                               (PRIMITIVE (*)(...))KruskalCmd);
        STk_add_new_cpp_primitive("kruskal", tc_subr_1_or_2,
                               (PRIMITIVE (*)(...))KruskalAnimCmd);
        STk_add_new_cpp_primitive("int-to-hex-string", tc_subr_1,
                                    (PRIMITIVE (*)(...))IntToHexString);
}
