2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
24 /*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
25 /*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
26 /*%% (c) Copyright 1993, 1994 Novell, Inc. */
27 /*%% $XConsortium: issort.c /main/3 1995/10/23 11:44:55 rswiston $ */
29 static char sccsid[] = "@(#)issort.c 1.3 89/07/17 Copyr 1988 Sun Micro";
33 * Copyright (c) 1988 by Sun Microsystems, Inc.
40 * ISAM sort package (sort in memory)
44 #include "isam_impl.h"
46 extern char *_isunix_malloc();
55 _issort_create(int reclen, int nrecs, int (*compfunc)())
59 p = (Issort *)_ismalloc(sizeof(*p)); /* Allocate new sorter */
60 memset((char *)p, 0,sizeof(*p));
62 p->ist_reclength = reclen; /* Record length in bytes */
63 p->ist_allocrecs = nrecs; /* Maximum number of records */
64 /* that can inserted */
65 p->ist_nrecs = 0; /* Current number of records */
66 p->ist_currec; /* Current position */
67 p->ist_compf = compfunc; /* Comparison function */
69 p->ist_array = _ismalloc((unsigned)(reclen * nrecs)); /* Allocate array */
70 /* for storing records */
82 _issort_destroy(Issort *srt)
84 assert(srt->ist_array != (char *) 0);
86 (void) free(srt->ist_array);
87 (void) free((char *)srt);
93 * Insert record to sorter
97 _issort_insert(Issort *srt, char *record)
99 assert(srt->ist_nrecs < srt->ist_allocrecs);
101 memcpy(srt->ist_array + srt->ist_nrecs * srt->ist_reclength,record,
104 srt->ist_currec = srt->ist_nrecs++;
114 _issort_sort(Issort *srt)
116 if (srt->ist_nrecs > 1)
117 qsort(srt->ist_array,srt->ist_nrecs,srt->ist_reclength,srt->ist_compf);
119 _issort_rewind(srt); /* Rewind for subsequent reads */
129 _issort_rewind(Issort *srt)
137 * Read record from sorter
141 _issort_read(Issort *srt)
143 return((srt->ist_currec < srt->ist_nrecs) ?
144 (srt->ist_array + srt->ist_currec++ * srt->ist_reclength) :