                                Source Listing                  24-APR-1996 13:22:48    DEC C T4.1-FT2                      Page   1
                                                                 6-DEC-1995 09:57:25    WORK$:[DAVENPORT.RTL]SORT.C;1

	      1 
	      2 /******************************************************************************
	      3  *
	      4  * Sort.C
	      5  *
	      6  ******************************************************************************/
	      7 
	      8 /******************************************************************************
	      9  *
	     10  * Headers
	     11  *
	     12  ******************************************************************************/
	     13 
	     14 #include <stdio.h>
	    396 
	    397 #include "intrinsic.h"
	    428 #include "sort.h"
	    438 
	    439 /******************************************************************************
	    440  *
	    441  * Local Constants and Macros
	    442  *
	    443  ******************************************************************************/
	    444 
	    445 /******************************************************************************
	    446  *
	    447  * Local Declarations
	    448  *
	    449  ******************************************************************************/
	    450 
	    451 /******************************************************************************
	    452  *
	    453  * Local Prototypes
	    454  *
	    455  ******************************************************************************/
	    456 
	    457 #ifdef VMS
	    458 static int local_strcmp  (String key, String *array, int index);
	    459 static int local_sortcmp (String *array, int index1, int index2);
	    460 static int local_swap    (String *array, int index1, int index2);
	    461 #else
       X    462 static int local_strcmp  ();
       X    463 static int local_sortcmp ();
       X    464 static int local_swap    ();
       X    465 #endif
	    466 
	    467 /******************************************************************************
	    468  *
	    469  * SearchBinary
	    470  *
	    471  ******************************************************************************/
	    472 
	    473 #ifdef VMS
	    474 extern int SearchBinary (Pointer array, int size, String key, IPFN fcn)
	    475 #else
       X    476 extern int SearchBinary (array, size, key, fcn)
       X    477 

                                Source Listing                  24-APR-1996 13:22:48    DEC C T4.1-FT2                      Page   2
                                                                 6-DEC-1995 09:57:25    WORK$:[DAVENPORT.RTL]SORT.C;1

       X    478   Pointer array;
       X    479   int     size;
       X    480   String  key;
       X    481   IPFN    fcn;
       X    482 #endif
	    483 
	    484 {
	    485   int    i,j,k,low,high;
	    486 
	    487   /* check size first */
	    488   if (size < 1) return(-1);
	    489 
	    490   /* default the compare function */
	    491   if (!fcn) fcn = local_strcmp;
	    492 
	    493   low  = 0;  
	    494   high = size - 1;
	    495 
	    496   do {
	    497     i = (low + high) / 2;
	    498     j = (*fcn)(key,array,i);
	    499     if (!j) break;
	    500     else if (j < 0) high = i - 1;
	    501     else low = i + 1;
	    502   } while (low <= high);
	    503 
	    504   if (low <= high) return(i);
	    505   else             return(-1);
	    506 }
	    507 
	    508 /******************************************************************************
	    509  *
	    510  * Sort
	    511  *
	    512  ******************************************************************************/
	    513 
	    514 #ifdef VMS
	    515 extern void Sort (Pointer array, int size, IPFN cmp, IPFN swap)
	    516 #else
       X    517 extern void Sort (array, size, cmp, swap)
       X    518 
       X    519   Pointer array;
       X    520   int     size;
       X    521   IPFN    cmp;
       X    522   IPFN    swap;
       X    523 #endif
	    524 
	    525 {
	    526   int result;
	    527   int index1, index2;
	    528 
	    529   /* default function */
	    530   if (cmp == NULL)  cmp  = local_sortcmp;
	    531   if (swap == NULL) swap = local_swap;
	    532 
	    533   /* sort */
	    534   for (index1 = 0; index1 < (size - 1); index1++) {

                                Source Listing                  24-APR-1996 13:22:48    DEC C T4.1-FT2                      Page   3
                                                                 6-DEC-1995 09:57:25    WORK$:[DAVENPORT.RTL]SORT.C;1

	    535     for (index2 = index1 + 1; index2 < size; index2++) {
	    536       /* compare */
	    537       result = (*cmp)(array,index1,index2);
	    538 
	    539       /* swap if needed */
	    540       if (result > 0) (*swap)(array,index1,index2);
	    541     }
	    542   }
	    543 
	    544   /* done */
	    545   return;
	    546 }
	    547 
	    548 /******************************************************************************
	    549  *
	    550  * local_strcmp
	    551  *
	    552  ******************************************************************************/
	    553 
	    554 #ifdef VMS
	    555 static int local_strcmp (String key, String *array, int index)
	    556 #else
       X    557 static int local_strcmp (key, array, index)
       X    558 
       X    559   String  key;
       X    560   String *array;
       X    561   int     index;
       X    562 #endif
	    563 
	    564 {
	    565   /* return the result */
	    566   return (strcmp(key,array[index]));
	    567 }
	    568 
	    569 /******************************************************************************
	    570  *
	    571  * local_sortcmp
	    572  *
	    573  ******************************************************************************/
	    574 
	    575 #ifdef VMS
	    576 static int local_sortcmp (String *array, int index1, int index2)
	    577 #else
       X    578 static int local_sortcmp (array, index1, index2)
       X    579 
       X    580   String *array;
       X    581   int     index1;
       X    582   int     index2;
       X    583 #endif
	    584 
	    585 {
	    586   /* return the result */
	    587   return (strcmp(array[index1],array[index2]));
	    588 }
	    589 
	    590 /******************************************************************************
	    591  *

                                Source Listing                  24-APR-1996 13:22:48    DEC C T4.1-FT2                      Page   4
                                                                 6-DEC-1995 09:57:25    WORK$:[DAVENPORT.RTL]SORT.C;1

	    592  * local_swap
	    593  *
	    594  ******************************************************************************/
	    595 
	    596 #ifdef VMS
	    597 static int local_swap (String *array, int index1, int index2)
	    598 #else
       X    599 static int local_swap (array, index1, index2)
       X    600 
       X    601   String *array;
       X    602   int     index1;
       X    603   int     index2;
       X    604 #endif
	    605 
	    606 {
	    607   String temp;
	    608 
	    609   /* swap */
	    610   temp          = array[index1];
	    611   array[index1] = array[index2];
	    612   array[index2] = temp;
	    613 
	    614   /* done */
	    615   return (1);
	    616 }


Command Line
------- ----

CC /NOANSI_ALIAS/ASSUME=(ACCURACY_SENSITIVE,ALIGNED_OBJECTS,WRITABLE_STRING_LITERALS)
/DEBUG=(SYMBOLS,TRACEBACK)/ENDIAN=LITTLE/EXTERN_MODEL=RELAXED_REFDEF/FLOAT=(
G_FLOAT)/GRANULARITY=QUADWORD/INSTRUCTION_SET=FLOATING_POINT/L_DOUBLE_SIZE=128
/LIST/NOMACHINE_CODE/MEMBER_ALIGNMENT/NAMES=UPPERCASE/NESTED_INCLUDE_DIRECTORY=INCLUDE_FILE
/OBJECT/NOOPTIMIZE/PREFIX=(ALL_ENTRIES)/PSECT_MODEL=NOMULTILANGUAGE/ROUNDING_MODE=NEAREST
/SHOW=(HEADER,SOURCE)/SIGNED_CHAR/STANDARD=VAXC/REENTRANCY=TOLERANT/WARNINGS
