Stxxl
1.4.0
|
00001 /*************************************************************************** 00002 * stream/test_stream1.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 * 00008 * Distributed under the Boost Software License, Version 1.0. 00009 * (See accompanying file LICENSE_1_0.txt or copy at 00010 * http://www.boost.org/LICENSE_1_0.txt) 00011 **************************************************************************/ 00012 00013 #include <limits> 00014 #include <stxxl/stream> 00015 00016 00017 struct Input 00018 { 00019 typedef unsigned value_type; 00020 value_type value; 00021 value_type rnd_value; 00022 stxxl::random_number32 rnd; 00023 value_type crc; 00024 Input(value_type init) : value(init) 00025 { 00026 rnd_value = rnd(); 00027 crc = rnd_value; 00028 } 00029 bool empty() const 00030 { 00031 return value == 1; 00032 } 00033 Input & operator ++ () 00034 { 00035 --value; 00036 rnd_value = rnd(); 00037 if (!empty()) 00038 crc += rnd_value; 00039 00040 return *this; 00041 } 00042 const value_type & operator * () const 00043 { 00044 return rnd_value; 00045 } 00046 }; 00047 00048 struct Cmp : std::binary_function<unsigned, unsigned, bool> 00049 { 00050 typedef unsigned value_type; 00051 bool operator () (const value_type & a, const value_type & b) const 00052 { 00053 return a < b; 00054 } 00055 value_type min_value() const 00056 { 00057 return (std::numeric_limits<value_type>::min)(); 00058 } 00059 value_type max_value() const 00060 { 00061 return (std::numeric_limits<value_type>::max)(); 00062 } 00063 }; 00064 00065 #define MULT (1000) 00066 00067 int main() 00068 { 00069 typedef stxxl::stream::runs_creator<Input, Cmp, 4096 * MULT, stxxl::RC> CreateRunsAlg; 00070 typedef CreateRunsAlg::sorted_runs_type SortedRunsType; 00071 00072 stxxl::stats * s = stxxl::stats::get_instance(); 00073 00074 std::cout << *s; 00075 00076 STXXL_MSG("Size of block type " << sizeof(CreateRunsAlg::block_type)); 00077 unsigned size = MULT * 1024 * 128 / (sizeof(Input::value_type) * 2); 00078 Input in(size + 1); 00079 CreateRunsAlg SortedRuns(in, Cmp(), 1024 * 128 * MULT); 00080 SortedRunsType& Runs = SortedRuns.result(); 00081 assert(stxxl::stream::check_sorted_runs(Runs, Cmp())); 00082 // merge the runs 00083 stxxl::stream::runs_merger<SortedRunsType, Cmp> merger(Runs, Cmp(), MULT * 1024 * 128); 00084 stxxl::vector<Input::value_type> array; 00085 STXXL_MSG(size << " " << Runs->elements); 00086 STXXL_MSG("CRC: " << in.crc); 00087 Input::value_type crc(0); 00088 for (unsigned i = 0; i < size; ++i) 00089 { 00090 //STXXL_MSG(*merger<< " must be "<< i+2 << ((*merger != i+2)?" WARNING":"")); 00091 //assert(*merger == i+2); 00092 crc += *merger; 00093 array.push_back(*merger); 00094 ++merger; 00095 } 00096 STXXL_MSG("CRC: " << crc); 00097 assert(stxxl::is_sorted(array.begin(), array.end(), Cmp())); 00098 assert(merger.empty()); 00099 00100 std::cout << *s; 00101 00102 return 0; 00103 }