aboutsummaryrefslogtreecommitdiff
path: root/pkg/utilities/nttools/tmatch/sortdist.x
blob: b0745676fe00ed4f37b902c4035018126d4accf7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#* HISTORY *
#* B.Simon	25-Aug-94	original
#  Phil Hodge	28-Sept-2005	in sortdist, add 'int cmpdist()'

# SORTDIST -- Sort the dist array 

procedure sortdist (ndist, dist, index)

int	ndist		# i: length of dist and index arrays
double	dist[ARB]	# i: indices of second table rows matching first
int	index[ARB]	# u: indices of first table rows, in sort order on exit
#--
int	cmpdist()
extern	cmpdist
pointer	sp, dist2

begin
	call smark (sp)
	call salloc (dist2, ndist, TY_DOUBLE)

	call amovd (dist, Memd[dist2], ndist)
	call gqsort (index, ndist, cmpdist, dist2)

	call sfree (sp)
end

# CMPDIST -- Compare two elements in the dist array

int procedure cmpdist (dist, ielem, jelem)

pointer	dist		# address of the dist array
int	ielem		# first element
int	jelem		# second element
#--
int	order

begin
	if (Memd[dist+ielem-1] < Memd[dist+jelem-1]) {
	    order = -1
	} else if (Memd[dist+ielem-1] > Memd[dist+jelem-1]) {
	    order = 1
	} else if (ielem < jelem) {
	    order = -1
	} else if (ielem < jelem) {
	    order = 1
	} else {
	    order = 0
	}
	return (order)
end