ADT
        aaron-thesis
        arm-eh
        ast-experimental
        cleanup-dtors
        ctor
        deferred_resn
        demangler
        enum
        forall-pointer-decay
        gc_noraii
        jacob/cs343-translation
        jenkins-sandbox
        memory
        new-ast
        new-ast-unique-expr
        new-env
        no_list
        persistent-indexer
        pthread-emulation
        qualifiedEnum
        resolv-new
        string
        with_gc
      
      
        
          | 
            Last change
 on this file since 421edab was             097e2b0, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago           | 
        
        
          | 
             
constructor/destructor, more example programs 
 
           | 
        
        
          
            
              - 
Property                 mode
 set to                 
100644
               
             
           | 
        
        
          | 
            File size:
            760 bytes
           | 
        
      
      
| Line |   | 
|---|
| 1 | extern "C" {
 | 
|---|
| 2 |     int printf( const char *, ... );
 | 
|---|
| 3 | }
 | 
|---|
| 4 | 
 | 
|---|
| 5 | // Insertion sort on a, of length n
 | 
|---|
| 6 | forall( type T | { int ?<?(T, T); } )
 | 
|---|
| 7 | void sort( T *a, unsigned long n ) {
 | 
|---|
| 8 |     if ( n <= 1 ) return;
 | 
|---|
| 9 |         
 | 
|---|
| 10 |     for ( unsigned long i = 1; i < n; i += 1 ) {
 | 
|---|
| 11 |         T x; x = a[i];
 | 
|---|
| 12 |         unsigned long j = i;
 | 
|---|
| 13 |         for ( j; j > 0 && x < a[j-1]; j -= 1 ) {
 | 
|---|
| 14 |             a[j] = a[j - 1];
 | 
|---|
| 15 |         } // for
 | 
|---|
| 16 |         a[j] = x;
 | 
|---|
| 17 |     } // for
 | 
|---|
| 18 | }
 | 
|---|
| 19 | 
 | 
|---|
| 20 | int main(void) {
 | 
|---|
| 21 |     const int size = 4;
 | 
|---|
| 22 |     int a[4] = { 0, 3, -2, 100 };
 | 
|---|
| 23 |     printf( "a:[%d %d %d %d]\n", a[0], a[1], a[2], a[3] );
 | 
|---|
| 24 |         
 | 
|---|
| 25 |     sort( a, size );
 | 
|---|
| 26 |     printf( "a:[%d %d %d %d]\n", a[0], a[1], a[2], a[3] );
 | 
|---|
| 27 |     {
 | 
|---|
| 28 |         // int ?<?(int, int) = ?>?;
 | 
|---|
| 29 |         int ?<?( int a, int b ) { return a > b; }
 | 
|---|
| 30 |         sort( a, size );
 | 
|---|
| 31 |     }
 | 
|---|
| 32 |     printf( "a:[%d %d %d %d]\n", a[0], a[1], a[2], a[3] );
 | 
|---|
| 33 | }
 | 
|---|
| 34 | 
 | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.