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#pragma ident	"%Z%%M%	%I%	%E% SMI"
16
17#include <stdio.h>
18#include <assert.h>
19
20static void putl(long, FILE *);
21
22void
23whash(FILE *ft, FILE *fa, FILE *fb, int nhash, int iflong,
24	    long *ptotct, int *phused)
25{
26	char line[100];
27	int hash = 0, hused = 0;
28	long totct = 0L;
29	int ct = 0;
30	long point;
31	long opoint = -1;
32	int m;
33	int k;
34	long lp;
35	long *hpt;
36	int *hfreq = NULL;
37
38	hpt = (long *)calloc(nhash+1, sizeof (*hpt));
39	assert(hpt != NULL);
40	hfreq = (int *)calloc(nhash, sizeof (*hfreq));
41	assert(hfreq != NULL);
42	hpt[0] = 0;
43	lp = 0;
44	while (fgets(line, 100, ft)) {
45		totct++;
46		sscanf(line, "%d %ld", &k, &point);
47		if (hash < k) {
48			hused++;
49			if (iflong) putl(-1L, fb);
50			else putw(-1, fb);
51			hfreq[hash] = ct;
52			while (hash < k) {
53				hpt[++hash] = lp;
54				hfreq[hash] = 0;
55			}
56			hpt[hash] = lp += iflong ? sizeof (long) : sizeof (int);
57			opoint = -1;
58			ct = 0;
59		}
60		if (point != opoint) {
61			if (iflong)
62				putl(opoint = point, fb);
63			else
64				putw((int)(opoint = point), fb);
65			lp += iflong ? sizeof (long) : sizeof (int);
66			ct++;
67		}
68	}
69	if (iflong) putl(-1L, fb);
70	else putw(-1, fb);
71	while (hash < nhash)
72		hpt[++hash] = lp;
73	fwrite(&nhash, sizeof (nhash), 1, fa);
74	fwrite(&iflong, sizeof (iflong), 1, fa);
75	fwrite(hpt, sizeof (*hpt), nhash, fa);
76	fwrite(hfreq, sizeof (*hfreq), nhash, fa);
77	*ptotct = totct;
78	*phused = hused;
79}
80
81static void
82putl(long ll, FILE *f)
83{
84	putw(ll, f);
85}
86
87long
88getl(FILE *f)
89{
90	return (getw(f));
91}
92