Stxxl  1.4.0
include/stxxl/bits/containers/pager.h
Go to the documentation of this file.
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, 2006 Roman Dementiev <dementiev@ira.uka.de>
00007  *  Copyright (C) 2011 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
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 #ifndef STXXL_PAGER_HEADER
00015 #define STXXL_PAGER_HEADER
00016 
00017 #include <list>
00018 #include <cassert>
00019 
00020 #include <stxxl/bits/noncopyable.h>
00021 #include <stxxl/bits/common/rand.h>
00022 #include <stxxl/bits/common/simple_vector.h>
00023 
00024 
00025 __STXXL_BEGIN_NAMESPACE
00026 
00027 //! \addtogroup stlcontinternals
00028 //! \{
00029 
00030 enum pager_type
00031 {
00032     random,
00033     lru
00034 };
00035 
00036 //! \brief Pager with \b random replacement strategy
00037 template <unsigned npages_>
00038 class random_pager
00039 {
00040     enum { n_pages = npages_ };
00041 
00042     typedef unsigned_type size_type;
00043 
00044     size_type num_pages;
00045     random_number<random_uniform_fast> rnd;
00046 
00047 public:
00048     random_pager(size_type num_pages = n_pages) : num_pages(num_pages) { }
00049     size_type kick()
00050     {
00051         return rnd(size());
00052     }
00053 
00054     void hit(size_type ipage)
00055     {
00056         STXXL_UNUSED(ipage);
00057         assert(ipage < size());
00058     }
00059 
00060     size_type size() const
00061     {
00062         return num_pages;
00063     }
00064 };
00065 
00066 //! \brief Pager with \b LRU replacement strategy
00067 template <unsigned npages_ = 0>
00068 class lru_pager : private noncopyable
00069 {
00070     enum { n_pages = npages_ };
00071 
00072     typedef unsigned_type size_type;
00073     typedef std::list<size_type> list_type;
00074 
00075     list_type history;
00076     simple_vector<list_type::iterator> history_entry;
00077 
00078 public:
00079     lru_pager(size_type num_pages = n_pages) : history_entry(num_pages)
00080     {
00081         for (size_type i = 0; i < size(); ++i)
00082             history_entry[i] = history.insert(history.end(), i);
00083     }
00084 
00085     size_type kick()
00086     {
00087         return history.back();
00088     }
00089 
00090     void hit(size_type ipage)
00091     {
00092         assert(ipage < size());
00093         history.splice(history.begin(), history, history_entry[ipage]);
00094     }
00095 
00096     void swap(lru_pager & obj)
00097     {
00098         history.swap(obj.history);
00099         history_entry.swap(obj.history_entry);
00100     }
00101 
00102     size_type size() const
00103     {
00104         return history_entry.size();
00105     }
00106 };
00107 
00108 //! \}
00109 
00110 __STXXL_END_NAMESPACE
00111 
00112 namespace std
00113 {
00114     template <unsigned npages_>
00115     void swap(stxxl::lru_pager<npages_> & a,
00116               stxxl::lru_pager<npages_> & b)
00117     {
00118         a.swap(b);
00119     }
00120 }
00121 
00122 #endif // !STXXL_PAGER_HEADER
00123 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines