dic_test.c
上传用户:gzelex
上传日期:2007-01-07
资源大小:707k
文件大小:4k
开发平台:

MultiPlatform

  1. #include <LEDA/_dictionary.h>
  2. #include <LEDA/impl/ch_hash.h>
  3. #include <LEDA/impl/dp_hash.h>
  4. #include <LEDA/impl/avl_tree.h>
  5. #include <LEDA/impl/bin_tree.h>
  6. #include <LEDA/impl/rs_tree.h>
  7. #include <LEDA/impl/rb_tree.h>
  8. #include <LEDA/impl/skiplist.h>
  9. #include <LEDA/impl/ab_tree.h>
  10. #include <LEDA/impl/bb_tree.h>
  11. void dic_test(dictionary<int,int>& D, int N, int* A, char* name)
  12.   cout << string("%-12s",name);
  13.   cout.flush();
  14.   float T;
  15.   float T0 = T = used_time();
  16.   int i;
  17.   for(i=0; i<N; i++)  D.insert(A[i],0);
  18.   cout << string("%10.2f",used_time(T));
  19.   cout.flush();
  20.   bool ok = true;
  21.   for(i=0; i<N; i++)  
  22.   { dic_item it = D.lookup(A[i]);
  23.     if (it == nil || D.key(it) != A[i])  ok = false;
  24.    }
  25.   if (!ok) error_handler(0,"errors in lookups");
  26.   cout << string("%10.2f",used_time(T));
  27.   cout.flush();
  28.   for(i=0; i<N; i++)  D.del(A[i]);
  29.   cout << string("%10.2f",used_time(T));
  30.   cout << string("%10.2f",used_time(T0));
  31.   if (!D.empty())
  32.   {  cout << " NOT EMPTY ";
  33.      cout << "size = " << D.size() << endl;
  34.    }
  35.   
  36.   newline;
  37.   memory_clear();
  38. }
  39. void dic_test(dictionary<float,float>& D, int N, float* A, char* name)
  40.   cout << string("%-12s",name);
  41.   cout.flush();
  42.   D.clear();
  43.   float T;
  44.   float T0 = T = used_time();
  45.   int i;
  46.   for(i=0; i<N; i++)  D.insert(A[i],0);
  47.   cout << string("%10.2f",used_time(T));
  48.   cout.flush();
  49.   for(i=0; i<N; i++)  D.lookup(A[i]);
  50.   cout << string("%10.2f",used_time(T));
  51.   cout.flush();
  52.   for(i=0; i<N; i++)  D.del(A[i]);
  53.   cout << string("%10.2f",used_time(T));
  54.   cout << string("%10.2f",used_time(T0));
  55.   newline;
  56.   memory_clear();
  57. }
  58. main()
  59. {
  60.   _dictionary<int,int,ch_hash> CHH_DIC;
  61.   _dictionary<int,int,dp_hash> DPH_DIC;
  62.   _dictionary<int,int,avl_tree> AVL_DIC;
  63.   _dictionary<int,int,bin_tree> BIN_DIC;
  64.   _dictionary<int,int,rb_tree>  RB_DIC;
  65.   _dictionary<int,int,rs_tree>  RS_DIC;
  66.   _dictionary<int,int,skiplist> SK_DIC;
  67.   _dictionary<int,int,bb_tree>  BB_DIC;
  68.   _dictionary<int,int,ab_tree>  AB_DIC;
  69.   _dictionary<float,float,avl_tree> AVL_DIC1;
  70.   _dictionary<float,float,bin_tree> BIN_DIC1;
  71.   _dictionary<float,float,rb_tree>  RB_DIC1;
  72.   _dictionary<float,float,rs_tree>  RS_DIC1;
  73.   _dictionary<float,float,skiplist> SK_DIC1;
  74.   _dictionary<float,float,bb_tree>  BB_DIC1;
  75.   _dictionary<float,float,ab_tree>  AB_DIC1;
  76.   int    N     = read_int("# keys = ");
  77.   int*   Int   = new int[N];
  78.   int*   Int1  = new int[N];
  79.   float* Float = new float[N];
  80.   int i;
  81.   for(i=0; i<N; i++) Float[i] = Int[i] = rand_int(0,10000000);
  82.   for(i=0; i<N; i++) Int1[i] = i;
  83.   newline;
  84.   cout << "                insert    lookup    delete     totaln";
  85.   newline;
  86.   dic_test(CHH_DIC,N,Int,"ch_hash");
  87. //dic_test(DPH_DIC,N,Int,"dp_hash");
  88.   newline;
  89.   dic_test(SK_DIC,N,Int,"skiplist");
  90.   dic_test(RS_DIC,N,Int,"rs_tree");
  91.   dic_test(BIN_DIC,N,Int,"bin_tree");
  92.   dic_test(RB_DIC,N,Int,"rb_tree");
  93.   dic_test(AVL_DIC,N,Int,"avl_tree");
  94.   dic_test(BB_DIC,N,Int,"bb_tree");
  95.   dic_test(AB_DIC,N,Int,"ab_tree");
  96.   newline;
  97. //dic_test(DPH_DIC,N,Int1,"dp_hash");
  98.   newline;
  99.   dic_test(SK_DIC,N,Int1,"skiplist");
  100.   dic_test(RS_DIC,N,Int1,"rs_tree");
  101.   //dic_test(BIN_DIC,N,Int1,"bin_tree");
  102.   dic_test(RB_DIC,N,Int1,"rb_tree");
  103.   dic_test(AVL_DIC,N,Int1,"avl_tree");
  104.   dic_test(BB_DIC,N,Int1,"bb_tree");
  105.   dic_test(AB_DIC,N,Int1,"ab_tree");
  106.   newline;
  107.   dic_test(RB_DIC1,N,Float,"rb_tree");
  108.   dic_test(AVL_DIC1,N,Float,"avl_tree");
  109.   dic_test(SK_DIC1,N,Float,"skiplist");
  110.   dic_test(RS_DIC1,N,Float,"rs_tree");
  111.   dic_test(BIN_DIC1,N,Float,"bin_tree");
  112.   dic_test(BB_DIC1,N,Float,"bb_tree");
  113.   dic_test(AB_DIC1,N,Float,"ab_tree");
  114.   newline;
  115.  
  116.   return 0;
  117. }