8 { printf("Usage: neighbours [-%%][-d] file\n");
16 int count,limit,delta;
19 int compare(unsigned short int *r1, unsigned short int *r2)
21 for (i=0;i<2;i++,r1++,r2++)
22 { if (*r1>*r2) return 1;
24 if (*r1<*r2) return -1;
29 int search(unsigned short *key,int *index)
30 {int l=0,h=count-1,i,c;
33 c=compare(table+2*i,key);
37 if (!c) { *index=i;return 1;}
43 void insert(unsigned short *key,int index)
47 table=realloc(table,(limit+=delta)*2*sizeof(short int));
49 { fprintf(stderr,"Couldn't realloc table. Table limit is %d\n",limit);
54 for(i=count;i>index;i--)
55 {table[2*i]=table[2*(i-1)];
56 table[2*i+1]=table[2*i-1];}
57 table[2*index]=key[0];
58 table[2*index+1]=key[1];
61 void delete(int index)
63 for(i=index+1;i<count;i++)
64 { table[2*(i-1)]=table[2*i];
65 table[2*i-1]=table[2*i+1];
70 void add_pair(int v1,int v2)
71 {int index;unsigned short int key[2];
72 if (v1==v2||v1==file->offsite||v2==file->offsite) return;
73 if (v1>v2) {key[0]=v2;key[1]=v1;}
74 else {key[0]=v1;key[1]=v2;}
75 if (!search(key,&index))
80 {int i,j;unsigned short int pair[2];
81 unsigned short *t=table;
84 { printf("%d\t%d\n",*t,*(t+1));
85 if (duplicates&&*t<*(t+1))
97 int main(int argc,char **argv)
98 {int i=1,j,k,rows,c,verbose=0;
99 struct option long_options[]={
104 while ((c=getopt_long(argc,argv,"%d",long_options,&j))!=-1)
106 { case '%':verbose=1;break;
107 case 'd':duplicates=1;break;
114 { fprintf(stderr,"No input file\n");
117 file=open_epp(argv[optind]);
118 if (!file) file=open_epp(default_ext(argv[optind],".epp"));
119 if (!file) {fprintf(stderr,"Cannot open file %s\n",argv[optind]);
122 set_epp_cache(file,2);
123 table=malloc(65536*2*sizeof(short int));
124 if (!table) { fprintf(stderr,"Not enough memory to allocate %d records\n",65536);
130 rows=file->lr-file->fr;
131 install_progress_indicator(verbose?show_percent:check_int);
132 for(i=file->fr,k=1;i<file->lr;i++,k++)
134 if ((c=(*EndLineProc)(i,k,rows)))
136 for(j=file->fc;j<file->lc;j++)
137 { add_pair((c=epp_get(file,j,i)),epp_get(file,j+1,i));
138 add_pair(c,epp_get(file,j,i+1));
139 add_pair(c,epp_get(file,j+1,i+1));
142 if(clear_progress(c)) exit(abs(c));