Scaffolding  0.1
This program can assemble genome scaffolds using the pairing information in paired-end reads.
set_queue.hpp
Go to the documentation of this file.
1 
2 
8 #ifndef SET_QUEUE_HPP
9 #define SET_QUEUE_HPP
10 
11 #include <map>
12 #include <set>
13 #include <cassert>
14 
15 #include <boost/heap/fibonacci_heap.hpp>
16 
17 
18 
19 namespace std{
21  template<typename T, typename Priority, class Compare = std::less<Priority>>
22  class set_queue
23  {
24  private:
25  typedef std::map<T, Priority> T_to_Prio_Type;
26  typedef std::map<Priority, std::set<T> > Prio_to_T_Type;
27 
28  typedef typename Prio_to_T_Type::iterator Prio_to_T_Iter;
29  typedef typename Prio_to_T_Type::const_iterator Prio_to_T_CIter;
30 
31  T_to_Prio_Type T_to_Prio;
32  Prio_to_T_Type Prio_to_T;
33 
34  const Compare comp;
35 
36  public:
37  typedef typename T_to_Prio_Type::iterator iterator;
38  typedef typename T_to_Prio_Type::const_iterator const_iterator;
39 
40 
41  private:
42 
44  std::set<T>& get_prio_class(const Priority& prio)
45  {
46  const Prio_to_T_Iter prio_class = Prio_to_T.find(prio);
47  // that should have given us the priority class
48  assert(prio_class != Prio_to_T.cend());
49  return prio_class->second;
50  }
51 
52  public:
53 
55  set_queue(const Compare& _comp = Compare()): comp(_comp)
56  {
57  }
58 
59  const_iterator& cbegin() const
60  {
61  return T_to_Prio.cbegin();
62  }
63  const_iterator& cend() const
64  {
65  return T_to_Prio.cend();
66  }
67 
69  bool empty() const
70  {
71  return T_to_Prio.empty();
72  }
73 
75 
76  std::pair<const_iterator, bool> insert(const T& element, const Priority& prio)
77  {
78  const std::pair<const_iterator, bool> try_insert = T_to_Prio.emplace(element, prio);
79  if(try_insert.second){
80  // if the element did not exist before, insert the pair also into Prio_to_T
81  const std::pair<Prio_to_T_Iter, bool> prios_iter =
82  Prio_to_T.emplace(std::piecewise_construct, std::make_tuple(prio), std::make_tuple());
83 
84  // insert the element into the set at the iterator
85  std::set<T>& T_set = prios_iter.first->second;
86  const std::pair<typename std::set<T>::iterator, bool> same_prio_iter =
87  T_set.insert(element);
88 
89  // since try_insert.second was true, same_prio_iter should not have contained the element
90  assert(same_prio_iter.second);
91  } else {
92  // if the element already exists, then update its priority
93  update_priority(try_insert.first, prio);
94  }
95  // finally, return the correct const_iterator
96  return try_insert;
97  }
98 
100  const_iterator& find(const T& element) const
101  {
102  return T_to_Prio.find(element);
103  }
104 
106 
107  std::pair<Priority, bool> get_priority(const T& element) const
108  {
109  const const_iterator element_it = T_to_Prio.find(element);
110  const bool success = (element_it == T_to_Prio.cend());
111  const Priority prio = (success ? Priority() : *element_it);
112  return std::pair<Priority, bool>(prio, success);
113  }
114 
116 
117  const const_iterator get_min_element() const
118  {
119  return cbegin();
120  }
121 
123  void erase(const const_iterator& iter)
124  {
125  assert(iter != cend());
126  const T& element = iter->first;
127  const Priority& prio = iter->second;
128  const Prio_to_T_Iter same_prio = Prio_to_T.find(prio);
129 
130  std::set<T>& T_set = same_prio->second;
131  T_set.erase(element);
132  if(T_set.empty()) Prio_to_T.erase(same_prio);
133 
134  T_to_Prio.erase(iter);
135  }
136 
138 
139  bool erase(const T& element)
140  {
141  const const_iterator iter = T_to_Prio.find(element);
142  if(iter != cend()) {
143  erase(iter);
144  return true;
145  } else return false;
146  }
147 
148 
150 
151  void set_priority(const const_iterator& iter, const Priority& prio, const bool update_only = false)
152  {
153  const T& element = iter->first;
154  const Priority& old_prio = iter->second;
155 
156  // only set the new value if update_only is false or it compares favorably to the old value
157  if(!update_only || comp(prio, old_prio)){
158  std::set<T>& old_class = get_prio_class(old_prio);
159 
160  const Prio_to_T_Iter new_prio_class_iter =
161  Prio_to_T.emplace(std::piecewise_construct, std::make_tuple(prio), std::make_tuple()).first;
162  std::set<T>& new_class = new_prio_class_iter->second;
163 
164  // move element from old class to new class
165  const typename std::set<T>::const_iterator element_iter = old_class.find(element);
166  assert(element_iter != old_class.cend());
167 
168  new_class.insert(element_iter, std::next(element_iter));
169  old_class.erase(element_iter);
170  // update T_to_Prio
171  T_to_Prio[element] = prio;
172  }
173  }
174 
176 
177  bool set_priority(const T& element, const Priority& prio, const bool update_only = false)
178  {
179  const const_iterator iter = find(element);
180  if(iter != cend()) {
181  set_priority(iter, prio, update_only);
182  return true;
183  } else return false;
184  }
185 
187 
188  void update_priority(const const_iterator& iter, const Priority& prio)
189  {
190  set_priority(iter, prio, true);
191  }
192 
194 
195  bool update_priority(const T& element, const Priority& prio)
196  {
197  return set_priority(element, prio, true);
198  }
199 
201  void clear()
202  {
203  while(!Prio_to_T.empty()){
204  Prio_to_T.begin()->clear();
205  Prio_to_T.erase(Prio_to_T.begin());
206  }
207  T_to_Prio.clear();
208  }
209  };
210 }// namespace
211 
212 
213 #endif
const_iterator & find(const T &element) const
find an element in the set_queue
Definition: set_queue.hpp:100
bool erase(const T &element)
remove an element from the queue, given the element
Definition: set_queue.hpp:139
bool set_priority(const T &element, const Priority &prio, const bool update_only=false)
modify the priority of a given element, return whether the element was found in the queue ...
Definition: set_queue.hpp:177
const const_iterator get_min_element() const
get an element of minimum priority
Definition: set_queue.hpp:117
Definition: low_priority_queue.hpp:23
void update_priority(const const_iterator &iter, const Priority &prio)
update the priority of an element, given an iterator
Definition: set_queue.hpp:188
std::pair< const_iterator, bool > insert(const T &element, const Priority &prio)
insert an element into the set_queue and return whether it was newly inserted
Definition: set_queue.hpp:76
T_to_Prio_Type::iterator iterator
iterators will point to objects in the map of objects to priorities
Definition: set_queue.hpp:37
bool empty() const
return whether the set_queue is empty
Definition: set_queue.hpp:69
a priority-queue allowing member queries
Definition: set_queue.hpp:22
bool update_priority(const T &element, const Priority &prio)
update the priority of a given element, return success
Definition: set_queue.hpp:195
void set_priority(const const_iterator &iter, const Priority &prio, const bool update_only=false)
modify the priority of an element, given an iterator
Definition: set_queue.hpp:151
set_queue(const Compare &_comp=Compare())
construct with comparator
Definition: set_queue.hpp:55
void erase(const const_iterator &iter)
remove an element from the queue, given its iterator
Definition: set_queue.hpp:123
void clear()
clear the container
Definition: set_queue.hpp:201
std::pair< Priority, bool > get_priority(const T &element) const
get priority of an element
Definition: set_queue.hpp:107