Code:
/* testing a fast way to find all partial (2 of the 3 numbers) matches,
within a large number of tuples.
Because of the great locality of the data, no sorting/searching/indexing,
etc., were found useful.
Adak, May 26, 2010
status: untested. 3042 matches found on Macha's 4.9sec.txt data file,
renamed as "tuples.txt".
my time: 0.05 seconds, on an Intel E6700 cpu, running at 2.66Ghz.
*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define R 124000
#define C 3
int getTuples(FILE *fp, int a[R][C]);
int main(void) {
int i, j, r, c, maxrows, a[R][C] = {{0}};
int found, match;
clock_t start, stop;
FILE *fp;
printf("\n\n\n");
//start=clock(); Overall program time
if((fp=fopen("tuples.txt", "rt")) == NULL) {
printf("\nError opening input file - exiting\n");
return 1;
}
maxrows=getTuples(fp, a);
if((fp=fopen("matches.txt", "wt")) == NULL) {
printf("\nError opening output file - exiting\n");
return 1;
}
//loaded up, ready to rock:
start=clock();
for(i=0,found=0;i<maxrows;i++) {
for(j=i+1;j<maxrows;j++) {
match=0;
if(a[i][0]==a[j][0] || a[i][0]==a[j][1] || a[i][0]==a[j][2]) {match++;}
if(a[i][1]==a[j][0] || a[i][1]==a[j][1] || a[i][1]==a[j][2]) {match++;}
if(a[i][2]==a[j][0] || a[i][2]==a[j][1] || a[i][2]==a[j][2]) {match++;}
if(match>1) {
found++;
/*
writes result to file
i,j are the triplet indices
*/
fprintf(fp,"%d %d\n", i,j);
/*
printf("%d %d\n", i,j);
printf("(%d %d %d)", a[i][0],a[i][1],a[i][2]);
printf("(%d %d %d)\n\n", a[j][0],a[j][1],a[j][2]);
*/
}
}
}
printf("\n\n Partial matches found: %d", found);
stop=clock();
printf("\n\n elapsed time: %f", (stop-start)/(double)CLOCKS_PER_SEC);
printf("\n\n\t\t\t press enter when ready");
i=getchar();
fclose(fp);
return 0;
}
int getTuples(FILE *fp, int a[R][C]) {
int i, r, c;
char z; //z="zee trash" char ;)
c = 0;
r=0;
int **array1 = malloc(r * sizeof(int *));
do{
i= fscanf(fp, "%d %d %d ", &a[r][c],&a[r][c+1],&a[r][c+2]);
if(i > 2){
printf("\n%4d: %4d %4d %4d\n", r, a[r][c],a[r][c+1],a[r][c+2]);
array1[i] = malloc(C * sizeof(int));
array1[i][0] = a[r][0];
array1[i][1] = a[r][1];
array1[i][2] = a[r][2];
printf("\narray1...%d %d %d\n\n",array1[i][0],array1[i][1],array1[i][2]);
++r;
}
}while((i > 2) && (r < R));
printf("\n\n There are %d Rows of tuples\n", r);
fclose(fp);
return r;
}