File: msort.i

package info (click to toggle)
yorick 1.5.08-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 7,508 kB
  • ctags: 7,937
  • sloc: ansic: 75,604; cpp: 1,282; lisp: 1,217; sh: 1,026; makefile: 616; fortran: 19
file content (78 lines) | stat: -rw-r--r-- 2,572 bytes parent folder | download | duplicates (5)
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
/*
   MSORT.I
   Multiple key sort routine.

   $Id$
 */
/*    Copyright (c) 1994.  The Regents of the University of California.
                    All rights reserved.  */

func msort(x, ..)
/* DOCUMENT msort(x1, x2, x3, ...)
     returns an index list which sorts the array X1 into increasing
     order.  Where X1 values are equal, the list will sort X2 into
     increasing order.  Where both X1 and X2 are equal, X3 will be
     in increasing order, and so on.  Finally, where all of the keys
     are equal, the returned list will leave the order unchanged
     from the input keys.

     The Xi may be numbers or strings (e.g.- X1 could be an integer
     while X2 was a string, and X3 was a real).  The Xi must all be
     conformable, and each dimension of X1 must be as large as the
     corresponding dimension of any otehr Xi.

     Hence, msort(x) will return the same list as sort(x), except
     where the values of x are equal, in which case msort leaves
     the order unchanged, while sort non-deterministically permutes
     equal elements.  This feature may cost a factor of two in speed,
     so don't use it unless you really need it.  In general, msort
     will call sort up to twice per input argument.

   SEE ALSO: sort, msort_rank
 */
{
  mxrank= numberof(x)-1;
  local list;
  rank= msort_rank(x, list);
  if (max(rank)==mxrank) return list;

  norm= 1.0/(mxrank+1.0);
  if (1.0+norm == 1.0) error, pr1(mxrank+1)+" is too large an array";

  n= more_args();
  while (n--) {
    x= next_arg();
    rank+= msort_rank(x)*norm;     /* adjust rank for next key */
    rank= msort_rank(rank, list);  /* renormalize adjusted rank */
    if (max(rank)==mxrank) return list;
  }

  /* use indgen as final key guaranteed to break up any remaining
     equal values */
  return sort(rank+indgen(0:mxrank)*norm);
}

func msort_rank(x, &list)
/* DOCUMENT msort_rank(x)
            msort_rank(x, list)
     returns a list of longs the same size and shape as X, whose
     values are the "rank" of the corresponding element of X among
     all the elements of X -- the smallest element has rank 0 and
     the largest has the largest rank, which is equal to one less
     than the number of distinct values in the array X.

     If LIST is present, it is set to the order list returned by
     sort(x(*)).

   SEE ALSO: msort, sort
 */
{
  rank= array(0, dimsof(x));
  if (numberof(x)<2) return rank;
  void= use_origins(0);
  list= sort(x(*));
  x= x(list);
  x= (x(1:-1)!=x(2:0))(cum);  /* NOT dif -- x may be strings */
  rank(list)= x;
  return rank;
}