Stxxl  1.4.0
algo/test_random_shuffle.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  algo/test_random_shuffle.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2007 Manuel Krings
00007  *  Copyright (C) 2007 Markus Westphal
00008  *  Copyright (C) 2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00009  *
00010  *  Distributed under the Boost Software License, Version 1.0.
00011  *  (See accompanying file LICENSE_1_0.txt or copy at
00012  *  http://www.boost.org/LICENSE_1_0.txt)
00013  **************************************************************************/
00014 
00015 // TODO: test both vector and non-vector variant of random_shuffle
00016 // TODO: test recursion, improve verboseness
00017 
00018 //! \example algo/test_random_shuffle.cpp
00019 //! Test \c stxxl::random_shuffle()
00020 
00021 #include <stxxl/vector>
00022 #include <stxxl/random_shuffle>
00023 
00024 
00025 template <typename type>
00026 struct counter
00027 {
00028     type value;
00029     counter(type v = type(0)) : value(v) { }
00030     type operator () ()
00031     {
00032         type old_val = value;
00033         value++;
00034         return old_val;
00035     }
00036 };
00037 
00038 void long_test()
00039 {
00040     typedef stxxl::vector<int> ext_vec_type;
00041     ext_vec_type STXXLVector(1024 * 1024 * 256 / sizeof(int));
00042 
00043     STXXL_MSG("Filling vector with increasing values...");
00044     stxxl::generate(STXXLVector.begin(), STXXLVector.end(),
00045                     counter<stxxl::uint64>(), 4);
00046 
00047     stxxl::uint64 i;
00048 
00049     STXXL_MSG("Begin: ");
00050     for (i = 0; i < 10; i++)
00051         STXXL_MSG(STXXLVector[i]);
00052 
00053     STXXL_MSG("End: ");
00054     for (i = STXXLVector.size() - 10; i < STXXLVector.size(); i++)
00055         STXXL_MSG(STXXLVector[i]);
00056 
00057     STXXL_MSG("Permute randomly...");
00058     stxxl::random_shuffle(STXXLVector.begin(), STXXLVector.end(), 1024 * 1024 * 128);
00059 
00060 
00061     STXXL_MSG("Begin: ");
00062     for (i = 0; i < 10; i++)
00063         STXXL_MSG(STXXLVector[i]);
00064 
00065     STXXL_MSG("End: ");
00066     for (i = STXXLVector.size() - 10; i < STXXLVector.size(); i++)
00067         STXXL_MSG(STXXLVector[i]);
00068 }
00069 
00070 void short_test()
00071 {
00072     STXXL_STATIC_ASSERT(sizeof(int) == 4);
00073     typedef stxxl::VECTOR_GENERATOR<int, 1, 2, 4096>::result vector_type;
00074     vector_type::size_type i;
00075     vector_type v(2048);
00076     for (i = 0; i < v.size(); ++i)
00077         v[i] = i / 1024 + 1;
00078 
00079     std::cout << v.size() << std::endl;
00080     std::cout << "before shuffle:" << std::endl;
00081     for (i = 0; i < v.size(); ++i) {
00082         std::cout << v[i] << " ";
00083         if ((i & 511) == 511)
00084             std::cout << std::endl;
00085     }
00086     std::cout << std::endl;
00087     v.flush();
00088     stxxl::random_shuffle(v.begin() + 512, v.begin() + 512 + 1024, 64 * 1024);
00089     std::cout << "after shuffle:" << std::endl;
00090     for (i = 0; i < v.size(); ++i) {
00091         std::cout << v[i] << " ";
00092         if ((i & 511) == 511)
00093             std::cout << std::endl;
00094     }
00095     std::cout << std::endl;
00096 }
00097 
00098 int main()
00099 {
00100     short_test();
00101     long_test();
00102 }
00103 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines