[2] | 1 | ;------------------------------------------------------------ |
---|
| 2 | ;------------------------------------------------------------ |
---|
| 3 | ;------------------------------------------------------------ |
---|
| 4 | ;+ |
---|
| 5 | ; NAME:inter |
---|
| 6 | ; |
---|
| 7 | ; PURPOSE:calcule l''intersection de 2 matrices D'ENTIERS POSITIFS |
---|
| 8 | ; |
---|
| 9 | ; CATEGORY:calcule sur les matrices |
---|
| 10 | ; |
---|
| 11 | ; CALLING SEQUENCE:res=inter(a,b) |
---|
| 12 | ; |
---|
| 13 | ; INPUTS:a et b:arrays of positive integers, which need |
---|
| 14 | ; not be sorted. Duplicate elements are ignored, as they have no |
---|
| 15 | ; effect on the result |
---|
| 16 | ; |
---|
| 17 | ; KEYWORD PARAMETERS: |
---|
| 18 | ; |
---|
| 19 | ; OUTPUTS:tableau |
---|
| 20 | ; |
---|
| 21 | ; COMMON BLOCKS: |
---|
| 22 | ; |
---|
| 23 | ; SIDE EFFECTS: |
---|
| 24 | ; |
---|
| 25 | ; The empty set is denoted by an array with the first element equal to |
---|
| 26 | ; -1. |
---|
| 27 | ; |
---|
| 28 | ; RESTRICTIONS: |
---|
| 29 | ; |
---|
| 30 | ; These functions will not be efficient on sparse sets with wide |
---|
| 31 | ; ranges, as they trade memory for efficiency. The HISTOGRAM function |
---|
| 32 | ; is used, which creates arrays of size equal to the range of the |
---|
| 33 | ; resulting set. |
---|
| 34 | ; |
---|
| 35 | ; EXAMPLE: |
---|
| 36 | ; |
---|
| 37 | ; a = [2,4,6,8] |
---|
| 38 | ; b = [6,1,3,2] |
---|
| 39 | ; inter(a,b) = [ 2, 6] ; Common elements |
---|
| 40 | ; |
---|
| 41 | ; MODIFICATION HISTORY: |
---|
| 42 | ; |
---|
| 43 | ; http://www.dfanning.com/tips/set_operations.html |
---|
| 44 | ;- |
---|
| 45 | ;------------------------------------------------------------ |
---|
| 46 | ;------------------------------------------------------------ |
---|
| 47 | ;------------------------------------------------------------ |
---|
| 48 | |
---|
| 49 | FUNCTION inter, a, b |
---|
| 50 | ; |
---|
| 51 | case 1 of |
---|
| 52 | n_elements(a) EQ 0:return, -1 |
---|
| 53 | n_elements(b) EQ 0:return, -1 |
---|
| 54 | n_elements(a) EQ 1 AND n_elements(b) NE 1: $ |
---|
| 55 | if (where(b EQ a[0]))[0] EQ -1 then return, -1 ELSE return, a[0] |
---|
| 56 | n_elements(b) EQ 1 AND n_elements(a) NE 1: $ |
---|
| 57 | if (where(a EQ b[0]))[0] EQ -1 then return, -1 ELSE return, b[0] |
---|
| 58 | n_elements(a) EQ 1 AND n_elements(b) EQ 1: $ |
---|
| 59 | if (where(a[0] EQ b[0]))[0] EQ -1 then return, -1 ELSE return, a[0] |
---|
| 60 | ELSE: |
---|
| 61 | ENDCASE |
---|
| 62 | ; |
---|
| 63 | minab = Min(a, Max=maxa) > Min(b, Max=maxb) ;Only need intersection of ranges |
---|
| 64 | maxab = maxa < maxb |
---|
| 65 | |
---|
| 66 | ; If either set is empty, or their ranges don't intersect: result = NULL. |
---|
| 67 | |
---|
| 68 | IF maxab LT minab OR maxab LT 0 THEN RETURN, -1 |
---|
| 69 | r = Where(Histogram(a, Min=minab, Max=maxab) $ |
---|
| 70 | *Histogram(b, Min=minab, Max=maxab), count) |
---|
| 71 | |
---|
| 72 | IF count EQ 0 THEN RETURN, -1 ELSE RETURN, r + minab |
---|
| 73 | END |
---|