Stxxl  1.3.2
pager.h
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_PAGER_HEADER
15 #define STXXL_PAGER_HEADER
16 
17 #include <list>
18 #include <cassert>
19 
20 #include <stxxl/bits/noncopyable.h>
21 #include <stxxl/bits/common/rand.h>
22 #include <stxxl/bits/common/simple_vector.h>
23 
24 
25 __STXXL_BEGIN_NAMESPACE
26 
29 
30 enum pager_type
31 {
32  random,
33  lru
34 };
35 
37 template <unsigned npages_>
39 {
41 
42 public:
43  enum { n_pages = npages_ };
44  random_pager() { }
45  int_type kick()
46  {
47  return rnd(npages_);
48  }
49  void hit(int_type ipage)
50  {
51  assert(ipage < int_type(npages_));
52  assert(ipage >= 0);
53  }
54 };
55 
57 template <unsigned npages_ = 0>
58 class lru_pager : private noncopyable
59 {
60  typedef unsigned_type size_type;
61  typedef std::list<size_type> list_type;
62 
63  list_type history;
64  simple_vector<list_type::iterator> history_entry;
65 
66 public:
67  enum { n_pages = npages_ };
68 
69  lru_pager() : history_entry(n_pages)
70  {
71  for (size_type i = 0; i < n_pages; ++i)
72  history_entry[i] = history.insert(history.end(), i);
73  }
74 
75  size_type kick()
76  {
77  return history.back();
78  }
79 
80  void hit(size_type ipage)
81  {
82  assert(ipage < n_pages);
83  history.splice(history.begin(), history, history_entry[ipage]);
84  }
85 
86  void swap(lru_pager & obj)
87  {
88  history.swap(obj.history);
89  history_entry.swap(obj.history_entry);
90  }
91 };
92 
93 // specialization, size is specified at runtime
94 template <>
95 class lru_pager<0> : private noncopyable
96 {
97  typedef unsigned_type size_type;
98  typedef std::list<size_type> list_type;
99 
100  size_type n_pages;
101  list_type history;
102  simple_vector<list_type::iterator> history_entry;
103 
104 public:
105  lru_pager(size_type npages_ = 0) : n_pages(npages_), history_entry(n_pages)
106  {
107  for (size_type i = 0; i < n_pages; ++i)
108  history_entry[i] = history.insert(history.end(), i);
109  }
110 
111  size_type kick()
112  {
113  return history.back();
114  }
115 
116  void hit(size_type ipage)
117  {
118  assert(ipage < n_pages);
119  history.splice(history.begin(), history, history_entry[ipage]);
120  }
121 
122  void swap(lru_pager & obj)
123  {
124  std::swap(n_pages, obj.n_pages);
125  history.swap(obj.history);
126  history_entry.swap(obj.history_entry);
127  }
128 };
129 
131 
132 __STXXL_END_NAMESPACE
133 
134 namespace std
135 {
136  template <unsigned npages_>
137  void swap(stxxl::lru_pager<npages_> & a,
138  stxxl::lru_pager<npages_> & b)
139  {
140  a.swap(b);
141  }
142 }
143 
144 #endif // !STXXL_PAGER_HEADER
145 // vim: et:ts=4:sw=4
Pager with random replacement strategy.
Definition: pager.h:38
Pager with LRU replacement strategy.
Definition: pager.h:58