STXXL  1.4.0
 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 
24 
26 
27 //! \addtogroup stlcont_vector
28 //! \{
29 
31 {
34 };
35 
36 //! Pager with \b random replacement strategy
37 template <unsigned npages_>
39 {
40  enum { n_pages = npages_ };
41 
43 
46 
47 public:
48  random_pager(size_type num_pages = n_pages) : num_pages(num_pages) { }
50  {
51  return rnd(size());
52  }
53 
54  void hit(size_type ipage)
55  {
56  STXXL_ASSERT(ipage < size());
57  }
58 
59  size_type size() const
60  {
61  return num_pages;
62  }
63 };
64 
65 //! Pager with \b LRU replacement strategy
66 template <unsigned npages_ = 0>
67 class lru_pager : private noncopyable
68 {
69  enum { n_pages = npages_ };
70 
72  typedef std::list<size_type> list_type;
73 
76 
77 public:
78  lru_pager(size_type num_pages = n_pages) : history_entry(num_pages)
79  {
80  for (size_type i = 0; i < size(); ++i)
81  history_entry[i] = history.insert(history.end(), i);
82  }
83 
85  {
86  return history.back();
87  }
88 
89  void hit(size_type ipage)
90  {
91  assert(ipage < size());
92  history.splice(history.begin(), history, history_entry[ipage]);
93  }
94 
95  void swap(lru_pager& obj)
96  {
97  history.swap(obj.history);
98  history_entry.swap(obj.history_entry);
99  }
100 
101  size_type size() const
102  {
103  return history_entry.size();
104  }
105 };
106 
107 //! \}
108 
110 
111 namespace std {
112 
113 template <unsigned npages_>
114 void swap(stxxl::lru_pager<npages_>& a,
116 {
117  a.swap(b);
118 }
119 
120 } // namespace std
121 
122 #endif // !STXXL_CONTAINERS_PAGER_HEADER
123 // vim: et:ts=4:sw=4
#define STXXL_ASSERT(condition)
Definition: verbose.h:157
size_type kick()
Definition: pager.h:84
void hit(size_type ipage)
Definition: pager.h:54
list_type history
Definition: pager.h:74
unsigned_type size_type
Definition: pager.h:71
size_type size() const
Definition: pager.h:59
size_type size() const
Definition: pager.h:101
lru_pager(size_type num_pages=n_pages)
Definition: pager.h:78
Pager with random replacement strategy.
Definition: pager.h:38
random_pager(size_type num_pages=n_pages)
Definition: pager.h:48
void hit(size_type ipage)
Definition: pager.h:89
size_type kick()
Definition: pager.h:49
pager_type
Definition: pager.h:30
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
simple_vector< list_type::iterator > history_entry
Definition: pager.h:75
unsigned_type size_type
Definition: pager.h:42
Pager with LRU replacement strategy.
Definition: pager.h:67
void swap(lru_pager &obj)
Definition: pager.h:95
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:67
size_type num_pages
Definition: pager.h:44
std::list< size_type > list_type
Definition: pager.h:72
random_number< random_uniform_fast > rnd
Definition: pager.h:45
#define STXXL_END_NAMESPACE
Definition: namespace.h:17