Stxxl  1.4.0
algo/test_scan.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  algo/test_scan.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002 Roman Dementiev <dementiev@mpi-sb.mpg.de>
00007  *
00008  *  Distributed under the Boost Software License, Version 1.0.
00009  *  (See accompanying file LICENSE_1_0.txt or copy at
00010  *  http://www.boost.org/LICENSE_1_0.txt)
00011  **************************************************************************/
00012 
00013 //! \example algo/test_scan.cpp
00014 //! This is an example of how to use \c stxxl::for_each() and \c stxxl::find() algorithms
00015 
00016 #include <iostream>
00017 #include <algorithm>
00018 
00019 #include <stxxl/vector>
00020 #include <stxxl/scan>
00021 
00022 using stxxl::int64;
00023 using stxxl::timestamp;
00024 
00025 
00026 template <typename type>
00027 struct counter
00028 {
00029     type value;
00030     counter(type v = type(0)) : value(v) { }
00031     type operator () ()
00032     {
00033         type old_val = value;
00034         value++;
00035         return old_val;
00036     }
00037 };
00038 
00039 template <typename type>
00040 struct square
00041 {
00042     void operator () (type & arg)
00043     {
00044         arg = arg * arg;
00045     }
00046 };
00047 
00048 template <typename type>
00049 struct fill_value
00050 {
00051     type val;
00052     fill_value(const type & v_) : val(v_) { }
00053 
00054     type operator () ()
00055     {
00056         return val;
00057     }
00058 };
00059 
00060 int main()
00061 {
00062     stxxl::vector<int64>::size_type i;
00063     stxxl::vector<int64> v(64 * int64(1024 * 1024));
00064     double b, e;
00065 
00066     STXXL_MSG("write " << (v.end() - v.begin()) << " elements ...");
00067 
00068     stxxl::generate(v.begin(), v.end(), counter<int64>(), 4);
00069 
00070 
00071     STXXL_MSG("for_each_m ...");
00072     b = timestamp();
00073     stxxl::for_each_m(v.begin(), v.end(), square<int64>(), 4);
00074     e = timestamp();
00075     STXXL_MSG("for_each_m time: " << (e - b));
00076 
00077 
00078     STXXL_MSG("check");
00079     for (i = 0; i < v.size(); ++i)
00080     {
00081         if (v[i] != int64(i * i))
00082             STXXL_MSG("Error at position " << i);
00083     }
00084 
00085     STXXL_MSG("Pos of value    1023: " << (stxxl::find(v.begin(), v.end(), 1023, 4) - v.begin()));
00086     STXXL_MSG("Pos of value 1048576: " << (stxxl::find(v.begin(), v.end(), 1024 * 1024, 4) - v.begin()));
00087     STXXL_MSG("Pos of value    1024: " << (stxxl::find(v.begin(), v.end(), 32 * 32, 4) - v.begin()));
00088 
00089     STXXL_MSG("generate ...");
00090     b = timestamp();
00091 
00092     stxxl::generate(v.begin() + 1, v.end() - 1, fill_value<int64>(555), 4);
00093     e = timestamp();
00094     STXXL_MSG("generate: " << (e - b));
00095 
00096 
00097     STXXL_MSG("check");
00098     if (v[0] != 0)
00099         STXXL_MSG("Error at position " << i);
00100     if (v[v.size() - 1] != int64((v.size() - 1) * (v.size() - 1)))
00101         STXXL_MSG("Error at position " << i);
00102 
00103     for (i = 1; i < v.size() - 1; ++i)
00104     {
00105         if (v[i] != 555)
00106             STXXL_MSG("Error at position " << i);
00107     }
00108 
00109     return 0;
00110 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines