# TODO: Add comment
# 
# Author: E.Korsching Jan 11, 2019
###############################################################################


column.align.insert <- function(x,y, ins=NULL){
	# align a vectors by inserting char(0) or NA or else  to a second reference vector
	#  to get the same layout like the reference vector by introducing gaps
	# assumption : shorter vector is full subset of longer vector
	# method : make unique , order ascending , fill gaps
	# input: two data.frames (or one column data.frames)
	#  the first columns need to contain the comparison criteria
	# related function:  join.dataframe()
	
	if(!is.data.frame(x)|!is.data.frame(y)){ stop("Check input data types - stop\n") }
	xlen <- nrow(x); xcol <- ncol(x)
	ylen <- nrow(y); ycol <- ncol(y)
	
	if(xlen==ylen){ stop("x and y have the same length - stop\n") }		# something might be wrong
	
	if(xlen<ylen){ ref <- 2 }else{ ref <- 1 }		# detect longer vector
	
	f1 <- function(z){
		if(z){
			if(!is.character(ins)){ stop("ins needs to be character - stop") }
		}else{
			if(is.character(ins)){ stop("ins needs to be numeric - stop") }
		}
	}
	
	if(is.null(ins)){	# create ins value
		if(ref==1){
			if(is.character(y[,1])){
				ins <- ""
			}else{
				ins <- NA
			}
		}else{
			if(is.character(x[,1])){
				ins <- ""
			}else{
				ins <- NA
			}
		}
	}else{	# check given ins value
		if(ref==1){
			f1(is.character(y[,1]),ins)
		}else{
			f1(is.character(x[,1]),ins)
		}
	}
	
	# start insertion
	x <- x[!duplicated(x[,1]),,drop=F]
	y <- y[!duplicated(y[,1]),,drop=F]
	x <- x[order(x[,1]),,drop=F]		# ascending
	y <- y[order(y[,1]),,drop=F]
#	cat("\n 0 x ",x, " xl ", xlen)
#	cat("\n 0 y ",y, " yl ", ylen)
	
	if(ref==1){		# reference is x
		z <- data.frame(matrix(ins, xlen, ycol), stringsAsFactors=F)
		# find y start index in x reference index
		pos.found <- grep(pattern=paste("^",y[1,1],"$",sep=""), x=x[,1], ignore.case=F, perl=F, value=F, fixed=F, invert=F)
		if(pos.found>1){
			for(m in 1:pos.found-1){		# if y not at the start, fill to the start
				z[m,] <- ins
			}
		}
		
		i <- 1	# y counter
		k <- pos.found	# x (ref) counter
		j <- T
		l <- T
		while(j){	# y loop
			if(y[i,1]!=x[k,1]){
				while(l){	# x loop
					if(y[i,1]==x[k,1]){
						z[k,] <- y[i,]
						l <- F
					}else{
						z[k,] <- ins
					}
					k <- k+1
				}
				i <- i+1
				l <- T
			}else{
				z[k,] <- y[i,]
				i <- i+1
				k <- k+1
			}
			if(i>ylen|k>xlen){ j <- F }
		}
		if(k<=xlen){
			for(m in k:xlen){		# if y not at the end, fill to the end
				z[m,] <- ins
			}
		}
	}else{		# reference is y
		z <- data.frame(matrix(ins, ylen, xcol), stringsAsFactors=F)
		# find x start index in y reference index
		pos.found <- grep(pattern=paste("^",x[1,1],"$",sep=""), x=y[,1], ignore.case=F, perl=F, value=F, fixed=F, invert=F)
		if(pos.found>1){
			for(m in 1:pos.found-1){		# if x not at the start, fill to the start
				z[m,] <- ins
			}
		}
		
		i <- 1	# x counter
		k <- pos.found	# y (ref) counter
		j <- T
		l <- T
		while(j){	# x loop
#			cat("\n 1  i ",i," k ",k)
			if(x[i,1]!=y[k,1]){
				while(l){	# y loop
#					cat("\n 2  i ",i," k ",k)
					if(x[i,1]==y[k,1]){
						z[k,] <- x[i,]
						l <- F
					}else{
						z[k,] <- ins
					}
					k <- k+1
				}
				i <- i+1
				l <- T
			}else{
#				cat("\n 3  else")
				z[k,] <- x[i,]
				i <- i+1
				k <- k+1
			}
			if(i>xlen|k>ylen){ j <- F }
		}
		if(k<=ylen){
			for(m in k:ylen){		# if x not at the end, fill to the end
#				cat("\n 4")
				z[m,] <- ins
			}
		}
	}
#	cat("\n")
	return(z)
}


# column.align.insert(x=data.frame(c(1,2,5,6,3,4,8,7)), y=data.frame(c(1,3,4,5)), ins=NULL)
# column.align.insert(x=data.frame(c(1,2,5,6,3,4,8,7)), y=data.frame(c(5,6,3,4,8)), ins=NULL)
# column.align.insert(x=data.frame(c(1,2,5,6,3,4,8,7)), y=data.frame(c(1,2,5,6,3,8,7)), ins=NULL)
# column.align.insert(x=data.frame(c(1,2,5,6,3,4,8,7)), y=data.frame(c(1,2,5,6,3,4,8,7)), ins=NULL)

# column.align.insert(x=data.frame(c(1,3,4,5)), y=data.frame(c(1,2,5,6,3,4,8,7)), ins=NULL)
# column.align.insert(x=data.frame(c(5,6,3,4,8)), y=data.frame(c(1,2,5,6,3,4,8,7)), ins=NULL)
# column.align.insert(x=data.frame(c(1,2,5,6,3,8,7)), y=data.frame(c(1,2,5,6,3,4,8,7)), ins=NULL)
# column.align.insert(x=data.frame(c(1,2,5,6,3,4,8,7)), y=data.frame(c(1,2,5,6,3,4,8,7)), ins=NULL)







