×

[PR]この広告は3ヶ月以上更新がないため表示されています。
ホームページを更新後24時間以内に表示されなくなります。

sample.c
  1  #include    <stdio.h>
  2  #include    <stdlib.h>
  3  #include    <time.h>
  4  
  5  #include    "IntArray.h"
  6  
  7  
  8  // Number of integers.
  9  #define     N   80
 10  
 11  
 12  // Population count (from Hacker's Delight).
 13  static  unsigned long   PopCount( unsigned long x )
 14  {
 15      x -= ( x >> 1 ) & 0x55555555;
 16      x = ( x & 0x33333333 ) + ( ( x >> 2 ) & 0x33333333 );
 17      x = ( x + ( x >> 4 ) ) & 0x0F0F0F0F;
 18      x += x >> 8;
 19      x += x >> 16;
 20  
 21      return  x & 0x3F;
 22  }
 23  
 24  // Add random integers.
 25  void    TestAdd( hIAry h )
 26  {
 27      IAryIdx     i, j;
 28      IAryInt     y;
 29      unsigned long   x;
 30  
 31      srand( time( NULL ) );
 32  
 33      printf( "Add:\n" );
 34      for ( i = 0; i < N; i += 20 )
 35      {
 36          for ( j = 0; j < 20 && i + j < N; j++ )
 37          {
 38              x = rand();
 39              if ( x >= RAND_MAX / 2 )
 40                  y = 0;
 41              else
 42                  y = PopCount( x ) - 8;
 43  
 44              printf( "%2ld ", y );
 45              IAry_AddInt( h, y );
 46          }
 47          printf( "\n" );
 48      }
 49  
 50      IAry_BuildIndex( h );
 51  }
 52  
 53  // Get integers.
 54  void    TestGet( hIAry h )
 55  {
 56      IAryIdx     i, j;
 57      IAryScan    scan;
 58  
 59      printf( "Get:\n" );
 60      for ( i = 0; i < N; i += 20 )
 61      {
 62          for ( j = 0; j < 20 && i + j < N; j++ )
 63              printf( "%2ld ", IAry_GetInt( h, i + j ) );
 64          printf( "\n" );
 65      }
 66  
 67      printf( "Scan:\n" );
 68      IAry_Start( h, &scan );
 69      for ( i = 0; i < N; i += 20 )
 70      {
 71          for ( j = 0; j < 20 && i + j < N; j++ )
 72              printf( "%2ld ", IAry_NextInt( &scan ) );
 73          printf( "\n" );
 74      }
 75  }
 76  
 77  int     main( int argc, char *argv[] )
 78  {
 79      int     ret;
 80      hIAry   h;
 81      IAryIdx     size;
 82  
 83      // Initialize integer array.
 84      ret = IAry_Open( &h );
 85      fprintf( stderr, "IAry_Open(): %d\n", ret );
 86  
 87      // Add random integers.
 88      TestAdd( h );
 89  
 90      // Get integers.
 91      TestGet( h );
 92  
 93      // Get size.
 94      size = IAry_Size( h );
 95      printf( "Size: %lu bytes (%.2f bytes/integer)\n", size, 1.0 * size / N );
 96  
 97      // Destroy integer array.
 98      IAry_Close( h );
 99  
100      return  0;
101  }