# TODO: Add comment
# 
# Author: E.Korsching  05-2026
###############################################################################



# create a reference list of vector x1 in x2

vec.pos.comparison <- function(x1, x2, ret=1){
	# compare two vectors and
	#  return matrix or vector of reference positions of : x1 in x2
	#  missing relations denoted by 0
	# x1,x2: vectors
	# ret: 1: return only references in x2 or matrix with positions in x1 and references in x2
	int0_test <- function(x){ if(identical(x, integer(0))) { return(0) }else{ return(x) } }
	
	xl1 <- length(x1)
	xl2 <- length(x2)
	if(xl1<1) stop("x1 <1")
	if(xl2<1) stop("x2 <1")
	o <- matrix(0,xl1,2)
	for(i in 1:xl1){
		o[i,1] <- i
		o[i,2] <- int0_test( which(x2 %in% x1[i], arr.ind=T) )
	}
	cat("\n length x1:",xl1," length x2:",xl2,"\n")
	if(ret==1){ return(o[,2]) }else{ return(o) }
}


#vec.pos.comparison(x1=letters[c(11,1:3,15)], x2=letters[10:1])
#vec.pos.comparison(x1=letters[c(11,1:3,15)], x2=letters[10:1], ret=2)
#vec.pos.comparison(x1=c(11,1:3,15), x2=c(10:1))
#vec.pos.comparison(x1=c(11,1:3,15), x2=c(10:1), ret=2)



vec.pos.differences <- function(x1, x2){
	# find difference in highly similar rank vectors
	# x1,x2: rank vectors might have int values including 0
	xl1 <- length(x1)
	xl2 <- length(x2)
	if(xl1!=xl2) stop("x1 != x2")
	idx <- x1==x2
	o <- cbind(x1,x2)
	cat("\n #",sum(idx)," / ",xl1,"\n")
	return(o[idx,,drop=F])
}


#vec.pos.differences(x1=c("a","b","c","d"), x2=c("a","c","d","b"))



