Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BoundedPriorityQueue.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @brief
11 /// @author Dominik Gront (dgront@chem.uw.edu.pl)
12 
13 #ifndef INCLUDED_protocols_frag_picker_BoundedPriorityQueue_hh
14 #define INCLUDED_protocols_frag_picker_BoundedPriorityQueue_hh
15 
16 // package headers
17 #include <core/types.hh>
18 #include <utility/pointer/ReferenceCount.hh>
19 #include <utility/vector1.hh>
20 
21 #include <iostream>
22 #include <algorithm>
23 
24 //Auto Headers
25 
26 
27 namespace protocols {
28 namespace frag_picker {
29 
30 using namespace std;
31 
32 template<class T, class StrictWeakOrdering>
33 class BoundedPriorityQueue : public utility::pointer::ReferenceCount {
34 public:
35  BoundedPriorityQueue(StrictWeakOrdering cmp, Size max_capacity) :
36  comp(cmp) {
37  max_capacity_ = max_capacity;
38  sorted = false;
39  initialized_ = false;
40  last_ = 0;
41 
42  n_sorts = 0;
43  n_denied = 0;
44  }
45 
47  std::cerr<<"sorts, denied: "<<n_sorts<<" "<<n_denied<<"\n";
48  }
49 
50  inline const T& top() {
52  return data_.front();
53  }
54 
55  inline bool push(const T& x) {
56 
57  if ((last_ == max_capacity_) && (comp(worst_, x))) {
58  n_denied++;
59  return false;
60  }
61  if (!initialized_) {
62  worst_ = x;
63  initialized_ = true;
64  last_ = 1;
65  data_.push_back(x);
66  sorted = false;
67  return true;
68  }
69 
70  if (last_ < max_capacity_) {
71  last_++;
72  data_.push_back(x);
73  sorted = false;
74  return true;
75  }
76  data_[last_] = x;
77  sorted = false;
79 
80  worst_ = data_.back();
81  sorted = true;
82 
83  return true;
84  }
85 
86  inline Size count_inserted() { return last_; }
87 
88  /// @brief Removes and returns a reference to the best element in the queue.
89  /// @detailed This element, when compared to any other element in the queue will give true.
90  inline T& pop() {
91 
93  data_.pop_back();
94  return t;
95  }
96 
97  inline void lazy_sort() {
98  if (!sorted) {
99  std::sort(data_.begin(), data_.end(), comp);
100  sorted = true;
101  n_sorts++;
102  }
103  }
104 
105  /// @brief sets new capacity for the container
106  inline void set_boundary(Size max_capacity) {
107  max_capacity_ = max_capacity;
108  }
109 
110  /// @brief Returns a reference to the worst element in the queue.
111  /// @detailed This element when compared to any other element in the queue will give false.
112  inline T& peek_back() {
114  return data_.back();
115  }
116 
117  inline T& peek_front() {
119  return data_.front();
120  }
121 
122  inline T& at(Size index) {
123  return data_.at(index);
124  }
125 
126  inline T& operator[](Size index) {
127  return data_[index];
128  }
129 
130  inline Size size() {
131  return data_.size();
132  }
133 
135  return data_;
136  }
137 
138  inline void clear() {
139  data_.clear();
140  }
141 private:
142  StrictWeakOrdering comp;
143  bool sorted;
149 
150  // debug info
153 };
154 
155 } // frag_picker
156 } // protocols
157 
158 #endif // INCLUDED_protocols_frag_picker_BoundedPriorityQueue_hh