1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
5
6 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
7 /* All Rights Reserved */
8
9 /*
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.
13 */
14
15 #include <stdio.h>
16 #include <assert.h>
17
18 static void putl(long, FILE *);
19
20 void
whash(FILE * ft,FILE * fa,FILE * fb,int nhash,int iflong,long * ptotct,int * phused)21 whash(FILE *ft, FILE *fa, FILE *fb, int nhash, int iflong,
22 long *ptotct, int *phused)
23 {
24 char line[100];
25 int hash = 0, hused = 0;
26 long totct = 0L;
27 int ct = 0;
28 long point;
29 long opoint = -1;
30 int m;
31 int k;
32 long lp;
33 long *hpt;
34 int *hfreq = NULL;
35
36 hpt = (long *)calloc(nhash+1, sizeof (*hpt));
37 assert(hpt != NULL);
38 hfreq = (int *)calloc(nhash, sizeof (*hfreq));
39 assert(hfreq != NULL);
40 hpt[0] = 0;
41 lp = 0;
42 while (fgets(line, 100, ft)) {
43 totct++;
44 sscanf(line, "%d %ld", &k, &point);
45 if (hash < k) {
46 hused++;
47 if (iflong) putl(-1L, fb);
48 else putw(-1, fb);
49 hfreq[hash] = ct;
50 while (hash < k) {
51 hpt[++hash] = lp;
52 hfreq[hash] = 0;
53 }
54 hpt[hash] = lp += iflong ? sizeof (long) : sizeof (int);
55 opoint = -1;
56 ct = 0;
57 }
58 if (point != opoint) {
59 if (iflong)
60 putl(opoint = point, fb);
61 else
62 putw((int)(opoint = point), fb);
63 lp += iflong ? sizeof (long) : sizeof (int);
64 ct++;
65 }
66 }
67 if (iflong) putl(-1L, fb);
68 else putw(-1, fb);
69 while (hash < nhash)
70 hpt[++hash] = lp;
71 fwrite(&nhash, sizeof (nhash), 1, fa);
72 fwrite(&iflong, sizeof (iflong), 1, fa);
73 fwrite(hpt, sizeof (*hpt), nhash, fa);
74 fwrite(hfreq, sizeof (*hfreq), nhash, fa);
75 *ptotct = totct;
76 *phused = hused;
77 }
78
79 static void
putl(long ll,FILE * f)80 putl(long ll, FILE *f)
81 {
82 putw(ll, f);
83 }
84
85 long
getl(FILE * f)86 getl(FILE *f)
87 {
88 return (getw(f));
89 }
90