Stxxl  1.4.0
containers/test_sorter.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  container/test_sorter.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2012 Timo Bingmann <bingmann@kit.edu>
00007  *  - based on algo/test_sort.cpp
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 containers/test_sorter.cpp
00015 //! This is an example of how to use \c stxxl::sorter() container
00016 
00017 #include <limits>
00018 #include <stxxl/sorter>
00019 
00020 #define RECORD_SIZE 8
00021 
00022 struct my_type
00023 {
00024     typedef unsigned key_type;
00025 
00026     key_type _key;
00027     char _data[RECORD_SIZE - 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     ~my_type() { }
00046 };
00047 
00048 std::ostream & operator << (std::ostream & o, const my_type & obj)
00049 {
00050     o << obj._key;
00051     return o;
00052 }
00053 
00054 bool operator == (const my_type & a, const my_type & b)
00055 {
00056     return a.key() == b.key();
00057 }
00058 
00059 bool operator <= (const my_type & a, const my_type & b)
00060 {
00061     return a.key() <= b.key();
00062 }
00063 
00064 struct Comparator : public std::less<my_type>
00065 {
00066     inline bool operator()(const my_type & a, const my_type & b) const
00067     {
00068         return a.key() < b.key();
00069     }
00070     my_type min_value() const
00071     {
00072         return my_type::min_value();
00073     }
00074     my_type max_value() const
00075     {
00076         return my_type::max_value();
00077     }
00078 };
00079 
00080 
00081 int main()
00082 {
00083 #if STXXL_PARALLEL_MULTIWAY_MERGE
00084     STXXL_MSG("STXXL_PARALLEL_MULTIWAY_MERGE");
00085 #endif
00086     unsigned memory_to_use = 128 * 1024 * 1024;
00087     enum { block_size = 8192 };
00088 
00089     // comparator object used for sorters
00090     Comparator cmp;
00091 
00092     // construct sorter type: stxxl::sorter<ValueType, ComparatorType, BlockSize>
00093     typedef stxxl::sorter<my_type, Comparator, block_size> sorter_type;
00094 
00095     {
00096         // test small number of items that can be sorted internally
00097 
00098         sorter_type s (cmp, memory_to_use);
00099 
00100         // put in some items
00101         s.push(42);
00102         s.push(0);
00103         s.push(23);
00104 
00105         // finish input, switch to sorting stage.
00106         s.sort();
00107 
00108         assert( *s == 0 );      ++s;
00109         assert( *s == 23 );     ++s;
00110         assert( *s == 42 );     ++s;
00111         assert( s.empty() );
00112     }
00113 
00114     {
00115         // large test with 384 MiB items
00116 
00117         const stxxl::uint64 n_records = stxxl::int64(3) * stxxl::int64(1024 * 1024) / sizeof(my_type);
00118 
00119         sorter_type s (cmp, memory_to_use);
00120 
00121         stxxl::random_number32 rnd;
00122 
00123         STXXL_MSG("Filling sorter..., input size = " << n_records << " elements (" << ((n_records * sizeof(my_type)) >> 20) << " MiB)");
00124 
00125         for (stxxl::uint64 i = 0; i < n_records; i++)
00126         {
00127             assert( s.size() == i );
00128 
00129             s.push( 1 + (rnd() % 0xfffffff) );
00130         }
00131 
00132         // finish input, switch to sorting stage.
00133         s.sort();
00134 
00135         STXXL_MSG("Checking order...");
00136 
00137         assert( !s.empty() );
00138         assert( s.size() == n_records );
00139 
00140         my_type prev = *s;      // get first item
00141         ++s;
00142 
00143         stxxl::uint64 count = n_records - 1;
00144 
00145         while ( !s.empty() )
00146         {
00147             assert( s.size() == count );
00148 
00149             if ( !(prev <= *s) ) STXXL_MSG("WRONG");
00150             assert( prev <= *s );
00151 
00152             ++s; --count;
00153         }
00154         STXXL_MSG("OK");
00155 
00156         // rewind and read output again
00157         s.rewind();
00158 
00159         STXXL_MSG("Checking order again...");
00160         
00161         assert( !s.empty() );
00162         assert( s.size() == n_records );
00163 
00164         prev = *s;      // get first item
00165         ++s;
00166 
00167         while ( !s.empty() )
00168         {
00169             if ( !(prev <= *s) ) STXXL_MSG("WRONG");
00170             assert( prev <= *s );
00171 
00172             ++s;
00173         }
00174         STXXL_MSG("OK");
00175 
00176         assert( s.size() == 0 );
00177 
00178         STXXL_MSG("Done");
00179     }
00180 
00181     return 0;
00182 }
00183 
00184 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines