Stxxl  1.4.0
stream/test_push_sort.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  stream/test_push_sort.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2004 Roman Dementiev <dementiev@mpi-sb.mpg.de>
00007  *  Copyright (C) 2009, 2010 Johannes Singler <singler@kit.edu>
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 stream/test_push_sort.cpp
00015 //! This is an example of how to use some basic algorithms from
00016 //! stream package. This example shows how to create
00017 //! \c sorted_runs data structure
00018 //! using \c stream::use_push specialization of \c stream::runs_creator class
00019 
00020 #include <limits>
00021 #include <stxxl/stream>
00022 
00023 const unsigned long long megabyte = 1024 * 1024;
00024 const unsigned int block_size = 1 * megabyte;
00025 
00026 typedef unsigned value_type;
00027 
00028 struct Cmp : public std::binary_function<value_type, value_type, bool>
00029 {
00030     typedef unsigned value_type;
00031     bool operator () (const value_type & a, const value_type & b) const
00032     {
00033         return a < b;
00034     }
00035     value_type min_value()
00036     {
00037         return (std::numeric_limits<value_type>::min)();
00038     }
00039     value_type max_value()
00040     {
00041         return (std::numeric_limits<value_type>::max)();
00042     }
00043 };
00044 
00045 int main()
00046 {
00047 #if STXXL_PARALLEL_MULTIWAY_MERGE
00048     STXXL_MSG("STXXL_PARALLEL_MULTIWAY_MERGE");
00049 #endif
00050     // special parameter type
00051     typedef stxxl::stream::use_push<value_type> InputType;
00052     typedef stxxl::stream::runs_creator<InputType, Cmp, 4096, stxxl::RC> CreateRunsAlg;
00053     typedef CreateRunsAlg::sorted_runs_type SortedRunsType;
00054 
00055     unsigned input_size = (50 * megabyte / sizeof(value_type));
00056 
00057     Cmp c;
00058     CreateRunsAlg SortedRuns(c, 1 * megabyte / 64);
00059     value_type checksum_before(0);
00060 
00061     stxxl::random_number32 rnd;
00062 
00063     for (unsigned cnt = input_size; cnt > 0; --cnt)
00064     {
00065         const value_type element = rnd();
00066         checksum_before += element;
00067         SortedRuns.push(element);               // push into the sorter
00068     }
00069 
00070     SortedRunsType& Runs = SortedRuns.result();  // get sorted_runs data structure
00071     assert(stxxl::stream::check_sorted_runs(Runs, Cmp()));
00072 
00073     // merge the runs
00074     stxxl::stream::runs_merger<SortedRunsType, Cmp> merger(Runs, Cmp(), 1 * megabyte);
00075     stxxl::vector<value_type, 4, stxxl::lru_pager<8>, block_size, STXXL_DEFAULT_ALLOC_STRATEGY> array;
00076     STXXL_MSG(input_size << " " << Runs->elements);
00077     STXXL_MSG("checksum before: " << checksum_before);
00078     value_type checksum_after(0);
00079     for (unsigned i = 0; i < input_size; ++i)
00080     {
00081         checksum_after += *merger;
00082         array.push_back(*merger);
00083         ++merger;
00084     }
00085     STXXL_MSG("checksum after:  " << checksum_after);
00086     assert(stxxl::is_sorted(array.begin(), array.end(), Cmp()));
00087     assert(checksum_before == checksum_after);
00088     assert(merger.empty());
00089 
00090     return 0;
00091 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines