×

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

sample.c
  1  #include    <stdio.h>
  2  #include    <time.h>
  3  
  4  #include    "tinyda.h"
  5  
  6  
  7  // Callback function to enumerate pairs of a key and a record.
  8  int     EnumCall( const char *key, void *rec, void *arg )
  9  {
 10      // If a record is zero, finish the enumeration.
 11      if ( *( int * )rec )
 12      {
 13          printf( "%d\t%s\n", *( int * )rec, key );
 14          return  *( int * )rec;
 15      }
 16  
 17      return  DA_OK;
 18  }
 19  
 20  int     main( int argc, char *argv[] )
 21  {
 22      int     i, ret;
 23      int     *val, idx;
 24      DAStat  stat;
 25      hDASet  set;
 26      hDADic  dic;
 27      clock_t     cl;
 28      const char  *s, *start, *input = "motor";
 29  
 30      // Create an empty keyset.
 31      if ( ( ret = DASet_Open( &set ) ) != DA_OK )
 32          fprintf( stderr, "DASet_Open(): %d\n", ret );
 33  
 34      // Load keysets and use tabs, CRs and LFs as delimitors.
 35      cl = clock();
 36      for ( i = 1; i < argc; i++ )
 37      {
 38          ret = DASet_Load( set, argv[i], "\t\r\n", sizeof( int ) );
 39          if ( ret != DA_OK )
 40              fprintf( stderr, "DADic_Load(): %s: %d\n", argv[i], ret );
 41      }
 42      cl = clock() - cl;
 43      printf( "Load: %.3f sec\n", 1.0 * cl / CLOCKS_PER_SEC );
 44  
 45      // Add keys to a keyset.
 46  //  if ( ( ret = DASet_Insert( set, "Tes", sizeof( int ) ) ) != DA_OK )
 47  //      fprintf( stderr, "DADic_Insert(): %d\n", ret );
 48  //  if ( ( ret = DASet_Insert( set, "Test", sizeof( int ) ) ) != DA_OK )
 49  //      fprintf( stderr, "DADic_Insert(): %d\n", ret );
 50  //  if ( ( ret = DASet_Insert( set, "Test-Key", sizeof( int ) ) ) != DA_OK )
 51  //      fprintf( stderr, "DADic_Insert(): %d\n", ret );
 52  
 53      // Build a dictionary.
 54      cl = clock();
 55      if ( ( ret = DADic_Build( &dic, set, sizeof( int ) ) ) != DA_OK )
 56          fprintf( stderr, "DADic_Build(): %d\n", ret );
 57      cl = clock() - cl;
 58      printf( "Build: %.3f sec\n", 1.0 * cl / CLOCKS_PER_SEC );
 59  
 60      // Destroy a keyset.
 61      if ( ( ret = DASet_Close( set ) ) != DA_OK )
 62          fprintf( stderr, "DASet_Close(): %d\n", ret );
 63  
 64      // Output dictionary information.
 65      if ( ( ret = DADic_Status( dic, &stat ) ) != DA_OK )
 66          fprintf( stderr, "DADic_Stat(): %d\n", ret );
 67      printf( "Size: 4 * %d + %d = %d\n", stat.nbc, stat.ntail, stat.size );
 68      printf( "Key: %d\n", stat.nkey );
 69      printf( "Max-len: %d\n", stat.maxlen );
 70      printf( "Boundary: %d\n", stat.boundary );
 71  
 72      // Search a key and set a record.
 73      if ( ( val = DADic_Search( dic, "pen" ) ) == NULL )
 74          fprintf( stderr, "DADic_Search(): Not found\n" );
 75      else
 76          *val = 100;
 77  
 78      // Output, destroy and input a dictionary.
 79      if ( ( ret = DADic_Save( dic, "test.dic" ) ) != DA_OK )
 80          fprintf( stderr, "DADic_Save(): %d\n", ret );
 81      if ( ( ret = DADic_Close( dic ) ) != DA_OK )
 82          fprintf( stderr, "DADic_Close(): %d\n", ret );
 83      if ( ( ret = DADic_Open( &dic, "test.dic" ) ) != DA_OK )
 84          fprintf( stderr, "DADic_Open(): %d\n", ret );
 85  
 86      // Prefix search.
 87      idx = 0;
 88      start = s = input;
 89      while ( ( val = DADic_Prefix( dic, &s, &idx ) ) != NULL )
 90          printf( "DADic_Prefix(): [%.*s]%s: %d\n", s - start, start, s, *val );
 91  
 92      // Infix search.
 93      idx = 0;
 94      start = s = input;
 95      while ( ( val = DADic_Infix( dic, &start, &s, &idx ) ) != NULL )
 96          printf( "DADic_Infix(): %.*s[%.*s]%s: %d\n",
 97                  start - input, input, s - start, start, s, *val );
 98  
 99      // Enumerate registered keys.
100      cl = clock();
101      if ( ( ret = DADic_Enum( dic, NULL, EnumCall ) ) != DA_OK )
102          fprintf( stderr, "DADic_Enum(): %d\n", ret );
103      cl = clock() - cl;
104      printf( "Enum: %.3f sec\n", 1.0 * cl / CLOCKS_PER_SEC );
105  
106      // Destroy a dictionary.
107      if ( ( ret = DADic_Close( dic ) ) != DA_OK )
108          fprintf( stderr, "DADic_Close(): %d\n", ret );
109  
110      return  0;
111  }