Stxxl
1.4.0
|
00001 /*************************************************************************** 00002 * algo/test_bad_cmp.cpp 00003 * 00004 * Part of the STXXL. See http://stxxl.sourceforge.net 00005 * 00006 * Copyright (C) 2002 Roman Dementiev <dementiev@mpi-sb.mpg.de> 00007 * Copyright (C) 2011 Andreas Beckmann <beckmann@cs.uni-frankfurt.de> 00008 * 00009 * Distributed under the Boost Software License, Version 1.0. 00010 * (See accompanying file LICENSE_1_0.txt or copy at 00011 * http://www.boost.org/LICENSE_1_0.txt) 00012 **************************************************************************/ 00013 00014 //! \example algo/test_bad_cmp.cpp 00015 //! This is an example of how NOT to use \c stxxl::sort() algorithm. 00016 //! Here min_value and max_value are used as keys which is forbidden. 00017 00018 #include <stxxl/mng> 00019 #include <stxxl/sort> 00020 #include <stxxl/vector> 00021 00022 00023 struct my_type 00024 { 00025 typedef unsigned key_type; 00026 00027 key_type _key; 00028 key_type _data; 00029 key_type key() const 00030 { 00031 return _key; 00032 } 00033 00034 my_type() { } 00035 my_type(key_type __key) : _key(__key), _data(0) { } 00036 00037 static my_type min_value() 00038 { 00039 return my_type((std::numeric_limits<key_type>::min)()); 00040 } 00041 static my_type max_value() 00042 { 00043 return my_type((std::numeric_limits<key_type>::max)()); 00044 } 00045 00046 ~my_type() { } 00047 }; 00048 00049 std::ostream & operator << (std::ostream & o, const my_type & obj) 00050 { 00051 o << obj._key; 00052 return o; 00053 } 00054 00055 bool operator < (const my_type & a, const my_type & b) 00056 { 00057 return a.key() < b.key(); 00058 } 00059 00060 bool operator == (const my_type & a, const my_type & b) 00061 { 00062 return a.key() == b.key(); 00063 } 00064 00065 bool operator != (const my_type & a, const my_type & b) 00066 { 00067 return a.key() != b.key(); 00068 } 00069 00070 struct cmp : public std::less<my_type> 00071 { 00072 my_type min_value() const 00073 { 00074 return my_type::min_value(); 00075 } 00076 my_type max_value() const 00077 { 00078 return my_type::max_value(); 00079 } 00080 }; 00081 00082 00083 int main() 00084 { 00085 #if STXXL_PARALLEL_MULTIWAY_MERGE 00086 STXXL_MSG("STXXL_PARALLEL_MULTIWAY_MERGE"); 00087 #endif 00088 unsigned memory_to_use = 128 * 1024 * 1024; 00089 typedef stxxl::vector<my_type> vector_type; 00090 00091 const stxxl::int64 n_records = 00092 stxxl::int64(384) * stxxl::int64(1024 * 1024) / sizeof(my_type); 00093 vector_type v(n_records); 00094 00095 stxxl::int64 aliens, not_stable; 00096 int bs = vector_type::block_type::size; 00097 00098 STXXL_MSG("Filling vector with min_value..., input size = " << v.size() << " elements (" << ((v.size() * sizeof(my_type)) >> 20) << " MiB)"); 00099 for (vector_type::size_type i = 0; i < v.size(); i++) { 00100 v[i]._key = 0; 00101 v[i]._data = i + 1; 00102 } 00103 00104 STXXL_MSG("Checking order..."); 00105 STXXL_MSG(((stxxl::is_sorted(v.begin(), v.end(), cmp())) ? "OK" : "WRONG")); 00106 00107 STXXL_MSG("Sorting (using " << (memory_to_use >> 20) << " MiB of memory)..."); 00108 stxxl::sort(v.begin(), v.end(), cmp(), memory_to_use); 00109 00110 STXXL_MSG("Checking order..."); 00111 STXXL_MSG(((stxxl::is_sorted(v.begin(), v.end(), cmp())) ? "OK" : "WRONG")); 00112 00113 aliens = not_stable = 0; 00114 for (vector_type::size_type i = 0; i < v.size(); i++) { 00115 if (v[i]._data < 1) 00116 ++aliens; 00117 else if (v[i]._data != i + 1) 00118 ++not_stable; 00119 v[i]._data = i + 1; 00120 } 00121 STXXL_MSG("elements that were not in the input: " << aliens); 00122 STXXL_MSG("elements not on their expected location: " << not_stable); 00123 00124 STXXL_MSG("Sorting subset (using " << (memory_to_use >> 20) << " MiB of memory)..."); 00125 stxxl::sort(v.begin() + bs - 1, v.end() - bs + 2, cmp(), memory_to_use); 00126 00127 STXXL_MSG("Checking order..."); 00128 STXXL_MSG(((stxxl::is_sorted(v.begin(), v.end(), cmp())) ? "OK" : "WRONG")); 00129 00130 aliens = not_stable = 0; 00131 for (vector_type::size_type i = 0; i < v.size(); i++) { 00132 if (v[i]._data < 1) 00133 ++aliens; 00134 else if (v[i]._data != i + 1) 00135 ++not_stable; 00136 v[i]._data = i + 1; 00137 } 00138 STXXL_MSG("elements that were not in the input: " << aliens); 00139 STXXL_MSG("elements not on their expected location: " << not_stable); 00140 00141 STXXL_MSG("Filling vector with max_value..., input size = " << v.size() << " elements (" << ((v.size() * sizeof(my_type)) >> 20) << " MiB)"); 00142 for (vector_type::size_type i = 0; i < v.size(); i++) { 00143 v[i]._key = unsigned(-1); 00144 v[i]._data = i + 1; 00145 } 00146 00147 STXXL_MSG("Sorting subset (using " << (memory_to_use >> 20) << " MiB of memory)..."); 00148 stxxl::sort(v.begin() + bs - 1, v.end() - bs + 2, cmp(), memory_to_use); 00149 00150 STXXL_MSG("Checking order..."); 00151 STXXL_MSG(((stxxl::is_sorted(v.begin(), v.end(), cmp())) ? "OK" : "WRONG")); 00152 00153 aliens = not_stable = 0; 00154 for (vector_type::size_type i = 0; i < v.size(); i++) { 00155 if (v[i]._data < 1) 00156 ++aliens; 00157 else if (v[i]._data != i + 1) 00158 ++not_stable; 00159 v[i]._data = i + 1; 00160 } 00161 STXXL_MSG("elements that were not in the input: " << aliens); 00162 STXXL_MSG("elements not on their expected location: " << not_stable); 00163 00164 STXXL_MSG("Done, output size=" << v.size() << " block size=" << bs); 00165 00166 return 0; 00167 } 00168 00169 // vim: et:ts=4:sw=4