Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LazySortedVector1.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_LazySortedVector1_hh
14 #define INCLUDED_protocols_frag_picker_LazySortedVector1_hh
15 
16 // package headers
17 #include <core/types.hh>
18 #include <utility/vector1.hh>
19 #include <utility/pointer/ReferenceCount.hh>
20 
21 // AUTO-REMOVED #include <iostream>
22 #include <algorithm>
23 
24 namespace protocols {
25 namespace frag_picker {
26 
27 template<class T, class StrictWeakOrdering>
29 public:
30  LazySortedVector1(StrictWeakOrdering cmp, Size sorted_capacity,Size max_capacity) :
31  comp(cmp) {
32  max_capacity_ = max_capacity;
33  sorted_ = false;
34  sorted_capacity_ = sorted_capacity;
35  last_ = 1;
36  data_.resize( max_capacity );
37  n_inserts_ = 0;
38  n_denied_ = 0;
39  n_sorts_ = 0;
40  }
41 
43 
44 // std::cerr<<n_inserts_<<" "<<n_denied_<<" "<<n_sorts_<<'\n';
45  }
46 
47  void resize(Size sorted_capacity,Size max_capacity) {
48 
49  max_capacity_ = max_capacity;
50  sorted_capacity_ = sorted_capacity;
51  data_.resize( max_capacity );
52  }
53 
54  inline const T& top() {
55 
56  if(!sorted_)
57  std::sort(data_.begin(), data_.begin() + last_ - 1, comp);
58  return data_[1];
59  }
60 
61  inline bool push(const T& x) {
62 
63  if(comp(worst_,x)) {
64  n_denied_++;
65  return false;
66  }
67 
68  if(last_ > max_capacity_) {
69  std::sort(data_.begin(), data_.begin() + last_ - 1, comp);
70  last_ = sorted_capacity_ + 1;
72 
73  n_sorts_++;
74  }
75  n_inserts_++;
76  data_[last_] = x;
77  last_++;
78  return true;
79  }
80 
81  inline void set_worst( T new_worst) { worst_ = new_worst; }
82  inline T peek_back() const { return data_[sorted_capacity_]; }
83  inline T peek_front() const { return data_[1]; }
84 
85  inline Size count_inserted() const { return data_.size(); }
86 
87 
88  /// @brief sets new capacity for the container
89  inline void set_boundary(Size sorted_capacity) {
90  sorted_capacity_ = sorted_capacity;
91  }
92 
93  inline T& at(Size index) {
94  if(!sorted_)
95  std::sort(data_.begin(), data_.begin() + last_ - 1, comp);
96  return data_.at(index);
97  }
98 
99  inline T& operator[](Size index) {
100  if(!sorted_)
101  std::sort(data_.begin(), data_.begin() + last_ - 1, comp);
102  return data_[index];
103  }
104 
105  inline Size size() const {
106  return std::min(sorted_capacity_,last_);
107  }
108 
110 
111  good_data_.clear();
112  if(last_<=2)
113  return good_data_;
114  good_data_.reserve(size());
115  if(!sorted_)
116  std::sort(data_.begin(), data_.begin() + last_ - 1, comp);
117 
118 // std::copy(data_.begin(), data_.begin() + size() - 1, good_data_.begin() );
119  for(Size i=1;i<=size();i++)
120  good_data_.push_back(data_[i]);
121 
122  return good_data_;
123  }
124 
125  inline void clear() {
126  data_.clear();
127  }
128 private:
131  StrictWeakOrdering comp;
132  bool sorted_;
137  // debug info:
141 };
142 
143 } // frag_picker
144 } // protocols
145 
146 #endif // INCLUDED_protocols_frag_picker_LazySortedVector1_hh