home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!think.com!spdcc!dirtydog.ima.isc.com!karl
- From: karl@ima.isc.com (Karl Heuer)
- Subject: Re: Sorting (continued)
- Message-ID: <1992Aug18.175649.19458@ima.isc.com>
- Sender: usenet@ima.isc.com (news)
- Organization: Interactive Systems, Cambridge, MA 02138-5302
- References: <1992Aug15.135836.23357@midway.uchicago.edu> <1992Aug18.175114.19201@ima.isc.com>
- Date: Tue, 18 Aug 1992 17:56:49 GMT
- Lines: 53
-
- In article <1992Aug15.135836.23357@midway.uchicago.edu>
- pynq@midway.uchicago.edu asks how to handle a sorting problem.
-
- I've already discussed the suggested algorithms; in this article I'd like to
- outline a possible sort engine which doesn't depend on the array model of
- qsort(). Given such an engine in our software toolchest, pynq's problem
- would be trivial.
-
- I think it should be possible to design something along the following lines:
-
- #include <sort.h>
- sort_t *mksort(int (*cmpf)(void const *, void const *),
- size_t eltsize, unsigned int flags, ...);
- int putsort(sort_t *, void const *);
- void *getsort(sort_t *);
- void rmsort(sort_t *);
-
- /* sample program */
- int mycompare(void const *x, void const *y) {
- return (strcmp(*(char * const *)x, *(char * const *)y));
- }
- main() {
- char *buf;
- sort_t *sp;
- void *p;
- if ((sp = mksort(mycompare, sizeof(char *), 0)) == NULL) die();
- while ((buf = fgetline(stdin)) != NULL) {
- if (putsort(sp, &buf) != 0) die();
- }
- while ((p = getsort(sp)) != NULL) puts(*(char **)p);
- return (0);
- }
-
- Here's the basic idea. |mksort()| creates an opaque object that will somehow
- hold all the state information. |putsort()| adds an element to the set; it
- returns |int| rather than |void| only because it might have to signal an error
- from an internal |malloc()|. |getsort()| returns a null pointer if the set is
- empty, otherwise it returns the "smallest" element, and deletes it from the
- set. |rmsort()| does any final cleanup.
-
- Although the usual sequence of calls would be as shown in the example, it's
- also possible to terminate the |getsort()| loop before exhausting the set.
- Also, if it can be done without compromising the speed, it would be nice if
- the package would allow additional calls to |putsort()| even after the user
- has started to retrieve them with |getsort()|.
-
- This package could be implemented using any of the algorithms listed in the
- original article, or any other method. Although the details are not visible
- to the user, hints on the anticipated usage can be provided through the
- |flags| argument (and perhaps additional arguments), in case no single
- algorithm has acceptable behavior for all cases.
-
- Karl W. Z. Heuer (karl@ima.isc.com or uunet!ima!karl), The Walking Lint
-