Stxxl  1.4.0
algo/test_sort.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  algo/test_sort.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) 2010 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_sort.cpp
00015 //! This is an example of how to use \c stxxl::sort() algorithm
00016 
00017 #include <stxxl/mng>
00018 #include <stxxl/sort>
00019 #include <stxxl/vector>
00020 
00021 
00022 #define RECORD_SIZE 8
00023 
00024 struct my_type
00025 {
00026     typedef unsigned key_type;
00027 
00028     key_type _key;
00029     char _data[RECORD_SIZE - sizeof(key_type)];
00030     key_type key() const
00031     {
00032         return _key;
00033     }
00034 
00035     my_type() { }
00036     my_type(key_type __key) : _key(__key) { }
00037 
00038     static my_type min_value()
00039     {
00040         return my_type((std::numeric_limits<key_type>::min)());
00041     }
00042     static my_type max_value()
00043     {
00044         return my_type((std::numeric_limits<key_type>::max)());
00045     }
00046 
00047     ~my_type() { }
00048 };
00049 
00050 std::ostream & operator << (std::ostream & o, const my_type & obj)
00051 {
00052     o << obj._key;
00053     return o;
00054 }
00055 
00056 bool operator < (const my_type & a, const my_type & b)
00057 {
00058     return a.key() < b.key();
00059 }
00060 
00061 bool operator == (const my_type & a, const my_type & b)
00062 {
00063     return a.key() == b.key();
00064 }
00065 
00066 bool operator != (const my_type & a, const my_type & b)
00067 {
00068     return a.key() != b.key();
00069 }
00070 
00071 struct cmp : public std::less<my_type>
00072 {
00073     my_type min_value() const
00074     {
00075         return my_type::min_value();
00076     }
00077     my_type max_value() const
00078     {
00079         return my_type::max_value();
00080     }
00081 };
00082 
00083 
00084 int main()
00085 {
00086 #if STXXL_PARALLEL_MULTIWAY_MERGE
00087     STXXL_MSG("STXXL_PARALLEL_MULTIWAY_MERGE");
00088 #endif
00089     unsigned memory_to_use = 128 * 1024 * 1024;
00090     typedef stxxl::vector<my_type> vector_type;
00091 
00092     {
00093         // test small vector that can be sorted internally
00094         vector_type v(3);
00095         v[0] = 42;
00096         v[1] = 0;
00097         v[2] = 23;
00098         STXXL_MSG("small vector unsorted " << v[0] << " " << v[1] << " " << v[2]);
00099         //stxxl::sort(v.begin(), v.end(), cmp(), memory_to_use);
00100         stxxl::stl_in_memory_sort(v.begin(), v.end(), cmp());
00101         STXXL_MSG("small vector sorted   " << v[0] << " " << v[1] << " " << v[2]);
00102         assert(stxxl::is_sorted(v.begin(), v.end(), cmp()));
00103     }
00104 
00105     const stxxl::int64 n_records =
00106         stxxl::int64(384) * stxxl::int64(1024 * 1024) / sizeof(my_type);
00107     vector_type v(n_records);
00108 
00109     stxxl::random_number32 rnd;
00110     STXXL_MSG("Filling vector..., input size = " << v.size() << " elements (" << ((v.size() * sizeof(my_type)) >> 20) << " MiB)");
00111     for (vector_type::size_type i = 0; i < v.size(); i++)
00112         v[i]._key = 1 + (rnd() % 0xfffffff);
00113 
00114     STXXL_MSG("Checking order...");
00115     STXXL_MSG(((stxxl::is_sorted(v.begin(), v.end(), cmp())) ? "OK" : "WRONG"));
00116 
00117     STXXL_MSG("Sorting (using " << (memory_to_use >> 20) << " MiB of memory)...");
00118     stxxl::sort(v.begin(), v.end(), cmp(), memory_to_use);
00119 
00120     STXXL_MSG("Checking order...");
00121     STXXL_MSG(((stxxl::is_sorted(v.begin(), v.end(), cmp())) ? "OK" : "WRONG"));
00122 
00123 
00124     STXXL_MSG("Done, output size=" << v.size());
00125 
00126     return 0;
00127 }
00128 
00129 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines