// frontier.c
//	JR
//	3/29/96	Written, and tested on homeo4.dat.
//		Tested on homeo6.dat, 
//              an example with a non-monogon circle.



CircList<DE> Homeo::generateLinkG0i(const Set<Name>& G0i)
{
//cout << "G0i = " << G0i << endl;
	// Find a peripheral edge, "a", in G0i
	Boolean foundOne=0;
	DE a;
	SetBrowser<Name> j(G0i);
	for(j.init();j.ok();j.advance())
	{	
		a=DE(j.current(),0);
		if( theNameManager.periphery().has(a.name()) )
		{ 	
			foundOne=1;
			break;
		}
	}
	assert(foundOne);
	
	// Find out which end of "a" is the clockwise end.
	Vertex& v(theGraph.originOf(a));
	v.rotateToJustAfterThe(a);
	if( theNameManager.periphery().has(v.ahead().name()) ) a=a.bar();
	// Now origin of "a" is the clockwise end.
//cout << "a is " << a << endl;
	CircList<DE> tmp;
	theGraph.findFrontier( a, 0, a, G0i, tmp );
	return tmp;
}

void Graph::findFrontier( DE a, int depth, 
			  DE e, const Set<Name>& G0i, CircList<DE>& Ai)
{
		// Stop if we're all the way round to "a" again.
	depth++;
	if(depth>1 && e==a) return;
//cout << "e=" << e << endl;
	Vertex& v(originOf(e));
	v.rotateToJustBeforeThe(e);
	CircListBrowser<DE> i(v);
	i.init();
	++i;	// skip past e
	for(	;i.ok();++i)
	{
		if( G0i.has(i.ahead().name()) )
		{
//cout << "Edge is " << i.ahead() << ", depth " << depth << " and going deeper" << endl;
			findFrontier( a, depth,
				      i.ahead().bar(), G0i, Ai);
		}
		else
		{
//cout << "Adding " << i.ahead() << " to Ai" << endl;
			Ai.pushb(i.ahead());
		}
	}
}
