;finds the intersection of two sets:
;returns 0 if the intersection is null
;returns the subscripts of the common elements in sub1 and sub2
;/sorted means the arrays are already sorted (if comparing sets of time
;structures this must be set)
function intersection, set1, set2, intersection, sub1=sub1, sub2=sub2, $
sorted=sorted
size1=size(set1)
size2=size(set2)
if size2[size2[0]+1] ne size1[size1[0]+1] then return, 0
intersection=replicate(set1[0], max([size2[size2[0]+2], size1[size1[0]+2]]))
if not keyword_set(sorted) then begin
loc1=heapsort(set1)
loc2=heapsort(set2)
set1s=set1[loc1]
set2s=set2[loc2]
endif else begin
set1s=set1
set2s=set2
endelse
sub1=intarr(size1[size1[0]+2])
sub2=intarr(size2[size2[0]+2])
ind1=0
ind2=0
ind_int=0
repeat begin
; print, ind1, ind2, ind_int, time_string(set1s[ind1]), ' ', time_string(set2s[ind2])
compare=time_compare(set1s[ind1], set2s[ind2])
if compare eq 0 then begin
intersection[ind_int]=set1s[ind1]
sub1[ind1]=1
sub2[ind2]=1
ind_int=ind_int+1
ind1=ind1+1
ind2=ind2+1
endif else if compare gt 0 then begin
ind2=ind2+1
endif else begin
ind1=ind1+1
endelse
endrep until ind1 ge size1[size1[0]+2] or ind2 ge size2[size2[0]+2]
if ind_int eq 0 then return, 0
intersection=intersection[0:ind_int-1]
if keyword_set(sorted) then begin
sub1=where(sub1)
sub2=where(sub2)
endif else begin
sub1=loc1[where(sub1)]
sub2=loc2[where(sub2)]
endelse
return, 1
end