Stxxl
1.4.0
|
00001 /*************************************************************************** 00002 * algo/test_stable_ksort.cpp 00003 * 00004 * Part of the STXXL. See http://stxxl.sourceforge.net 00005 * 00006 * Copyright (C) 2003, 2008 Roman Dementiev <dementiev@mpi-sb.mpg.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 //! \example algo/test_stable_ksort.cpp 00014 //! This is an example of how to use \c stxxl::ksort() algorithm 00015 00016 #include <stxxl/mng> 00017 #include <stxxl/stable_ksort> 00018 #include <stxxl/ksort> 00019 #include <stxxl/vector> 00020 00021 00022 struct my_type 00023 { 00024 typedef unsigned key_type; 00025 00026 key_type _key; 00027 char _data[128 - sizeof(key_type)]; 00028 key_type key() const 00029 { 00030 return _key; 00031 } 00032 00033 my_type() { } 00034 my_type(key_type __key) : _key(__key) { } 00035 00036 static my_type min_value() 00037 { 00038 return my_type((std::numeric_limits<key_type>::min)()); 00039 } 00040 static my_type max_value() 00041 { 00042 return my_type((std::numeric_limits<key_type>::max)()); 00043 } 00044 }; 00045 00046 bool operator < (const my_type & a, const my_type & b) 00047 { 00048 return a.key() < b.key(); 00049 } 00050 00051 int main() 00052 { 00053 #if STXXL_PARALLEL_MULTIWAY_MERGE 00054 STXXL_MSG("STXXL_PARALLEL_MULTIWAY_MERGE"); 00055 #endif 00056 unsigned memory_to_use = 44 * 1024 * 1024; 00057 typedef stxxl::vector<my_type> vector_type; 00058 const stxxl::int64 n_records = 2 * 32 * stxxl::int64(1024 * 1024) / sizeof(my_type); 00059 vector_type v(n_records); 00060 00061 stxxl::random_number32 rnd; 00062 STXXL_MSG("Filling vector... " << rnd() << " " << rnd() << " " << rnd()); 00063 for (vector_type::size_type i = 0; i < v.size(); i++) 00064 v[i]._key = (rnd() / 2) * 2; 00065 00066 STXXL_MSG("Checking order..."); 00067 STXXL_MSG(((stxxl::is_sorted(v.begin(), v.end())) ? "OK" : "WRONG")); 00068 00069 STXXL_MSG("Sorting..."); 00070 stxxl::stable_ksort(v.begin(), v.end(), memory_to_use); 00071 00072 STXXL_MSG("Checking order..."); 00073 STXXL_MSG(((stxxl::is_sorted(v.begin(), v.end())) ? "OK" : "WRONG")); 00074 00075 00076 return 0; 00077 }