;+
;
; @file_comments
; calculate the intersection between 2 matrices of whole numbers
;
; @categories
; Calculation
;
; @param a {in}{required}
; arrays of positive integers, which need not to be
; sorted. Duplicate elements are ignored, as they have no effect on the
; result
;
; @param b {in}{required}
; see a
;
; @returns
; tableau
;
; @restrictions
; The empty set is denoted by an array with the first element equal to
; -1.
;
; @restrictions
; These functions will not be efficient on sparse sets with wide
; ranges, as they trade memory for efficiency.
; The HISTOGRAM function
; is used, which creates arrays of size equal to the range of the
; resulting set.
;
; @examples
; IDL> a = [2,4,6,8]
; IDL> b = [6,1,3,2]
; IDL> inter(a,b) = [ 2, 6] ; Common elements
;
; @history
;
;
; @version
; $Id$
;
;-
FUNCTION inter, a, b
;
compile_opt idl2, strictarrsubs
;
case 1 of
n_elements(a) EQ 0:return, -1
n_elements(b) EQ 0:return, -1
n_elements(a) EQ 1 AND n_elements(b) NE 1: $
if (where(b EQ a[0]))[0] EQ -1 then return, -1 ELSE return, a[0]
n_elements(b) EQ 1 AND n_elements(a) NE 1: $
if (where(a EQ b[0]))[0] EQ -1 then return, -1 ELSE return, b[0]
n_elements(a) EQ 1 AND n_elements(b) EQ 1: $
if (where(a[0] EQ b[0]))[0] EQ -1 then return, -1 ELSE return, a[0]
ELSE:
ENDCASE
;
minab = Min(a, Max=maxa) > Min(b, Max=maxb) ;Only need intersection of ranges
maxab = maxa < maxb
; If either set is empty, or their ranges don't intersect: result = NULL.
IF maxab LT minab OR maxab LT 0 THEN RETURN, -1
r = Where(Histogram(a, Min=minab, Max=maxab) $
*Histogram(b, Min=minab, Max=maxab), count)
IF count EQ 0 THEN RETURN, -1 ELSE RETURN, r + minab
END