Stxxl
1.4.0
|
00001 // This file contains the example snippets from the stream introduction. 00002 00003 #include <stxxl/stream> 00004 #include <stxxl/vector> 00005 #include <stxxl/sorter> 00006 00007 #include <vector> 00008 #include <limits.h> 00009 00010 struct counter_object 00011 { 00012 // This stream produces a sequence of integers. 00013 typedef int value_type; 00014 00015 private: 00016 // A class attribute to save the current value. 00017 int m_current_value; 00018 00019 public: 00020 // A constructor to set the initial value to 1. 00021 counter_object() 00022 : m_current_value(1) 00023 { 00024 } 00025 00026 // The retrieve operator returning the current value. 00027 const value_type& operator* () const 00028 { 00029 return m_current_value; 00030 } 00031 00032 // Increment operator advancing to the next integer. 00033 counter_object& operator++ () 00034 { 00035 ++m_current_value; 00036 return *this; 00037 } 00038 00039 // Empty indicator, which in this case can check the current value. 00040 bool empty() const 00041 { 00042 return (m_current_value > 1000); 00043 } 00044 }; 00045 00046 template <typename InputStream> 00047 struct squaring_object 00048 { 00049 // This stream produces a sequence of integers. 00050 typedef int value_type; 00051 00052 private: 00053 // A reference to another stream of integers, which are our input. 00054 InputStream& m_input_stream; 00055 00056 // A temporary value buffer to hold the current square in for retrieval. 00057 value_type m_current_value; 00058 00059 public: 00060 // A constructor taking another stream of integers as input. 00061 squaring_object(InputStream& input_stream) 00062 : m_input_stream(input_stream) 00063 { 00064 if (!m_input_stream.empty()) 00065 { 00066 m_current_value = *m_input_stream; 00067 m_current_value = m_current_value * m_current_value; 00068 } 00069 } 00070 00071 // The retrieve operator returning the square of the input stream. 00072 const value_type& operator* () const 00073 { 00074 return m_current_value; 00075 } 00076 00077 // Increment operator: handled by incrementing the input stream. 00078 squaring_object& operator++ () 00079 { 00080 ++m_input_stream; 00081 if (!m_input_stream.empty()) 00082 { 00083 m_current_value = *m_input_stream; 00084 m_current_value = m_current_value * m_current_value; 00085 } 00086 return *this; 00087 } 00088 00089 // Empty indicator: this stream is empty when the input stream is. 00090 bool empty() const 00091 { 00092 return m_input_stream.empty(); 00093 } 00094 }; 00095 00096 // define comparator class: compare right-most decimal and then absolute value 00097 struct CompareMod10 00098 { 00099 // comparison operator() returning true if (a < b) 00100 inline bool operator() (int a, int b) const 00101 { 00102 if ((a % 10) == (b % 10)) 00103 return a < b; 00104 else 00105 return (a % 10) < (b % 10); 00106 } 00107 00108 // smallest possible integer value 00109 int min_value() const { return INT_MIN; } 00110 // largest possible integer value 00111 int max_value() const { return INT_MAX; } 00112 }; 00113 00114 int main() 00115 { 00116 { 00117 counter_object counter; 00118 00119 while (!counter.empty()) 00120 { 00121 std::cout << *counter << " "; 00122 ++counter; 00123 } 00124 std::cout << std::endl; 00125 } 00126 00127 { 00128 for (counter_object cnt; !cnt.empty(); ++cnt) 00129 { 00130 std::cout << *cnt << " "; 00131 } 00132 std::cout << std::endl; 00133 } 00134 00135 { 00136 counter_object counter; 00137 squaring_object<counter_object> squares(counter); 00138 00139 while (!squares.empty()) 00140 { 00141 std::cout << *squares << " "; 00142 ++squares; 00143 } 00144 std::cout << std::endl; 00145 } 00146 00147 { 00148 std::vector<int> intvector; 00149 // (fill intvector) 00150 00151 // define stream class iterating over an integer vector 00152 typedef stxxl::stream::iterator2stream< std::vector<int>::const_iterator > intstream_type; 00153 00154 // instantiate the stream object, iterate from begin to end of intvector. 00155 intstream_type intstream (intvector.begin(), intvector.end()); 00156 00157 // plug in squaring object after vector iterator stream. 00158 squaring_object<intstream_type> squares(intstream); 00159 } 00160 00161 { 00162 stxxl::vector<int> intvector; 00163 // (fill intvector) 00164 00165 // define stream class iterating over an integer vector 00166 typedef stxxl::stream::vector_iterator2stream< stxxl::vector<int>::const_iterator > intstream_type; 00167 00168 // instantiate the stream object, iterate from begin to end of intvector. 00169 intstream_type intstream (intvector.begin(), intvector.end()); 00170 00171 // plug in squaring object after vector iterator stream. 00172 squaring_object<intstream_type> squares(intstream); 00173 } 00174 00175 { 00176 // construct the squared counter stream 00177 counter_object counter; 00178 squaring_object<counter_object> squares(counter); 00179 00180 // allocate vector of 100 integers 00181 std::vector<int> intvector (100); 00182 00183 // materialize 100 integers from stream and put into vector 00184 stxxl::stream::materialize(squares, intvector.begin(), intvector.end()); 00185 } 00186 00187 { 00188 // construct the squared counter stream 00189 counter_object counter; 00190 squaring_object<counter_object> squares(counter); 00191 00192 // allocate STXXL vector of 100 integers 00193 stxxl::vector<int> intvector (100); 00194 00195 // materialize 100 integers from stream and put into STXXL vector 00196 stxxl::stream::materialize(squares, intvector.begin(), intvector.end()); 00197 } 00198 00199 { 00200 static const int ram_use = 10*1024*1024; // amount of memory to use in runs creation 00201 00202 counter_object counter; // the counter stream from first examples 00203 00204 // define a runs sorter for the counter stream which order by CompareMod10 object. 00205 typedef stxxl::stream::runs_creator<counter_object, CompareMod10> rc_counter_type; 00206 00207 // instance of CompareMod10 comparator class 00208 CompareMod10 comparemod10; 00209 00210 // instance of runs_creator which reads the counter stream. 00211 rc_counter_type rc_counter (counter, comparemod10, ram_use); 00212 00213 // define a runs merger for the sorted runs from rc_counter. 00214 typedef stxxl::stream::runs_merger<rc_counter_type::sorted_runs_type, CompareMod10> rm_counter_type; 00215 00216 // instance of runs_merger which merges sorted runs from rc_counter. 00217 rm_counter_type rm_counter (rc_counter.result(), comparemod10, ram_use); 00218 00219 // read sorted stream: runs_merger also conforms to the stream interface. 00220 while (!rm_counter.empty()) 00221 { 00222 std::cout << *rm_counter << " "; 00223 ++rm_counter; 00224 } 00225 std::cout << std::endl; 00226 } 00227 00228 { 00229 static const int ram_use = 10*1024*1024; // amount of memory to use in runs creation 00230 00231 // define a runs sorter which accepts imperative push()s and orders by CompareMod10 object. 00232 typedef stxxl::stream::runs_creator<stxxl::stream::use_push<int>, CompareMod10> rc_counter_type; 00233 00234 // instance of CompareMod10 comparator class. 00235 CompareMod10 comparemod10; 00236 00237 // instance of runs_creator which waits for input. 00238 rc_counter_type rc_counter (comparemod10, ram_use); 00239 00240 // write sequence of integers into runs 00241 for (int i = 1; i <= 1000; ++i) 00242 rc_counter.push(i); 00243 00244 // define a runs merger for the sorted runs from rc_counter. 00245 typedef stxxl::stream::runs_merger<rc_counter_type::sorted_runs_type, CompareMod10> rm_counter_type; 00246 00247 // instance of runs_merger which merges sorted runs from rc_counter. 00248 rm_counter_type rm_counter (rc_counter.result(), comparemod10, ram_use); 00249 00250 // read sorted stream: runs_merger also conforms to the stream interface. 00251 while (!rm_counter.empty()) 00252 { 00253 std::cout << *rm_counter << " "; 00254 ++rm_counter; 00255 } 00256 std::cout << std::endl; 00257 } 00258 00259 { 00260 static const int ram_use = 10*1024*1024; // amount of memory to use in runs creation 00261 00262 // define a runs sorter which accepts imperative push()s and orders by CompareMod10 object. 00263 typedef stxxl::sorter<int, CompareMod10> sr_counter_type; 00264 00265 // instance of CompareMod10 comparator class. 00266 CompareMod10 comparemod10; 00267 00268 // instance of sorter which waits for input. 00269 sr_counter_type sr_counter (comparemod10, ram_use); 00270 00271 // write sequence of integers into sorter, which creates sorted runs 00272 for (int i = 1; i <= 1000; ++i) 00273 sr_counter.push(i); 00274 00275 // signal sorter that the input stream is finished and switch to output mode. 00276 sr_counter.sort(); 00277 00278 // read sorted stream: sorter also conforms to the stream interface. 00279 while (!sr_counter.empty()) 00280 { 00281 std::cout << *sr_counter << " "; 00282 ++sr_counter; 00283 } 00284 std::cout << std::endl; 00285 } 00286 } 00287