STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
addressable_queues.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/common/addressable_queues.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2010-2011 Raoul Steffen <[email protected]>
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_COMMON_ADDRESSABLE_QUEUES_HEADER
14 #define STXXL_COMMON_ADDRESSABLE_QUEUES_HEADER
15 
16 #include <set>
17 #include <list>
18 #include <map>
19 
20 #include <stxxl/bits/namespace.h>
21 
23 
24 //! An internal fifo queue that allows removing elements addressed with (a copy of) themselves.
25 //! \tparam KeyType Type of contained elements.
26 template <typename KeyType>
28 {
29  typedef std::list<KeyType> container_t;
30  typedef typename container_t::iterator container_iter_t;
31  typedef std::map<KeyType, container_iter_t> meta_t;
32  typedef typename meta_t::iterator meta_iter_t;
33 
36 
37 public:
38  //! Type of handle to an entry. For use with insert and remove.
40 
41  //! Create an empty queue.
44 
45  //! Check if queue is empty.
46  //! \return If queue is empty.
47  bool empty() const
48  { return vals.empty(); }
49 
50  //! Insert new element. If the element is already in, it is moved to the back.
51  //! \param e Element to insert.
52  //! \return pair<handle, bool> Iterator to element; if element was newly inserted.
53  std::pair<handle, bool> insert(const KeyType& e)
54  {
55  container_iter_t ei = vals.insert(vals.end(), e);
56  std::pair<handle, bool> r = meta.insert(std::make_pair(e, ei));
57  if (! r.second)
58  {
59  // element was already in
60  vals.erase(r.first->second);
61  r.first->second = ei;
62  }
63  return r;
64  }
65 
66  //! Erase element from the queue.
67  //! \param e Element to remove.
68  //! \return If element was in.
69  bool erase(const KeyType& e)
70  {
71  handle mi = meta.find(e);
72  if (mi == meta.end())
73  return false;
74  vals.erase(mi->second);
75  meta.erase(mi);
76  return true;
77  }
78 
79  //! Erase element from the queue.
80  //! \param i Iterator to element to remove.
81  void erase(handle i)
82  {
83  vals.erase(i->second);
84  meta.erase(i);
85  }
86 
87  //! Access top element in the queue.
88  //! \return Const reference to top element.
89  const KeyType & top() const
90  { return vals.front(); }
91 
92  //! Remove top element from the queue.
93  //! \return Top element.
94  KeyType pop()
95  {
96  assert(! empty());
97  const KeyType e = top();
98  meta.erase(e);
99  vals.pop_front();
100  return e;
101  }
102 };
103 
104 //! An internal priority queue that allows removing elements addressed with (a copy of) themselves.
105 //! \tparam KeyType Type of contained elements.
106 //! \tparam PriorityType Type of Priority.
107 template <typename KeyType, typename PriorityType, class Cmp = std::less<PriorityType> >
109 {
110  struct cmp // like < for pair, but uses Cmp for < on first
111  {
112  bool operator () (const std::pair<PriorityType, KeyType>& left,
113  const std::pair<PriorityType, KeyType>& right) const
114  {
115  Cmp c;
116  return c(left.first, right.first) ||
117  ((! c(right.first, left.first)) && left.second < right.second);
118  }
119  };
120 
121  typedef std::set<std::pair<PriorityType, KeyType>, cmp> container_t;
122  typedef typename container_t::iterator container_iter_t;
123  typedef std::map<KeyType, container_iter_t> meta_t;
124  typedef typename meta_t::iterator meta_iter_t;
125 
128 
129 public:
130  //! Type of handle to an entry. For use with insert and remove.
132 
133  //! Create an empty queue.
136 
137  //! Check if queue is empty.
138  //! \return If queue is empty.
139  bool empty() const
140  { return vals.empty(); }
141 
142  //! Insert new element. If the element is already in, it's priority is updated.
143  //! \param e Element to insert.
144  //! \param o Priority of element.
145  //! \return pair<handle, bool> Iterator to element; if element was newly inserted.
146  std::pair<handle, bool> insert(const KeyType& e, const PriorityType o)
147  {
148  std::pair<container_iter_t, bool> s = vals.insert(std::make_pair(o, e));
149  std::pair<handle, bool> r = meta.insert(std::make_pair(e, s.first));
150  if (! r.second && s.second)
151  {
152  // was already in with different priority
153  vals.erase(r.first->second);
154  r.first->second = s.first;
155  }
156  return r;
157  }
158 
159  //! Erase element from the queue.
160  //! \param e Element to remove.
161  //! \return If element was in.
162  bool erase(const KeyType& e)
163  {
164  handle mi = meta.find(e);
165  if (mi == meta.end())
166  return false;
167  vals.erase(mi->second);
168  meta.erase(mi);
169  return true;
170  }
171 
172  //! Erase element from the queue.
173  //! \param i Iterator to element to remove.
174  void erase(handle i)
175  {
176  vals.erase(i->second);
177  meta.erase(i);
178  }
179 
180  //! Access top (= min) element in the queue.
181  //! \return Const reference to top element.
182  const KeyType & top() const
183  { return vals.begin()->second; }
184 
185  //! Remove top (= min) element from the queue.
186  //! \return Top element.
187  KeyType pop()
188  {
189  assert(! empty());
190  const KeyType e = top();
191  meta.erase(e);
192  vals.erase(vals.begin());
193  return e;
194  }
195 };
196 
198 
199 #endif // !STXXL_COMMON_ADDRESSABLE_QUEUES_HEADER
container_t::iterator container_iter_t
std::pair< handle, bool > insert(const KeyType &e)
Insert new element. If the element is already in, it is moved to the back.
meta_iter_t handle
Type of handle to an entry. For use with insert and remove.
An internal priority queue that allows removing elements addressed with (a copy of) themselves...
std::pair< handle, bool > insert(const KeyType &e, const PriorityType o)
Insert new element. If the element is already in, it&#39;s priority is updated.
const KeyType & top() const
Access top (= min) element in the queue.
bool erase(const KeyType &e)
Erase element from the queue.
const KeyType & top() const
Access top element in the queue.
std::map< KeyType, container_iter_t > meta_t
addressable_fifo_queue()
Create an empty queue.
meta_iter_t handle
Type of handle to an entry. For use with insert and remove.
bool empty() const
Check if queue is empty.
container_t::iterator container_iter_t
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
std::list< KeyType > container_t
addressable_priority_queue()
Create an empty queue.
bool erase(const KeyType &e)
Erase element from the queue.
std::map< KeyType, container_iter_t > meta_t
KeyType pop()
Remove top (= min) element from the queue.
void erase(handle i)
Erase element from the queue.
void erase(handle i)
Erase element from the queue.
An internal fifo queue that allows removing elements addressed with (a copy of) themselves.
bool empty() const
Check if queue is empty.
KeyType pop()
Remove top element from the queue.
std::set< std::pair< PriorityType, KeyType >, cmp > container_t
#define STXXL_END_NAMESPACE
Definition: namespace.h:17