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

// this simple demo program implements a dummy graph function and wraps
// it into the STk interpreter.

Set<Set<Vertex*> > evenOddRanks(const Graph *g)
{
	const SortedArray<Vertex*>& verts = g->vertices();
	int i, order = g->order();
	Set<Vertex*> even_rank_vertices, odd_rank_vertices;
	for (i=0; i<order; i+=2) {
		even_rank_vertices.insert(verts[i]);
		if (i+1 < order)
			odd_rank_vertices.insert(verts[i+1]);
	}
	Set<Set<Vertex*> > result;
	result.insert(even_rank_vertices);
	result.insert(odd_rank_vertices);
	return result;
}

PRIMITIVE evenOddRanksWrapper(SCM g)
{
	Graph *gr;
	if (!(gr = getGraph(g)))   // see src/stkWrapper/stkUtils.cc
		Err("even-odd-ranks: expected <graph*> ", g);

	Set<Set<Vertex*> > result = evenOddRanks(gr);
	Set<Set<Vertex*> >* new_copy = new Set<Set<Vertex*> >(result);

	return STk_make_CXXwrapper(
		Wrapper<Set<Set<Vertex*> > >::type,	// stkWrapper/Wrapper.h
		Wrapper<Set<Set<Vertex*> > >::name,
		(void *) new_copy,	
		LINK_DYNAMIC);		// tells STk's garbage collector
					// to delete new_copy when the user
					// is finished with it
}

//
// This is what must go into userinit.cc
//
void STk_evenOddRanks_Init()
{
        STk_add_new_cpp_primitive("even-odd-ranks", 	// cmd name
				  tc_subr_1,		// # parms
                                  (PRIMITIVE (*)(...))evenOddRanksWrapper);
}
