[2] | 1 | ;+ |
---|
| 2 | ; |
---|
[232] | 3 | ; @file_comments |
---|
[163] | 4 | ; calculate the intersection between 2 matrices of whole numbers |
---|
[2] | 5 | ; |
---|
[232] | 6 | ; @categories |
---|
[157] | 7 | ; Calculation |
---|
[2] | 8 | ; |
---|
[133] | 9 | ; @param a {in}{required} arrays of positive integers, which need not to be |
---|
[163] | 10 | ; sorted. Duplicate elements are ignored, as they have no effect on the |
---|
[31] | 11 | ; result |
---|
[2] | 12 | ; |
---|
[133] | 13 | ; @param b {in}{required} see a |
---|
[2] | 14 | ; |
---|
[133] | 15 | ; @returns tableau |
---|
[2] | 16 | ; |
---|
[133] | 17 | ; @restrictions The empty set is denoted by an array with the first element equal to |
---|
[2] | 18 | ; -1. |
---|
| 19 | ; |
---|
[133] | 20 | ; @restrictions These functions will not be efficient on sparse sets with wide |
---|
[2] | 21 | ; ranges, as they trade memory for efficiency. The HISTOGRAM function |
---|
| 22 | ; is used, which creates arrays of size equal to the range of the |
---|
| 23 | ; resulting set. |
---|
| 24 | ; |
---|
[232] | 25 | ; @examples |
---|
| 26 | ; IDL> a = [2,4,6,8] |
---|
| 27 | ; IDL> b = [6,1,3,2] |
---|
| 28 | ; IDL> inter(a,b) = [ 2, 6] ; Common elements |
---|
[2] | 29 | ; |
---|
[232] | 30 | ; @history |
---|
| 31 | ; <a href="http://www.dfanning.com/tips/set_operations.html"/> |
---|
[2] | 32 | ; |
---|
[232] | 33 | ; @version |
---|
| 34 | ; $Id$ |
---|
[133] | 35 | ; |
---|
[2] | 36 | ;- |
---|
[231] | 37 | ; |
---|
[2] | 38 | FUNCTION inter, a, b |
---|
| 39 | ; |
---|
[114] | 40 | compile_opt idl2, strictarrsubs |
---|
| 41 | ; |
---|
[2] | 42 | case 1 of |
---|
| 43 | n_elements(a) EQ 0:return, -1 |
---|
| 44 | n_elements(b) EQ 0:return, -1 |
---|
| 45 | n_elements(a) EQ 1 AND n_elements(b) NE 1: $ |
---|
| 46 | if (where(b EQ a[0]))[0] EQ -1 then return, -1 ELSE return, a[0] |
---|
| 47 | n_elements(b) EQ 1 AND n_elements(a) NE 1: $ |
---|
| 48 | if (where(a EQ b[0]))[0] EQ -1 then return, -1 ELSE return, b[0] |
---|
| 49 | n_elements(a) EQ 1 AND n_elements(b) EQ 1: $ |
---|
| 50 | if (where(a[0] EQ b[0]))[0] EQ -1 then return, -1 ELSE return, a[0] |
---|
| 51 | ELSE: |
---|
| 52 | ENDCASE |
---|
| 53 | ; |
---|
| 54 | minab = Min(a, Max=maxa) > Min(b, Max=maxb) ;Only need intersection of ranges |
---|
| 55 | maxab = maxa < maxb |
---|
| 56 | |
---|
| 57 | ; If either set is empty, or their ranges don't intersect: result = NULL. |
---|
| 58 | |
---|
| 59 | IF maxab LT minab OR maxab LT 0 THEN RETURN, -1 |
---|
| 60 | r = Where(Histogram(a, Min=minab, Max=maxab) $ |
---|
| 61 | *Histogram(b, Min=minab, Max=maxab), count) |
---|
| 62 | |
---|
| 63 | IF count EQ 0 THEN RETURN, -1 ELSE RETURN, r + minab |
---|
| 64 | END |
---|