open-axiom repository from github
/*1Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.2All rights reserved.3Copyright (C) 2007-2008, Gabriel Dos Reis.4All rights reserved.56Redistribution and use in source and binary forms, with or without7modification, are permitted provided that the following conditions are8met:910- Redistributions of source code must retain the above copyright11notice, this list of conditions and the following disclaimer.1213- Redistributions in binary form must reproduce the above copyright14notice, this list of conditions and the following disclaimer in15the documentation and/or other materials provided with the16distribution.1718- Neither the name of The Numerical Algorithms Group Ltd. nor the19names of its contributors may be used to endorse or promote products20derived from this software without specific prior written permission.2122THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS23IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED24TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A25PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER26OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,27EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,28PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR29PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF30LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING31NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS32SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.33*/3435#define _MSORT3D_C36#include "openaxiom-c-macros.h"373839/*****************************************************40* Mergesort routine *41* *42* This file depends on the file msort.h. There, a *43* data type called linkElement is defined. It is *44* used here and is the main structure being sorted *45* here. You can sort any linked structure, under *46* any name - so long as it has a next field (see *47* below). The define statement, below, renames *48* linkElement to linkThing. All you need to do *49* is change the define statement to rename *50* your structure to linkThing. The first argument *51* you pass to the sort routine is a pointer to *52* the unsorted list. The function returns with *53* that same pointer pointing to a sorted list. *54* *55* Usage: *56* linkElement *msort(p,min,max,compare) *57* linkElement *L; *58* int min,max; *59* int (*compare)(); *60* *61* e.g. *62* msort(L,0,N,compare); *63* *64* where *65* L is the list of things to be sorted, *66* it is expected to be a linked list *67* where the following element is pointed *68* to by a field called "next" *69* 0 is the index of the first element *70* (since this routine is called recursively, *71* this field is kept for clarity; it will *72* always be zero at top level) *73* N the number of elements in the list *74* minus one *75* compare(X,Y) is a comparison function that *76* returns a -1 if X is less than Y *77* 0 if X is the same as Y *78* and 1 if X is greater than Y *79* *80*****************************************************/818283#include "header.h"8485#include "all_3d.H1"868788#define linkThing poly89909192/**********************93* merge(p,q,compare) *94**********************/9596linkThing *97merge(linkThing *p, linkThing *q,int (*compare)(linkThing *, linkThing *))98{99linkThing *returnVal,*current,*pN,*qN;100101/* return if only one item - take out when insert sort implemented */102if (!p) return(q); else if (!q) return(p);103104/* set up the head of the list (first element) */105if (compare(p,q) <= 0) {106returnVal = current = p;107pN = p->next;108qN = q;109} else {110returnVal = current = q;111pN = p;112qN = q->next;113}114115/* merge the two lists */116while ((pN != NULL) && (qN != NULL)) {117if (compare(pN,qN) <= 0) { /* pN <= qN */118current->next = pN;119current = pN;120pN = pN->next;121} else {122current->next = qN;123current = qN;124qN = qN->next;125}126}127128/* tag on the tail end */129if (pN == NULL) current->next = qN;130else current->next = pN;131132return(returnVal);133134} /* merge() */135136137138/*********************************139* msort: the top level function *140*********************************/141142linkThing *143msort(linkThing *p,int min,int max,int (*compare)(linkThing *, linkThing *))144{145int mid;146int i;147linkThing *q,*temp,*xxx;148149if (min == max) return p;150else {151mid = (min + max - 1)/2;152/* e.g. [min,max] = [1,6] => mid=3 => q points to 4th */153for (i=min,q=p; i<mid; i++,q=q->next);154temp = q->next;155q->next = 0;156xxx = merge(msort(p,min,mid,compare),157msort(temp,mid+1,max,compare), compare);158159return(xxx);160}161162} /* msort() */163164165166167168