コード例 #1
0
ファイル: benchtree.cpp プロジェクト: goldslime88/E3jDUhELkE
/* Get average compares over number of nodes.
   * Input
   *    numOfNodes - number of the nodes in a data structure.
   *   	typeOfContainer - type of data structure: bst, rst, set.
   *    orderOfInsertion - insert sorted numbers or shuffled numbers.
   *Outout
   *	Average compares of find operation over number of nodes.
   */ 
double avgcomps(int numOfNodes,string typeOfContainer,string orderOfInsertion){
	double avgcomps;
	vector<countint> v;
	v.clear();
	for(int i=0;i < numOfNodes;++ i) {
  		v.push_back(i);
	}

	//Is it sorted or shuffled?
	if(orderOfInsertion == "shuffled"){
		std::random_shuffle ( v.begin(), v.end(), myrandom);
	}

	std::vector<countint>::iterator vit = v.begin();
 	std::vector<countint>::iterator ven = v.end();

	//The type of the containner is rst
	if(typeOfContainer == "rst"){
		RST<countint> s;
		for(vit = v.begin(); vit != ven; ++vit) {
    		s.insert(*vit);
 		}
 		countint::clearcount();
		for(vit = v.begin(); vit != ven; ++vit) {
  			s.find(*vit);
		}
		avgcomps = countint::getcount()/(double)numOfNodes;
	}

	//The type of the containner is bst
	if(typeOfContainer == "bst"){
		BST<countint> s;
		for(vit = v.begin(); vit != ven; ++vit) {
    		s.insert(*vit);
 		}
 		countint::clearcount();
		for(vit = v.begin(); vit != ven; ++vit) {
  			s.find(*vit);
		}
		avgcomps = countint::getcount()/(double)numOfNodes;
	}

	//The type of the containner is set
	if(typeOfContainer == "set"){
		std::set<countint> s;	
		for(vit = v.begin(); vit != ven; ++vit) {
    		s.insert(*vit);
 		}
 		countint::clearcount();
		for(vit = v.begin(); vit != ven; ++vit) {
  			s.find(*vit);
		}
		avgcomps = countint::getcount()/(double)numOfNodes;
	}
	return avgcomps;
}
コード例 #2
0
ファイル: benchtree.cpp プロジェクト: ElliotY/cse100
// creates and populates the appropriate data structure
// returns the avg number of comparisons for a single run
double single(int n, bool sorted, std::string type)
{
	std::vector<countint> v;
	for (int i = 0; i < n; ++i) {
		v.push_back(i);
	}

	if (!sorted) {
		std::random_shuffle(v.begin(), v.end());
	}

	/*
	std::cout << std::endl;
	std::cout << "INSERTING VALUES: " << std::endl;

	for (size_t i = 0; i < v.size(); ++i) {
		std::cout << v[i] << std::endl;
		std::cout << std::endl;
	}
	*/

	// lots of copy and pasting here because of scoping issues
	if (type == "set") {
		std::set<countint> t;
		for (size_t i = 0; i < v.size(); ++i) {
			t.insert(v[i]);
		}
		countint::clearcount();
		for (size_t i = 0; i < v.size(); ++i) {
			t.find(v[i]);
		}
	} else if (type == "bst") {
		BST<countint> t;
		for (size_t i = 0; i < v.size(); ++i) {
			t.insert(v[i]);
		}
		countint::clearcount();
		for (size_t i = 0; i < v.size(); ++i) {
			t.find(v[i]);
		}
	} else {
		RST<countint> t;
		for (size_t i = 0; i < v.size(); ++i) {
			t.insert(v[i]);
		}
		countint::clearcount();
		for (size_t i = 0; i < v.size(); ++i) {
			t.find(v[i]);
		}
	}

	return countint::getcount() / (double)n;
}
コード例 #3
0
int test_RST_rotate(int N) {
  
  cout << endl << "### Testing RST rotateLeft and rotateRight functionality ..." << endl << endl;
  
  /* Create an STL vector of some countints, in sorted order */
  vector<countint> v;
  vector<countint> v_sorted;
  for(int i=0; i<N; i++) {
    //    v.push_back(i);
    v.push_back(i);
    v_sorted.push_back(i);
  }
  
  // Create a reproducible psuedo-random insertion order
  srand ( unsigned ( 149 ) );
  std::random_shuffle ( v.begin(), v.end(), myrandom);

  /* Create an empty instance of RST holding countint */
  RST<countint> r = RST<countint>();

  /* Insert the data items, in order */
  cout << "Inserting " << N << " random keys in initially empty RST...";
  vector<countint>::iterator vit = v.begin();
  vector<countint>::iterator ven = v.end();
  for(; vit != ven; ++vit) {
    // all these inserts are unique, so should return true
    if(! r.BSTinsert(*vit) ) {
      cout << endl << "Incorrect return value when inserting " << *vit << endl;
      return -1;
    }
  }
  cout << " done." << endl;
  
  cout << "Rotating an arbitrary node with rotateLeft... ";
  vit = v.begin();
  ven = v.end();
  for(; vit != ven; ++vit) {
    int ret = r.findAndRotate(*vit, true);
    if (ret == 0)
    {
      break;
    } else if (ret < 0)
    {
      cout << endl << "Failed to rotate node " << *vit << endl;
      return -1;
    }
  }
  
  //  r.inorder();
  
  /* Test iterator; should iterate the entire tree inorder */
  cout << "Checking traversal using iterator...";
  vit = v_sorted.begin();
  BST<countint>::iterator en = r.end();
  BST<countint>::iterator it = r.begin();
  int i = 0;
  for(; it != en; ++it) {
    //    cout << *it << endl;
    if(*it != *vit) {
      cout << endl << "Incorrect inorder iteration of RST." << endl;
      return -1;
    }
    ++i;
    ++vit;
  }
  if(i!=N) {
    cout << endl << "Early termination during inorder iteration of RST." << endl;
    return -1;
  }
  cout << " OK." << endl;
  
  cout << "Rotating an arbitrary node with rotateRight... ";
  vit = v.begin();
  ven = v.end();
  for(; vit != ven; ++vit) {
    int ret = r.findAndRotate(*vit, false);
    if (ret == 0)
    {
      break;
    } else if (ret < 0)
    {
      cout << endl << "Failed to rotate node " << *vit << endl;
      return -1;
    }
  }
  
  //  r.inorder();
  
  /* Test iterator; should iterate the entire tree inorder */
  cout << "Checking traversal using iterator...";
  vit = v_sorted.begin();
  en = r.end();
  it = r.begin();
  i = 0;
  for(; it != en; ++it) {
    //    cout << *it << endl;
    if(*it != *vit) {
      cout << endl << "Incorrect inorder iteration of RST." << endl;
      return -1;
    }
    ++i;
    ++vit;
  }
  if(i!=N) {
    cout << endl << "Early termination during inorder iteration of RST." << endl;
    return -1;
  }
  cout << " OK." << endl;

  cout << endl << "### ROTATION TESTS PASSED ####" << endl << endl;
  
  return 0;
}
コード例 #4
0
int test_RST_insert(int N) {

  cout << "### Testing RST insert functionality ..." << endl << endl;
  
  double  maxcompsperkey =  (log(N) * 2.5); 

  /* Create an STL vector of some countints, in sorted order */
  vector<countint> v;
  for(int i=0; i<N; i++) {
    //    v.push_back(i);
    v.push_back(i);
  }

  /* Create an empty instance of RST holding countint */
  RST<countint> r = RST<countint>();

  /* Clear the comparison counter */
  countint::clearcount();

  /* Insert the data items, in order */
  cout << "Inserting " << N << " sorted keys in initially empty RST...";
  vector<countint>::iterator vit = v.begin();
  vector<countint>::iterator ven = v.end();
  for(; vit != ven; ++vit) {
    // all these inserts are unique, so should return true
    if(! r.insert(*vit) ) {
      cout << endl << "Incorrect return value when inserting " << *vit << endl;
      return -1;
    }
  }
  cout << " done." << endl;
  
  //  r.inorder();
  
  /* Test iterator; should iterate the entire tree inorder */
  cout << "Checking traversal using iterator...";
  vit = v.begin();
  BST<countint>::iterator en = r.end();
  BST<countint>::iterator it = r.begin();
  int i = 0;
  for(; it != en; ++it) {
    //    cout << *it << endl;
    if(*it != *vit) {
      cout << endl << "Incorrect inorder iteration of RST." << endl;
      return -1;
    }
    ++i;
    ++vit;
  }
  if(i!=N) {
    cout << endl << "Early termination during inorder iteration of RST." << endl;
    return -1;
  }
  cout << " OK." << endl;

  /* How many comparisons did it take to do the inserts, avg per key? */
  double compsperkey =  countint::getcount() / (double) N;
  cout << "That took " << compsperkey << " average comparisons per key, ";
  if(compsperkey <= maxcompsperkey) cout << "OK. " << endl;
  else if (compsperkey <= maxcompsperkey * 2) cout << "could be better... max is " << maxcompsperkey << endl;
  else {
    cout << "way too many!" << endl;
    return -1;
  }

  /* Test iterator; should iterate the entire tree inorder */
  cout << "Checking traversal using iterator...";
  vit = v.begin();
  en = r.end();
  it = r.begin();
  i = 0;
  for(; it != en; ++it) {
    //    cout << *it << endl;
    if(*it != *vit) {
      cout << endl << "Incorrect inorder iteration of RST." << endl;
      return -1;
    }
    ++i;
    ++vit;
  }
  if(i!=N) {
    cout << endl << "Early termination during inorder iteration of RST." << endl;
    return -1;
  }
  cout << " OK." << endl;
  
  cout << endl << "### INSERTION TESTS PASSED ####" << endl << endl;
  
  return 0;
}
コード例 #5
0
ファイル: test_BSTRST.cpp プロジェクト: Elblonko/CSE100-RST
/**
 * A simple test driver for the BST class template.
 * P1 CSE 100 2013
 * Author: P. Kube (c) 2013
 */
int main() {

  /* Create an STL vector of some ints */
  vector<int> v;
  /*
  v.push_back(3);
  v.push_back(4);
  v.push_back(1);
  v.push_back(100);
  v.push_back(-33);
  v.push_back(-20);
  v.push_back(6);
  v.push_back(0);
  v.push_back(-1);
  v.push_back(-2);
  v.push_back(-3);
  v.push_back(-4);
  */

  /* Create an instance of BST holding int */
  BST<int> b;

  /* Insert a few data items. */
  vector<int>::iterator vit = v.begin();
  vector<int>::iterator ven = v.end();
  for(; vit != ven; ++vit) {
    // all these inserts are unique, so should return a std::pair
    // with second part true
    std::pair<BST<int>::iterator,bool> pr = b.insert(*vit);
    if(! pr.second ) {
      cout << "Incorrect bool return value when inserting " << *vit << endl;
      return -1;
    }
    if(*(pr.first) != *vit) {
      cout << "Incorrect iterator return value when inserting " << *vit << endl;
      return -1;
    }  
  }

  /* Test size. */
  cout << "Size is: " << b.size() << endl;
  if(b.size() != v.size()) {
    cout << "... which is incorrect." << endl;
    return -1;
  }

  /* Test find return value. */
  vit = v.begin();
  for(; vit != ven; ++vit) { 
    if(*(b.find(*vit)) != *vit) {
      cout << "Incorrect return value when finding " << *vit << endl;
      return -1;
    }
  }
  
  /* Sort the vector, to compare with inorder iteration on the BST */
  sort(v.begin(),v.end());

  /* Test BST iterator; should iterate inorder */
  cout << "traversal using iterator:" << endl;
  vit = v.begin();
  BST<int>::iterator en = b.end();
  BST<int>::iterator it = b.begin();
  for(; vit < ven; ++vit) {
    if(! (it != en) ) {
      cout << *it << "," << *vit << ": Early termination of BST iteration." << endl;
      return -1;
    }
    cout << *it << endl;
    if(*it != *vit) {
      cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
      return -1;
    }
    ++it;
  }
  /** Added a basic clear tests that checks for correct size after delete */
  cout << "OK." << endl;
  b.clear();
  cout << ((b.size() == 0) 
          ? "PASS: clear() size of tree: "
          : "FAIL: clear() Size of tree: ") << b.size()  << endl;
  

  /** ================================ Added test Cases =====================*/ 

  /* Test fixtures */
  vector<int> v2;
  srand(unsigned(time(NULL)));
  BST<int> b2;
  BST<int>* treeDestructorTest = new BST<int>();
  unsigned int numInspected = 0;
  unsigned int sizeOfInput = 0;

  for(int i = 1000; i >= -1000; i--){
    v2.push_back(i);
    sizeOfInput++;
  }

  /* randomize the ordering of -1000 - 1000 before inserting 
   * so we don't just test a linked list
   */
  std::random_shuffle(v2.begin(), v2.end());

  vector<int>::iterator vit2 = v2.begin();
  vector<int>::iterator ven2 = v2.end();

  /* insert all the items in the randomized vector into the bst 
   * and check the boolean return
   */
  while( vit2 != ven2 ){
    if( !(b2.insert(*vit2)).second ){
      cout << "incorrect boolean return value of insert for: "<< *vit2 <<endl;
      return -1;
    }
    ++vit2;    
  }

  cout << "PASS: insert() unique value, boolean return. " << endl;

  /* try to insert the values again. checking for correct return 
   * boolean on duplicate insertion.
   */
  for( vit2 = v2.begin(); vit2 != ven2; ++vit2 ){
    if( (b2.insert(*vit2)).second ){
      cout << "incorrect boolean return value of duplicate " 
           << "insert for: "<< *vit2 << endl;
      return -1;
    }
  } 

  cout << "PASS: insert() duplicate value, boolean return. " << endl;

  /** sort the vector for inorder traversal */
  sort(v2.begin(), v2.end());

  vector<int>::iterator vit3 = v2.begin();
  vector<int>::iterator ven3 = v2.end();
  
  BST<int>::iterator front = b2.begin();
  BST<int>::iterator back = b2.end();

  /* check that each returned element is the correct successor */
  for( ; vit3 != ven3; ++front, ++vit3 ){
    if( *front !=  *vit3 ){
      cout << "FAIL: inorder traversal failed on : " << *front 
           << " " << *vit3 << endl;
      return -1;
    }
    ++numInspected;
  }

  cout << "PASS: iterator correct element ordering in traversal. " << endl;

  /* check we traversed all the elements in the tree */
  if( numInspected != sizeOfInput ){
    cout << "FAIL: incomplete tree traversal. Read " << numInspected
         << " of " << sizeOfInput << " elements." << endl;
    return -1;
  }else{
    cout << "PASS: iterator number of elements read Expected: " 
         << sizeOfInput << " Result: " 
         << numInspected << endl;
  }

  /* randomize the vector again */
  std::random_shuffle(v2.begin(), v2.end());

  /* test find on all the items in the vector & bst */
  for(vit3 = v2.begin(); vit3 != ven3; ++vit3){
    if( *(b2.find(*vit3)) != *vit3 ){
      cout << "FAIL: find return value did not match: " << *vit3 << endl;  
      return -1;
    }
  }

  cout << "PASS: find() returned all correct values." << endl;
  cout << ( (b2.size() == sizeOfInput) 
            ? "PASS: size() returned expected tree size. " 
            : "FAIL: size() returned non-matching tree size. " ) << endl;
  cout << ( (b2.empty() == false)
            ? "PASS: empty() returned false when not empty. "
            : "FAIL: empty() returned true when not empty. " ) << endl;
  b2.clear();
  cout << ( (b2.empty() == true)
            ? "PASS: empty() returned true when empty. "
            : "FAIL: empty() returned false when empty. " ) << endl;
  
  /** Test for memory leaks when allocating a tree on the heap 
   *  using valgrind will tell you if this passes or not
   */
  treeDestructorTest->insert(1);
  treeDestructorTest->insert(2);
  treeDestructorTest->insert(0);

  treeDestructorTest->clear();

  treeDestructorTest->insert(3);
  treeDestructorTest->insert(5);
  treeDestructorTest->insert(0);
  delete treeDestructorTest;

  cout << "PASS: No errors when calling destructor after clear(). i" << endl;
  cout << "      Run valgrind for memory leak analysis. " << endl;


/*****************************START Testing for RST***********************/
    RST<int> r;

  /* Insert a few data items. */
  vit = v.begin();
  //vector<int>::iterator ven = v.end();
  for(; vit != ven; ++vit) {
    // all these inserts are unique, so should return a std::pair
    // with second part true
    std::pair<RST<int>::iterator,bool> pr = r.insert(*vit);
    if(! pr.second ) {
      cout << "Incorrect bool return value when inserting into RST " << *vit << endl;
      return -1;
    }
    if(*(pr.first) != *vit) {
      cout << "Incorrect iterator return value when inserting into RST " << *vit << endl;
      return -1;
    }  
  }

  //PASS insert rst test
  cout << "PASS Insert into RST" << endl;

   /* Test size. */
  cout << "Size is: " << r.size() << endl;
  if(r.size() != v.size()) {
    cout << "... which is incorrect." << endl;
    return -1;
  }

  /* Test find return value. */
  vit = v.begin();
  for(; vit != ven; ++vit) { 
    if(*(r.find(*vit)) != *vit) {
      cout << "Incorrect return value when finding " << *vit << endl;
      return -1;
    }
  }
  
  /* Sort the vector, to compare with inorder iteration on the BST */
  sort(v.begin(),v.end());

  /* Test BST iterator; should iterate inorder */
  cout << "traversal using iterator:" << endl;
  vit = v.begin();
  //BST<int>::iterator en = r.end();
  //BST<int>::iterator it = r.begin();
  for(; vit < ven; ++vit) {
    if(! (it != en) ) {
      cout << *it << "," << *vit << ": Early termination of BST iteration." << endl;
      return -1;
    }
    cout << *it << endl;
    if(*it != *vit) {
      cout << *it << "," << *vit << ": Incorrect inorder iteration of BST." << endl;
      return -1;
    }
    ++it;
  }
  /** Added a basic clear tests that checks for correct size after delete */
  cout << "OK." << endl;
  r.clear();
  cout << ((r.size() == 0) 
          ? "PASS: clear() size of tree: "
          : "FAIL: clear() Size of tree: ") << r.size()  << endl;
  




//END OF FILE
}
コード例 #6
0
ファイル: benchtree.cpp プロジェクト: hongmincschen/cs100f
int main(int argc, char *argv[]) {
  
  int N = 0;
  int R = 0;

  int first = 0;
  int second = 0;

  double avgcomps = 0;
  double total = 0;
  double sum = 0;
  double square = 0;
  double sdev = 0;
  double squares = 0;

  // Check the user type everything in the correct format, before the benchmark
  if (argc == 5) {
    if (!strcmp(argv[1],"bst")) first = 1;
    if (!strcmp(argv[1],"rst")) first = 2;
    if (!strcmp(argv[1],"set")) first = 3;
    if (!strcmp(argv[2],"sorted")) second = 1;
    if (!strcmp(argv[2],"shuffled")) second = 2;
    if (argv[3] != 0) N = atoi(argv[3]);
    if (argv[4] != 0) R = atoi(argv[4]);
  }

  // If the format is incorrect, then terminate the program.
  if ((first == 0) || (second == 0) || (N == 0) || (R == 0)) {
    cout << "usage..." << endl;
    return -1;
  }

  cout << "# Benchmarking average number of comparisons for successful find" 
	<< endl;
  cout << "# Data structure: " << argv[1] << endl;
  cout << "# Data: " << argv[2] << endl;
  cout << "# N is a powers of 2, minus 1, from 1 to " << N << endl;
  cout << "# Averaging over " << R << " for each N" << endl;
  cout << "#" << endl;
  cout << "# N\tavgcomps\tsdev" << endl;

  vector<countint> v;

  vector<countint>::iterator vit;
  vector<countint>::iterator ven;

  // Begin the benchmark. The first loop is for all j in 1,2,... N = 2^j - 1
  for (int i = 1; i <= N; i+=i+1) {
    srand(time(0));

    // For R in 1,2,..., number of runs
    for (int j = 0; j < R; ++j) {

      // Every run should use new data structures.
      BST<countint> bst = BST<countint>();
      RST<countint> rst = RST<countint>();
      set<countint> s = set<countint>();
      v.clear();

      // Perform the insert of vector
      for (int k = 0; k < i; ++k) v.push_back(k);

      // Shuffle the elements if the user input "shuffle" in argv[2]
      if (second == 2) random_shuffle(v.begin(),v.end());

      vit = v.begin();
      ven = v.end();

      // Insert all elements in bst, rst, or set
      for (vit = v.begin(); vit != ven; ++vit) {
        if (first == 1) bst.insert(*vit);
        if (first == 2) rst.insert(*vit);
        if (first == 3) s.insert(*vit);
      }

      countint::clearcount();

      // Now perform find, and record average # comparisons per successful find
      for (vit = v.begin(); vit != ven; ++vit) {
        if (first == 1) bst.find(*vit);
        if (first == 2) rst.find(*vit);
        if (first == 3) s.find(*vit);
      }
      avgcomps = countint::getcount()/((double)i);
      sum += avgcomps;
      square += avgcomps*avgcomps;

    }
 
    // Record the datas, and print out the total, and the standard deviation
    total = sum/R;
    squares = square/R;
    sdev = sqrt(fabs(squares - (total*total)));
    cout << i << "\t" << total << "\t" << sdev << endl;

    // Also erase the sum and the square
    sum = 0;
    square = 0;

  }

  return 0;
}
コード例 #7
0
ファイル: benchtree.cpp プロジェクト: Elblonko/CSE100-RST
int main(int argc, char * argv[]){

    /* Variable Declarations */
    double stdev = 0;
    double avgTotal =0;
    int maxSize = 0;    //max size of tree
    int index = 1;
    int numRuns = 0;    //Number of benchmark runs to preform
    int N = 1;
    int power = 1;
    bool shuffle = false;
    bool bst = false;
    bool rst = false;
    bool set1 = false;

    //Switch statement to parse user input
    while (argv[index] != NULL ){
        //get argument
        //string word ( argv[index]);

        if( !strcmp(argv[index],"bst") )
        {
            //Code to generate BST
            bst = true;
        }
        else if ( !strcmp (argv[index], "rst") )
        {
            //Code to generate RST
            rst = true;
        }
        else if ( !strcmp(argv[index], "set" ) )
        {
            //Code to generate c++ set
            set1 = true;
        }
        else if ( !strcmp(argv[index], "shuffled" ) )
        {
            //Code to shuffle input
            shuffle = true;    
        }

        index++;
    }

    //Get the max size as 3rd user argument
    maxSize = atoi( argv[3] );


    //Get the number of benchmark runs to preform
    numRuns = atoi( argv[4] ) - 1;


    if (bst){
        BST<countint> s;

        //cout messages to user
        cout << "# Benchmarking average number of comparisons for successful find" << endl;
        cout << "# Data Structure: " << argv[1] << endl;
        cout << "# Data: " << argv[2] << endl;
        cout << "# N is powers of 2, minus 1, from 1 to " << argv[3] << endl;
        cout << "# Averaging over 5 runs for each N" << endl;
        cout << "#" << endl;
        cout << "# N    avgcomps    stdev"<< endl;

        while ( N < maxSize ){


            vector<countint> v;
            v.clear();
            for(int i = N; 0 < i  ; --i) {
                v.push_back(i);
            }


            vector<countint>::iterator vit = v.begin();
            vector<countint>::iterator ven = v.end();


            double totalSquared = 0;

            //For the number of runs passed in
            for ( int run = 0; run < numRuns; run++ ){ 

                //if user wanted elements shuffeled
                if (shuffle){
                    std::random_shuffle( v.begin(),v.end() );
                }


                //Insert into the vector
                vit = v.begin();
                s.clear();
                for(int i = 0; i < N ; i++ ){
                    //cerr << "variable being inserted: " << *vit << endl;
                    s.insert(*vit);
                    vit++;
                }

                //create avgComps variable
                double avgcomps = 0;
                //now start the count variable for find
                countint::clearcount();
                for(vit = v.begin(); vit != ven; ++vit){
                    s.find(*vit);
                }

                //now get avg computations
                avgcomps = countint::getcount() / (double) N;

                //now calculate the standard deviation
                avgTotal = avgTotal + avgcomps;

                totalSquared = totalSquared + pow(avgcomps,TWO);
            }

            //average over the amount of runs
            avgTotal = (avgTotal / numRuns);
            //get the s variable t^2/R
            totalSquared = (totalSquared/numRuns);

            stdev = sqrt(   (totalSquared) - (pow(avgTotal,TWO)) );

            //print statements
            cout << setw(6) << N << setw(10) << avgTotal << setw(15) << stdev << endl;

            //reset avgTotal
            avgTotal = 0;
            stdev = 0;
            totalSquared = 0;

            power++;
            //increase N by power of 2
            N = pow(TWO,power) -1;

        }

    }


    else if (rst){
        RST<countint> s;

        //cout messages to user
        cout << "# Benchmarking average number of comparisons for successful find" << endl;
        cout << "# Data Structure: " << argv[1] << endl;
        cout << "# Data: " << argv[2] << endl;
        cout << "# N is powers of 2, minus 1, from 1 to " << argv[3] << endl;
        cout << "# Averaging over 5 runs for each N" << endl;
        cout << "#" << endl;
        cout << "# N    avgcomps    stdev"<< endl;

        while ( N < maxSize ){


            vector<countint> v;
            v.clear();
            for(int i = N; 0 < i  ; --i) {
                v.push_back(i);
            }


            vector<countint>::iterator vit = v.begin();
            vector<countint>::iterator ven = v.end();


            double totalSquared = 0;

            //For the number of runs passed in
            for ( int run = 0; run < numRuns; run++ ){ 

                //if user wanted elements shuffeled
                if (shuffle){
                    std::random_shuffle( v.begin(),v.end() );
                }

                //Insert into the vector
                vit = v.begin();
                s.clear();
                for(int i = 0; i < N ; i++ ){
                    //cerr << "variable being inserted: " << *vit << endl;
                    s.insert(*vit);
                    vit++;
                }
                //create avgComps variable
                double avgcomps = 0;
                //now start the count variable for find
                countint::clearcount();
                for(vit = v.begin(); vit != ven; ++vit){
                    s.find(*vit);
                }

                //now get avg computations
                avgcomps = countint::getcount() / (double) N;

                //now calculate the standard deviation
                avgTotal = avgTotal + avgcomps;

                totalSquared = totalSquared + pow(avgcomps,TWO);
            }

            //average over the amount of runs
            avgTotal = (avgTotal / numRuns);
            //get the s variable t^2/R
            totalSquared = (totalSquared/numRuns);

            stdev = sqrt(   (totalSquared) - (pow(avgTotal,TWO)) );

            //print statements
            cout << setw(6) << N << setw(10) << avgTotal << setw(15) << stdev << endl;

            //reset avgTotal
            avgTotal = 0;
            stdev = 0;
            totalSquared = 0;

            power++;
            //increase N by power of 2
            N = pow(TWO,power) -1;

        }

    }

    if(set1){
        set<countint> s;

        //cout messages to user
        cout << "# Benchmarking average number of comparisons for successful find" << endl;
        cout << "# Data Structure: " << argv[1] << endl;
        cout << "# Data: " << argv[2] << endl;
        cout << "# N is powers of 2, minus 1, from 1 to " << argv[3] << endl;
        cout << "# Averaging over 5 runs for each N" << endl;
        cout << "#" << endl;
        cout << "# N    avgcomps    stdev"<< endl;

        while ( N < maxSize ){


            vector<countint> v;
            v.clear();
            for(int i = N; 0 < i  ; --i) {
                v.push_back(i);
            }

            vector<countint>::iterator vit = v.begin();
            vector<countint>::iterator ven = v.end();


            double totalSquared = 0;

            //For the number of runs passed in
            for ( int run = 0; run < numRuns; run++ ){ 

                //if user wanted elements shuffeled
                if (shuffle){
                    std::random_shuffle( v.begin(),v.end() );
                }
                //Insert into the vector
                vit = v.begin();
                s.clear();
                for(int i = 0; i < N ; i++ ){
                    //cerr << "variable being inserted: " << *vit << endl;
                    s.insert(*vit);
                    vit++;
                }
                //create avgComps variable
                double avgcomps = 0;
                //now start the count variable for find
                countint::clearcount();
                for(vit = v.begin(); vit != ven; ++vit){
                    s.find(*vit);
                }

                //now get avg computations
                avgcomps = countint::getcount() / (double) N;

                //now calculate the standard deviation
                avgTotal = avgTotal + avgcomps;

                totalSquared = totalSquared + pow(avgcomps,TWO);
            }

            //average over the amount of runs
            avgTotal = (avgTotal / numRuns);
            //get the s variable t^2/R
            totalSquared = (totalSquared/numRuns);

            stdev = sqrt(   (totalSquared) - (pow(avgTotal,TWO)) );

            //print statements
            cout << setw(6) << N << setw(10) << avgTotal << setw(15) << stdev << endl;

            //reset avgTotal
            avgTotal = 0;
            stdev = 0;
            totalSquared = 0;

            power++;
            //increase N by power of 2
            N = pow(TWO,power) -1;

        }

    }


    //endfile
}
コード例 #8
0
ファイル: benchtree.cpp プロジェクト: jwl028/CSE100-P2
int main(int argc, char** argv) {
  string dataStruc;
  string order;
  int maxSize;
  int numRuns;
  int maxN;
  int N;
  //Check that there are 4 arguments
  if (argc == 5) {
    //Selected data structure
    if(strcmp(argv[1],"bst")==0 || strcmp(argv[1],"rst")==0 || strcmp(argv[1],"set")==0) {
      dataStruc = argv[1];
    }
    else {
      cout << "Please enter either 'bst', 'rst', or 'set' for the first argument" << endl;
      return (0);
    } 
    //Sorted or shuffled 
    if(strcmp(argv[2],"sorted")==0 || strcmp(argv[2],"shuffled")==0 ) {
      order = argv[2];
    }    
    else {
      cout << "Please enter either 'sorted' or 'shuffled' for the second argument" << endl;
      return (0);
    } 
    //Maximum tree size (has to be greater than or equal to 1)
    for(unsigned int a = 0; a < strlen(argv[3]); a++) {
      if(isdigit(argv[3][a]) == 0) {
        cout << "Please enter an integer greater than or equal to 1 for the third argument" << endl;
        return(0);
      }
    }
    maxSize = atoi(argv[3]);
    if (maxSize == 0) {
      cout << "Please enter an integer greater than or equal to 1 for the third argument" << endl;
      return(0);
    }
    maxN = log(maxSize + 1) / log(2);
    //# times calculations are run (has to be greater than or equal to 1)
    for(unsigned int a = 0; a < strlen(argv[4]); a++) {
      if(isdigit(argv[4][a]) == 0) {
        cout << "Please enter an integer greater than or equal to 1 for the fourth argument" << endl;
        return(0);
      }
    }
    numRuns = atoi(argv[4]);
    if (numRuns == 0) {
      cout << "Please enter an integer greater than or equal to 1 for the fourth argument" << endl;
      return(0);
    }
  }
  //Exits if 4 arguments are not supplied
  else {
    cout << "Please enter 4 arguments" << endl;
    return(0);
  }

  //Description of benchmark
  cout << "# Benchmarking average number of comparisons for successful find" << endl;
  cout << "# Data structure: " << dataStruc << endl;
  cout << "# Data: " << order << endl;
  cout << "# N is powers of 2, minus 1, from 1 to " << maxSize << endl;
  cout << "# Averaging over " << numRuns << " runs for each N" << endl;
  cout << "#" << endl;
  cout << "# N\tavgcomps\tstdev" << endl;

  //N in 2^n + 1 steps
  for (int i=0; i<maxN; i++) {
    //Calculate N
    N = pow(2,(i+1))-1;
    //Reset values for each N
    double avgcomps = 0.0;
    double sumsqcomp = 0.0;
    double stdev = 0.0;
    
    //Insert and find on RST data structure.
    //Originally implemented one loop and if/else statements for each data structure declaration.
    //However, tree.insert() and tree.find() would not compile because of scope.  Therefore, multiple
    //loops were created for each if statement. 
    if(dataStruc.compare("rst")==0) {
      for (int j=0; j<numRuns; j++) {
        RST<countint> tree = RST<countint>();
        vector<countint> v;
        countint::clearcount();
        v.clear();
        for(int k=0; k<N; ++k) {
          v.push_back(k);
        }
        if(order == "shuffled") {
          random_shuffle(v.begin(),v.end());
        }
        //Insert elements into the tree  
        vector<countint>::iterator vit = v.begin();
        vector<countint>::iterator ven = v.end();
        for(vit = v.begin(); vit != ven; ++vit) {
          tree.insert(*vit);
        }
        //Clear count
        countint::clearcount();
        //Find elements in tree
        for(vit = v.begin(); vit != ven; ++vit) {
          tree.find(*vit);
        }
        //Compute the average comparison for one run
        avgcomps += countint::getcount()/(double)N;
        //Compute the average squared comparison for one run
        sumsqcomp += pow((countint::getcount()/(double)N),2.0);   
      }
      //Compute the average comparison for multiple runs
      avgcomps = avgcomps/(double)numRuns;
      //Compute the average squared comparison for multiple run
      sumsqcomp = sumsqcomp/(double)numRuns;   
      //Compute standard deviation
      stdev = sqrt(abs(pow(avgcomps,2.0)-sumsqcomp));
      cout << N << "\t" << avgcomps << "\t\t" << stdev << endl;
    }

    //Insert and find on BST data structure.
    else if(dataStruc.compare("bst")==0) {
      for (int j=0; j<numRuns; j++) {
        BST<countint> tree = BST<countint>();
        vector<countint> v;
        countint::clearcount();
        v.clear();
        for(int k=0; k<N; ++k) {
          v.push_back(k);
        }
        if(order == "shuffled") {
          random_shuffle(v.begin(),v.end());
        }  
        //Insert elements into the tree  
        vector<countint>::iterator vit = v.begin();
        vector<countint>::iterator ven = v.end();
        for(vit = v.begin(); vit != ven; ++vit) {
          tree.insert(*vit);
        }
        //Clear count
        countint::clearcount();
        //Find elements in tree
        for(vit = v.begin(); vit != ven; ++vit) {
          tree.find(*vit);
        }  
        //Compute the average comparison for one run
        avgcomps += countint::getcount()/(double)N;
        //Compute the average squared comparison for one run
        sumsqcomp += pow((countint::getcount()/(double)N),2.0);   

      }
      //Compute the average comparison for multiple runs
      avgcomps = avgcomps/(double)numRuns;
      //Compute the average squared comparison for multiple run
      sumsqcomp = sumsqcomp/(double)numRuns;   
      //Compute standard deviation
      stdev = sqrt(abs(pow(avgcomps,2.0)-sumsqcomp));
      cout << N << "\t" << avgcomps << "\t\t" << stdev << endl;
    }
  
    //Insert and find on set data structure.
    else if(dataStruc.compare("set")==0) {
      for (int j=0; j<numRuns; j++) {
        set<countint> tree;
        vector<countint> v;
        countint::clearcount();
        v.clear();
        for(int k=0; k<N; ++k) {
          v.push_back(k);
        }
        if(order == "shuffled") {
          random_shuffle(v.begin(),v.end());
        }  
        //Insert elements into the tree  
        vector<countint>::iterator vit = v.begin();
        vector<countint>::iterator ven = v.end();
        for(vit = v.begin(); vit != ven; ++vit) {
          tree.insert(*vit);
        }
        //Clear count
        countint::clearcount();
        //Find elements in tree
        for(vit = v.begin(); vit != ven; ++vit) {
          tree.find(*vit);
        }  
        //Compute the average comparison for one run
        avgcomps += countint::getcount()/(double)N;
        //Compute the average squared comparison for one run
        sumsqcomp += pow((countint::getcount()/(double)N),2.0);   
      }
      //Compute the average comparison for multiple runs
      avgcomps = avgcomps/(double)numRuns;
      //Compute the average squared comparison for multiple run
      sumsqcomp = sumsqcomp/(double)numRuns;   
      //Compute standard deviation
      stdev = sqrt(abs(pow(avgcomps,2.0)-sumsqcomp));
      cout << N << "\t" << avgcomps << "\t\t" << stdev << endl;
    }
  } 
}