File: test_ext_merger.cpp

package info (click to toggle)
libstxxl 1.4.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,476 kB
  • sloc: cpp: 45,101; ansic: 4,071; perl: 610; sh: 555; xml: 174; makefile: 18
file content (102 lines) | stat: -rw-r--r-- 2,913 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/***************************************************************************
 *  tests/containers/test_ext_merger.cpp
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2003 Roman Dementiev <dementiev@mpi-sb.mpg.de>
 *  Copyright (C) 2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
 *
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE_1_0.txt or copy at
 *  http://www.boost.org/LICENSE_1_0.txt)
 **************************************************************************/

#include <limits>
#include <iterator>
#include <stxxl/priority_queue>

typedef int my_type;
typedef stxxl::typed_block<4096, my_type> block_type;

struct dummy_merger
{
    int& cnt;
    dummy_merger(int& c) : cnt(c) { }
    template <class OutputIterator>
    void multi_merge(OutputIterator b, OutputIterator e)
    {
        while (b != e)
        {
            * b = cnt;
            ++b;
            ++cnt;
        }
    }
};

struct my_cmp : public std::greater<my_type>
{
    my_type min_value() const
    {
        return std::numeric_limits<my_type>::max();
    }
    my_type max_value() const
    {
        return std::numeric_limits<my_type>::min();
    }
};

my_type * make_sequence(dummy_merger& dummy, int l)
{
    my_type* seq = new my_type[l + 1];  // + sentinel
    dummy.multi_merge(seq, seq + l);
    seq[l] = my_cmp().min_value();      // sentinel
    return seq;
}

// forced instantiation
template class stxxl::priority_queue_local::ext_merger<block_type, my_cmp, 5>;
template class stxxl::priority_queue_local::loser_tree<my_type, my_cmp, 8>;

using stxxl::priority_queue_local::ext_merger;
using stxxl::priority_queue_local::loser_tree;

int main()
{
    stxxl::read_write_pool<block_type> pool(1, 2);
    int cnt = 0;
    dummy_merger dummy(cnt);
    std::vector<my_type> output(1024 * 3);

    ext_merger<block_type, my_cmp, 5> merger(&pool);
    merger.insert_segment(dummy, 1024 * 3);
    cnt = 20;
    merger.insert_segment(dummy, 1024 * 4);
    cnt = 10;
    merger.insert_segment(dummy, 1024 * 4);
    cnt = -100;
    merger.insert_segment(dummy, 1024 * 4);
    merger.insert_segment(dummy, 1024 * 4);
    merger.multi_merge(output.begin(), output.end());
    STXXL_CHECK(stxxl::is_sorted(output.begin(), output.end()));

    loser_tree<my_type, my_cmp, 8> loser;
    my_type* seq1 = make_sequence(dummy, 1024);
    cnt = 20;
    my_type* seq2 = make_sequence(dummy, 1024);
    cnt = 10;
    my_type* seq3 = make_sequence(dummy, 1024);
    cnt = -100;
    my_type* seq4 = make_sequence(dummy, 1024);
    my_type* out = new my_type[4 * 1024];
    loser.init();
    loser.insert_segment(seq1, 1024);
    loser.insert_segment(seq2, 1024);
    loser.insert_segment(seq3, 1024);
    loser.insert_segment(seq4, 1024);

    loser.multi_merge(out, out + 1024);
    STXXL_CHECK(stxxl::is_sorted(out, out + 1024));

    delete[] out;
}