Stxxl  1.2.1
btree_pager.h
1 /***************************************************************************
2  * include/stxxl/bits/containers/btree/btree_pager.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2006 Roman Dementiev <dementiev@ira.uka.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_CONTAINERS_BTREE__BTREE_PAGER_H
14 #define STXXL_CONTAINERS_BTREE__BTREE_PAGER_H
15 
16 #include <memory>
17 #include <list>
18 
19 #include <stxxl/bits/noncopyable.h>
20 #include <stxxl/bits/common/utils.h>
21 #include <stxxl/bits/compat_auto_ptr.h>
22 
23 
24 __STXXL_BEGIN_NAMESPACE
25 
26 namespace btree
27 {
28  class lru_pager : private noncopyable
29  {
30  unsigned_type npages_;
31  typedef std::list<int_type> list_type;
32 
33  compat_auto_ptr<list_type>::result history;
34  std::vector<list_type::iterator> history_entry;
35 
36  public:
37  lru_pager() : npages_(0)
38  { }
39 
40  lru_pager(unsigned_type npages) :
41  npages_(npages),
42  history(new list_type),
43  history_entry(npages_)
44  {
45  for (unsigned_type i = 0; i < npages_; i++)
46  {
47  history_entry[i] = history->insert(history->end(), static_cast<int_type>(i));
48  }
49  }
50  ~lru_pager() { }
51  int_type kick()
52  {
53  return history->back();
54  }
55  void hit(int_type ipage)
56  {
57  assert(ipage < int_type(npages_));
58  assert(ipage >= 0);
59  history->splice(history->begin(), *history, history_entry[ipage]);
60  }
61  void swap(lru_pager & obj)
62  {
63  std::swap(npages_, obj.npages_);
64  // workaround for buggy GCC 3.4 STL
65  //std::swap(history,obj.history);
66  compat_auto_ptr<list_type>::result tmp = obj.history;
67  obj.history = history;
68  history = tmp;
69  std::swap(history_entry, obj.history_entry);
70  }
71  };
72 }
73 
74 __STXXL_END_NAMESPACE
75 
76 
77 namespace std
78 {
79  inline void swap(stxxl::btree::lru_pager & a,
80  stxxl::btree::lru_pager & b)
81  {
82  a.swap(b);
83  }
84 }
85 
86 #endif /* STXXL_CONTAINERS_BTREE__BTREE_PAGER_H */