// Skip to the next window and return true, if there is one more:
bool PositionWindow::next()
{
	// Calculate how many positions we can skip with the 
	// first element without loosing any window of a size
	// within the defined proximity range:
	PostingIteratorInterface* itr = m_set.begin()->itr;
	std::set<Element>::iterator top = getWinTopElement();
#ifdef STRUS_LOWLEVEL_DEBUG
	printf( "top [%s %u] begin [%s %u]\n",
			top->itr->featureid(), top->pos,
			m_set.begin()->itr->featureid(), m_set.begin()->pos);
#endif
	Index posdiff = top->pos - m_set.begin()->pos;
	Index skipsize = posdiff > (Index)m_range ? (posdiff - (Index)m_range) : 1;
	// Change the position of the first element in the 
	// sliding window by the calculated size:
	Index pos = m_set.begin()->pos;

#ifdef STRUS_LOWLEVEL_DEBUG
	printf( "delete %s %u\n", itr->featureid(), pos);
#endif
	m_set.erase( m_set.begin());

	Index newpos = itr->skipPos( pos + skipsize);
	if (newpos)
	{
#ifdef STRUS_LOWLEVEL_DEBUG
		printf( "insert %s %u\n", itr->featureid(), newpos);
#endif
		m_set.insert( Element( itr, newpos));
	}
	else
	{
		--m_setsize;
	}
	// Return, if there is valid window left:
	return m_setsize >= m_cardinality;
}
// Constructor that fills the sliding window implemented as set 
// with the argument element start positions:
PositionWindow::PositionWindow(
		const std::vector<PostingIteratorInterface*>& args,
		unsigned int range_,
		unsigned int cardinality_,
		Index firstpos_)
	:m_args(args)
	,m_setsize(0)
	,m_range(range_)
	,m_cardinality(cardinality_>0?cardinality_:args.size())
{
	std::vector<PostingIteratorInterface*>::iterator
		ai = m_args.begin(), ae = m_args.end();
	for (; ai != ae; ++ai)
	{
		PostingIteratorInterface* itr = *ai;
		Index pos = itr->skipPos( firstpos_);
		if (pos)
		{
			m_set.insert( Element( itr, pos));
			++m_setsize;
		}
	}
}
// Skip to the next window and return true, if there is one more:
bool PositionWindow::next()
{
	// Calculate how many positions we can skip with the 
	// first element without loosing any window of a size
	// within the defined proximity range:
	PostingIteratorInterface* itr = m_set.begin()->itr;
	std::set<Element>::iterator top = getWinTopElement();
	Index posdiff = top->pos - m_set.begin()->pos;
	Index skipsize = posdiff > m_range ? (posdiff - m_range) : 1;
	// Change the position of the first element in the 
	// sliding window by the calculated size:
	Index newpos = itr->skipPos( m_set.begin()->pos + skipsize);
	if (newpos)
	{
		m_set.insert( Element( itr, newpos));
	}
	else
	{
		--m_setsize;
	}
	m_set.erase( m_set.begin());
	// Return, if there is valid window left:
	return m_setsize >= m_cardinality;
}