Scaffolding  0.1
This program can assemble genome scaffolds using the pairing information in paired-end reads.
low_priority_queue.hpp
Go to the documentation of this file.
1 
2 
16 #ifndef LOW_PRIORITY_QUEUE
17 #define LOW_PRIORITY_QUEUE
18 
19 #include <list>
20 #include <map>
21 
22 #warning TODO: exchange the list of value_type's to a list of Elements and write a custom iterator class returning value_type on dereference
23 namespace std{
24 
26 
27  template<class Element, class Priority = size_t>
29  public:
30  // public types
31  typedef typename std::pair<Element, Priority> value_type;
32 
33 #if GCC_VERSION >= 40900
34  typedef typename list<value_type>::const_iterator const_iterator;
35 #else
36  typedef typename list<value_type>::iterator const_iterator;
37 #endif
38 
39  private:
40  // private types
41  typedef typename list<value_type>::iterator iterator;
42  typedef typename std::map<Priority, list<value_type> > PrioToList;
43  typedef typename PrioToList::iterator PrioToListIter;
44  PrioToList prio_to_list;
45  public:
46 
49 
52  prio_to_list(lpq.prio_to_list)
53  {}
54 
56  const bool empty() const
57  {
58  return prio_to_list.empty();
59  }
60 
62  const_iterator insert(const value_type& value)
63  {
64  const Priority& prio = value.second;
65 
66  const PrioToListIter prio_it = prio_to_list.DEFAULT_EMPLACE(prio).first;
67  list<value_type>& vlist = prio_it->second;
68  vlist.push_back(value);
69  DEBUG5(cout << "inserted item of priority "<<vlist.back().second<<endl);
70  return std::prev(vlist.end());
71  }
72 
74  void erase(const const_iterator& it)
75  {
76  const Priority& prio = it->second;
77 
78  // find the priority list that it is going to be deleted from
79  const typename PrioToList::iterator p_iter = prio_to_list.find(prio);
80  assert(p_iter != prio_to_list.end());
81  list<value_type>& vlist = p_iter->second;
82  vlist.erase(it);
83  // if we removed the last item of a priority, then remove its priority class
84  if(vlist.empty()) prio_to_list.erase(p_iter);
85  }
86 
88  void change_priority(const const_iterator& it, const Priority& new_prio)
89  {
90  const Priority& old_prio = it->second;
91 
92  if(old_prio != new_prio){
93  const typename PrioToList::iterator old_list = prio_to_list.find(old_prio);
94  assert(old_list != prio_to_list.end());
95 
96  // if the priority didn't exist yet, insert an empty list to hold the value
97  const typename PrioToList::iterator new_list = prio_to_list.DEFAULT_EMPLACE(new_prio).first;
98  list<value_type>& vlist = new_list->second;
99 
100  vlist.splice(vlist.end(), old_list->second, it);
101  vlist.back().second = new_prio;
102 
103  if(old_list->second.empty()) prio_to_list.erase(old_list);
104  }// if
105  }
106 
108  void decrement_priority(const const_iterator& it)
109  {
110  change_priority(it, it->second - 1);
111  }
113  void increment_priority(const const_iterator& it)
114  {
115  change_priority(it, it->second + 1);
116  }
117 
119  const value_type& get_min() const
120  {
121  assert(!prio_to_list.empty());
122  assert(!prio_to_list.begin()->second.empty());
123  return prio_to_list.begin()->second.front();
124  }
126  void pop_min()
127  {
128  assert(!empty());
129 
130  list<value_type>& vlist = prio_to_list.begin()->second;
131  vlist.pop_front();
132  if(vlist.empty()) prio_to_list.erase(prio_to_list.begin());
133  }
134 
136  const value_type& get_max() const
137  {
138  assert(!prio_to_list.empty());
139  assert(!prio_to_list.rbegin()->second.empty());
140  return prio_to_list.rbegin()->second.back();
141  }
143  void pop_max()
144  {
145  assert(!empty());
146 
147  list<value_type>& vlist = prio_to_list.rbegin()->second;
148  vlist.pop_front();
149  if(vlist.empty()) prio_to_list.erase(prio_to_list.rbegin());
150  }
151 
152  };
153 
154 };
155 
156 #endif
157 
a low-priority queue of Elements with Priorities
Definition: low_priority_queue.hpp:28
Definition: low_priority_queue.hpp:23
void increment_priority(const const_iterator &it)
increment the priority of an item, given as iterator (see change_priority())
Definition: low_priority_queue.hpp:113
const bool empty() const
return whether the low-priority queue is empty
Definition: low_priority_queue.hpp:56
void change_priority(const const_iterator &it, const Priority &new_prio)
change the priority of an item in the low-priority queue
Definition: low_priority_queue.hpp:88
void decrement_priority(const const_iterator &it)
decrement the priority of an item, given as iterator (see change_priority())
Definition: low_priority_queue.hpp:108
void pop_max()
remove the max of the low-priority queue
Definition: low_priority_queue.hpp:143
low_priority_queue()
construct an empty low-priority queue
Definition: low_priority_queue.hpp:48
const value_type & get_max() const
return the max of the low-priority queue - this is an item of maximum priority
Definition: low_priority_queue.hpp:136
const_iterator insert(const value_type &value)
insert an element into the low-priority queue, returning a const_iterator to the newly inserted item ...
Definition: low_priority_queue.hpp:62
const value_type & get_min() const
return the min of the low-priority queue - this is any item of minimum priority
Definition: low_priority_queue.hpp:119
void erase(const const_iterator &it)
erase an element (given as iterator) from the low-priority queue
Definition: low_priority_queue.hpp:74
low_priority_queue(const low_priority_queue< Element, Priority > &lpq)
copy construct a low-priority queue
Definition: low_priority_queue.hpp:51
void pop_min()
remove the min of the low-priority queue
Definition: low_priority_queue.hpp:126