Stxxl  1.4.0
containers/btree/test_corr_insert_erase.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  containers/btree/test_corr_insert_erase.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2006 Roman Dementiev <dementiev@ira.uka.de>
00007  *
00008  *  Distributed under the Boost Software License, Version 1.0.
00009  *  (See accompanying file LICENSE_1_0.txt or copy at
00010  *  http://www.boost.org/LICENSE_1_0.txt)
00011  **************************************************************************/
00012 
00013 #include <iostream>
00014 
00015 #include <stxxl/bits/containers/btree/btree.h>
00016 #include <stxxl/scan>
00017 #include <stxxl/sort>
00018 #include <stxxl/random_shuffle>
00019 
00020 
00021 struct comp_type : public std::less<int>
00022 {
00023     static int max_value()
00024     {
00025         return (std::numeric_limits<int>::max)();
00026     }
00027     static int min_value()
00028     {
00029         return (std::numeric_limits<int>::min)();
00030     }
00031 };
00032 
00033 typedef stxxl::btree::btree<int, double, comp_type, 4096, 4096, stxxl::SR> btree_type;
00034 //typedef stxxl::btree::btree<int,double,comp_type,10,11,stxxl::SR> btree_type;
00035 
00036 std::ostream & operator << (std::ostream & o, const std::pair<int, double> & obj)
00037 {
00038     o << obj.first << " " << obj.second;
00039     return o;
00040 }
00041 
00042 
00043 struct rnd_gen
00044 {
00045     stxxl::random_number32 rnd;
00046     int operator () ()
00047     {
00048         return (rnd() >> 2) * 3;
00049     }
00050 };
00051 
00052 bool operator == (const std::pair<int, double> & a, const std::pair<int, double> & b)
00053 {
00054     return a.first == b.first;
00055 }
00056 
00057 int main(int argc, char * argv[])
00058 {
00059     if (argc < 2)
00060     {
00061         STXXL_MSG("Usage: " << argv[0] << " #log_ins");
00062         return -1;
00063     }
00064 
00065     const int log_nins = atoi(argv[1]);
00066     if (log_nins > 31) {
00067         STXXL_ERRMSG("This test can't do more than 2^31 operations, you requested 2^" << log_nins);
00068         return -1;
00069     }
00070 
00071     btree_type BTree(1024 * 128, 1024 * 128);
00072 
00073     const stxxl::uint64 nins = 1ULL << log_nins;
00074 
00075     stxxl::ran32State = time(NULL);
00076 
00077 
00078     stxxl::vector<int> Values(nins);
00079     STXXL_MSG("Generating " << nins << " random values");
00080     stxxl::generate(Values.begin(), Values.end(), rnd_gen(), 4);
00081 
00082     STXXL_MSG("Sorting the random values");
00083     stxxl::sort(Values.begin(), Values.end(), comp_type(), 128 * 1024 * 1024);
00084 
00085     STXXL_MSG("Deleting unique values");
00086     stxxl::vector<int>::iterator NewEnd = std::unique(Values.begin(), Values.end());
00087     Values.resize(NewEnd - Values.begin());
00088 
00089     STXXL_MSG("Randomly permute input values");
00090     stxxl::random_shuffle(Values.begin(), Values.end(), 128 * 1024 * 1024);
00091 
00092     stxxl::vector<int>::const_iterator it = Values.begin();
00093     STXXL_MSG("Inserting " << Values.size() << " random values into btree");
00094     for ( ; it != Values.end(); ++it)
00095         BTree.insert(std::pair<int, double>(*it, double(*it) + 1.0));
00096 
00097 
00098     STXXL_MSG("Number of elements in btree: " << BTree.size());
00099 
00100     STXXL_MSG("Searching " << Values.size() << " existing elements and erasing them");
00101     stxxl::vector<int>::const_iterator vIt = Values.begin();
00102 
00103     for ( ; vIt != Values.end(); ++vIt)
00104     {
00105         btree_type::iterator bIt = BTree.find(*vIt);
00106         assert(bIt != BTree.end());
00107         bool f = BTree.erase((*vIt) + 1);       // erasing non-existent element
00108         assert(f == 0);
00109         f = BTree.erase(*vIt);                  // erasing existing element
00110         assert(f == 1);
00111         bIt = BTree.find(*vIt);                 // checking it is not there
00112         assert(bIt == BTree.end());
00113         f = BTree.erase(*vIt);                  // trying to erase it again
00114         assert(f == 0);
00115     }
00116 
00117     assert(BTree.empty());
00118 
00119     STXXL_MSG("Test passed.");
00120 
00121     return 0;
00122 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines