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