• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List

pager.h

00001 /***************************************************************************
00002  *  include/stxxl/bits/containers/pager.h
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002-2003 Roman Dementiev <[email protected]>
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 #ifndef STXXL_PAGER_HEADER
00014 #define STXXL_PAGER_HEADER
00015 
00016 #include <list>
00017 #include <cassert>
00018 
00019 #include <stxxl/bits/noncopyable.h>
00020 #include <stxxl/bits/common/rand.h>
00021 #include <stxxl/bits/common/simple_vector.h>
00022 #include <stxxl/bits/compat_unique_ptr.h>
00023 
00024 
00025 __STXXL_BEGIN_NAMESPACE
00026 
00029 
00030 enum pager_type
00031 {
00032     random,
00033     lru
00034 };
00035 
00037 template <unsigned npages_>
00038 class random_pager
00039 {
00040     random_number<random_uniform_fast> rnd;
00041 
00042 public:
00043     enum { n_pages = npages_ };
00044     random_pager() { }
00045     ~random_pager() { }
00046     int_type kick()
00047     {
00048         return rnd(npages_);
00049     }
00050     void hit(int_type ipage)
00051     {
00052         assert(ipage < int_type(npages_));
00053         assert(ipage >= 0);
00054     }
00055 };
00056 
00058 template <unsigned npages_>
00059 class lru_pager : private noncopyable
00060 {
00061     typedef std::list<int_type> list_type;
00062 
00063     compat_unique_ptr<list_type>::result history;
00064     simple_vector<list_type::iterator> history_entry;
00065 
00066 public:
00067     enum { n_pages = npages_ };
00068 
00069     lru_pager() : history(new list_type), history_entry(npages_)
00070     {
00071         for (unsigned_type i = 0; i < npages_; i++)
00072             history_entry[i] = history->insert(history->end(), static_cast<int_type>(i));
00073     }
00074     ~lru_pager() { }
00075     int_type kick()
00076     {
00077         return history->back();
00078     }
00079     void hit(int_type ipage)
00080     {
00081         assert(ipage < int_type(npages_));
00082         assert(ipage >= 0);
00083 #if defined(__GXX_EXPERIMENTAL_CXX0X__) && ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100) == 40500)
00084         // HACK! Remove ASAP!
00085         // work around C++ Standard Library Issue http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#1133
00086         history->splice(history->begin(), std::move(*history), history_entry[ipage]);
00087 #else
00088         history->splice(history->begin(), *history, history_entry[ipage]);
00089 #endif
00090     }
00091     void swap(lru_pager & obj)
00092     {
00093         std::swap(history, obj.history);
00094         std::swap(history_entry, obj.history_entry);
00095     }
00096 };
00097 
00099 
00100 __STXXL_END_NAMESPACE
00101 
00102 namespace std
00103 {
00104     template <unsigned npages_>
00105     void swap(stxxl::lru_pager<npages_> & a,
00106               stxxl::lru_pager<npages_> & b)
00107     {
00108         a.swap(b);
00109     }
00110 }
00111 
00112 #endif // !STXXL_PAGER_HEADER

Generated by  doxygen 1.7.1