Stxxl
1.4.0
|
00001 /*************************************************************************** 00002 * containers/btree/test_corr_insert_find.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 00018 00019 struct comp_type : public std::less<int> 00020 { 00021 static int max_value() 00022 { 00023 return (std::numeric_limits<int>::max)(); 00024 } 00025 static int min_value() 00026 { 00027 return (std::numeric_limits<int>::min)(); 00028 } 00029 }; 00030 00031 typedef stxxl::btree::btree<int, double, comp_type, 4096, 4096, stxxl::SR> btree_type; 00032 00033 00034 std::ostream & operator << (std::ostream & o, const std::pair<int, double> & obj) 00035 { 00036 o << obj.first << " " << obj.second; 00037 return o; 00038 } 00039 00040 00041 struct rnd_gen 00042 { 00043 stxxl::random_number32 rnd; 00044 int operator () () 00045 { 00046 return (rnd() >> 2) * 3; 00047 } 00048 }; 00049 00050 bool operator == (const std::pair<int, double> & a, const std::pair<int, double> & b) 00051 { 00052 return a.first == b.first; 00053 } 00054 00055 int main(int argc, char * argv[]) 00056 { 00057 if (argc < 2) 00058 { 00059 STXXL_MSG("Usage: " << argv[0] << " #log_ins"); 00060 return -1; 00061 } 00062 00063 const int log_nins = atoi(argv[1]); 00064 if (log_nins > 31) { 00065 STXXL_ERRMSG("This test can't do more than 2^31 operations, you requested 2^" << log_nins); 00066 return -1; 00067 } 00068 00069 btree_type BTree(1024 * 128, 1024 * 128); 00070 00071 const stxxl::uint64 nins = 1ULL << log_nins; 00072 00073 stxxl::ran32State = time(NULL); 00074 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("Number of elements in btree: " << BTree.size()); 00087 00088 STXXL_MSG("Searching " << nins << " existing elements"); 00089 stxxl::vector<int>::const_iterator vIt = Values.begin(); 00090 00091 for ( ; vIt != Values.end(); ++vIt) 00092 { 00093 btree_type::iterator bIt = BTree.find(*vIt); 00094 assert(bIt != BTree.end()); 00095 assert(bIt->first == *vIt); 00096 } 00097 00098 STXXL_MSG("Searching " << nins << " non-existing elements"); 00099 stxxl::vector<int>::const_iterator vIt1 = Values.begin(); 00100 00101 for ( ; vIt1 != Values.end(); ++vIt1) 00102 { 00103 btree_type::iterator bIt = BTree.find((*vIt1) + 1); 00104 assert(bIt == BTree.end()); 00105 } 00106 00107 STXXL_MSG("Test passed."); 00108 00109 return 0; 00110 }