Stxxl
1.4.0
|
00001 /*************************************************************************** 00002 * algo/copy_and_sort_file.cpp 00003 * 00004 * Part of the STXXL. See http://stxxl.sourceforge.net 00005 * 00006 * Copyright (C) 2010 Andreas Beckmann <beckmann@cs.uni-frankfurt.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 //! \example algo/copy_and_sort_file.cpp 00014 //! This example imports a file into an \c stxxl::vector without copying its 00015 //! content and then sorts it using \c stxxl::stream::sort while writing the 00016 //! sorted output to a different file. 00017 00018 #include <limits> 00019 #include <stxxl/io> 00020 #include <stxxl/vector> 00021 #include <stxxl/stream> 00022 00023 00024 struct my_type 00025 { 00026 typedef unsigned key_type; 00027 00028 key_type _key; 00029 char _data[128 - 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 00048 00049 inline bool operator < (const my_type & a, const my_type & b) 00050 { 00051 return a.key() < b.key(); 00052 } 00053 00054 inline bool operator == (const my_type & a, const my_type & b) 00055 { 00056 return a.key() == b.key(); 00057 } 00058 00059 struct Cmp 00060 { 00061 typedef my_type first_argument_type; 00062 typedef my_type second_argument_type; 00063 typedef bool result_type; 00064 bool operator () (const my_type & a, const my_type & b) const 00065 { 00066 return a < b; 00067 } 00068 static my_type min_value() 00069 { 00070 return my_type::min_value(); 00071 } 00072 static my_type max_value() 00073 { 00074 return my_type::max_value(); 00075 } 00076 }; 00077 00078 std::ostream & operator << (std::ostream & o, const my_type & obj) 00079 { 00080 o << obj._key; 00081 return o; 00082 } 00083 00084 00085 int main(int argc, char ** argv) 00086 { 00087 if (argc < 3) 00088 { 00089 std::cout << "Usage: " << argv[0] << " infile outfile" << std::endl; 00090 return -1; 00091 } 00092 00093 const unsigned memory_to_use = 512 * 1024 * 1024; 00094 const unsigned int block_size = sizeof(my_type) * 4096; 00095 00096 typedef stxxl::vector<my_type, 1, stxxl::lru_pager<2>, block_size> vector_type; 00097 00098 stxxl::syscall_file in_file(argv[1], stxxl::file::DIRECT | stxxl::file::RDONLY); 00099 stxxl::syscall_file out_file(argv[2], stxxl::file::DIRECT | stxxl::file::RDWR | stxxl::file::CREAT); 00100 vector_type input(&in_file); 00101 vector_type output(&out_file); 00102 output.resize(input.size()); 00103 00104 #ifdef BOOST_MSVC 00105 typedef stxxl::stream::streamify_traits<vector_type::iterator>::stream_type input_stream_type; 00106 #else 00107 typedef __typeof__(stxxl::stream::streamify(input.begin(), input.end())) input_stream_type; 00108 #endif 00109 input_stream_type input_stream = stxxl::stream::streamify(input.begin(), input.end()); 00110 00111 typedef Cmp comparator_type; 00112 typedef stxxl::stream::sort<input_stream_type, comparator_type, block_size> sort_stream_type; 00113 sort_stream_type sort_stream(input_stream, comparator_type(), memory_to_use); 00114 00115 vector_type::iterator o = stxxl::stream::materialize(sort_stream, output.begin(), output.end()); 00116 assert(o == output.end()); 00117 00118 if (1) { 00119 STXXL_MSG("Checking order..."); 00120 STXXL_MSG((stxxl::is_sorted(output.begin(), output.end(), comparator_type()) ? "OK" : "WRONG")); 00121 } 00122 00123 return 0; 00124 } 00125 00126 // vim: et:ts=4:sw=4