Home | History | Annotate | Line # | Download | only in sort
fsort.c revision 1.32.12.1
      1  1.32.12.1      matt /*	$NetBSD: fsort.c,v 1.32.12.1 2010/04/21 05:27:12 matt Exp $	*/
      2       1.26  jdolecek 
      3       1.26  jdolecek /*-
      4       1.26  jdolecek  * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
      5       1.26  jdolecek  * All rights reserved.
      6       1.26  jdolecek  *
      7       1.26  jdolecek  * This code is derived from software contributed to The NetBSD Foundation
      8       1.26  jdolecek  * by Ben Harris and Jaromir Dolecek.
      9       1.26  jdolecek  *
     10       1.26  jdolecek  * Redistribution and use in source and binary forms, with or without
     11       1.26  jdolecek  * modification, are permitted provided that the following conditions
     12       1.26  jdolecek  * are met:
     13       1.26  jdolecek  * 1. Redistributions of source code must retain the above copyright
     14       1.26  jdolecek  *    notice, this list of conditions and the following disclaimer.
     15       1.26  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.26  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     17       1.26  jdolecek  *    documentation and/or other materials provided with the distribution.
     18       1.26  jdolecek  *
     19       1.26  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.26  jdolecek  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.26  jdolecek  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.26  jdolecek  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.26  jdolecek  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.26  jdolecek  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.26  jdolecek  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.26  jdolecek  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.26  jdolecek  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.26  jdolecek  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.26  jdolecek  * POSSIBILITY OF SUCH DAMAGE.
     30       1.26  jdolecek  */
     31        1.2     bjh21 
     32        1.1     bjh21 /*-
     33        1.1     bjh21  * Copyright (c) 1993
     34        1.1     bjh21  *	The Regents of the University of California.  All rights reserved.
     35        1.1     bjh21  *
     36        1.1     bjh21  * This code is derived from software contributed to Berkeley by
     37        1.1     bjh21  * Peter McIlroy.
     38        1.1     bjh21  *
     39        1.1     bjh21  * Redistribution and use in source and binary forms, with or without
     40        1.1     bjh21  * modification, are permitted provided that the following conditions
     41        1.1     bjh21  * are met:
     42        1.1     bjh21  * 1. Redistributions of source code must retain the above copyright
     43        1.1     bjh21  *    notice, this list of conditions and the following disclaimer.
     44        1.1     bjh21  * 2. Redistributions in binary form must reproduce the above copyright
     45        1.1     bjh21  *    notice, this list of conditions and the following disclaimer in the
     46        1.1     bjh21  *    documentation and/or other materials provided with the distribution.
     47       1.25       agc  * 3. Neither the name of the University nor the names of its contributors
     48        1.1     bjh21  *    may be used to endorse or promote products derived from this software
     49        1.1     bjh21  *    without specific prior written permission.
     50        1.1     bjh21  *
     51        1.1     bjh21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     52        1.1     bjh21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     53        1.1     bjh21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     54        1.1     bjh21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     55        1.1     bjh21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     56        1.1     bjh21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     57        1.1     bjh21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58        1.1     bjh21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59        1.1     bjh21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60        1.1     bjh21  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61        1.1     bjh21  * SUCH DAMAGE.
     62        1.1     bjh21  */
     63        1.1     bjh21 
     64        1.1     bjh21 /*
     65  1.32.12.1      matt  * Read in a block of records (until 'enough').
     66  1.32.12.1      matt  * sort, write to temp file.
     67  1.32.12.1      matt  * Merge sort temp files into output file
     68  1.32.12.1      matt  * Small files miss out the temp file stage.
     69  1.32.12.1      matt  * Large files might get multiple merges.
     70       1.16  jdolecek  */
     71        1.1     bjh21 #include "sort.h"
     72        1.1     bjh21 #include "fsort.h"
     73        1.1     bjh21 
     74        1.2     bjh21 #ifndef lint
     75  1.32.12.1      matt __RCSID("$NetBSD: fsort.c,v 1.32.12.1 2010/04/21 05:27:12 matt Exp $");
     76        1.2     bjh21 __SCCSID("@(#)fsort.c	8.1 (Berkeley) 6/6/93");
     77        1.2     bjh21 #endif /* not lint */
     78        1.2     bjh21 
     79        1.1     bjh21 #include <stdlib.h>
     80        1.1     bjh21 #include <string.h>
     81        1.1     bjh21 
     82       1.13  jdolecek #define SALIGN(n) ((n+sizeof(length_t)-1) & ~(sizeof(length_t)-1))
     83        1.8  jdolecek 
     84        1.1     bjh21 void
     85  1.32.12.1      matt fsort(struct filelist *filelist, int nfiles, FILE *outfp, struct field *ftbl)
     86        1.1     bjh21 {
     87  1.32.12.1      matt 	RECHEADER **keylist;
     88  1.32.12.1      matt 	RECHEADER **keypos, **keyp;
     89  1.32.12.1      matt 	RECHEADER *buffer;
     90  1.32.12.1      matt 	size_t bufsize = DEFBUFSIZE;
     91       1.30  jdolecek 	u_char *bufend;
     92  1.32.12.1      matt 	int mfct = 0;
     93  1.32.12.1      matt 	int c, nelem;
     94        1.8  jdolecek 	get_func_t get;
     95  1.32.12.1      matt 	RECHEADER *crec;
     96  1.32.12.1      matt 	RECHEADER *nbuffer;
     97  1.32.12.1      matt 	FILE *fp, *tmp_fp;
     98  1.32.12.1      matt 	int file_no;
     99  1.32.12.1      matt 	int max_recs = DEBUG('m') ? 16 : MAXNUM;
    100  1.32.12.1      matt 
    101  1.32.12.1      matt 	buffer = malloc(bufsize);
    102  1.32.12.1      matt 	bufend = (u_char *)buffer + bufsize;
    103  1.32.12.1      matt 	/* Allocate double length keymap for radix_sort */
    104  1.32.12.1      matt 	keylist = malloc(2 * max_recs * sizeof(*keylist));
    105  1.32.12.1      matt 	if (buffer == NULL || keylist == NULL)
    106  1.32.12.1      matt 		err(2, "failed to malloc initial buffer or keylist");
    107  1.32.12.1      matt 
    108  1.32.12.1      matt 	if (SINGL_FLD)
    109  1.32.12.1      matt 		/* Key and data are one! */
    110  1.32.12.1      matt 		get = makeline;
    111        1.1     bjh21 	else
    112  1.32.12.1      matt 		/* Key (merged key fields) added before data */
    113  1.32.12.1      matt 		get = makekey;
    114  1.32.12.1      matt 
    115  1.32.12.1      matt 	file_no = 0;
    116  1.32.12.1      matt 	fp = fopen(filelist->names[0], "r");
    117  1.32.12.1      matt 	if (fp == NULL)
    118  1.32.12.1      matt 		err(2, "%s", filelist->names[0]);
    119  1.32.12.1      matt 
    120  1.32.12.1      matt 	/* Loop through reads of chunk of input files that get sorted
    121  1.32.12.1      matt 	 * and then merged together. */
    122        1.1     bjh21 	for (;;) {
    123  1.32.12.1      matt 		keypos = keylist;
    124  1.32.12.1      matt 		nelem = 0;
    125  1.32.12.1      matt 		crec = buffer;
    126  1.32.12.1      matt 		makeline_copydown(crec);
    127  1.32.12.1      matt 
    128  1.32.12.1      matt 		/* Loop reading records */
    129  1.32.12.1      matt 		for (;;) {
    130  1.32.12.1      matt 			c = get(fp, crec, bufend, ftbl);
    131  1.32.12.1      matt 			/* 'c' is 0, EOF or BUFFEND */
    132  1.32.12.1      matt 			if (c == 0) {
    133  1.32.12.1      matt 				/* Save start of key in input buffer */
    134  1.32.12.1      matt 				*keypos++ = crec;
    135  1.32.12.1      matt 				if (++nelem == max_recs) {
    136        1.1     bjh21 					c = BUFFEND;
    137        1.1     bjh21 					break;
    138        1.1     bjh21 				}
    139  1.32.12.1      matt 				crec = (RECHEADER *)(crec->data + SALIGN(crec->length));
    140       1.19  jdolecek 				continue;
    141       1.19  jdolecek 			}
    142  1.32.12.1      matt 			if (c == EOF) {
    143  1.32.12.1      matt 				/* try next file */
    144  1.32.12.1      matt 				if (++file_no >= nfiles)
    145  1.32.12.1      matt 					/* no more files */
    146  1.32.12.1      matt 					break;
    147  1.32.12.1      matt 				fp = fopen(filelist->names[file_no], "r");
    148  1.32.12.1      matt 				if (fp == NULL)
    149  1.32.12.1      matt 					err(2, "%s", filelist->names[file_no]);
    150  1.32.12.1      matt 				continue;
    151        1.1     bjh21 			}
    152  1.32.12.1      matt 			if (nelem >= max_recs
    153  1.32.12.1      matt 			    || (bufsize >= MAXBUFSIZE && nelem > 8))
    154  1.32.12.1      matt 				/* Need to sort and save this lot of data */
    155  1.32.12.1      matt 				break;
    156  1.32.12.1      matt 
    157  1.32.12.1      matt 			/* c == BUFFEND, and we can process more data */
    158  1.32.12.1      matt 			/* Allocate a larger buffer for this lot of data */
    159  1.32.12.1      matt 			bufsize *= 2;
    160  1.32.12.1      matt 			nbuffer = realloc(buffer, bufsize);
    161  1.32.12.1      matt 			if (!nbuffer) {
    162  1.32.12.1      matt 				err(2, "failed to realloc buffer to %zu bytes",
    163  1.32.12.1      matt 					bufsize);
    164        1.9    itojun 			}
    165        1.8  jdolecek 
    166  1.32.12.1      matt 			/* patch up keylist[] */
    167  1.32.12.1      matt 			for (keyp = &keypos[-1]; keyp >= keylist; keyp--)
    168  1.32.12.1      matt 				*keyp = nbuffer + (*keyp - buffer);
    169  1.32.12.1      matt 
    170  1.32.12.1      matt 			crec = nbuffer + (crec - buffer);
    171  1.32.12.1      matt 			buffer = nbuffer;
    172  1.32.12.1      matt 			bufend = (u_char *)buffer + bufsize;
    173  1.32.12.1      matt 		}
    174  1.32.12.1      matt 
    175  1.32.12.1      matt 		/* Sort this set of records */
    176  1.32.12.1      matt 		radix_sort(keylist, keylist + max_recs, nelem);
    177  1.32.12.1      matt 
    178  1.32.12.1      matt 		if (c == EOF && mfct == 0) {
    179  1.32.12.1      matt 			/* all the data is (sorted) in the buffer */
    180  1.32.12.1      matt 			append(keylist, nelem, outfp,
    181  1.32.12.1      matt 			    DEBUG('k') ? putkeydump : putline);
    182  1.32.12.1      matt 			break;
    183        1.1     bjh21 		}
    184        1.1     bjh21 
    185  1.32.12.1      matt 		/* Save current data to a temporary file for a later merge */
    186  1.32.12.1      matt 		if (nelem != 0) {
    187  1.32.12.1      matt 			tmp_fp = ftmp();
    188  1.32.12.1      matt 			append(keylist, nelem, tmp_fp, putrec);
    189  1.32.12.1      matt 			save_for_merge(tmp_fp, geteasy, ftbl);
    190  1.32.12.1      matt 		}
    191  1.32.12.1      matt 		mfct = 1;
    192  1.32.12.1      matt 
    193  1.32.12.1      matt 		if (c == EOF) {
    194  1.32.12.1      matt 			/* merge to output file */
    195  1.32.12.1      matt 			merge_sort(outfp,
    196  1.32.12.1      matt 			    DEBUG('k') ? putkeydump : putline, ftbl);
    197  1.32.12.1      matt 			break;
    198        1.1     bjh21 		}
    199       1.19  jdolecek 	}
    200        1.1     bjh21 
    201  1.32.12.1      matt 	free(keylist);
    202  1.32.12.1      matt 	keylist = NULL;
    203  1.32.12.1      matt 	free(buffer);
    204  1.32.12.1      matt 	buffer = NULL;
    205        1.1     bjh21 }
    206