-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryheaps.bb
367 lines (303 loc) · 13.6 KB
/
binaryheaps.bb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
;;;; binaryheaps.bb V1.1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Matheus Cansian
;;;; mscansian at gmail.com
;;;;
;;;; This LIB provide tools to create Binary Heaps ordered lists. It
;;;; can handle more than one simultaneous list and ascending and
;;;; descending order.
;;;;
;;;; HOW-TO: Create a list with New(), use one of the sorting
;;;; constants to select with each sorting order your list will
;;;; operate. Then use the functions Add(), Remove() and Modify(), to
;;;; manipulate the data within the list.
;;;; If you need a debug tool, you can use Draw() to show all your
;;;; list elements.
;; LIB CONFIGURATIONS ;;
Const BinaryHeap_MaxElements% = 1000 ;Max of elements each list can store
Const BinaryHeap_MaxSimultaneous% = 5 ;Max simultaneous list you can have
;Note: Memory storage is calculated by multiplying MaxElements with MaxSimultaneous.
; eg: MaxElements = 1000 and MaxSimultaneous = 5
; 1000 * 5 = 5000 bytes or approx. 5 KB
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; LIB ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;; Probably there's nothing you need to change down there! ;;;;;;;;;;;;
;; LIB CONSTANTS ;;
Const BinaryHeap_Version$ = "1.1"
Const BinaryHeap_SORT_SMALLEST = 1
Const BinaryHeap_SORT_BIGGEST = 2
;; LIB ARRAYS ;;
Dim BinaryHeap_Sort%(BinaryHeap_MaxSimultaneous%) ;What kind of sorting method?
Dim BinaryHeap_Elements%(BinaryHeap_MaxSimultaneous%) ;Count the number of elements
Dim BinaryHeap_Value%(BinaryHeap_MaxSimultaneous%, BinaryHeap_MaxElements%)
Dim BinaryHeap_Data% (BinaryHeap_MaxSimultaneous%, BinaryHeap_MaxElements%)
;;; <summary>Create a list ordered list</summary>
;;; <param name="SortMethod">How the list will be sorted? Use the constants above</param>
;;; <remarks></remarks>
;;; <returns></returns>
;;; <subsystem></subsystem>
;;; <example></example>
Function BinaryHeap_New%(SortMethod%)
For Cont% = 1 To BinaryHeap_MaxSimultaneous%
If BinaryHeap_Sort%(Cont%) = 0 Then
BinaryHeap_Sort%(Cont%) = SortMethod%
Return Cont%
EndIf
Next
Return 0
End Function
;;; <summary>Delete a binary heap thread</summary>
;;; <param name="BinaryHeapThread">BinaryHeap Thread ID</param>
;;; <remarks></remarks>
;;; <returns>1 if the thread was deleted, 0 if the thread dont exists</returns>
;;; <subsystem></subsystem>
;;; <example></example>
Function BinaryHeap_Delete%(BinaryHeapThread%)
If (BinaryHeapThread% <= 0) Or (BinaryHeapThread% > BinaryHeap_MaxSimultaneous%) Then Return 0
If BinaryHeap_Sort%(BinaryHeapThread%) = 0 Then Return 0
;Clear Sort and Elements variables
BinaryHeap_Sort%(BinaryHeapThread%) = 0
BinaryHeap_Elements%(BinaryHeapThread%) = 0
;Clear Value and Data variables
For Cont% = 1 To BinaryHeap_MaxElements%
BinaryHeap_Value%(BinaryHeapThread%, Cont%) = 0
BinaryHeap_Data% (BinaryHeapThread%, Cont%) = 0
Next
Return 1
End Function
Function BinaryHeap_Add%(BinaryHeapThread%, Value%, HeapData%)
;Check HeapThread
If (BinaryHeapThread% <= 0) Or (BinaryHeapThread% > BinaryHeap_MaxSimultaneous%) Then Return 0
If BinaryHeap_Sort%(BinaryHeapThread%) = 0 Then Return 0 ;Thread dont exists
;Check if max elements reached
If BinaryHeap_Elements%(BinaryHeapThread%) >= BinaryHeap_MaxElements% Then Return 0 ;Max elements reached
;Add Elements counter
BinaryHeap_Elements%(BinaryHeapThread%) = BinaryHeap_Elements%(BinaryHeapThread%) + 1
;Get the last element position
Local MyElement% = BinaryHeap_Elements%(BinaryHeapThread%)
;Add element to the end of the list
BinaryHeap_Value%(BinaryHeapThread%, MyElement) = Value%
BinaryHeap_Data%(BinaryHeapThread%, MyElement) = HeapData%
;Get sorting method
Local Sort_Method% = BinaryHeap_Sort%(BinaryHeapThread%)
Local MyValue%, ParentValue%, ParentElement%
Repeat
;Get parent position
ParentElement% = Floor(MyElement%/2)
If ParentElement% <= 0 Then Exit
;Get elements values
MyValue% = BinaryHeap_Value%(BinaryHeapThread%, MyElement)
ParentValue% = BinaryHeap_Value%(BinaryHeapThread%, ParentElement)
;Compare data
If (MyValue% >= ParentValue% And Sort_Method% = BinaryHeap_SORT_SMALLEST) Or (MyValue% <= ParentValue% And Sort_Method% = BinaryHeap_SORT_BIGGEST) Then
;Leave it alone
Exit
Else
;Swap elements
BinaryHeap_Private_Swap(BinaryHeapThread%, MyElement, ParentElement)
;Get new element position
MyElement = ParentElement
EndIf
Forever
Return 1
End Function
;;; <summary>Get the first heap element</summary>
;;; <param name="BinaryHeapThread"></param>
;;; <remarks></remarks>
;;; <returns></returns>
;;; <subsystem></subsystem>
;;; <example></example>
Function BinaryHeap_Remove%(BinaryHeapThread%)
;Check HeapThread
If (BinaryHeapThread% <= 0) Or (BinaryHeapThread% > BinaryHeap_MaxSimultaneous%) Then Return 0
If BinaryHeap_Sort%(BinaryHeapThread%) = 0 Then Return 0 ;Thread dont exists
;Check if the are elements
If BinaryHeap_Elements%(BinaryHeapThread%) <= 0 Then Return 0 ;No elements
;Decrease Elements counter
BinaryHeap_Elements%(BinaryHeapThread%) = BinaryHeap_Elements%(BinaryHeapThread%) - 1
;Save first element data
Local FirstData% = BinaryHeap_Data% (BinaryHeapThread%, 1)
;Delete first element
BinaryHeap_Value% (BinaryHeapThread%, 1) = 0
BinaryHeap_Data% (BinaryHeapThread%, 1) = 0
;Swap last element with first
BinaryHeap_Private_Swap(BinaryHeapThread%, 1, BinaryHeap_Elements%(BinaryHeapThread%)+1)
;Get sorting method
Local Sort_Method% = BinaryHeap_Sort%(BinaryHeapThread%)
Local MyValue%, Child1Value%, Child2Value%, Child1Active%, Child2Active%, Child1Element%, Child2Element%, MyElement% = 1
Repeat
;Get element child
Child1Element% = Floor(MyElement%*2)
Child2Element% = Floor(MyElement%*2)+1
;Get elements value
MyValue% = BinaryHeap_Value%(BinaryHeapThread%, MyElement%)
If (Child1Element%<=BinaryHeap_MaxElements%) Then Child1Value% = BinaryHeap_Value%(BinaryHeapThread%, Child1Element%):Else:Child1Value% = 0
If (Child2Element%<=BinaryHeap_MaxElements%) Then Child2Value% = BinaryHeap_Value%(BinaryHeapThread%, Child2Element%):Else:Child2Value% = 0
;Get elements status
If (Child1Element%<=BinaryHeap_MaxElements%) Then Child1Active% = (BinaryHeap_Data%(BinaryHeapThread%, Child1Element%)<>0):Else:Child1Active% = 0
If (Child2Element%<=BinaryHeap_MaxElements%) Then Child2Active% = (BinaryHeap_Data%(BinaryHeapThread%, Child2Element%)<>0):Else:Child2Active% = 0
;Compare data
If (Child1Active% And ((MyValue% >= Child1Value% And Sort_Method% = BinaryHeap_SORT_SMALLEST) Or (MyValue% <= Child1Value% And Sort_Method% = BinaryHeap_SORT_BIGGEST))) Or (Child2Active% And ((MyValue% >= Child2Value% And Sort_Method% = BinaryHeap_SORT_SMALLEST) Or (MyValue% <= Child2Value% And Sort_Method% = BinaryHeap_SORT_BIGGEST))) Then
If Child1Active% And ((Child1Value% <= Child2Value% And Sort_Method% = BinaryHeap_SORT_SMALLEST) Or (Child1Value% >= Child2Value% And Sort_Method% = BinaryHeap_SORT_BIGGEST))
;Swap with child 1
BinaryHeap_Private_Swap(BinaryHeapThread%, MyElement, Child1Element)
;Get new element position
MyElement = Child1Element
ElseIf Child2Active%
;Swap with child 2
BinaryHeap_Private_Swap(BinaryHeapThread%, MyElement, Child2Element)
;Get new element position
MyElement = Child2Element
Else
Exit
EndIf
Else
;Leave it alone
Exit
EndIf
Forever
Return FirstData%
End Function
;;; <summary>Change an element value</summary>
;;; <param name="BinaryHeapThread"></param>
;;; <param name="Value"></param>
;;; <param name="HeapData"></param>
;;; <param name="NewValue"></param>
;;; <remarks></remarks>
;;; <returns>1 if succeed, 0 if fail</returns>
;;; <subsystem></subsystem>
;;; <example></example>
Function BinaryHeap_Modify%(BinaryHeapThread%, Value%, HeapData%, NewValue%)
;Check HeapThread
If (BinaryHeapThread% <= 0) Or (BinaryHeapThread% > BinaryHeap_MaxSimultaneous%) Then Return 0
If BinaryHeap_Sort%(BinaryHeapThread%) = 0 Then Return 0 ;Thread dont exists
;Store number of elements
TotalElements% = BinaryHeap_Elements%(BinaryHeapThread%)
;Search for the element
For Cont% = 1 To TotalElements%
If BinaryHeap_Value%(BinaryHeapThread%, Cont) = Value% And BinaryHeap_Data%(BinaryHeapThread%, Cont) = HeapData% Then
MyElement% = Cont%
Exit
EndIf
Next
;Change element data
BinaryHeap_Value%(BinaryHeapThread%, MyElement%) = NewValue%
;Get sorting method
Local Sort_Method% = BinaryHeap_Sort%(BinaryHeapThread%)
Local MyValue%, ParentValue%, ParentElement%
Repeat
;Get parent position
ParentElement% = Floor(MyElement%/2)
If ParentElement% <= 0 Then Exit
;Get elements values
MyValue% = BinaryHeap_Value%(BinaryHeapThread%, MyElement)
ParentValue% = BinaryHeap_Value%(BinaryHeapThread%, ParentElement)
;Compare data
If (MyValue% >= ParentValue% And Sort_Method% = BinaryHeap_SORT_SMALLEST) Or (MyValue% <= ParentValue% And Sort_Method% = BinaryHeap_SORT_BIGGEST) Then
;Leave it alone
Exit
Else
;Swap elements
BinaryHeap_Private_Swap(BinaryHeapThread%, MyElement, ParentElement)
;Get new element position
MyElement = ParentElement
EndIf
Forever
Return 1
End Function
;;; <summary>Draw the BinaryHeaps structure in the screen</summary>
;;; <param name="BinaryHeapThread"></param>
;;; <param name="SkipKey">Keycode to exit, or 0 to exit automatically</param>
;;; <param name="Levels">Number of levels to draw, or 0 to draw all</param>
;;; <remarks></remarks>
;;; <returns>Level drawn or 0 for failure</returns>
;;; <subsystem></subsystem>
;;; <example></example>
Function BinaryHeap_Draw%(BinaryHeapThread%, SkipKey%=0, Levels%=0)
;Check HeapThread
If (BinaryHeapThread% <= 0) Or (BinaryHeapThread% > BinaryHeap_MaxSimultaneous%) Then Return 0
If BinaryHeap_Sort%(BinaryHeapThread%) = 0 Then Return 0 ;Thread dont exists
;Get graphics size
Local Width% = GraphicsWidth()
Local Height% = GraphicsHeight()
;Clear screen
SetBuffer BackBuffer():ClsColor 200,200,200
;Get total os elements
Local MaxElements% = BinaryHeap_Elements%(BinaryHeapThread%)
;Get number of levels
Local Elements% = 0, MaxLevels%
Repeat
Elements% = Elements% * 2
Elements% = Elements% + 1
MaxLevels% = MaxLevels% + 1
If Elements% >= MaxElements% Then Exit
Forever
If Levels% = 0 Then Levels% = MaxLevels%
If MaxElements% <= 1 Then Levels% = 2
Local FirstPosition%, LevelSpacing%, LevelElements%, LevelSpacingSteps%, LastLevelSize%
Local OffsetX%, OffsetY%, Value%, HeapActive%
Repeat
;Get input keys
If KeyDown(200) Then OffsetY = OffsetY + 10
If KeyDown(208) Then OffsetY = OffsetY - 10
If KeyDown(205) Then OffsetX = OffsetX - 10
If KeyDown(203) Then OffsetX = OffsetX + 10
If KeyHit(201) And Levels% < MaxLevels% Then Levels% = Levels% + 1
If KeyHit(209) And Levels% > 1 Then Levels% = Levels% - 1
Cls
;Draw header
Color 0,0,0
Text OffsetX+Width%/2, OffsetY+5,"Exploring BinaryHeap: "+BinaryHeapThread%+" | TotalElements: "+MaxElements%,1,0
Text OffsetX+Width%/2, OffsetY+25,"Use PAGEUP/DOWN to change the number of levels and keyboard arrows to move",1,0
;Draw Elements
LastLevelSize% = ((2^Levels%)/2)*StringWidth(" 100 ")*2
For Level% = 1 To Levels%
FirstPosition% = 2^(Level%-1)
LevelSpacing% = LastLevelSize% / 2^Level%
LevelElements% = (2^Level%)/2
LevelSpacingSteps% = -(LevelElements%-1)
For Pos% = 0 To LevelElements%-1
;Check if maximum reached
If FirstPosition%+Pos% > BinaryHeap_MaxElements% Then Exit
;Get element value and data%
Value% = BinaryHeap_Value%(BinaryHeapThread%, FirstPosition%+Pos%)
HeapActive% = (BinaryHeap_Data%(BinaryHeapThread%, FirstPosition%+Pos%)<>0)
;Draw child lines
If Level% < Levels%
Color 180,180,180
;These lines are a total mess!!
Line OffsetX+(Width%/2)+LevelSpacing%*(LevelSpacingSteps%+2*Pos%), OffsetY+Level%*20+40, OffsetX+(Width%/2)+(LastLevelSize% / 2^(Level%+1))*((-((2^(Level%+1))/2-1))+2*Pos%*2), OffsetY+(Level%+1)*20+40
Line OffsetX+(Width%/2)+LevelSpacing%*(LevelSpacingSteps%+2*Pos%), OffsetY+Level%*20+40, OffsetX+(Width%/2)+(LastLevelSize% / 2^(Level%+1))*((-((2^(Level%+1))/2-1))+2*((Pos%*2)+1)), OffsetY+(Level%+1)*20+40
EndIf
;Draw element value
If HeapActive% <> 0 Then Color 0,0,0:Else:Color 230,100,100
Text OffsetX+(Width%/2)+LevelSpacing%*(LevelSpacingSteps%+2*Pos%), OffsetY+Level%*20+40,Value%,1,1
Next
Next
Flip
Until (KeyHit(SkipKey)) Or SkipKey%=0
Return Levels%
End Function
;;;;;;;;;;;;;;;;;;;;;;;; PRIVATE FUNCTIONS ;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;; Please, don't use them in your code ;;;;;;;;;;;;;;;
;;; <summary>Swap two elements</summary>
;;; <param name="BinaryHeapThread">BinaryHeap thread ID</param>
;;; <param name="Position1">Position of the first element</param>
;;; <param name="Position2">Position of the second element</param>
;;; <remarks></remarks>
;;; <returns>1 for successful 0 for failure</returns>
;;; <subsystem></subsystem>
;;; <example></example>
Function BinaryHeap_Private_Swap(BinaryHeapThread%, Position1%, Position2%)
;Check HeapThread
If (BinaryHeapThread% <= 0) Or (BinaryHeapThread% > BinaryHeap_MaxSimultaneous%) Then Return 0
If BinaryHeap_Sort%(BinaryHeapThread%) = 0 Then Return 0 ;Thread dont exists
;Store temp variables
Local TempValue% = BinaryHeap_Value% (BinaryHeapThread%, Position1%)
Local TempData% = BinaryHeap_Data% (BinaryHeapThread%, Position1%)
;Change first
BinaryHeap_Value% (BinaryHeapThread%, Position1%) = BinaryHeap_Value% (BinaryHeapThread%, Position2%)
BinaryHeap_Data% (BinaryHeapThread%, Position1%) = BinaryHeap_Data% (BinaryHeapThread%, Position2%)
BinaryHeap_Value%(BinaryHeapThread%, Position2%) = TempValue%
BinaryHeap_Data% (BinaryHeapThread%, Position2%) = TempData%
Return 1
End Function