Stxxl  1.4.0
containers/test_vector.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  containers/test_vector.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002, 2003, 2006 Roman Dementiev <dementiev@mpi-sb.mpg.de>
00007  *  Copyright (C) 2010 Johannes Singler <singler@kit.edu>
00008  *
00009  *  Distributed under the Boost Software License, Version 1.0.
00010  *  (See accompanying file LICENSE_1_0.txt or copy at
00011  *  http://www.boost.org/LICENSE_1_0.txt)
00012  **************************************************************************/
00013 
00014 //! \example containers/test_vector.cpp
00015 //! This is an example of use of \c stxxl::vector and
00016 //! \c stxxl::VECTOR_GENERATOR. Vector type is configured
00017 //! to store 64-bit integers and have 2 pages each of 1 block
00018 
00019 #include <iostream>
00020 #include <algorithm>
00021 #include <stxxl/vector>
00022 #include <stxxl/scan>
00023 
00024 
00025 struct element  // 24 bytes, not a power of 2 intentionally
00026 {
00027     stxxl::int64 key;
00028     stxxl::int64 load0;
00029     stxxl::int64 load1;
00030 
00031     element & operator = (stxxl::int64 i)
00032     {
00033         key = i;
00034         load0 = i + 42;
00035         load1 = i ^ 42;
00036         return *this;
00037     }
00038 
00039     bool operator == (const element & e2) const
00040     {
00041         return key == e2.key && load0 == e2.load0 && load1 == e2.load1;
00042     }
00043 };
00044 
00045 struct counter
00046 {
00047     int value;
00048     counter(int v) : value(v) { }
00049     int operator () ()
00050     {
00051         int old_val = value;
00052         value++;
00053         return old_val;
00054     }
00055 };
00056 
00057 template <class my_vec_type>
00058 void test_const_iterator(const my_vec_type & x)
00059 {
00060     typename my_vec_type::const_iterator i = x.begin();
00061     i = x.end() - 1;
00062     i.block_externally_updated();
00063     i.flush();
00064     i++;
00065     ++i;
00066     --i;
00067     i--;
00068     *i;
00069 }
00070 
00071 
00072 int main()
00073 {
00074     try
00075     {
00076         // use non-randomized striping to avoid side effects on random generator
00077         typedef stxxl::VECTOR_GENERATOR<element, 2, 2, (2 * 1024 * 1024), stxxl::striping>::result vector_type;
00078         vector_type v(64 * 1024 * 1024 / sizeof(element));
00079 
00080         // test assignment const_iterator = iterator
00081         vector_type::const_iterator c_it = v.begin();
00082         STXXL_UNUSED(c_it);
00083 
00084         unsigned int big_size = 1024 * 1024 * 2 * 16 * 16;
00085         typedef stxxl::vector<double> vec_big;
00086         vec_big my_vec(big_size);
00087 
00088         vec_big::iterator big_it = my_vec.begin();
00089         big_it += 6;
00090 
00091         test_const_iterator(v);
00092 
00093         stxxl::random_number32 rnd;
00094         int offset = rnd();
00095 
00096         STXXL_MSG("write " << v.size() << " elements");
00097 
00098         stxxl::ran32State = 0xdeadbeef;
00099         vector_type::size_type i;
00100 
00101         // fill the vector with increasing sequence of integer numbers
00102         for (i = 0; i < v.size(); ++i)
00103         {
00104             v[i].key = i + offset;
00105             assert(v[i].key == stxxl::int64(i + offset));
00106         }
00107 
00108 
00109         // fill the vector with random numbers
00110         stxxl::generate(v.begin(), v.end(), stxxl::random_number32(), 4);
00111         v.flush();
00112 
00113         STXXL_MSG("seq read of " << v.size() << " elements");
00114 
00115         stxxl::ran32State = 0xdeadbeef;
00116 
00117         // testing swap
00118         vector_type a;
00119         std::swap(v, a);
00120         std::swap(v, a);
00121 
00122         for (i = 0; i < v.size(); i++)
00123             assert(v[i].key == rnd());
00124 
00125         // check again
00126         STXXL_MSG("clear");
00127 
00128         v.clear();
00129 
00130         stxxl::ran32State = 0xdeadbeef + 10;
00131 
00132         v.resize(64 * 1024 * 1024 / sizeof(element));
00133 
00134         STXXL_MSG("write " << v.size() << " elements");
00135         stxxl::generate(v.begin(), v.end(), stxxl::random_number32(), 4);
00136 
00137         stxxl::ran32State = 0xdeadbeef + 10;
00138 
00139         STXXL_MSG("seq read of " << v.size() << " elements");
00140 
00141         for (i = 0; i < v.size(); i++)
00142             assert(v[i].key == rnd());
00143 
00144         STXXL_MSG("copy vector of " << v.size() << " elements");
00145 
00146         vector_type v_copy0(v);
00147         assert(v == v_copy0);
00148 
00149         vector_type v_copy1;
00150         v_copy1 = v;
00151         assert(v == v_copy1);
00152     }
00153     catch (const std::exception & ex)
00154     {
00155         STXXL_MSG("Caught exception: " << ex.what());
00156     }
00157     catch (...)
00158     {
00159         STXXL_MSG("Caught unknown exception.");
00160     }
00161 
00162     return 0;
00163 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines