Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
/*
2
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
3
All rights reserved.
4
Copyright (C) 2007-2008, Gabriel Dos Reis.
5
All rights reserved.
6
7
Redistribution and use in source and binary forms, with or without
8
modification, are permitted provided that the following conditions are
9
met:
10
11
- Redistributions of source code must retain the above copyright
12
notice, this list of conditions and the following disclaimer.
13
14
- Redistributions in binary form must reproduce the above copyright
15
notice, this list of conditions and the following disclaimer in
16
the documentation and/or other materials provided with the
17
distribution.
18
19
- Neither the name of The Numerical Algorithms Group Ltd. nor the
20
names of its contributors may be used to endorse or promote products
21
derived from this software without specific prior written permission.
22
23
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
27
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
*/
35
36
#define _MSORT3D_C
37
#include "openaxiom-c-macros.h"
38
39
40
/*****************************************************
41
* Mergesort routine *
42
* *
43
* This file depends on the file msort.h. There, a *
44
* data type called linkElement is defined. It is *
45
* used here and is the main structure being sorted *
46
* here. You can sort any linked structure, under *
47
* any name - so long as it has a next field (see *
48
* below). The define statement, below, renames *
49
* linkElement to linkThing. All you need to do *
50
* is change the define statement to rename *
51
* your structure to linkThing. The first argument *
52
* you pass to the sort routine is a pointer to *
53
* the unsorted list. The function returns with *
54
* that same pointer pointing to a sorted list. *
55
* *
56
* Usage: *
57
* linkElement *msort(p,min,max,compare) *
58
* linkElement *L; *
59
* int min,max; *
60
* int (*compare)(); *
61
* *
62
* e.g. *
63
* msort(L,0,N,compare); *
64
* *
65
* where *
66
* L is the list of things to be sorted, *
67
* it is expected to be a linked list *
68
* where the following element is pointed *
69
* to by a field called "next" *
70
* 0 is the index of the first element *
71
* (since this routine is called recursively, *
72
* this field is kept for clarity; it will *
73
* always be zero at top level) *
74
* N the number of elements in the list *
75
* minus one *
76
* compare(X,Y) is a comparison function that *
77
* returns a -1 if X is less than Y *
78
* 0 if X is the same as Y *
79
* and 1 if X is greater than Y *
80
* *
81
*****************************************************/
82
83
84
#include "header.h"
85
86
#include "all_3d.H1"
87
88
89
#define linkThing poly
90
91
92
93
/**********************
94
* merge(p,q,compare) *
95
**********************/
96
97
linkThing *
98
merge(linkThing *p, linkThing *q,int (*compare)(linkThing *, linkThing *))
99
{
100
linkThing *returnVal,*current,*pN,*qN;
101
102
/* return if only one item - take out when insert sort implemented */
103
if (!p) return(q); else if (!q) return(p);
104
105
/* set up the head of the list (first element) */
106
if (compare(p,q) <= 0) {
107
returnVal = current = p;
108
pN = p->next;
109
qN = q;
110
} else {
111
returnVal = current = q;
112
pN = p;
113
qN = q->next;
114
}
115
116
/* merge the two lists */
117
while ((pN != NULL) && (qN != NULL)) {
118
if (compare(pN,qN) <= 0) { /* pN <= qN */
119
current->next = pN;
120
current = pN;
121
pN = pN->next;
122
} else {
123
current->next = qN;
124
current = qN;
125
qN = qN->next;
126
}
127
}
128
129
/* tag on the tail end */
130
if (pN == NULL) current->next = qN;
131
else current->next = pN;
132
133
return(returnVal);
134
135
} /* merge() */
136
137
138
139
/*********************************
140
* msort: the top level function *
141
*********************************/
142
143
linkThing *
144
msort(linkThing *p,int min,int max,int (*compare)(linkThing *, linkThing *))
145
{
146
int mid;
147
int i;
148
linkThing *q,*temp,*xxx;
149
150
if (min == max) return p;
151
else {
152
mid = (min + max - 1)/2;
153
/* e.g. [min,max] = [1,6] => mid=3 => q points to 4th */
154
for (i=min,q=p; i<mid; i++,q=q->next);
155
temp = q->next;
156
q->next = 0;
157
xxx = merge(msort(p,min,mid,compare),
158
msort(temp,mid+1,max,compare), compare);
159
160
return(xxx);
161
}
162
163
} /* msort() */
164
165
166
167
168