EMAN2
grid_queue.cpp
Go to the documentation of this file.
1// Copyright (C) 2005-2008 Washington University in St Louis, Baylor College of Medicine. All rights reserved
2// Author: Tao Ju, Refactored by Sasakthi S. Abeysinghe (sasakthi@gmail.com)
3// Description: Grid queue
4
5#include "grid_queue.h"
6using namespace wustl_mm::SkeletonMaker;
7
8 GridQueue::GridQueue( ) {
9 head = NULL ;
10 tail = NULL ;
11 numEles = 0 ;
12 }
13
15 return head ;
16 }
17
19 return numEles ;
20 }
21
22 /* Naive bubble sort */
23 void GridQueue::sort( int eles ) {
24 //printf("Sorting elements with descending scores...\n") ;
25 gridQueueEle* pre ;
26 gridQueueEle* e1 ;
27 gridQueueEle* e2 ;
28
29 for ( int i = eles - 1 ; i > 0 ; i -- )
30 {
31 pre = NULL ;
32 e1 = head ;
33 e2 = e1->next ;
34
35 for ( int j = 0 ; j < i ; j ++ )
36 {
37 if ( e1->score < e2->score )
38 {
39 swapEle( pre, e1, e2 ) ;
40 }
41 else if ( e1->score == e2->score && rand() < RAND_MAX / 2)
42 {
43 swapEle( pre, e1, e2 ) ;
44 }
45
46 if ( pre == NULL )
47 {
48 pre = head ;
49 }
50 else
51 {
52 pre = pre->next;
53 }
54 e1 = pre->next ;
55 e2 = e1->next ;
56 }
57 }
58
59 /* Debugging
60 pre = head ;
61 while ( pre != NULL )
62 {
63 printf("%d ", pre->score ) ;
64 pre = pre->next ;
65 }*/
66 }
67
68
69 void GridQueue::pushQueue( int xx, int yy, int zz ) {
70 gridQueueEle* ele = new gridQueueEle ;
71 ele->x = xx ;
72 ele->y = yy ;
73 ele->z = zz ;
74 ele->score = 0 ;
75 ele->next = NULL ;
76 if ( head == NULL )
77 {
78 head = ele ;
79 }
80 else
81 {
82 tail->next = ele ;
83 }
84 tail = ele ;
85 numEles ++ ;
86 }
87
88 int GridQueue::popQueue( int& xx, int& yy, int& zz ) {
89 if ( head == NULL )
90 {
91 return 0 ;
92 }
93
94 xx = head->x ;
95 yy = head->y ;
96 zz = head->z ;
97
98 gridQueueEle* temp = head ;
99 head = head->next ;
100 delete temp ;
101
102 if ( head == NULL )
103 {
104 tail = NULL ;
105 }
106 numEles -- ;
107
108 return 1 ;
109 }
110
111 /* Switching two elements */
113 {
114 if ( pre != NULL )
115 {
116 e1->next = e2->next ;
117 e2->next = pre->next ;
118 pre->next = e2 ;
119
120 if ( tail == e2 )
121 {
122 tail = e1 ;
123 }
124 }
125 else
126 {
127 e1->next = e2->next ;
128 e2->next = e1 ;
129 head = e2 ;
130
131 if ( tail == e2 )
132 {
133 tail = e1 ;
134 }
135 }
136 }
int popQueue(int &xx, int &yy, int &zz)
Definition: grid_queue.cpp:88
void pushQueue(int xx, int yy, int zz)
Definition: grid_queue.cpp:69
void swapEle(gridQueueEle *pre, gridQueueEle *e1, gridQueueEle *e2)
Definition: grid_queue.cpp:112