Stxxl  1.4.0
containers/test_ext_merger.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  containers/test_ext_merger.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  *  Copyright (C) 2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00008  *
00009  *  Distributed under the Boost Software License, Version 1.0.
00010  *  (See accompanying file LICENSE_1_0.txt or copy at
00011  *  http://www.boost.org/LICENSE_1_0.txt)
00012  **************************************************************************/
00013 
00014 #include <limits>
00015 #include <iterator>
00016 #include <stxxl/priority_queue>
00017 
00018 using stxxl::priority_queue_local::ext_merger;
00019 using stxxl::priority_queue_local::loser_tree;
00020 
00021 typedef int my_type;
00022 typedef stxxl::typed_block<4096, my_type> block_type;
00023 
00024 
00025 struct dummy_merger
00026 {
00027     int & cnt;
00028     dummy_merger(int & c) : cnt(c) { }
00029     template <class OutputIterator>
00030     void multi_merge(OutputIterator b, OutputIterator e)
00031     {
00032         while (b != e)
00033         {
00034             *b = cnt;
00035             ++b;
00036             ++cnt;
00037         }
00038     }
00039 };
00040 
00041 struct my_cmp : public std::greater<my_type>
00042 {
00043     my_type min_value() const
00044     {
00045         return (std::numeric_limits<my_type>::max)();
00046     }
00047     my_type max_value() const
00048     {
00049         return (std::numeric_limits<my_type>::min)();
00050     }
00051 };
00052 
00053 my_type * make_sequence(dummy_merger & dummy, int l)
00054 {
00055     my_type * seq = new my_type[l + 1]; // + sentinel
00056     dummy.multi_merge(seq, seq + l);
00057     seq[l] = my_cmp().min_value();      // sentinel
00058     return seq;
00059 }
00060 
00061 int main()
00062 {
00063     stxxl::read_write_pool<block_type> pool(1, 2);
00064     int cnt = 0;
00065     dummy_merger dummy(cnt);
00066     std::vector<my_type> output(1024 * 3);
00067 
00068     ext_merger<block_type, my_cmp, 5> merger(&pool);
00069     merger.insert_segment(dummy, 1024 * 3);
00070     cnt = 20;
00071     merger.insert_segment(dummy, 1024 * 4);
00072     cnt = 10;
00073     merger.insert_segment(dummy, 1024 * 4);
00074     cnt = -100;
00075     merger.insert_segment(dummy, 1024 * 4);
00076     merger.insert_segment(dummy, 1024 * 4);
00077     merger.multi_merge(output.begin(), output.end());
00078 
00079     loser_tree<my_type, my_cmp, 8> loser;
00080     my_type * seq1 = make_sequence(dummy, 1024);
00081     cnt = 20;
00082     my_type * seq2 = make_sequence(dummy, 1024);
00083     cnt = 10;
00084     my_type * seq3 = make_sequence(dummy, 1024);
00085     cnt = -100;
00086     my_type * seq4 = make_sequence(dummy, 1024);
00087     my_type * out = new my_type[4 * 1024];
00088     loser.init();
00089     loser.insert_segment(seq1, 1024);
00090     loser.insert_segment(seq2, 1024);
00091     loser.insert_segment(seq3, 1024);
00092     loser.insert_segment(seq4, 1024);
00093 
00094     loser.multi_merge(out, out + 1024);
00095     std::copy(out, out + 1024, std::ostream_iterator<my_type>(std::cout, "\n"));
00096 
00097     delete[] out;
00098 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines