2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
7 /* All Rights Reserved */
10 * Copyright (c) 1980 Regents of the University of California.
11 * All rights reserved. The Berkeley software License Agreement
12 * specifies the terms and conditions for redistribution.
15 #pragma ident "%Z%%M% %I% %E% SMI"
19 static int *coord
= 0;
21 extern int *hfreq
, hfrflg
;
30 extern void *zalloc();
32 static long getl(FILE *);
33 static int hcomp(int, int);
34 static void hexch(int, int);
37 doquery(long *hpt
, int nhash
, FILE *fb
, int nitem
,
38 char *qitem
[], unsigned *mptr
)
41 union ptr prevdrop
, master
;
42 int nf
= 0, best
= 0, nterm
= 0, i
, g
, j
;
45 extern int lmaster
, colevel
, reached
;
49 master
.b
= (long *)mptr
;
55 fprintf(stderr
, "entering doquery nitem %d\n", nitem
);
56 fprintf(stderr
, "first few hashes are %ld %ld %ld %ld %ld\n",
57 hpt
[0], hpt
[1], hpt
[2], hpt
[3], hpt
[4]);
58 fprintf(stderr
, "and frequencies are %d %d %d %d %d\n",
59 hfreq
[0], hfreq
[1], hfreq
[2], hfreq
[3], hfreq
[4]);
63 coord
= (int *)zalloc(lmaster
, sizeof (lmaster
));
66 prevdrop
.b
= (long *)zalloc(lmaster
, sizeof (long));
68 prevdrop
.a
= (unsigned *)zalloc(lmaster
, sizeof (int));
69 prevcoord
= (int *)zalloc(lmaster
, sizeof (lmaster
));
71 prevdrop
.a
= master
.a
;
75 fprintf(stderr
, "nitem %d\n", nitem
);
77 for (i
= 0; i
< nitem
; i
++) {
78 hh
[i
] = hash(qitem
[i
])%nhash
;
80 fprintf(stderr
, "query wd X%sX has hash %d\n", qitem
[i
], hh
[i
]);
84 fprintf(stderr
, "past that loop nhash %d hpt is %lo\n", nhash
, hpt
);
87 for (i
= 0; i
< nitem
; i
++)
88 fprintf(stderr
, "item %s hash %d hfreq %d\n",
89 qitem
[i
], hh
[i
], hfreq
[hh
[i
]]);
90 /* if possible, sort query into decreasing frequency of hashes */
92 shell(nitem
, hcomp
, hexch
);
94 for (i
= 0; i
< nitem
; i
++)
95 fprintf(stderr
, "item hash %d frq %d\n", hh
[i
], hfreq
[hh
[i
]]);
99 fprintf(stderr
, "first item hash %d lp %ld 0%lo\n", hh
[0], lp
, lp
);
102 assert(fseek(fb
, lp
, 0) != -1);
103 for (i
= 0; i
< lmaster
; i
++) {
105 master
.b
[i
] = getl(fb
);
107 master
.a
[i
] = getw(fb
);
111 fprintf(stderr
, "master has %ld\n", (master
.b
[i
]));
113 fprintf(stderr
, "master has %d\n", (master
.a
[i
]));
117 if (master
.b
[i
] == -1L) break;
119 if (master
.a
[i
] == -1) break;
123 for (nterm
= 1; nterm
< nitem
; nterm
++) {
125 fprintf(stderr
, "item %d, hash %d\n", nterm
, hh
[nterm
]);
128 for (j
= 0; j
< nf
; j
++) {
130 prevdrop
.b
[j
] = master
.b
[j
];
132 prevdrop
.a
[j
] = master
.a
[j
];
133 prevcoord
[j
] = coord
[j
];
137 assert(fseek(fb
, lp
, 0) != -1);
139 fprintf(stderr
, "item %d hash %d seek to %ld\n",
140 nterm
, hh
[nterm
], lp
);
150 fprintf(stderr
, "next term finds %ld\n", k
);
155 "bfwh j %d nf %d master %ld k %ld\n",
156 j
, nf
, prevdrop
.b
[j
], (long)(k
));
159 "bfwh j %d nf %d master %ld k %ld\n",
160 j
, nf
, prevdrop
.a
[j
], (long)(k
));
163 (iflong
? prevdrop
.b
[j
] : prevdrop
.a
[j
]) < k
) {
166 fprintf(stderr
, "j %d nf %d prevdrop "
167 "%ld prevcoord %d colevel %d nterm "
168 "%d k %ld\n", j
, nf
, prevdrop
.b
[j
],
169 prevcoord
[j
], colevel
, nterm
,
172 fprintf(stderr
, "j %d nf %d prevdrop "
173 "%ld prevcoord %d colevel %d nterm "
174 "%d k %ld\n", j
, nf
, prevdrop
.a
[j
],
175 prevcoord
[j
], colevel
, nterm
,
178 if (prevcoord
[j
] + colevel
<= nterm
)
183 master
.b
[g
] = prevdrop
.b
[j
];
185 master
.a
[g
] = prevdrop
.a
[j
];
186 coord
[g
++] = prevcoord
[j
++];
189 fprintf(stderr
, " not skip g "
190 "%d doc %d coord %d note "
191 "%d\n", g
, master
.b
[g
-1],
192 coord
[g
-1], master
.b
[j
-1]);
194 fprintf(stderr
, " not skip g "
195 "%d doc %ld coord %d nterm "
196 "%d\n", g
, master
.a
[g
-1],
202 if (colevel
== 0 && j
>= nf
) break;
204 (iflong
? prevdrop
.b
[j
] : prevdrop
.a
[j
]) == k
) {
209 coord
[g
++] = prevcoord
[j
++]+1;
212 fprintf(stderr
, " at g %d item %ld "
213 "coord %d note %ld\n", g
,
214 master
.b
[g
-1], coord
[g
-1],
217 fprintf(stderr
, " at g %d item %d "
218 "coord %d note %d\n", g
,
219 master
.a
[g
-1], coord
[g
-1],
223 if (colevel
>= nterm
) {
232 fprintf(stderr
, "now have %d items\n", g
);
236 if ((iflong
? prevdrop
.b
[j
] : prevdrop
.a
[j
]) +
240 master
.b
[g
] = prevdrop
.b
[j
];
242 master
.a
[g
] = prevdrop
.a
[j
];
243 coord
[g
++] = prevcoord
[j
];
246 fprintf(stderr
, "copied over "
248 master
.b
[g
-1], coord
[g
-1]);
250 fprintf(stderr
, "copied over "
252 master
.a
[g
-1], coord
[g
-1]);
259 for (j
= 0; j
< nf
; j
++)
260 if (coord
[j
] > best
) best
= coord
[j
];
262 fprintf(stderr
, "colevel %d best %d\n", colevel
, best
);
265 for (g
= j
= 0; j
< nf
; j
++)
266 if (coord
[j
] == best
) {
268 master
.b
[g
++] = master
.b
[j
];
270 master
.a
[g
++] = master
.a
[j
];
274 fprintf(stderr
, "yet got %d\n", nf
);
278 fprintf(stderr
, " returning with %d\n", nf
);
281 free(prevdrop
, lmaster
, iflong
? sizeof (long): sizeof (int));
282 free(prevcoord
, lmaster
, sizeof (lmaster
));
285 for (g
= 0; g
< nf
; g
++)
287 fprintf(stderr
, ":%ld\n", master
.b
[g
]);
289 fprintf(stderr
, ":%d\n", master
.a
[g
]);
301 hcomp(int n1
, int n2
)
303 return (hfreq
[hh
[n1
]] <= hfreq
[hh
[n2
]]);
307 hexch(int n1
, int n2
)