##########################################################################
#
# Module: DataList						   
#                                     					   
###########################################################################

DataList :=module()
   option package;
   description "generation and operation for data storage";
   global  
	_tower,
	_datalist;
  export
	SetTower,
	GetTower,
	GetData,
	Pairsearch,
	UpdateList;

_tower := x,[],[];
_datalist:=[];

# Complement Representation
# _datalist= [S_1,..,S_n]
# S_i is a list recording the normalized elements and the corresponding representation of the complement spaces at the i level;
# Elements in S_i have form [z, W], where z is a normalized element in the i level;


##################################################
# Name: SetTower
# Calling Sequence：SetTower(x, T, dT)
# Input:  x, the first variable of a tower;
#             T, a list of generators;
#             dT, a list of the derivatives of the generators of the tower
# Output: NULL, but set _tower to be the input
##################################################
SetTower:=proc(x,T,dT)
	local n,i;

	n:=nops(T);
	_datalist:=[seq([],i=1..n)];
	_tower:=x,T,dT;
	return;

end proc:



##################################################
# Name: GetTower
# Calling Sequence：GetTower()
# Input: NULL
# Output: _tower
##################################################
GetTower:=proc()

	return _tower;

end proc:


##################################################
# Name: GetData
# Calling Sequence：GetData()
# Input: NULL
# Output: _datalist
##################################################
GetData:=proc()

	return _datalist;

end proc:





###################################################
# Name: Pairsearch
# Calling Sequence：Pairsearch(x, TT, dTT, ff)
# Input:  x,  a variable of the base field C(x);
#             TT, a nonempty list of Liouvillian monomial;
#             dTT, a list of derivatives of TT;
#              ff, a normalized element in C(x,TT); 
# Output: a complement representation W corresponding to f, or false if there doesn't exist one
#######################################################
Pairsearch := proc(x, T, dT, f) 
	local  i, ff, level, flag; 

	level := nops(T); 
	ff := normal(f); 

	flag := 0; 
	for i in _tower[level] do 
		if normal( i[1] - ff )=0 then 
			flag := 1; 
			break; 
		end if; 
	end do; 

	if flag = 0 then 
		return false; 
	else 
		return i[2]; 
	end if; 

end proc:

#################################################
# Name: UpdateList
# Calling sequence: UpdateList(x,T,dT,f,W)
# Input: x,   an indeterminate
#            T,   a nonempty list of indeterminates; 
#            dT, derivatives of T;
#            f,  a normalized element in C(x, T);
#            W, the f-complement in T[-1]'s level;
# Output: NULL, but add [f,W] into _tower at proper positation.
##################################################
UpdateList:= proc(x, T, dT, f, W)
	local level, temp ;

	level := nops(T); 
	temp:=_tower[level];
	temp:=[ [f,W], op(temp)];
	_datalist:=subsop( level = temp, _datalist);
	return ;

end proc:



end module:
