1 /*
2   Teem: Tools to process and visualize scientific data and images             .
3   Copyright (C) 2008, 2007, 2006, 2005  Gordon Kindlmann
4   Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998  University of Utah
5 
6   This library is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Lesser General Public License
8   (LGPL) as published by the Free Software Foundation; either
9   version 2.1 of the License, or (at your option) any later version.
10   The terms of redistributing and/or modifying this software also
11   include exceptions to the LGPL that facilitate static linking.
12 
13   This library is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   Lesser General Public License for more details.
17 
18   You should have received a copy of the GNU Lesser General Public License
19   along with this library; if not, write to Free Software Foundation, Inc.,
20   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21 */
22 
23 
24 #include "../limn.h"
25 
26 char *info = ("test intersection of two lists.");
27 
28 static unsigned int
flipListIntx(unsigned int * dstC,const unsigned int * _srcA,const unsigned int * _srcB)29 flipListIntx(unsigned int *dstC,
30              const unsigned int *_srcA, const unsigned int *_srcB) {
31   const unsigned int *srcA, *srcB;
32   unsigned int numA, numB, numC, idxA, idxB;
33 
34   numA = _srcA[0];
35   srcA = _srcA + 1;
36   numB = _srcB[0];
37   srcB = _srcB + 1;
38   numC = 0;
39   for (idxA=0; idxA<numA; idxA++) {
40     for (idxB=0; idxB<numB; idxB++) {
41       if (srcA[idxA] == srcB[idxB]) {
42         dstC[numC++] = srcA[idxA];
43       }
44     }
45   }
46   return numC;
47 }
48 
49 int
main(int argc,const char * argv[])50 main(int argc, const char *argv[]) {
51   const char *me;
52   hestOpt *hopt=NULL;
53   airArray *mop;
54 
55   unsigned int *srcA, *srcB, *dstC, *_srcA, *_srcB, numA, numB, numC, idx;
56 
57   me = argv[0];
58   hestOptAdd(&hopt, "a", "vals", airTypeUInt, 1, -1, &srcA, NULL,
59              "list of values", &numA);
60   hestOptAdd(&hopt, "b", "vals", airTypeUInt, 1, -1, &srcB, NULL,
61              "list of values", &numB);
62   hestParseOrDie(hopt, argc-1, argv+1, NULL,
63                  me, info, AIR_TRUE, AIR_TRUE, AIR_TRUE);
64   mop = airMopNew();
65   airMopAdd(mop, hopt, (airMopper)hestOptFree, airMopAlways);
66   airMopAdd(mop, hopt, (airMopper)hestParseFree, airMopAlways);
67 
68   fprintf(stderr, "a =");
69   for (idx=0; idx<numA; idx++) {
70     fprintf(stderr, " %u", srcA[idx]);
71   }
72   fprintf(stderr, "\n");
73   fprintf(stderr, "b =");
74   for (idx=0; idx<numB; idx++) {
75     fprintf(stderr, " %u", srcB[idx]);
76   }
77   fprintf(stderr, "\n");
78 
79   _srcA = AIR_CAST(unsigned int*, calloc(1+numA, sizeof(unsigned int)));
80   airMopAdd(mop, _srcA, airFree, airMopAlways);
81   _srcB = AIR_CAST(unsigned int*, calloc(1+numB, sizeof(unsigned int)));
82   airMopAdd(mop, _srcB, airFree, airMopAlways);
83   dstC = AIR_CAST(unsigned int*, calloc(1+AIR_MAX(numA,numB),
84                                         sizeof(unsigned int)));
85   airMopAdd(mop, dstC, airFree, airMopAlways);
86 
87   _srcA[0] = numA;
88   for (idx=0; idx<numA; idx++) {
89     _srcA[1+idx] = srcA[idx];
90   }
91   _srcB[0] = numB;
92   for (idx=0; idx<numB; idx++) {
93     _srcB[1+idx] = srcB[idx];
94   }
95 
96   numC = flipListIntx(dstC, _srcA, _srcB);
97   fprintf(stderr, "intx(a,b) =");
98   for (idx=0; idx<numC; idx++) {
99     fprintf(stderr, " %u", dstC[idx]);
100   }
101   fprintf(stderr, "\n");
102 
103   airMopOkay(mop);
104   return 0;
105 }
106 
107