algo/test_stable_ksort.cpp

This is an example of how to use stxxl::ksort() algorithm

00001 /***************************************************************************
00002  *  algo/test_stable_ksort.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 
00015 
00016 #include <stxxl/mng>
00017 #include <stxxl/stable_ksort>
00018 #include <stxxl/ksort>
00019 #include <stxxl/vector>
00020 
00021 
00022 struct my_type
00023 {
00024     typedef unsigned key_type;
00025 
00026     key_type _key;
00027     char _data[128 - sizeof(key_type)];
00028     key_type key() const
00029     {
00030         return _key;
00031     }
00032 
00033     my_type() { }
00034     my_type(key_type __key) : _key(__key) { }
00035 
00036     static my_type min_value()
00037     {
00038         return my_type((std::numeric_limits<key_type>::min)());
00039     }
00040     static my_type max_value()
00041     {
00042         return my_type((std::numeric_limits<key_type>::max)());
00043     }
00044 };
00045 
00046 bool operator < (const my_type & a, const my_type & b)
00047 {
00048     return a.key() < b.key();
00049 }
00050 
00051 int main()
00052 {
00053     unsigned memory_to_use = 44 * 1024 * 1024;
00054     typedef stxxl::vector<my_type> vector_type;
00055     const stxxl::int64 n_records = 2 * 32 * stxxl::int64(1024 * 1024) / sizeof(my_type);
00056     vector_type v(n_records);
00057 
00058     stxxl::random_number32 rnd;
00059     STXXL_MSG("Filling vector... " << rnd() << " " << rnd() << " " << rnd());
00060     for (vector_type::size_type i = 0; i < v.size(); i++)
00061         v[i]._key = (rnd() / 2) * 2;
00062 
00063 
00064     STXXL_MSG("Checking order...");
00065     STXXL_MSG(((stxxl::is_sorted(v.begin(), v.end())) ? "OK" : "WRONG"));
00066 
00067     STXXL_MSG("Sorting...");
00068     stxxl::stable_ksort(v.begin(), v.end(), memory_to_use);
00069 
00070     STXXL_MSG("Checking order...");
00071     STXXL_MSG(((stxxl::is_sorted(v.begin(), v.end())) ? "OK" : "WRONG"));
00072 
00073 
00074     return 0;
00075 }

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