Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
heap.cc
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 /// @file heap.cc
11 /// @brief class definition for a heap object based on Charlie
12 /// Strauss's heap code ported over from rosetta++.
13 /// @author James Thompson
14 /// @author Charlie Strauss
15 
16 // Rosetta Headers
17 #include <utility/heap.hh>
18 
19 // ObjexxFCL Headers
20 
21 // C++ Headers
22 namespace utility {
23 
24 // @brief Auto-generated virtual destructor
26 
27 
28 //------------------------------------------------------------------------------
29 //------------------------------------------------------------------------------
30 // heap functions
31 // heap is a list integers the value associated with each integer is
32 // in the coheap (or covalue) array. Comparisons <=> are made between
33 // the covalues. The heap entries are thus sorted on the basis of
34 // their covalues. In typical usage, this is an index type sort
35 // the heap values are the indices, and the covalues the value.
36 //
37 // heap is simply an array with two extra storage elments at the
38 // front the first is the max dimension of the heap, the second is
39 // the current number of entries (the third is the minimum value in
40 // heap and the start of the heap). When dimensioning space for it be
41 // sure to add 2 elements more than you think you need.
42 //
43 // heap_init set up an empty heap
44 // heap_insert insert a new value into the heap
45 // heap_extract extract the lowset value (always located in heap(3))
46 //
47 // heap_replace replace the lowest value
48 // (equivalent to heap_extract; heap_insert but faster)
49 // If you call heap_insert with a full heap (ie last = maxsize) then
50 // heap_replace gets called instead.
51 
52 // charlie strauss 1999
53 //------------------------------------------------------------------------------
54 
55 ////////////////////////////////////////////////////////////////////////////////
56 /// @brief sets up an empty heap and stores the dimensioned size
57 /////////////////////////////////////////////////////////////////////////////////
58 void
60  int max_items
61 )
62 {
63  heap_size() = 0;
64  heap_capacity() = max_items;
65 }
66 
67 ////////////////////////////////////////////////////////////////////////////////
68 ///
69 /// @brief
70 /// modifes heap and last_val return val and err.
71 /////////////////////////////////////////////////////////////////////////////////
72 void
74  int & val,
75  float & coval,
76  bool & err
77 )
78 {
79  err = true;
80  //if ( heap_(-1) < 1 ) return;
81  if ( heap_size() < 1 ) return;
82 
83  //int temp_val = heap_(0);
84  //float temp_coval = coheap_(0);
85  int temp_val = heap_[0];
86  float temp_coval = coheap_[0];
87  err = false;
88  --heap_size();
89 
90  if ( heap_size() == 0 ) { // special case for single value in heap
91  val = temp_val; // PB bugfix
92  coval = temp_coval;
93  return;
94  }
95 
96  //heap_(0) = heap_(heap_(-1)); // move last value to front
97  //coheap_(0) = coheap_(heap_(-1));
98  heap_ [0] = heap_ [ heap_size() ]; // move last value to front
99  coheap_[0] = coheap_[ heap_size() ];
100 
101  heap_down(1);
102  // we use a temporary copy so that this can be done in place if need be
103  val = temp_val;
104  coval = temp_coval;
105 }
106 
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 ///
110 /// @brief
111 /// modifies heap and last_dummy, inserts val, returns err
112 /// requires heap_max to be previously set via heap_init
113 /////////////////////////////////////////////////////////////////////////////////
114 void
116  int val,
117  float coval,
118  bool & err
119 )
120 {
121  if ( heap_size() >= heap_capacity() ) {
122  // list is full, use replace instead
123  err = true;
124  if ( coheap_[0] < coval ) heap_replace(val,coval);
125  return;
126  }
127 
128  err = false;
129  //heap_(heap_(-1)) = val; // empty spot on end (zero offset)
130  //coheap_(heap_(-1)) = coval;
131  heap_ [ heap_size() ] = val; // empty spot on end (zero offset)
132  coheap_[ heap_size() ] = coval;
133 
134  ++heap_size();
135  heap_up( heap_size() );
136 
137 }
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 /////////////////////////////////////////////////////////////////////////////////
141 void
143  int val,
144  float coval
145 )
146 {
147  // modifes heap
148 
149  //bool err;
150  //err = false;
151 
152  heap_[0] = val; // overwrite the lowest element
153  coheap_[0] = coval;
154  heap_down(1);
155 }
156 
157 void
158 heap::reset_coval( int val, float coval )
159 {
160  int index = index_for_val( val );
161  if ( index == -1 ) return; // ERROR
162 
163  if ( coheap_[ index ] < coval ) {
164  increase_coval( index, coval );
165  } else if ( coheap_[ index ] > coval ) {
166  decrease_coval( index, coval );
167  } // else, noop -- if the old coval == new coval, don't shuffle any part of the heap
168 }
169 
170 
171 /// @brief returns the smallest covalue stored in the heap.
172 float
174 {
175  return coheap_[ 0 ];
176 }
177 
178 float
179 heap::coval( int index ) const
180 {
181  return coheap_[ index ];
182 }
183 
184 int
185 heap::val( int index ) const
186 {
187  return heap_[ index ];
188 }
189 
190 int
191 heap::size() const
192 {
193  return heap_[ heap_.size() - 1 ];
194 }
195 
196 int
198 {
199  return heap_[ heap_.size() - 2 ];
200 }
201 
202 
203 ////////////////////////////////////////////////////////////////////////////////
204 /////////////////////////////////////////////////////////////////////////////////
205 void
207  int index_in
208 )
209 {
210  float coiv,cocv2;
211  int indx,iv,/*cv,*/cv2,last;
212  indx = index_in-1; // convert to zero offset matrix
213  last = heap_size() - 1; // convert to zero offset matrix
214 
215  if ( last <= 0 ) return; // empty or single element
216  if ( indx > last ) return; // dumbass
217 
218  iv = heap_ [indx]; // the inserted value
219  coiv = coheap_[indx];
220 
221  while ( indx < last ) {
222  int child = 2*indx+1;
223 
224  if ( child > last ) break; // GHAA goto L20; // loop escape
225 
226  int cv = heap_[child];
227  float cocv = coheap_[child];
228 
229  if ( child < last ) {
230  cv2 = heap_[child+1];
231  cocv2 = coheap_[child+1];
232 
233  if ( cocv2 < cocv ) {
234  cv = cv2;
235  cocv = cocv2;
236 
237  ++child;
238  }
239  }
240 
241  if ( coiv <= cocv ) break; /// GHAA goto L20; // loop escape
242  coheap_[indx] = cocv;
243  heap_[indx] = cv;
244  indx = child;
245  }
246 
247  /// WTF? L20:; // loop escape
248  heap_[indx] = iv;
249  coheap_[indx] = coiv;
250 }
251 
252 
253 ////////////////////////////////////////////////////////////////////////////////
254 /////////////////////////////////////////////////////////////////////////////////
255 void
257  int index_in
258 )
259 {
260  float covalue;//,copv;
261 
262  int indx,value;
263  indx = index_in-1; // convert to zero offset matrix
264 
265 
266  value = heap_[indx];
267  covalue = coheap_[indx];
268 
269  while ( indx != 0 ) {
270  int parent = static_cast< int >((indx-1)/2);
271  int pv = heap_[parent];
272  float copv = coheap_[parent];
273  if ( copv < covalue ) break; // GHAA goto L20; // loop escape
274  coheap_[indx] = copv;
275  heap_[indx] = pv;
276  indx = parent;
277  }
278 
279  // GHAA L20:; // loop escape
280  coheap_[indx] = covalue;
281  heap_[indx] = value;
282 }
283 
284 int &
286 {
287  return heap_[ heap_.size() - 1 ];
288 }
289 
290 int &
292 {
293  return heap_[ heap_.size() - 2 ];
294 }
295 
296 
297 void
298 heap::decrease_coval( int index, float coval )
299 {
300  coheap_[ index ] = coval;
301  heap_up( ++index ); // so the index can be decremented again...
302 }
303 
304 void
305 heap::increase_coval( int index, float coval )
306 {
307  coheap_[ index ] = coval;
308  heap_down( ++index ); // so the index can be decremented again...
309 }
310 
311 int
313  for ( int ii = 0, ii_end = heap_size(); ii < ii_end; ++ii ) {
314  if ( heap_[ ii ] == val ) {
315  return ii;
316  }
317  }
318  return -1;
319 }
320 
321 
322 } // ns utility
void heap_replace(int val, float coval)
Definition: heap.cc:142
virtual ~heap()
Definition: heap.cc:25
int & heap_capacity()
Definition: heap.cc:291
void increase_coval(int index, float coval)
Definition: heap.cc:305
void heap_insert(int val, float coval, bool &err)
Inserts a value into the heap that is sorted by coval.
Definition: heap.cc:115
void heap_extract(int &val, float &coval, bool &err)
Extracts the val,coval pair with the lowest coval from the heap. This modifies the heap...
Definition: heap.cc:73
utility::vector0< float > coheap_
Definition: heap.hh:105
Fstring::size_type index(Fstring const &s, Fstring const &ss)
First Index Position of a Substring in an Fstring.
Definition: Fstring.hh:2180
float heap_head() const
returns the smallest covalue stored in the heap.
Definition: heap.cc:173
void heap_init(int max_items)
sets up an empty heap and stores the dimensioned size
Definition: heap.cc:59
void heap_down(int index_in)
Definition: heap.cc:206
member1 value
Definition: Tag.cc:296
int capacity() const
Definition: heap.cc:197
void decrease_coval(int index, float coval)
Definition: heap.cc:298
class definition for a heap object based on Charlie Strauss's heap code ported over from rosetta++...
int val(int index) const
Definition: heap.cc:185
int & heap_size()
Definition: heap.cc:285
int index_for_val(int val)
Definition: heap.cc:312
void reset_coval(int val, float coval)
Definition: heap.cc:158
int size() const
Definition: heap.cc:191
utility::vector0< int > heap_
Definition: heap.hh:104
void heap_up(int index_in)
Definition: heap.cc:256
float coval(int index) const
Definition: heap.cc:179