File: stream1.cpp

package info (click to toggle)
libstxxl 1.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,256 kB
  • ctags: 6,830
  • sloc: cpp: 39,594; ansic: 4,217; perl: 566; sh: 555; xml: 174; makefile: 21
file content (297 lines) | stat: -rw-r--r-- 9,124 bytes parent folder | download | duplicates (5)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
/***************************************************************************
 *  examples/stream/stream1.cpp
 *
 *  This file contains the example snippets from the stream tutorial.
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2012-2013 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)
 **************************************************************************/

#include <stxxl/stream>
#include <stxxl/vector>
#include <stxxl/sorter>

#include <vector>
#include <climits>

struct counter_object
{
    // This stream produces a sequence of integers.
    typedef int value_type;

private:
    // A class attribute to save the current value.
    int m_current_value;

public:
    // A constructor to set the initial value to 1.
    counter_object()
        : m_current_value(1)
    { }

    // The retrieve operator returning the current value.
    const value_type& operator * () const
    {
        return m_current_value;
    }

    // Increment operator advancing to the next integer.
    counter_object& operator ++ ()
    {
        ++m_current_value;
        return *this;
    }

    // Empty indicator, which in this case can check the current value.
    bool empty() const
    {
        return (m_current_value > 1000);
    }
};

template <typename InputStream>
struct squaring_object
{
    // This stream produces a sequence of integers.
    typedef int value_type;

private:
    // A reference to another stream of integers, which are our input.
    InputStream& m_input_stream;

    // A temporary value buffer to hold the current square in for retrieval.
    value_type m_current_value;

public:
    // A constructor taking another stream of integers as input.
    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;
        }
    }

    // The retrieve operator returning the square of the input stream.
    const value_type& operator * () const
    {
        return m_current_value;
    }

    // Increment operator: handled by incrementing the input stream.
    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;
    }

    // Empty indicator: this stream is empty when the input stream is.
    bool empty() const
    {
        return m_input_stream.empty();
    }
};

// define comparator class: compare right-most decimal and then absolute value
struct CompareMod10
{
    // comparison operator() returning true if (a < b)
    inline bool operator () (int a, int b) const
    {
        if ((a % 10) == (b % 10))
            return a < b;
        else
            return (a % 10) < (b % 10);
    }

    // smallest possible integer value
    int min_value() const { return INT_MIN; }
    // largest possible integer value
    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;
        // (fill intvector)

        // define stream class iterating over an integer vector
        typedef stxxl::stream::iterator2stream<std::vector<int>::const_iterator> intstream_type;

        // instantiate the stream object, iterate from begin to end of intvector.
        intstream_type intstream(intvector.begin(), intvector.end());

        // plug in squaring object after vector iterator stream.
        squaring_object<intstream_type> squares(intstream);
    }

    {
        stxxl::vector<int> intvector;
        // (fill intvector)

        // define stream class iterating over an integer vector
        typedef stxxl::stream::vector_iterator2stream<stxxl::vector<int>::const_iterator> intstream_type;

        // instantiate the stream object, iterate from begin to end of intvector.
        intstream_type intstream(intvector.begin(), intvector.end());

        // plug in squaring object after vector iterator stream.
        squaring_object<intstream_type> squares(intstream);
    }

    {
        // construct the squared counter stream
        counter_object counter;
        squaring_object<counter_object> squares(counter);

        // allocate vector of 100 integers
        std::vector<int> intvector(100);

        // materialize 100 integers from stream and put into vector
        stxxl::stream::materialize(squares, intvector.begin(), intvector.end());
    }

    {
        // construct the squared counter stream
        counter_object counter;
        squaring_object<counter_object> squares(counter);

        // allocate STXXL vector of 100 integers
        stxxl::vector<int> intvector(100);

        // materialize 100 integers from stream and put into STXXL vector
        stxxl::stream::materialize(squares, intvector.begin(), intvector.end());
    }

    {
        static const int ram_use = 10 * 1024 * 1024; // amount of memory to use in runs creation

        counter_object counter;                      // the counter stream from first examples

        // define a runs sorter for the counter stream which order by CompareMod10 object.
        typedef stxxl::stream::runs_creator<counter_object, CompareMod10> rc_counter_type;

        // instance of CompareMod10 comparator class
        CompareMod10 comparemod10;

        // instance of runs_creator which reads the counter stream.
        rc_counter_type rc_counter(counter, comparemod10, ram_use);

        // define a runs merger for the sorted runs from rc_counter.
        typedef stxxl::stream::runs_merger<rc_counter_type::sorted_runs_type, CompareMod10> rm_counter_type;

        // instance of runs_merger which merges sorted runs from rc_counter.
        rm_counter_type rm_counter(rc_counter.result(), comparemod10, ram_use);

        // read sorted stream: runs_merger also conforms to the stream interface.
        while (!rm_counter.empty())
        {
            std::cout << *rm_counter << " ";
            ++rm_counter;
        }
        std::cout << std::endl;
    }

    {
        static const int ram_use = 10 * 1024 * 1024;   // amount of memory to use in runs creation

        // define a runs sorter which accepts imperative push()s and orders by CompareMod10 object.
        typedef stxxl::stream::runs_creator<stxxl::stream::use_push<int>, CompareMod10> rc_counter_type;

        // instance of CompareMod10 comparator class.
        CompareMod10 comparemod10;

        // instance of runs_creator which waits for input.
        rc_counter_type rc_counter(comparemod10, ram_use);

        // write sequence of integers into runs
        for (int i = 1; i <= 1000; ++i)
            rc_counter.push(i);

        // define a runs merger for the sorted runs from rc_counter.
        typedef stxxl::stream::runs_merger<rc_counter_type::sorted_runs_type, CompareMod10> rm_counter_type;

        // instance of runs_merger which merges sorted runs from rc_counter.
        rm_counter_type rm_counter(rc_counter.result(), comparemod10, ram_use);

        // read sorted stream: runs_merger also conforms to the stream interface.
        while (!rm_counter.empty())
        {
            std::cout << *rm_counter << " ";
            ++rm_counter;
        }
        std::cout << std::endl;
    }

    {
        static const int ram_use = 10 * 1024 * 1024;   // amount of memory to use in runs creation

        // define a runs sorter which accepts imperative push()s and orders by CompareMod10 object.
        typedef stxxl::sorter<int, CompareMod10> sr_counter_type;

        // instance of CompareMod10 comparator class.
        CompareMod10 comparemod10;

        // instance of sorter which waits for input.
        sr_counter_type sr_counter(comparemod10, ram_use);

        // write sequence of integers into sorter, which creates sorted runs
        for (int i = 1; i <= 1000; ++i)
            sr_counter.push(i);

        // signal sorter that the input stream is finished and switch to output mode.
        sr_counter.sort();

        // read sorted stream: sorter also conforms to the stream interface.
        while (!sr_counter.empty())
        {
            std::cout << *sr_counter << " ";
            ++sr_counter;
        }
        std::cout << std::endl;
    }
}