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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
/***************************************************************************
* tests/containers/test_sorter.cpp
*
* Part of the STXXL. See http://stxxl.sourceforge.net
*
* Copyright (C) 2012 Timo Bingmann <tb@panthema.net>
*
* 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)
**************************************************************************/
//! \example containers/test_sorter.cpp
//! This is an example of how to use \c stxxl::sorter() container
#include <limits>
#include <stxxl/sorter>
#define RECORD_SIZE 16
struct my_type
{
typedef unsigned key_type;
key_type m_key;
char m_data[RECORD_SIZE - sizeof(key_type)];
key_type key() const
{
return m_key;
}
my_type() { }
my_type(key_type k)
: m_key(k)
{
#if STXXL_WITH_VALGRIND
memset(m_data, 0, sizeof(m_data));
#endif
}
static my_type min_value()
{
return my_type(std::numeric_limits<key_type>::min());
}
static my_type max_value()
{
return my_type(std::numeric_limits<key_type>::max());
}
~my_type() { }
};
std::ostream& operator << (std::ostream& o, const my_type& obj)
{
o << obj.m_key;
return o;
}
bool operator == (const my_type& a, const my_type& b)
{
return a.key() == b.key();
}
bool operator <= (const my_type& a, const my_type& b)
{
return a.key() <= b.key();
}
struct Comparator : public std::less<my_type>
{
inline bool operator () (const my_type& a, const my_type& b) const
{
return a.key() < b.key();
}
my_type min_value() const
{
return my_type::min_value();
}
my_type max_value() const
{
return my_type::max_value();
}
};
// forced instantiation
template class stxxl::sorter<my_type, Comparator, 8192>;
int main()
{
#if STXXL_PARALLEL_MULTIWAY_MERGE
STXXL_MSG("STXXL_PARALLEL_MULTIWAY_MERGE");
#endif
unsigned memory_to_use = 128 * 1024 * 1024;
enum { block_size = 8192 };
// comparator object used for sorters
Comparator cmp;
// construct sorter type: stxxl::sorter<ValueType, ComparatorType, BlockSize>
typedef stxxl::sorter<my_type, Comparator, block_size> sorter_type;
{
// test small number of items that can be sorted internally
sorter_type s(cmp, memory_to_use);
// put in some items
s.push(42);
s.push(0);
s.push(23);
// finish input, switch to sorting stage.
s.sort();
STXXL_CHECK(*s == 0);
++s;
STXXL_CHECK(*s == 23);
++s;
STXXL_CHECK(*s == 42);
++s;
STXXL_CHECK(s.empty());
}
{
// large test with 384 MiB items
const stxxl::uint64 n_records = stxxl::int64(384) * stxxl::int64(1024 * 1024) / sizeof(my_type);
sorter_type s(cmp, memory_to_use);
stxxl::random_number32 rnd;
STXXL_MSG("Filling sorter..., input size = " << n_records << " elements (" << ((n_records * sizeof(my_type)) >> 20) << " MiB)");
for (stxxl::uint64 i = 0; i < n_records; i++)
{
STXXL_CHECK(s.size() == i);
s.push(1 + (rnd() % 0xfffffff));
}
// finish input, switch to sorting stage.
s.sort();
STXXL_MSG("Checking order...");
STXXL_CHECK(!s.empty());
STXXL_CHECK(s.size() == n_records);
my_type prev = *s; // get first item
++s;
stxxl::uint64 count = n_records - 1;
while (!s.empty())
{
STXXL_CHECK(s.size() == count);
if (!(prev <= *s)) STXXL_MSG("WRONG");
STXXL_CHECK(prev <= *s);
++s;
--count;
}
STXXL_MSG("OK");
// rewind and read output again
s.rewind();
STXXL_MSG("Checking order again...");
STXXL_CHECK(!s.empty());
STXXL_CHECK(s.size() == n_records);
prev = *s; // get first item
++s;
while (!s.empty())
{
if (!(prev <= *s)) STXXL_MSG("WRONG");
STXXL_CHECK(prev <= *s);
++s;
}
STXXL_MSG("OK");
STXXL_CHECK(s.size() == 0);
STXXL_MSG("Done");
}
return 0;
}
// vim: et:ts=4:sw=4
|