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

Go to the SVN repository for this file.

1 #ifndef SRA__READER__SRA__LIMITED_RESOURCE_MAP__HPP
2 #define SRA__READER__SRA__LIMITED_RESOURCE_MAP__HPP
3 
4 /* $Id: limited_resource_map.hpp 98508 2022-11-29 17:22:22Z vasilche $
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  * Author: Eugene Vasilchenko
30  *
31  */
32 
33 
34 /// @file limited_resource_map.hpp
35 /// Generic map with additional resource limited by some value
36 
37 
38 #include <corelib/ncbistd.hpp>
39 #include <set>
40 #include <list>
41 
42 
44 
45 
46 template<class Key, class Value, class Resource, class Less = less<Key>>
48 {
49 public:
50  typedef Key key_type;
51  typedef Value mapped_type;
52  typedef pair<const key_type, mapped_type> value_type;
53  typedef Resource resource_type;
54 
55 private:
56  struct SNode;
57  struct SLess;
58  typedef Less TLess;
60  typedef typename TMap::iterator TMapIterator;
62  typedef list<TMapIterator> TRemoveList;
63  typedef typename TRemoveList::iterator TRemoveListIterator;
64  struct SNode : public value_type {
66  : value_type(value)
67  {
68  }
70  : value_type(key, value)
71  {
72  }
73  SNode(const key_type& key)
75  {
76  }
79  };
80  struct SLess : TLess {
82  {
83  }
84  SLess(const TLess& key_comp)
85  : TLess(key_comp)
86  {
87  }
88  bool operator()(const SNode& a, const SNode& b) const {
89  return TLess::operator()(a.first, b.first);
90  }
91  };
93 public:
94 
95  explicit
96  limited_resource_map(const resource_type& resource_limit)
97  : m_ResourceLimit(resource_limit),
99  {
100  }
101 
103  return m_ResourceLimit;
104  }
105  void set_resource_limit(resource_type resource_limit) {
106  m_ResourceLimit = resource_limit;
107  x_GC();
108  }
110  return m_ResourceUsed;
111  }
112 
113  bool empty(void) const {
114  return m_Map.empty();
115  }
116  size_t size(void) const {
117  return m_Map.size();
118  }
119  void clear(void) {
120  m_RemoveList.clear();
121  m_Map.clear();
123  }
124 
126  TMapIterator iter = m_Map.find(key);
127  if ( iter != m_Map.end() ) {
128  x_MarkUsed(iter);
129  return iter->second;
130  }
131  else {
132  return mapped_type();
133  }
134  }
135  void put(const key_type& key, const mapped_type& value, const resource_type& resource_used) {
136  pair<TMapIterator, bool> ins = m_Map.insert(SNode(key, value));
137  if ( ins.second ) {
138  x_MarkAdded(ins.first);
139  }
140  else {
141  x_MarkUsed(ins.first);
142  m_ResourceUsed -= ins.first->m_ResourceUsed;
143  }
144  const_cast<resource_type&>(ins.first->m_ResourceUsed) = resource_used;
145  m_ResourceUsed += ins.first->m_ResourceUsed;
146  x_GC();
147  }
148  void erase(const key_type& key) {
149  TMapIterator iter = m_Map.find(key);
150  if ( iter != m_Map.end() ) {
151  x_Erase(iter);
152  }
153  }
154 
155 protected:
156  SNode& x_GetNode(TMapIterator iter) {
157  return const_cast<SNode&>(*iter);
158  }
159  void x_GC(void) {
160  while ( resource_used() > get_resource_limit() ) {
161  TRemoveListIterator remove_iter = m_RemoveList.begin();
162  if ( remove_iter == m_RemoveList.end() ) {
163  // empty
164  return;
165  }
166  if ( next(remove_iter) == m_RemoveList.end() ) {
167  // don't remove last element
168  return;
169  }
170  x_Erase(*remove_iter);
171  }
172  }
173  void x_Erase(TMapIterator iter) {
174  m_ResourceUsed -= iter->m_ResourceUsed;
175  m_RemoveList.erase(iter->m_RemoveListIter);
176  m_Map.erase(iter);
177  }
180  m_RemoveList.insert(m_RemoveList.end(), iter);
181  }
183  m_RemoveList.splice(m_RemoveList.end(), m_RemoveList,
184  iter->m_RemoveListIter);
185  }
186 
187 private:
191 };
192 
193 
195 
196 
197 #endif /* SRA__READER__SRA__LIMITED_RESOURCE_MAP__HPP */
CRef –.
Definition: ncbiobj.hpp:618
void set_resource_limit(resource_type resource_limit)
list< TMapIterator > TRemoveList
resource_type resource_used() const
pair< const key_type, mapped_type > value_type
void x_MarkAdded(TMapIterator iter)
resource_type get_resource_limit(void) const
limited_resource_map(const resource_type &resource_limit)
set< SNode, SLess > TMap
void put(const key_type &key, const mapped_type &value, const resource_type &resource_used)
mapped_type get(const key_type &key)
TRemoveList::iterator TRemoveListIterator
void x_MarkUsed(TMapIterator iter)
SNode & x_GetNode(TMapIterator iter)
void erase(const key_type &key)
TMap::const_iterator TMapConstIterator
void x_Erase(TMapIterator iter)
iterator_bool insert(const value_type &val)
Definition: set.hpp:149
void clear()
Definition: set.hpp:153
parent_type::iterator iterator
Definition: set.hpp:80
size_type size() const
Definition: set.hpp:132
bool empty() const
Definition: set.hpp:133
const_iterator find(const key_type &key) const
Definition: set.hpp:137
void erase(iterator pos)
Definition: set.hpp:151
const_iterator end() const
Definition: set.hpp:136
parent_type::const_iterator const_iterator
Definition: set.hpp:79
Include a standard set of the NCBI C++ Toolkit most basic headers.
static DLIST_TYPE *DLIST_NAME() next(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:56
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
const struct ncbi::grid::netcache::search::fields::KEY key
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2107
unsigned int a
Definition: ncbi_localip.c:102
bool operator()(const SNode &a, const SNode &b) const
SNode(const value_type &value)
SNode(const key_type &key, const mapped_type &value)
TRemoveListIterator m_RemoveListIter
Modified on Wed Apr 17 13:08:07 2024 by modify_doxy.py rev. 669887