File: sortdata.c

package info (click to toggle)
xlispstat 3.52.14-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,560 kB
  • ctags: 12,676
  • sloc: ansic: 91,357; lisp: 21,759; sh: 1,525; makefile: 521; csh: 1
file content (178 lines) | stat: -rw-r--r-- 4,297 bytes parent folder | download | duplicates (4)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* sortdata - Sorting, ordering and ranking functions. Uses C qsort.   */
/* XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney                  */
/* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
/* You may give out copies of this software; for conditions see the    */
/* file COPYING included with this distribution.                       */
 
#include "xlisp.h"
#include "xlstat.h"

/* forward declarations */
LOCAL LVAL lsort(V);
LOCAL int tiebreak P2H(LVAL *, LVAL *);

/* tiebreaker routine */
LOCAL int tiebreak P2C(LVAL *, px, LVAL *, py)
{
  int ix = getfixnum(px[1]);
  int iy = getfixnum(py[1]);

  if (ix < iy) return(-1);
  else return(1);
}

/* comparison routine for qsort */
#ifdef ANSI
LOCAL int lcomp(const void *px, const void *py)
#else
LOCAL int lcomp(px, py)
     ALLOCTYPE *px, *py;
#endif /* ANSI */
{
  LVAL x, y;	
  FIXTYPE ix, iy;
  double fx, fy;
  
  x = *((LVAL *) px);
  y = *((LVAL *) py);
  
  if (fixp(x) && fixp(y)) {
    ix = getfixnum(x);
    iy = getfixnum(y);
    if (ix < iy) return(-1);
    else if (ix > iy) return(1);
    else return tiebreak((LVAL *) px, (LVAL *) py);
  }
  else if (realp(x) && realp(y)) {
    fx = makefloat(x);
    fy = makefloat(y);
    if (fx < fy) return(-1);
    else if (fx > fy) return(1);
    else return tiebreak((LVAL *) px, (LVAL *) py);
  }
  else if (stringp(x) && stringp(y)) {
    ix = strcmp(getstring(x), getstring(y));
    return ix ? ix : tiebreak((LVAL *) px, (LVAL *) py);
  }
  else xlfail("can't compare these");
  return 0; /* not reached */
}

/* for defining SORT-DATA with SORT function */
LVAL xssortcmp(V)
{
  LVAL x, y;

  x = xlgetarg();
  y = xlgetarg();
  xllastarg();
  
  if (fixp(x) && fixp(y))
    return (getfixnum(x) > getfixnum(y)) ? NIL : s_true;
  else if (realp(x) && realp(y))
    return (makefloat(x) > makefloat(y)) ? NIL : s_true;
  else if (stringp(x) && stringp(y))
    return (strcmp(getstring(x), getstring(y)) > 0) ? NIL : s_true;
  else {
    xlfail("can't compare these");
    return 0; /* not reached */
  }
}

/* for defining ORDER with SORT function */
LVAL xsordercmp(V)
{
  LVAL x, y;

  x = car(xlgacons());
  y = car(xlgacons());
  xllastarg();
  
  if (fixp(x) && fixp(y))
    return (getfixnum(x) > getfixnum(y)) ? NIL : s_true;
  else if (realp(x) && realp(y))
    return (makefloat(x) > makefloat(y)) ? NIL : s_true;
  else if (stringp(x) && stringp(y))
    return (strcmp(getstring(x), getstring(y)) > 0) ? NIL : s_true;
  else {
    xlfail("can't compare these");
    return 0; /* not reached */
  }
}

/* internal sort and order routine. Returns list of list of sorted values */
/* and corresponding indices into original sequence (result of ORDER).    */
LOCAL LVAL lsort(V)
{
  LVAL x, sortx, result, nextx, nexti, 
  result_x, result_i;
  int i, n;
  
  /* protect some pointers */
  xlstkcheck(5);
  xlsave(x);
  xlsave(sortx);
  xlsave(result);
  xlsave(result_x);
  xlsave(result_i);
  
  x = xlgetarg();
  x = elementseq(x);
  x = coerce_to_list(x);
  xllastarg();
  
  n = llength(x);
  
  /* copy x and indices to sortx */
  sortx = newvector(2 * n);
  for (i = 0, nextx = x; i < n; i++, nextx = cdr(nextx)) {
    setelement(sortx, 2 * i, car(nextx));
    setelement(sortx, 2 * i + 1, cvfixnum((FIXTYPE) i));
  }
  
  /* sort the data and get the indices */
  qsort(&getelement(sortx, 0), n, 2 * sizeof(LVAL), lcomp);
  
  /* copy the arrays to lists */
  result_x = mklist(n, NIL);
  result_i = mklist(n, NIL);
  for (i = 0, nextx = result_x, nexti = result_i; i < n;
       i++, nextx = cdr(nextx), nexti = cdr(nexti)) {
    rplaca(nextx, getelement(sortx, 2 * i));
    rplaca(nexti, getelement(sortx, 2 * i + 1));
  }
  
  result = list2(result_x, result_i);
  
  /* restore the stack frame */
  xlpopn(5);
  
  return(result);
}

/* Built in SORT-DATA function */
LVAL xssortdata(V) { return(car(lsort())); }

/* Built in ORDER function */
LVAL xsorder(V) { return(car(cdr(lsort()))); }

/* Built in RANK function */
LVAL xsrank(V)
{
  LVAL x, result;
  
  /* create a new stack frame */
  xlstkcheck(2);
  xlsave(x);
  xlsave(result);
  
  x = peekarg(0);
  result = xsorder();
  result = xlcallsubr1(xsorder, result);
  result = makecompound(x, result);
  
  /* restore the stack frame */
  xlpopn(2);
  
  return(result);
}