NCBI C++ ToolKit
mru_list.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_WIDGETS_FL___MRU_LIST__HPP
2 #define GUI_WIDGETS_FL___MRU_LIST__HPP
3 
4 /* $Id: mru_list.hpp 31318 2014-09-18 17:48:09Z katargir $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Andrey Yazhuk
30  *
31  * File Description:
32  *
33  */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <corelib/ncbistl.hpp>
37 
38 #include <algorithm>
39 #include <map>
40 
41 
43 
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 /// CMRUList
47 ///
48 /// CMRUList is an adapter around a standard STL container implementing MRU list
49 /// behavior.
50 template<class T> class CMRUList
51 {
52 public:
53  typedef list<T> TItems;
54 
55  CMRUList(size_t max_size = 7)
56  {
57  SetMaxSize((int)max_size);
58  }
59 
60  void AddItem(T item)
61  {
62  typename TItems::iterator it
63  = std::find(m_Items.begin(), m_Items.end(), item);
64 
65  if (it != m_Items.begin() || m_Items.empty()) {
66  if (it != m_Items.end())
67  m_Items.erase(it);
68  m_Items.push_front(item);
69  if(m_Items.size() > m_MaxSize)
70  m_Items.pop_back();
71  }
72  }
73 
74  void RemoveItem(T item)
75  {
76  typename TItems::iterator it
77  = std::find(m_Items.begin(), m_Items.end(), item);
78 
79  if (it != m_Items.end())
80  m_Items.erase(it);
81  }
82 
83  void SetMaxSize(int max_size)
84  {
85  if(max_size >= 0) {
86  m_MaxSize = max_size;
87  int excess = (int)(m_Items.size() - m_MaxSize);
88  while(0 < excess--) {
89  m_Items.pop_back();
90  }
91  }
92  }
93 
94  size_t GetMaxSize() const { return m_MaxSize; }
95 
96  const TItems& GetItems() const
97  {
98  return m_Items;
99  }
100  void GetItems(TItems& items)
101  {
102  items = m_Items;
103  }
104  void GetItems(vector<T>& items) const
105  {
106  ITERATE(typename TItems, it, m_Items) {
107  items.push_back(*it);
108  }
109  }
110  void SetItems(const TItems& items)
111  {
112  m_Items = items;
113  }
114  void SetItems(const vector<T>& items)
115  {
116  m_Items.clear();
117  ITERATE(typename vector<T>, it, items) {
118  m_Items.push_back(*it);
119  }
120  }
122  {
123  return m_Items;
124  }
125 
126 private:
128  size_t m_MaxSize;
129 };
130 
131 
132 ////////////////////////////////////////////////////////////////////////////////
133 /// CTimeMRUList - a MRU list where "recent" items are determined based on
134 /// provided time, not on insertion order
135 template<class T> class CTimeMRUList
136 {
137 public:
139  typedef T value_type;
140 
141  CTimeMRUList(int max_size = 10) : m_MaxSize(max_size) {}
142 
143  size_t GetMaxSize() const { return m_MaxSize; }
144  void SetMaxSize(size_t max_size);
145 
146  void Clear();
147  void Add(T elem, time_t time = 0);
148 
149  const TTimeToTMap& GetMap() const { return m_TimeToT; }
150 
151 protected:
154 };
155 
156 
157 template<class T> void CTimeMRUList<T>::SetMaxSize(size_t max_size)
158 {
159  if(m_TimeToT.size() > max_size) {
160  // delete some of the elements from the end
161  size_t n = m_TimeToT.size() - max_size;
162  for( size_t i = 0; i < n; i++ ) {
163  m_TimeToT.erase(m_TimeToT.begin());
164  }
165  }
166  m_MaxSize = (int)max_size;
167 }
168 
169 
170 template<class T> void CTimeMRUList<T>::Clear()
171 {
172  m_TimeToT.clear();
173 }
174 
175 
176 template<class T> void CTimeMRUList<T>::Add(T elem, time_t tm)
177 {
178  if(tm == 0) {
179  tm = time(NULL); //now
180  }
181  // elem may be already in the list, we need to erase it
182  for( typename TTimeToTMap::iterator it = m_TimeToT.begin();
183  it != m_TimeToT.end(); it++ ) {
184  if(it->second == elem) {
185  m_TimeToT.erase(it);
186  break;
187  }
188  }
189  m_TimeToT.insert(typename TTimeToTMap::value_type(tm, elem));
190  SetMaxSize(m_MaxSize);
191 }
192 
193 
195 
196 #endif // GUI_WIDGETS_FL___MRU_LIST__HPP
CMRUList.
Definition: mru_list.hpp:51
void SetMaxSize(int max_size)
Definition: mru_list.hpp:83
CMRUList(size_t max_size=7)
Definition: mru_list.hpp:55
void GetItems(TItems &items)
Definition: mru_list.hpp:100
size_t GetMaxSize() const
Definition: mru_list.hpp:94
list< T > TItems
Definition: mru_list.hpp:53
void GetItems(vector< T > &items) const
Definition: mru_list.hpp:104
TItems m_Items
Definition: mru_list.hpp:127
TItems & SetItems()
Definition: mru_list.hpp:121
const TItems & GetItems() const
Definition: mru_list.hpp:96
size_t m_MaxSize
Definition: mru_list.hpp:128
void SetItems(const TItems &items)
Definition: mru_list.hpp:110
void SetItems(const vector< T > &items)
Definition: mru_list.hpp:114
void AddItem(T item)
Definition: mru_list.hpp:60
void RemoveItem(T item)
Definition: mru_list.hpp:74
CTimeMRUList - a MRU list where "recent" items are determined based on provided time,...
Definition: mru_list.hpp:136
void Clear()
Definition: mru_list.hpp:170
const TTimeToTMap & GetMap() const
Definition: mru_list.hpp:149
size_t GetMaxSize() const
Definition: mru_list.hpp:143
void Add(T elem, time_t time=0)
Definition: mru_list.hpp:176
CTimeMRUList(int max_size=10)
Definition: mru_list.hpp:141
TTimeToTMap m_TimeToT
Definition: mru_list.hpp:153
multimap< time_t, T > TTimeToTMap
Definition: mru_list.hpp:138
void SetMaxSize(size_t max_size)
Definition: mru_list.hpp:157
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define T(s)
Definition: common.h:230
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NULL
Definition: ncbistd.hpp:225
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
int i
yy_size_t n
The NCBI C++/STL use hints.
Modified on Wed Apr 17 13:08:51 2024 by modify_doxy.py rev. 669887