http://stxxl.sourceforge.net
<tb@panthema.net>
http://www.boost.org/LICENSE_1_0.txt
#include <stxxl/stream>
#include <stxxl/vector>
#include <stxxl/sorter>
#include <vector>
#include <climits>
struct counter_object
{
typedef int value_type;
private:
int m_current_value;
public:
counter_object()
: m_current_value(1)
{ }
const value_type& operator * () const
{
return m_current_value;
}
counter_object& operator ++ ()
{
++m_current_value;
return *this;
}
bool empty() const
{
return (m_current_value > 1000);
}
};
template <typename InputStream>
struct squaring_object
{
typedef int value_type;
private:
InputStream& m_input_stream;
value_type m_current_value;
public:
squaring_object(InputStream& input_stream)
: m_input_stream(input_stream)
{
if (!m_input_stream.empty())
{
m_current_value = *m_input_stream;
m_current_value = m_current_value * m_current_value;
}
}
const value_type& operator * () const
{
return m_current_value;
}
squaring_object& operator ++ ()
{
++m_input_stream;
if (!m_input_stream.empty())
{
m_current_value = *m_input_stream;
m_current_value = m_current_value * m_current_value;
}
return *this;
}
bool empty() const
{
return m_input_stream.empty();
}
};
struct CompareMod10
{
inline bool operator () (int a, int b) const
{
if ((a % 10) == (b % 10))
return a < b;
else
return (a % 10) < (b % 10);
}
int min_value() const { return INT_MIN; }
int max_value() const { return INT_MAX; }
};
int main()
{
{
counter_object counter;
while (!counter.empty())
{
std::cout << *counter << " ";
++counter;
}
std::cout << std::endl;
}
{
for (counter_object cnt; !cnt.empty(); ++cnt)
{
std::cout << *cnt << " ";
}
std::cout << std::endl;
}
{
counter_object counter;
squaring_object<counter_object> squares(counter);
while (!squares.empty())
{
std::cout << *squares << " ";
++squares;
}
std::cout << std::endl;
}
{
std::vector<int> intvector;
typedef stxxl::stream::iterator2stream<std::vector<int>::const_iterator> intstream_type;
intstream_type intstream(intvector.begin(), intvector.end());
squaring_object<intstream_type> squares(intstream);
}
{
stxxl::vector<int> intvector;
typedef stxxl::stream::vector_iterator2stream<stxxl::vector<int>::const_iterator> intstream_type;
intstream_type intstream(intvector.begin(), intvector.end());
squaring_object<intstream_type> squares(intstream);
}
{
counter_object counter;
squaring_object<counter_object> squares(counter);
std::vector<int> intvector(100);
stxxl::stream::materialize(squares, intvector.begin(), intvector.end());
}
{
counter_object counter;
squaring_object<counter_object> squares(counter);
stxxl::vector<int> intvector(100);
stxxl::stream::materialize(squares, intvector.begin(), intvector.end());
}
{
static const int ram_use = 10 * 1024 * 1024;
counter_object counter;
typedef stxxl::stream::runs_creator<counter_object, CompareMod10> rc_counter_type;
CompareMod10 comparemod10;
rc_counter_type rc_counter(counter, comparemod10, ram_use);
typedef stxxl::stream::runs_merger<rc_counter_type::sorted_runs_type, CompareMod10> rm_counter_type;
rm_counter_type rm_counter(rc_counter.result(), comparemod10, ram_use);
while (!rm_counter.empty())
{
std::cout << *rm_counter << " ";
++rm_counter;
}
std::cout << std::endl;
}
{
static const int ram_use = 10 * 1024 * 1024;
typedef stxxl::stream::runs_creator<stxxl::stream::use_push<int>, CompareMod10> rc_counter_type;
CompareMod10 comparemod10;
rc_counter_type rc_counter(comparemod10, ram_use);
for (int i = 1; i <= 1000; ++i)
rc_counter.push(i);
typedef stxxl::stream::runs_merger<rc_counter_type::sorted_runs_type, CompareMod10> rm_counter_type;
rm_counter_type rm_counter(rc_counter.result(), comparemod10, ram_use);
while (!rm_counter.empty())
{
std::cout << *rm_counter << " ";
++rm_counter;
}
std::cout << std::endl;
}
{
static const int ram_use = 10 * 1024 * 1024;
typedef stxxl::sorter<int, CompareMod10> sr_counter_type;
CompareMod10 comparemod10;
sr_counter_type sr_counter(comparemod10, ram_use);
for (int i = 1; i <= 1000; ++i)
sr_counter.push(i);
sr_counter.sort();
while (!sr_counter.empty())
{
std::cout << *sr_counter << " ";
++sr_counter;
}
std::cout << std::endl;
}
}