Stxxl  1.2.1
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 Roman Dementiev <dementiev@mpi-sb.mpg.de>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #ifndef STXXL_PAGER_HEADER
14 #define STXXL_PAGER_HEADER
15 
16 #include <list>
17 
18 #include <stxxl/bits/noncopyable.h>
19 #include <stxxl/bits/common/rand.h>
20 #include <stxxl/bits/common/simple_vector.h>
21 #include <stxxl/bits/compat_auto_ptr.h>
22 
23 
24 __STXXL_BEGIN_NAMESPACE
25 
28 
29 enum pager_type
30 {
31  random,
32  lru
33 };
34 
36 template <unsigned npages_>
38 {
40 
41 public:
42  enum { n_pages = npages_ };
43  random_pager() { }
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_>
58 class lru_pager : private noncopyable
59 {
60  typedef std::list<int_type> list_type;
61 
62  compat_auto_ptr<list_type>::result history;
63  simple_vector<list_type::iterator> history_entry;
64 
65 public:
66  enum { n_pages = npages_ };
67 
68  lru_pager() : history(new list_type), history_entry(npages_)
69  {
70  for (unsigned_type i = 0; i < npages_; i++)
71  history_entry[i] = history->insert(history->end(), static_cast<int_type>(i));
72  }
73  ~lru_pager() { }
74  int_type kick()
75  {
76  return history->back();
77  }
78  void hit(int_type ipage)
79  {
80  assert(ipage < int_type(npages_));
81  assert(ipage >= 0);
82  history->splice(history->begin(), *history, history_entry[ipage]);
83  }
84  void swap(lru_pager & obj)
85  {
86  // workaround for buggy GCC 3.4 STL
87  //std::swap(history,obj.history);
88  compat_auto_ptr<list_type>::result tmp = obj.history;
89  obj.history = history;
90  history = tmp;
91  std::swap(history_entry, obj.history_entry);
92  }
93 };
94 
96 
97 __STXXL_END_NAMESPACE
98 
99 namespace std
100 {
101  template <unsigned npages_>
102  void swap(stxxl::lru_pager<npages_> & a,
103  stxxl::lru_pager<npages_> & b)
104  {
105  a.swap(b);
106  }
107 }
108 
109 #endif // !STXXL_PAGER_HEADER