STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pager.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/containers/pager.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002, 2003, 2006 Roman Dementiev <[email protected]>
7  * Copyright (C) 2011 Andreas Beckmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_CONTAINERS_PAGER_HEADER
15 #define STXXL_CONTAINERS_PAGER_HEADER
16 
17 #include <list>
18 #include <cassert>
19 
20 #include <stxxl/bits/noncopyable.h>
21 #include <stxxl/bits/common/rand.h>
23 
25 
26 //! \addtogroup stlcont_vector
27 //! \{
28 
30 {
33 };
34 
35 //! Pager with \b random replacement strategy
36 template <unsigned npages_>
38 {
39  enum { n_pages = npages_ };
40 
42 
45 
46 public:
47  random_pager(size_type num_pages = n_pages) : num_pages(num_pages) { }
49  {
50  return rnd(size());
51  }
52 
53  void hit(size_type ipage)
54  {
55  STXXL_ASSERT(ipage < size());
56  }
57 
58  size_type size() const
59  {
60  return num_pages;
61  }
62 };
63 
64 //! Pager with \b LRU replacement strategy
65 template <unsigned npages_ = 0>
66 class lru_pager : private noncopyable
67 {
68  enum { n_pages = npages_ };
69 
71  typedef std::list<size_type> list_type;
72 
75 
76 public:
77  lru_pager(size_type num_pages = n_pages) : history_entry(num_pages)
78  {
79  for (size_type i = 0; i < size(); ++i)
80  history_entry[i] = history.insert(history.end(), i);
81  }
82 
84  {
85  return history.back();
86  }
87 
88  void hit(size_type ipage)
89  {
90  assert(ipage < size());
91  history.splice(history.begin(), history, history_entry[ipage]);
92  }
93 
94  void swap(lru_pager& obj)
95  {
96  history.swap(obj.history);
97  history_entry.swap(obj.history_entry);
98  }
99 
100  size_type size() const
101  {
102  return history_entry.size();
103  }
104 };
105 
106 //! \}
107 
109 
110 namespace std {
111 
112 template <unsigned npages_>
113 void swap(stxxl::lru_pager<npages_>& a,
115 {
116  a.swap(b);
117 }
118 
119 } // namespace std
120 
121 #endif // !STXXL_CONTAINERS_PAGER_HEADER
122 // vim: et:ts=4:sw=4
#define STXXL_ASSERT(condition)
Definition: verbose.h:220
size_type kick()
Definition: pager.h:83
void hit(size_type ipage)
Definition: pager.h:53
list_type history
Definition: pager.h:73
unsigned_type size_type
Definition: pager.h:70
size_type size() const
Definition: pager.h:58
size_type size() const
Definition: pager.h:100
lru_pager(size_type num_pages=n_pages)
Definition: pager.h:77
Pager with random replacement strategy.
Definition: pager.h:37
random_pager(size_type num_pages=n_pages)
Definition: pager.h:47
void hit(size_type ipage)
Definition: pager.h:88
size_type kick()
Definition: pager.h:48
pager_type
Definition: pager.h:29
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
simple_vector< list_type::iterator > history_entry
Definition: pager.h:74
unsigned_type size_type
Definition: pager.h:41
Pager with LRU replacement strategy.
Definition: pager.h:66
void swap(lru_pager &obj)
Definition: pager.h:94
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:64
size_type num_pages
Definition: pager.h:43
std::list< size_type > list_type
Definition: pager.h:71
random_number< random_uniform_fast > rnd
Definition: pager.h:44
#define STXXL_END_NAMESPACE
Definition: namespace.h:17