algo/sort_file.cpp

This example imports a file into an stxxl::vector without copying its content and then sorts it using stxxl::sort.

00001 /***************************************************************************
00002  *  algo/sort_file.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002-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 
00016 
00017 #include <stxxl/io>
00018 #include <stxxl/mng>
00019 #include <stxxl/ksort>
00020 #include <stxxl/sort>
00021 #include <stxxl/vector>
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 bool operator < (const my_type & a, const my_type & b)
00050 {
00051     return a.key() < b.key();
00052 }
00053 
00054 struct Cmp
00055 {
00056     bool operator () (const my_type & a, const my_type & b) const
00057     {
00058         return a < b;
00059     }
00060     static my_type min_value()
00061     {
00062         return my_type::min_value();
00063     }
00064     static my_type max_value()
00065     {
00066         return my_type::max_value();
00067     }
00068 };
00069 
00070 std::ostream & operator << (std::ostream & o, const my_type & obj)
00071 {
00072     o << obj._key;
00073     return o;
00074 }
00075 
00076 int main()
00077 {
00078     stxxl::syscall_file f("./in", stxxl::file::DIRECT | stxxl::file::RDWR);
00079 
00080     unsigned memory_to_use = 50 * 1024 * 1024;
00081     typedef stxxl::vector<my_type> vector_type;
00082     {
00083         vector_type v(&f);
00084 
00085         /*
00086                 STXXL_MSG("Printing...");
00087                 for(stxxl::int64 i=0; i < v.size(); i++)
00088                         STXXL_MSG(v[i].key());
00089          */
00090 
00091         STXXL_MSG("Checking order...");
00092         STXXL_MSG(((stxxl::is_sorted(v.begin(), v.end())) ? "OK" : "WRONG"));
00093 
00094         STXXL_MSG("Sorting...");
00095         stxxl::ksort(v.begin(), v.end(), memory_to_use);
00096         //stxxl::sort(v.begin(),v.end()-101,Cmp(),memory_to_use);
00097 
00098         STXXL_MSG("Checking order...");
00099         STXXL_MSG(((stxxl::is_sorted(v.begin(), v.end())) ? "OK" : "WRONG"));
00100     }
00101     STXXL_MSG("OK");
00102 
00103     return 0;
00104 }

Generated on Thu Jun 4 10:21:43 2009 for Stxxl by  doxygen 1.4.7