/*
 * FirstVariableOffsetFront.h
 *
 *  Created on: 5. 11. 2014
 *      Author: Jan Travnicek
 */

#ifndef _FIRST_VARIABLE_OFFSET_FRONT_H_
#define _FIRST_VARIABLE_OFFSET_FRONT_H_

#include <tree/ranked/PrefixRankedPattern.h>
#include <tree/ranked/PrefixRankedNonlinearPattern.h>

namespace tree {

namespace properties {

class FirstVariableOffsetFront {
public:
	template < class SymbolType, class RankType >
	static size_t offset ( const tree::PrefixRankedPattern < SymbolType, RankType > & pattern );
	template < class SymbolType, class RankType >
	static size_t offset ( const tree::PrefixRankedNonlinearPattern < SymbolType, RankType > & pattern );

};

template < class SymbolType, class RankType >
size_t FirstVariableOffsetFront::offset ( const tree::PrefixRankedPattern < SymbolType, RankType > & pattern ) {
	return offset ( tree::PrefixRankedNonlinearPattern < SymbolType, RankType > ( pattern ) );
}

template < class SymbolType, class RankType >
size_t FirstVariableOffsetFront::offset ( const tree::PrefixRankedNonlinearPattern < SymbolType, RankType > & pattern ) {
	 // find the distance between the beginning of the pattern and the index
	 // of the first symbol representing the variable's bar
	size_t res = pattern.getContent ( ).size ( ) + 1;

	for ( int i = ( int ) pattern.getContent ( ).size ( ) - 1; i >= 0; i-- )
		if ( pattern.getContent ( )[i] == pattern.getSubtreeWildcard ( ) || pattern.getNonlinearVariables ( ).count ( pattern.getContent ( )[i] ) )
			res = i;

	return res;
}

} /* namespace properties */

} /* namespace tree */

#endif /* _FIRST_VARIABLES_OFFSET_FRONT_H_ */