!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
!     isort.h                           Sorting functions
!       V 1.0                           L. Ross Raszewski
!
! This is a fairly simple library that implements quicksort
! functionality for arrays in inform.
!  sortwords(array,begin,end,func);
! sorts a word array from -->begin to -->(end-1)
! func(x,y) should return > 0 if x should come BEFORE y,
!                         < 0 if x should come AFTER y,
!                         ==0 if the order of x and y doesn't matter.
!
! sortbytes provides similar functionality for byte arrays.
!
! e-mail me at [email protected]

[ sortwords arr begin end func i j temp;
 i=begin+1;
 j=end-1;
 if ((end-begin)<=1) return;
 while(i<j)
 {
  while(i<j && func(arr-->j,arr-->begin)<0) {
   j--;
   }
  while(i<j && func(arr-->i,arr-->begin)>0) {
   i++;
  }
  if (i<j)
  {
   temp=arr-->i;
   arr-->i=arr-->j;
   arr-->j=temp;
  }
 }
 if (func(arr-->begin,arr-->i)<0) {
 temp=arr-->begin;
 arr-->begin=arr-->i;
 arr-->i=temp;
 j++;
 }
 sortwords(arr,begin,i,func);
 sortwords(arr,j,end,func);
];

[ sortbytes arr begin end func i j temp;
 i=begin+1;
 j=end-1;
 if ((end-begin)<=1) return;
 while(i<j)
 {
  while(i<j && func(arr->j,arr->begin)<0) {
   j--;
   }
  while(i<j && func(arr->i,arr->begin)>0) {
   i++;
  }
  if (i<j)
  {
   temp=arr->i;
   arr->i=arr->j;
   arr->j=temp;
  }
 }
 if (func(arr->begin,arr->i)<0) {
 temp=arr->begin;
 arr->begin=arr->i;
 arr->i=temp;
 j++;
 }
 sortbytes(arr,begin,i,func);
 sortbytes(arr,j,end,func);
];