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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/***************************************************************************
* tests/stream/test_loop.cpp
*
* example for building a loop of stream operations
*
* Part of the STXXL. See http://stxxl.sourceforge.net
*
* Copyright (C) 2011 Jaroslaw Fedorowicz <fedorow@cs.uni-frankfurt.de>
* Copyright (C) 2011 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)
**************************************************************************/
//! \example stream/test_loop.cpp
//! This is an example of how to use some basic algorithms from the
//! stream package to form a loop iterating over the data.
//! Some input is generated, sorted, some elements are filtered out.
//! The remaining elements are transformed, sorted and processed in the
//! next pass. The loop will terminate if at most one element remains.
//! A split sorter is used to cut the data flow (and type dependency) cycle.
#include <iostream>
#include <limits>
#include <stxxl/mng>
#include <stxxl/vector>
#include <stxxl/stream>
using std::cout;
using std::endl;
const stxxl::uint64 memory_to_use = 128ul * 1024 * 1024;
bool verbose;
struct random_generator
{
typedef stxxl::random_number32::value_type value_type;
typedef stxxl::uint64 size_type;
value_type current;
size_type count;
stxxl::random_number32 rnd;
random_generator(size_type _count) : count(_count)
{
if (verbose) cout << "Random Stream: ";
current = rnd();
}
value_type operator * () const
{
return current;
}
random_generator& operator ++ ()
{
count--;
if (verbose) {
cout << current << ", ";
if (empty()) cout << endl;
}
current = rnd();
return *this;
}
bool empty() const
{
return (count == 0);
}
};
template <typename value_type>
struct Cmp : std::binary_function<value_type, value_type, bool>
{
bool operator () (const value_type& a, const value_type& b) const
{
return a < b;
}
static value_type min_value()
{
return value_type(std::numeric_limits<value_type>::min());
}
static value_type max_value()
{
return value_type(std::numeric_limits<value_type>::max());
}
};
template <typename Input>
struct filter
{
typedef typename Input::value_type value_type;
typedef stxxl::uint64 size_type;
Input& input;
value_type filter_value;
size_type& counter;
void apply_filter()
{
while (!input.empty() && *input == filter_value) {
++input;
counter++;
}
}
filter(Input& _input, value_type _filter_value, size_type& _counter) : input(_input), filter_value(_filter_value), counter(_counter)
{
apply_filter();
}
const value_type operator * () const
{
return *input;
}
filter& operator ++ ()
{
++input;
apply_filter();
return *this;
}
bool empty() const
{
return input.empty();
}
};
template <typename Input>
struct output
{
typedef typename Input::value_type value_type;
Input& input;
output(Input& _input) : input(_input) { }
const value_type operator * () const
{
return *input;
}
output& operator ++ ()
{
if (verbose) cout << *input << ", ";
++input;
if (empty() && verbose)
cout << endl;
return *this;
}
bool empty() const
{
return input.empty();
}
};
template <typename Input>
struct shuffle
{
typedef typename Input::value_type value_type;
Input& input;
value_type current, next;
bool even, is_empty;
// from http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
int count_bits(stxxl::uint64 v)
{
int c;
for (c = 0; v; c++) {
v &= v - 1;
}
return c;
}
void apply_shuffle()
{
is_empty = input.empty();
if (!is_empty) {
current = *input;
++input;
if (!input.empty()) {
STXXL_STATIC_ASSERT(sizeof(value_type) == 4);
stxxl::uint64 combined = current;
combined = combined << 32 | *input;
combined = (1ul << count_bits(combined)) - 1;
current = combined >> 32;
next = (value_type)combined;
}
}
}
shuffle(Input& _input) : input(_input), current(0), next(0), even(true), is_empty(false)
{
apply_shuffle();
}
value_type operator * () const
{
return current;
}
shuffle& operator ++ ()
{
even = !even;
is_empty = input.empty();
if (even) {
++input;
apply_shuffle();
} else {
current = next;
}
return *this;
}
bool empty() const
{
return is_empty;
}
};
typedef random_generator input_generator_type;
typedef Cmp<input_generator_type::value_type> cmp;
typedef stxxl::stream::runs_creator<input_generator_type, cmp> runs_creator_type0;
typedef runs_creator_type0::sorted_runs_type sorted_runs_type;
typedef stxxl::stream::runs_merger<sorted_runs_type, cmp> runs_merger_type;
typedef output<runs_merger_type> output_type;
typedef filter<output_type> filter_type0;
typedef filter<filter_type0> filter_type1;
typedef shuffle<filter_type1> shuffle_type;
typedef stxxl::stream::runs_creator<shuffle_type, cmp> runs_creator_type1;
// force instantiation of whole chain
template class stxxl::stream::runs_creator<shuffle_type, cmp>;
int main(int argc, char** argv)
{
if (argc < 2) {
cout << "Usage: " << argv[0] << " count [Options]\nOptions: -v \t prints elements of each iteration\n";
return EXIT_FAILURE;
}
stxxl::block_manager::get_instance();
verbose = (argc == 3) && !strcmp(argv[2], "-v");
stxxl::uint64 total = stxxl::atouint64(argv[1]);
input_generator_type input_stream(total);
runs_creator_type0 runs_creator(input_stream, cmp(), memory_to_use);
sorted_runs_type sorted_runs = runs_creator.result();
stxxl::uint64 counter = 0;
int i;
for (i = 0; counter < total - 1; ++i) {
if (verbose) cout << "Iteration " << i << ": ";
runs_merger_type runs_merger(sorted_runs, cmp(), memory_to_use);
output_type output_stream(runs_merger);
filter_type0 filter0(output_stream, 0, counter);
filter_type1 filter1(filter0, filter_type1::value_type(-1), counter);
shuffle_type shuffled_stream(filter1);
runs_creator_type1 runs_creator(shuffled_stream, cmp(), memory_to_use);
sorted_runs = runs_creator.result();
}
runs_merger_type runs_merger(sorted_runs, cmp(), memory_to_use);
while (!runs_merger.empty()) {
if (verbose) cout << "Iteration " << i << ": " << *runs_merger << endl;
++runs_merger;
}
cout << "\nIteration needed: " << i << endl;
}
// vim: et:ts=4:sw=4
|