HPCToolkit
LRUList.hpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 //***************************************************************************
59 
60 #ifndef LRULIST_H_
61 #define LRULIST_H_
62 
63 #include <vector>
64 #include <list>
65 
66 
67 using std::list;
68 using std::vector;
69 
70 namespace TraceviewerServer {
71 
72 
73 
74 template <typename T>
75 class LRUList {
76 private:
78  int usedPages;
80  list<T*> useOrder;
81  list<T*> removed;
82  vector<typename list<T*>::iterator> iters;
83 public:
95  LRUList(int expectedMaxSize)//Up to linear time
96  {
97  iters.reserve(expectedMaxSize);
98  totalPages = 0;
99  usedPages = 0;
100  currentFront = -1;
101  }
102  int addNew(T* toAdd)//constant time
103  {
104  useOrder.push_front(toAdd);
105  int index = totalPages++;
106  currentFront = index;
107  iters.push_back(useOrder.begin());
108  usedPages++;
109  return index;
110  }
111  int addNewUnused(T* toAdd)//constant time
112  {
113  removed.push_front(toAdd);
114  int index = totalPages++;
115  iters.push_back(removed.begin());
116  return index;
117  }
118  void putOnTop(int index)//Constant time
119  {
120  if (index == currentFront) return;
121  typename list<T*>::iterator it;
122  it = iters[index];
123  useOrder.splice(useOrder.begin(), useOrder, it);
124  currentFront = index;
125  }
127  {
128  return useOrder.back();
129  }
130 
131  void removeLast()//Constant time
132  {
133  removed.splice(removed.end(), useOrder, --useOrder.end());
134  usedPages--;
135  }
136  void reAdd(int index)//Constant time
137  {
138  typename list<T*>::iterator it = iters[index];
139  useOrder.splice(useOrder.begin(), removed, it);
140  currentFront = index;
141  usedPages++;
142  }
143  int getTotalPageCount()//Constant time
144  {
145  return totalPages;
146  }
148  {
149  return usedPages;
150  }
151  virtual ~LRUList()
152  {
153 
154  }
155 
156  /*int dump()
157  {
158  int x = 0;
159  puts("Objects \"in use\" from most recently used to least recently used");
160  typename list<T*>::iterator it = useOrder.begin();
161  for(; it != useOrder.end(); ++it){
162  printf("%d\n", ((TestData*)*it)->index);
163  x++;
164  }
165  puts("Objects \"not in use\" from most recently used to least recently used");
166  it = removed.begin();
167  for(; it != removed.end(); ++it){
168  printf("%d\n", ((TestData*)*it)->index);
169  }
170  cout << "Used count: " << usedPages <<" supposed to be " << x<<endl;
171  return x;
172  }*/
173 };
174 
175 } /* namespace TraceviewerServer */
176 #endif /* LRULIST_H_ */
LRUList(int expectedMaxSize)
Definition: LRUList.hpp:95
void reAdd(int index)
Definition: LRUList.hpp:136
vector< typename list< T * >::iterator > iters
Definition: LRUList.hpp:82
int addNewUnused(T *toAdd)
Definition: LRUList.hpp:111
void(* T)(int code, va_list_box *box, int put(int c, void *cl), void *cl, unsigned char flags[256], int width, int precision)
Definition: fmt.h:62
void putOnTop(int index)
Definition: LRUList.hpp:118