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 $ */
30 * Copyright (c) 1988 by Sun Microsystems, Inc.
37 * ISAM sort package (sort in memory)
41 #include "isam_impl.h"
43 extern char *_isunix_malloc();
52 _issort_create(int reclen, int nrecs, int (*compfunc)())
56 p = (Issort *)_ismalloc(sizeof(*p)); /* Allocate new sorter */
57 memset((char *)p, 0,sizeof(*p));
59 p->ist_reclength = reclen; /* Record length in bytes */
60 p->ist_allocrecs = nrecs; /* Maximum number of records */
61 /* that can inserted */
62 p->ist_nrecs = 0; /* Current number of records */
63 p->ist_currec; /* Current position */
64 p->ist_compf = compfunc; /* Comparison function */
66 p->ist_array = _ismalloc((unsigned)(reclen * nrecs)); /* Allocate array */
67 /* for storing records */
79 _issort_destroy(Issort *srt)
81 assert(srt->ist_array != (char *) 0);
83 (void) free(srt->ist_array);
84 (void) free((char *)srt);
90 * Insert record to sorter
94 _issort_insert(Issort *srt, char *record)
96 assert(srt->ist_nrecs < srt->ist_allocrecs);
98 memcpy(srt->ist_array + srt->ist_nrecs * srt->ist_reclength,record,
101 srt->ist_currec = srt->ist_nrecs++;
111 _issort_sort(Issort *srt)
113 if (srt->ist_nrecs > 1)
114 qsort(srt->ist_array,srt->ist_nrecs,srt->ist_reclength,srt->ist_compf);
116 _issort_rewind(srt); /* Rewind for subsequent reads */
126 _issort_rewind(Issort *srt)
134 * Read record from sorter
138 _issort_read(Issort *srt)
140 return((srt->ist_currec < srt->ist_nrecs) ?
141 (srt->ist_array + srt->ist_currec++ * srt->ist_reclength) :