Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 
11 // Rosetta Headers
12 #include <protocols/frags/heap.hh>
13 
14 // ObjexxFCL Headers
15 // AUTO-REMOVED #include <ObjexxFCL/ObjexxFCL.hh>
16 #include <ObjexxFCL/FArray1A.hh>
17 
18 //Auto using namespaces
19 namespace ObjexxFCL { } using namespace ObjexxFCL; // AUTO USING NS
20 //Auto using namespaces end
21 
22 // #include <ObjexxFCL/FArray2D.hh>
23 // #include <ObjexxFCL/FArray3D.hh>
24 // #include <ObjexxFCL/FArray4D.hh>
25 //#include <ObjexxFCL/format.hh>
26 
27 // C++ Headers
28 //#include <algorithm>
29 //#include <iostream>
30 
31 namespace protocols {
32 namespace frags {
33 
34 //------------------------------------------------------------------------------
35 //------------------------------------------------------------------------------
36 // heap functions
37 // heap is a list integers the value associated with each integer is
38 // in the coheap (or covalue) array. Comparisons <=> are made between
39 // the covalues. The heap entries are thus sorted on the basis of
40 // their covalues. In typical usage, this is an index type sort
41 // the heap values are the indicies, and the covalues the value.
42 //
43 // heap is simply an array with two extra storage elments at the
44 // front the first is the max dimension of the heap, the second is
45 // the current number of entries (the third is the minimum value in
46 // heap and the start of the heap). When dimensioning space for it be
47 // sure to add 2 elements more than you think you need.
48 //
49 // heap_init set up an empty heap
50 // heap_insert insert a new value into the heap
51 // heap_extract extract the lowset value (always located in heap(3))
52 //
53 // heap_replace replace the lowest value
54 // (equivalent to heap_extract; heap_insert but faster)
55 // If you call heap_insert with a full heap (ie last = maxsize) then
56 // heap_replace gets called instead.
57 
58 // charlie strauss 1999
59 //------------------------------------------------------------------------------
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 /// @begin heap_init
63 ///
64 /// @brief
65 /// sets up an empty heap and stores the dimensioned size
66 ///
67 /// @detailed
68 ///
69 /// @param heap - [in/out]? -
70 /// @param coheap - [in/out]? -
71 /// @param max_items - [in/out]? -
72 ///
73 /// @global_read
74 ///
75 /// @global_write
76 ///
77 /// @remarks
78 ///
79 /// @references
80 ///
81 /// @authors
82 ///
83 /// @last_modified
84 /////////////////////////////////////////////////////////////////////////////////
85 void
87  FArray1A_int heap,
88  FArray1A_float coheap,
89  int max_items
90 )
91 {
92  heap.dimension( SRange( -2, star ) );
93  coheap.dimension( SRange( 0, star ) );
94 
95  heap(-1) = 0;
96  heap(-2) = max_items;
97 }
98 
99 ////////////////////////////////////////////////////////////////////////////////
100 /// @begin heap_extract
101 ///
102 /// @brief
103 /// modifes heap and last_val
104 /// return val and err.
105 ///
106 /// @detailed
107 ///
108 /// @param heap - [in/out]? - convert to zero offset matrix
109 /// @param coheap - [in/out]? -
110 /// @param val - [in/out]? -
111 /// @param coval - [in/out]? -
112 /// @param err - [in/out]? -
113 ///
114 /// @global_read
115 ///
116 /// @global_write
117 ///
118 /// @remarks
119 ///
120 /// @references
121 ///
122 /// @authors
123 ///
124 /// @last_modified
125 /////////////////////////////////////////////////////////////////////////////////
126 void
128  FArray1A_int heap, // convert to zero offset matrix
129  FArray1A_float coheap,
130  int & val,
131  float & coval,
132  bool & err
133 )
134 {
135  heap.dimension( SRange( -2, star ) );
136  coheap.dimension( SRange( 0, star ) );
137 
138  err = true;
139  if ( heap(-1) < 1 ) return;
140 
141  int temp_val = heap(0);
142  float temp_coval = coheap(0);
143  err = false;
144  --heap(-1);
145 
146  if ( heap(-1) == 0 ) { // special case for single value in heap
147  val = temp_val; // PB bugfix
148  coval = temp_coval;
149  return;
150  }
151 
152  heap(0) = heap(heap(-1)); // move last value to front
153  coheap(0) = coheap(heap(-1));
154 
155  heap_down(heap,coheap,1);
156 // we use a temporary copy so that this can be done in place if need be
157  val = temp_val;
158  coval = temp_coval;
159 }
160 
161 
162 ////////////////////////////////////////////////////////////////////////////////
163 /// @begin heap_insert
164 ///
165 /// @brief
166 /// modifes heap and last_dummy, inserts val, returns err
167 /// requires heap_max to be previously set via heap_init
168 ///
169 /// @detailed
170 ///
171 /// @param heap - [in/out]? - convert to zero offset matrix
172 /// @param coheap - [in/out]? -
173 /// @param val - [in/out]? -
174 /// @param coval - [in/out]? -
175 /// @param err - [in/out]? -
176 ///
177 /// @global_read
178 ///
179 /// @global_write
180 ///
181 /// @remarks
182 ///
183 /// @references
184 ///
185 /// @authors
186 ///
187 /// @last_modified
188 /////////////////////////////////////////////////////////////////////////////////
189 void
191  FArray1A_int heap, // convert to zero offset matrix
192  FArray1A_float coheap,
193  int val,
194  float coval,
195  bool & err
196 )
197 {
198  heap.dimension( SRange( -2, star ) );
199  coheap.dimension( SRange( 0, star ) );
200 
201  if ( heap(-1) >= heap(-2) ) { // list is full, use replace instead
202  err = true;
203  if ( coheap(0) < coval ) heap_replace(heap,coheap,val,coval);
204  return;
205  }
206 
207  err = false;
208  heap(heap(-1)) = val; // empty spot on end (zero offset)
209  coheap(heap(-1)) = coval;
210 
211  ++heap(-1);
212  heap_up(heap,coheap,heap(-1));
213 
214 }
215 
216 ////////////////////////////////////////////////////////////////////////////////
217 /// @begin heap_replace
218 ///
219 /// @brief
220 ///
221 /// @detailed
222 ///
223 /// @param heap - [in/out]? - convert to zero offset matrix
224 /// @param coheap - [in/out]? -
225 /// @param val - [in/out]? -
226 /// @param coval - [in/out]? -
227 ///
228 /// @global_read
229 ///
230 /// @global_write
231 ///
232 /// @remarks
233 ///
234 /// @references
235 ///
236 /// @authors
237 ///
238 /// @last_modified
239 /////////////////////////////////////////////////////////////////////////////////
240 void
242  FArray1A_int heap, // convert to zero offset matrix
243  FArray1A_float coheap,
244  int val,
245  float coval
246 )
247 {
248  heap.dimension( SRange( -2, star ) );
249  coheap.dimension( SRange( 0, star ) );
250 
251 // modifes heap
252 
253  //bool err;
254  //err = false; set but never used ~Labonte
255 
256  heap(0) = val; // overwrite the lowest element
257  coheap(0) = coval;
258  heap_down(heap,coheap,1);
259 }
260 
261 ////////////////////////////////////////////////////////////////////////////////
262 /// @begin heap_down
263 ///
264 /// @brief
265 ///
266 /// @detailed
267 ///
268 /// @param heap - [in/out]? - convert to zero offset matrix
269 /// @param coheap - [in/out]? -
270 /// @param index_in - [in/out]? -
271 ///
272 /// @global_read
273 ///
274 /// @global_write
275 ///
276 /// @remarks
277 ///
278 /// @references
279 ///
280 /// @authors
281 ///
282 /// @last_modified
283 /////////////////////////////////////////////////////////////////////////////////
284 void
286  FArray1A_int heap, // convert to zero offset matrix
287  FArray1A_float coheap,
288  int index_in
289 )
290 {
291  heap.dimension( SRange( -2, star ) );
292  coheap.dimension( SRange( 0, star ) );
293  float coiv,cocv,cocv2;
294  int indx,child,iv,cv,cv2,last;
295  indx = index_in-1; // convert to zero offset matrix
296  last = heap(-1)-1; // convert to zero offset matrix
297 
298  if ( last <= 0 ) return; // empty or single element
299  if ( indx > last ) return; // dumbass
300 
301  iv = heap(indx); // the inserted value
302  coiv = coheap(indx);
303 
304  while ( indx < last ) {
305  child = 2*indx+1;
306 
307  if ( child > last ) goto L20; // loop escape
308 
309  cv = heap(child);
310  cocv = coheap(child);
311 
312  if ( child < last ) {
313  cv2 = heap (child+1);
314  cocv2 = coheap(child+1);
315 
316  if ( cocv2 < cocv ) {
317  cv = cv2;
318  cocv = cocv2;
319 
320  ++child;
321  }
322  }
323 
324  if ( coiv <= cocv ) goto L20; // loop escape
325  coheap(indx) = cocv;
326  heap(indx) = cv;
327  indx = child;
328  }
329 
330 L20:; // loop escape
331  heap(indx) = iv;
332  coheap(indx) = coiv;
333 }
334 
335 
336 ////////////////////////////////////////////////////////////////////////////////
337 /// @begin heap_up
338 ///
339 /// @brief
340 ///
341 /// @detailed
342 ///
343 /// @param heap - [in/out]? - convert to zero offset matrix
344 /// @param coheap - [in/out]? -
345 /// @param index_in - [in/out]? -
346 ///
347 /// @global_read
348 ///
349 /// @global_write
350 ///
351 /// @remarks
352 ///
353 /// @references
354 ///
355 /// @authors
356 ///
357 /// @last_modified
358 /////////////////////////////////////////////////////////////////////////////////
359 void
361  FArray1A_int heap, // convert to zero offset matrix
362  FArray1A_float coheap,
363  int & index_in
364 )
365 {
366  heap.dimension( SRange( -2, star ) );
367  coheap.dimension( SRange( 0, star ) );
368  float covalue,copv;
369 
370  int indx,parent,value,pv;
371  indx = index_in-1; // convert to zero offset matrix
372 
373 
374  value = heap(indx);
375  covalue = coheap(indx);
376 
377  while ( indx != 0 ) {
378  parent = static_cast< int >((indx-1)/2);
379  pv = heap(parent);
380  copv = coheap(parent);
381  if ( copv < covalue ) goto L20; // loop escape
382  coheap(indx) = copv;
383  heap(indx) = pv;
384  indx = parent;
385  }
386 
387 L20:; // loop escape
388  coheap(indx) = covalue;
389  heap(indx) = value;
390 }
391 
392 
393 } // ns frags
394 } // ns protocols