Stxxl  1.4.0
stream/test_sorted_runs.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  stream/test_sorted_runs.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2003 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_sorted_runs.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 from sorted sequences
00018 //! using \c stream::from_sorted_sequences 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 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 
00046 int main()
00047 {
00048 #if STXXL_PARALLEL_MULTIWAY_MERGE
00049     STXXL_MSG("STXXL_PARALLEL_MULTIWAY_MERGE");
00050 #endif
00051     // special parameter type
00052     typedef stxxl::stream::from_sorted_sequences<value_type> InputType;
00053     typedef stxxl::stream::runs_creator<InputType, Cmp, 4096, stxxl::RC> CreateRunsAlg;
00054     typedef CreateRunsAlg::sorted_runs_type SortedRunsType;
00055 
00056     unsigned input_size = (50 * megabyte / sizeof(value_type));
00057 
00058     Cmp c;
00059     CreateRunsAlg SortedRuns(c, 10 * megabyte);
00060     value_type checksum_before(0);
00061 
00062     stxxl::random_number32 rnd;
00063     stxxl::random_number<> rnd_max;
00064     for (unsigned cnt = input_size; cnt > 0; )
00065     {
00066         unsigned run_size = rnd_max(cnt) + 1;           // random run length
00067         cnt -= run_size;
00068         STXXL_MSG("current run size: " << run_size);
00069 
00070         std::vector<unsigned> tmp(run_size);            // create temp storage for current run
00071         // fill with random numbers
00072         std::generate(tmp.begin(), tmp.end(), rnd _STXXL_FORCE_SEQUENTIAL);
00073         std::sort(tmp.begin(), tmp.end(), c);           // sort
00074         for (unsigned j = 0; j < run_size; ++j)
00075         {
00076             checksum_before += tmp[j];
00077             SortedRuns.push(tmp[j]);                    // push sorted values to the current run
00078         }
00079         SortedRuns.finish();                            // finish current run
00080     }
00081 
00082 
00083     SortedRunsType& Runs = SortedRuns.result();          // get sorted_runs data structure
00084     assert(check_sorted_runs(Runs, Cmp()));
00085     // merge the runs
00086     stxxl::stream::runs_merger<SortedRunsType, Cmp> merger(Runs, Cmp(), 10 * megabyte);
00087     stxxl::vector<value_type, 4, stxxl::lru_pager<8>, block_size, STXXL_DEFAULT_ALLOC_STRATEGY> array;
00088     STXXL_MSG(input_size << " " << Runs->elements);
00089     STXXL_MSG("checksum before: " << checksum_before);
00090     value_type checksum_after(0);
00091     for (unsigned i = 0; i < input_size; ++i)
00092     {
00093         checksum_after += *merger;
00094         array.push_back(*merger);
00095         ++merger;
00096     }
00097     STXXL_MSG("checksum after:  " << checksum_after);
00098     assert(stxxl::is_sorted(array.begin(), array.end(), Cmp()));
00099     assert(checksum_before == checksum_after);
00100     assert(merger.empty());
00101 
00102     return 0;
00103 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines