xref: /illumos-gate/usr/src/cmd/nscd/nscd_biggest.c (revision f166393f4d30d59a005967d6c6d2869ef830b75d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*f166393fSesolom  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*f166393fSesolom  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * routines to find largest n numbers, carrying 4 bytes of data
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate int *
387c478bd9Sstevel@tonic-gate maken(int n)
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate 	int * ret;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 	n++;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 	ret = (int *) memset(malloc(2 * n *sizeof (int)),
457c478bd9Sstevel@tonic-gate 	    -1, 2 * n * sizeof (int));
467c478bd9Sstevel@tonic-gate 	ret[0] = n - 1;
477c478bd9Sstevel@tonic-gate 	return (ret);
487c478bd9Sstevel@tonic-gate }
497c478bd9Sstevel@tonic-gate 
50*f166393fSesolom int
517c478bd9Sstevel@tonic-gate insertn(int * table, int n, int data)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	int size = *table;
547c478bd9Sstevel@tonic-gate 	int guess, base, last;
557c478bd9Sstevel@tonic-gate 	int olddata;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	if (table[1] > n)
587c478bd9Sstevel@tonic-gate 		return (data);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	if (table[size] < n)  /* biggest so far */
617c478bd9Sstevel@tonic-gate 		guess = size;
627c478bd9Sstevel@tonic-gate 	else {
637c478bd9Sstevel@tonic-gate 		base = 1;
647c478bd9Sstevel@tonic-gate 		last = size;
657c478bd9Sstevel@tonic-gate 		while (last >= base) {
667c478bd9Sstevel@tonic-gate 			guess = (last+base)/2;
677c478bd9Sstevel@tonic-gate 			if (table[guess] == n)
687c478bd9Sstevel@tonic-gate 				goto doit;
697c478bd9Sstevel@tonic-gate 			if (table[guess] > n)
707c478bd9Sstevel@tonic-gate 				last = guess -1;
717c478bd9Sstevel@tonic-gate 			else
727c478bd9Sstevel@tonic-gate 				base = guess + 1;
737c478bd9Sstevel@tonic-gate 		}
747c478bd9Sstevel@tonic-gate 		guess = last;
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 	doit:
777c478bd9Sstevel@tonic-gate 	olddata = table[2 + size];
787c478bd9Sstevel@tonic-gate 	memmove(table + 1, table+2, sizeof (int) * (guess-1));
797c478bd9Sstevel@tonic-gate 	memmove(table + 2 + size, table + 3 + size, sizeof (int) * (guess-1));
807c478bd9Sstevel@tonic-gate 	table[guess + size + 1] = data;
817c478bd9Sstevel@tonic-gate 	table[guess] = n;
827c478bd9Sstevel@tonic-gate 	return (olddata);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  *  test code
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate #if 0
897c478bd9Sstevel@tonic-gate int
907c478bd9Sstevel@tonic-gate main(int argc, char * argv[])
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	int * t;
937c478bd9Sstevel@tonic-gate 	char buffer[100];
947c478bd9Sstevel@tonic-gate 	int i, n;
957c478bd9Sstevel@tonic-gate 	char * tmp;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	t = maken(100);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	for (i = 0; i < 1100; i++) {
1007c478bd9Sstevel@tonic-gate 		n = random();
1017c478bd9Sstevel@tonic-gate 		sprintf(buffer, "trial %d: %d", i, n);
1027c478bd9Sstevel@tonic-gate 		tmp = (char *)insertn(t, n, (int)strdup(buffer));
1037c478bd9Sstevel@tonic-gate 		if (tmp != -1) {
1047c478bd9Sstevel@tonic-gate 			printf("freeing %s\n", tmp);
1057c478bd9Sstevel@tonic-gate 			free(tmp);
1067c478bd9Sstevel@tonic-gate 		}
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	for (i = 1; i <= 100; i++) {
1107c478bd9Sstevel@tonic-gate 		printf("%d: %s\n", i, t[100 + 1 + i]);
1117c478bd9Sstevel@tonic-gate 		free((char *)t[100 + 1 + i]);
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	free(t);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate #endif
117