Stxxl  1.4.0
containers/btree/test_corr_insert_scan.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  containers/btree/test_corr_insert_scan.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 
00019 
00020 struct comp_type : public std::less<int>
00021 {
00022     static int max_value()
00023     {
00024         return (std::numeric_limits<int>::max)();
00025     }
00026     static int min_value()
00027     {
00028         return (std::numeric_limits<int>::min)();
00029     }
00030 };
00031 
00032 typedef stxxl::btree::btree<int, double, comp_type, 4096, 4096, stxxl::SR> btree_type;
00033 //typedef stxxl::btree::btree<int,double,comp_type,10,11,stxxl::SR> btree_type;
00034 
00035 std::ostream & operator << (std::ostream & o, const std::pair<int, double> & obj)
00036 {
00037     o << obj.first << " " << obj.second;
00038     return o;
00039 }
00040 
00041 
00042 struct rnd_gen
00043 {
00044     stxxl::random_number32 rnd;
00045     int operator () ()
00046     {
00047         return (rnd() >> 2);
00048     }
00049 };
00050 
00051 bool operator == (const std::pair<int, double> & a, const std::pair<int, double> & b)
00052 {
00053     return a.first == b.first;
00054 }
00055 
00056 int main(int argc, char * argv[])
00057 {
00058     if (argc < 2)
00059     {
00060         STXXL_MSG("Usage: " << argv[0] << " #log_ins");
00061         return -1;
00062     }
00063 
00064     const int log_nins = atoi(argv[1]);
00065     if (log_nins > 31) {
00066         STXXL_ERRMSG("This test can't do more than 2^31 operations, you requested 2^" << log_nins);
00067         return -1;
00068     }
00069 
00070     btree_type BTree(1024 * 128, 1024 * 128);
00071 
00072     const stxxl::uint64 nins = 1ULL << log_nins;
00073 
00074     stxxl::ran32State = time(NULL);
00075 
00076     stxxl::vector<int> Values(nins);
00077     STXXL_MSG("Generating " << nins << " random values");
00078     stxxl::generate(Values.begin(), Values.end(), rnd_gen(), 4);
00079 
00080     stxxl::vector<int>::const_iterator it = Values.begin();
00081     STXXL_MSG("Inserting " << nins << " random values into btree");
00082     for ( ; it != Values.end(); ++it)
00083         BTree.insert(std::pair<int, double>(*it, double(*it) + 1.0));
00084 
00085 
00086     STXXL_MSG("Sorting the random values");
00087     stxxl::sort(Values.begin(), Values.end(), comp_type(), 128 * 1024 * 1024);
00088 
00089     STXXL_MSG("Deleting unique values");
00090     stxxl::vector<int>::iterator NewEnd = std::unique(Values.begin(), Values.end());
00091     Values.resize(NewEnd - Values.begin());
00092 
00093     assert(BTree.size() == Values.size());
00094     STXXL_MSG("Size without duplicates: " << Values.size());
00095 
00096     STXXL_MSG("Comparing content");
00097 
00098     stxxl::vector<int>::const_iterator vIt = Values.begin();
00099     btree_type::iterator bIt = BTree.begin();
00100 
00101     for ( ; vIt != Values.end(); ++vIt, ++bIt)
00102     {
00103         assert(*vIt == bIt->first);
00104         assert(double(bIt->first) + 1.0 == bIt->second);
00105         assert(bIt != BTree.end());
00106     }
00107 
00108     assert(bIt == BTree.end());
00109 
00110     STXXL_MSG("Test passed.");
00111 
00112     return 0;
00113 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines