[2] | 1 | ;------------------------------------------------------------ |
---|
| 2 | ;------------------------------------------------------------ |
---|
| 3 | ;------------------------------------------------------------ |
---|
| 4 | ;+ |
---|
| 5 | ; NAME:different |
---|
| 6 | ; |
---|
| 7 | ; PURPOSE:calcule les elements differents de 2 matrices D'ENTIERS POSITIFS |
---|
| 8 | ; |
---|
| 9 | ; CATEGORY:calcule sur les matrices |
---|
| 10 | ; |
---|
| 11 | ; CALLING SEQUENCE:res=different(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 | ; different(a,b) = [ 4, 8] ; Elements in A but not in B |
---|
| 40 | ; |
---|
| 41 | ; MODIFICATION HISTORY: |
---|
| 42 | ; |
---|
| 43 | ; http://www.dfanning.com/tips/set_operations.html |
---|
| 44 | ;- |
---|
| 45 | ;------------------------------------------------------------ |
---|
| 46 | ;------------------------------------------------------------ |
---|
| 47 | ;------------------------------------------------------------ |
---|
| 48 | FUNCTION different, a, b |
---|
| 49 | |
---|
| 50 | ; = a and (not b) = elements in A but not in B |
---|
| 51 | |
---|
| 52 | mina = Min(a, Max=maxa) |
---|
| 53 | minb = Min(b, Max=maxb) |
---|
| 54 | IF (minb GT maxa) OR (maxb LT mina) THEN RETURN, a ;No intersection... |
---|
| 55 | r = Where(Histogram(a, Min=mina, Max=maxa) $ |
---|
| 56 | *(1-Histogram(b, Min=mina, Max=maxa)), count) |
---|
| 57 | IF count eq 0 THEN RETURN, -1 ELSE RETURN, r + mina |
---|
| 58 | END |
---|