xref: /illumos-gate/usr/src/cmd/nscd/nscd_biggest.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1994 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <string.h>
31*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  * routines to find largest n numbers, carrying 4 bytes of data
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate int *
38*7c478bd9Sstevel@tonic-gate maken(int n)
39*7c478bd9Sstevel@tonic-gate {
40*7c478bd9Sstevel@tonic-gate 	int * ret;
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate 	n++;
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate 	ret = (int *) memset(malloc(2 * n *sizeof (int)),
45*7c478bd9Sstevel@tonic-gate 	    -1, 2 * n * sizeof (int));
46*7c478bd9Sstevel@tonic-gate 	ret[0] = n - 1;
47*7c478bd9Sstevel@tonic-gate 	return (ret);
48*7c478bd9Sstevel@tonic-gate }
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate insertn(int * table, int n, int data)
51*7c478bd9Sstevel@tonic-gate {
52*7c478bd9Sstevel@tonic-gate 	int size = *table;
53*7c478bd9Sstevel@tonic-gate 	int guess, base, last;
54*7c478bd9Sstevel@tonic-gate 	int olddata;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	if (table[1] > n)
57*7c478bd9Sstevel@tonic-gate 		return (data);
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	if (table[size] < n)  /* biggest so far */
60*7c478bd9Sstevel@tonic-gate 		guess = size;
61*7c478bd9Sstevel@tonic-gate 	else {
62*7c478bd9Sstevel@tonic-gate 		base = 1;
63*7c478bd9Sstevel@tonic-gate 		last = size;
64*7c478bd9Sstevel@tonic-gate 		while (last >= base) {
65*7c478bd9Sstevel@tonic-gate 			guess = (last+base)/2;
66*7c478bd9Sstevel@tonic-gate 			if (table[guess] == n)
67*7c478bd9Sstevel@tonic-gate 				goto doit;
68*7c478bd9Sstevel@tonic-gate 			if (table[guess] > n)
69*7c478bd9Sstevel@tonic-gate 				last = guess -1;
70*7c478bd9Sstevel@tonic-gate 			else
71*7c478bd9Sstevel@tonic-gate 				base = guess + 1;
72*7c478bd9Sstevel@tonic-gate 		}
73*7c478bd9Sstevel@tonic-gate 		guess = last;
74*7c478bd9Sstevel@tonic-gate 	}
75*7c478bd9Sstevel@tonic-gate 	doit:
76*7c478bd9Sstevel@tonic-gate 	olddata = table[2 + size];
77*7c478bd9Sstevel@tonic-gate 	memmove(table + 1, table+2, sizeof (int) * (guess-1));
78*7c478bd9Sstevel@tonic-gate 	memmove(table + 2 + size, table + 3 + size, sizeof (int) * (guess-1));
79*7c478bd9Sstevel@tonic-gate 	table[guess + size + 1] = data;
80*7c478bd9Sstevel@tonic-gate 	table[guess] = n;
81*7c478bd9Sstevel@tonic-gate 	return (olddata);
82*7c478bd9Sstevel@tonic-gate }
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate /*
85*7c478bd9Sstevel@tonic-gate  *  test code
86*7c478bd9Sstevel@tonic-gate  */
87*7c478bd9Sstevel@tonic-gate #if 0
88*7c478bd9Sstevel@tonic-gate int
89*7c478bd9Sstevel@tonic-gate main(int argc, char * argv[])
90*7c478bd9Sstevel@tonic-gate {
91*7c478bd9Sstevel@tonic-gate 	int * t;
92*7c478bd9Sstevel@tonic-gate 	char buffer[100];
93*7c478bd9Sstevel@tonic-gate 	int i, n;
94*7c478bd9Sstevel@tonic-gate 	char * tmp;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	t = maken(100);
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < 1100; i++) {
99*7c478bd9Sstevel@tonic-gate 		n = random();
100*7c478bd9Sstevel@tonic-gate 		sprintf(buffer, "trial %d: %d", i, n);
101*7c478bd9Sstevel@tonic-gate 		tmp = (char *)insertn(t, n, (int)strdup(buffer));
102*7c478bd9Sstevel@tonic-gate 		if (tmp != -1) {
103*7c478bd9Sstevel@tonic-gate 			printf("freeing %s\n", tmp);
104*7c478bd9Sstevel@tonic-gate 			free(tmp);
105*7c478bd9Sstevel@tonic-gate 		}
106*7c478bd9Sstevel@tonic-gate 	}
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= 100; i++) {
109*7c478bd9Sstevel@tonic-gate 		printf("%d: %s\n", i, t[100 + 1 + i]);
110*7c478bd9Sstevel@tonic-gate 		free((char *)t[100 + 1 + i]);
111*7c478bd9Sstevel@tonic-gate 	}
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	free(t);
114*7c478bd9Sstevel@tonic-gate }
115*7c478bd9Sstevel@tonic-gate #endif
116