Ignore:
Timestamp:
Jan 4, 2024, 12:12:08 PM (5 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
cfbc56ec, f0c9c9b
Parents:
ca2feff
Message:

update command-line processing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/concurrency/examples/quickSort.cfa

    rca2feff r40002c5  
    1111// Created On       : Wed Dec  6 12:15:52 2017
    1212// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Wed Feb 12 18:24:47 2020
    14 // Update Count     : 177
    15 //
    16 
    17 #include <fstream.hfa>
    18 #include <stdlib.hfa>
    19 #include <kernel.hfa>
     13// Last Modified On : Mon Jan  1 12:07:59 2024
     14// Update Count     : 188
     15//
     16
     17#include <fstream.hfa>                                                                  // sin/sout
     18#include <stdlib.hfa>                                                                   // convert
    2019#include <thread.hfa>
     20#include <math.hfa>                                                                             // sqrt
    2121#include <string.h>                                                                             // strcmp
    2222
     
    8282} // main
    8383
    84 
    85 bool convert( int & val, const char * nptr ) {                  // convert C string to integer
    86         char * eptr;
    87         int temp = strto( nptr, &eptr, 10 );                            // do not change val on false
    88         // true => entire string valid with no extra characters
    89         return *nptr != '\0' && *eptr == '\0' ? val = temp, true : false;
    90 } // convert
    91 
    92 void usage( char * argv[] ) {
    93         sout | "Usage:" | argv[0] | "( -s unsorted-file [ sorted-file ] | -t size (>= 0) [ depth (>= 0) ] )";
    94         exit( EXIT_FAILURE );                                                           // TERMINATE!
    95 } // usage
    96 
     84// convert(...) throws out_of_range or invalid_argument
     85ExceptionDecl( cmd_error );
    9786
    9887int main( int argc, char * argv[] ) {
    99         ifstream & unsortedfile = sin;
    100         ofstream & sortedfile = sout;                                           // default value
    101         int depth = 0, size;
    102 
    103         if ( argc != 1 ) {                                                                      // do not use defaults
    104                 if ( argc < 2 || argc > 4 ) usage( argv );              // wrong number of options
    105                 if ( strcmp( argv[1], "-t" ) == 0 ) {                   // timing ?
    106                         &unsortedfile = (ifstream *)0;                          // no input
     88        ifstream unsortedfile = sin;                                            // default values
     89        ofstream sortedfile = sout;
     90        // Must be signed because of the conversion routine.
     91        intmax_t depth = 0;
     92        intmax_t size = -1;                                                                     // -1 means time mode not activated
     93
     94        try {
     95                if ( 1 < argc && strcmp( argv[1], "-t" ) == 0 ) { // time mode ?
    10796                        choose ( argc ) {
    10897                          case 4:
    109                                 if ( ! convert( depth, argv[3] ) || depth < 0 ) usage( argv );
     98                                depth = convert( argv[3] );                             // invalid integer ?
     99                                if ( depth < 0 ) throw ExceptionInst( cmd_error );
    110100                                fallthrough;
    111101                          case 3:
    112                                 if ( ! convert( size, argv[2] ) || size < 0 ) usage( argv );
     102                                size = convert( argv[2] );                              // invalid integer ?
     103                                if ( size < 0 ) throw ExceptionInst( cmd_error );
     104                          default:                                                                      // wrong number of options
     105                                throw ExceptionInst( cmd_error );
    113106                        } // choose
    114107                } else {                                                                                // sort file
    115108                        choose ( argc ) {
    116                           case 3:
    117                                 &sortedfile = new( (const char *)argv[2] ); // open the output file
    118                                 if ( fail( sortedfile ) ) {
    119                                         serr | "Error! Could not open sorted output file \"" | argv[2] | "\"";
    120                                         usage( argv );
     109                          case 4:
     110                                depth = convert( argv[3] );                             // invalid integer ?
     111                                if ( depth < 0 ) throw ExceptionInst( cmd_error );
     112                                fallthrough;
     113                          case 3: case 2:
     114                                if ( strcmp( argv[1], "d" ) != 0 ) {
     115                                        try {                                                           // open input file first as output creates file
     116                                                open( unsortedfile, argv[1] );
     117                                        } catch( open_failure * ) {                     // open failed ?
     118                                                serr | "Error! Could not open unsorted input file \"" | argv[1] | "\"";
     119                                                throw ExceptionInst( cmd_error );
     120                                        } // try
     121                                } // if
     122                                if ( argc > 2 && strcmp( argv[2], "d" ) != 0 ) {
     123                                        try {
     124                                                open( sortedfile, argv[2] );
     125                                        } catch( open_failure * ) {                     // open failed ?
     126                                                serr | "Error! Could not open sorted output file \"" | argv[2] | "\"";
     127                                                throw ExceptionInst( cmd_error );
     128                                        } // try
    121129                                } // if
    122130                                fallthrough;
    123                           case 2:
    124                                 &unsortedfile = new( (const char *)argv[1] ); // open the input file
    125                                 if ( fail( unsortedfile ) ) {
    126                                         serr | "Error! Could not open unsorted input file \"" | argv[1] | "\"";
    127                                         usage( argv );
    128                                 } // if
     131                          case 1: ;                                                                     // defaults
     132                          default:                                                                      // wrong number of options
     133                                throw ExceptionInst( cmd_error );
    129134                        } // choose
    130135                } // if
    131         } // if
     136        } catch( exception_t * ) {                                                      // catch any
     137                exit | "Usage: " | argv[0] |                                    // TERMINATE
     138                        " ( [ unsorted-file | 'd' [ sorted-file | 'd' [ depth (>= 0) ] ] ]"
     139                        " | -t size (>= 0) [ depth (>= 0) ] )";
     140        } //  try
     141
     142        enum { ValuesPerLine = 22 };                                            // number of values printed per line
     143
    132144        sortedfile | nlOff;                                                                     // turn off auto newline
    133145
    134         enum { ValuesPerLine = 22 };                                            // number of values printed per line
    135 
    136         if ( &unsortedfile ) {                                                          // generate output ?
     146        if ( size == -1 ) {                                                                     // generate output ?
    137147                for () {
    138148                        unsortedfile | size;                                            // read number of elements in the list
    139149                  if ( eof( unsortedfile ) ) break;
    140                         int * values = alloc( size );                           // values to be sorted, too large to put on stack
     150
     151                        int * values = aalloc( size );                          // values to be sorted, too large to put on stack
    141152                        for ( counter; size ) {                                         // read unsorted numbers
    142153                                unsortedfile | values[counter];
     
    146157                        } // for
    147158                        sortedfile | nl;
     159
    148160                        if ( size > 0 ) {                                                       // values to sort ?
    149161                                Quicksort QS = { values, size - 1, 0 }; // sort values
     
    158170                        delete( values );
    159171                } // for
    160                 if ( &unsortedfile != &sin ) delete( &unsortedfile ); // close input/output files
    161                 if ( &sortedfile != &sout ) delete( &sortedfile );
    162         } else {
     172        } else {                                                                                        // timing
     173                PRNG prng;                                                                             
    163174                processor processors[ (1 << depth) - 1 ] __attribute__(( unused )); // create 2^depth-1 kernel threads
    164 
    165                 int * values = alloc( size );                                   // values to be sorted, too large to put on stack
     175                int * values = aalloc( size );                                  // values to be sorted, too large to put on stack
     176
    166177                for ( counter; size ) {                                                 // generate unsorted numbers
    167178                        values[counter] = size - counter;                       // descending values
    168179                } // for
    169                 for ( i; 200 ) {                                                                // random shuffle a few values
    170                         swap( values[rand() % size], values[rand() % size] );
     180
     181                unsigned int times = sqrt( size );
     182                for ( unsigned int counter = 0; counter < times; counter += 1 ) {
     183                        swap( values[0], values[prng(size)] );          // randomize unsorted numbers
    171184                } // for
    172185                {
Note: See TracChangeset for help on using the changeset viewer.