source: trunk/SRC/ToBeReviewed/MATRICE/inter.pro @ 114

Last change on this file since 114 was 114, checked in by smasson, 18 years ago

new compilation options (compile_opt idl2, strictarrsubs) in each routine

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 2.1 KB
Line 
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 not to be
14; sorted. Duplicate elements are ignored, as they have noeffect on the
15; 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
49FUNCTION inter, a, b
50;
51;
52  compile_opt idl2, strictarrsubs
53;
54   case 1 of
55      n_elements(a) EQ 0:return,  -1
56      n_elements(b) EQ 0:return,  -1
57      n_elements(a) EQ 1 AND n_elements(b) NE 1: $
58       if (where(b EQ a[0]))[0] EQ -1 then return, -1 ELSE return,  a[0]
59      n_elements(b) EQ 1 AND n_elements(a) NE 1: $
60       if (where(a EQ b[0]))[0] EQ -1 then return, -1 ELSE return,  b[0]
61      n_elements(a) EQ 1 AND n_elements(b) EQ 1: $
62       if (where(a[0] EQ b[0]))[0] EQ -1 then return, -1 ELSE return,  a[0]
63      ELSE:
64   ENDCASE
65;
66minab = Min(a, Max=maxa) > Min(b, Max=maxb) ;Only need intersection of ranges
67maxab = maxa < maxb
68
69   ; If either set is empty, or their ranges don't intersect: result = NULL.
70
71IF maxab LT minab OR maxab LT 0 THEN RETURN, -1
72r = Where(Histogram(a, Min=minab, Max=maxab) $
73          *Histogram(b, Min=minab, Max=maxab), count)
74
75IF count EQ 0 THEN RETURN, -1 ELSE RETURN, r + minab
76END
Note: See TracBrowser for help on using the repository browser.