-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module1.bas
410 lines (265 loc) · 12.3 KB
/
Module1.bas
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
Attribute VB_Name = "Module1"
'**********************************************************************************
'
' BinTri - A fast binary search program which exploits additional return values.
'
'**********************************************************************************
' UIUC license
'
' Copyright (c) 2021 Richard Marsden. All rights reserved.
'
' Developed by: marsden.richard.john@gmail.com
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of
' this software and associated documentation files (the "Software"), to deal with
' the Software without restriction, including without limitation the rights
' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
' of the Software, and to permit persons to whom the Software is furnished to
' do so, subject to the following conditions:
'
' * Redistributions of source code must retain the above copyright notice,
' this list of conditions and the following disclaimers.
' * Redistributions in binary form must reproduce the above copyright notice,
' this list of conditions and the following disclaimers in the documentation
' and/or other materials provided with the distribution.
' * Neither the names of Richard Marsden, nor the names of its contributors
' may be used to endorse or promote products derived from this Software
' without specific prior written permission.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
' SOFTWARE.
'*************************************************************************
'
' 'NOTE: Testing is by UnREMing
'
'******************************************************************************
' In VBA these are module declarations i.e. Applies to everything on this page.
'******************************************************************************
Option Explicit 'All variables must be declared
Option Base 0 'arrays start at element 0
Option Compare Binary 'All comparisions in this module are case sensitive. IMPORTANT !!!
'Option Compare Text '........................................insensitive.
'*************************************
Public G_arr(999999) As String '****** A global array (0..999999) = 1 million elements ****
'Public G_arr(8999999) As Long '****** A global array (0..8999999) = 9 million elements ****
Public G_count As Long '****** A global which is used for test purposes only ***
'***********************************************************************************
'NOTE #1: If translating this code to another programming language (and version number!)
' Check how yours handles integer/integer. The \ sign in VBA is integer division
' operator and means 3 \ 2 = 1........ whereas 3 / 2 = 2 (rounds up!)
'***********************************************************************************
'***********************************************************************************
'
'NOTE: MonoBlock is derived from "monobound_binary_search" published in
' https://github.com/scandum/binary_search/blob/master/binary_search.c
'
'***********************************************************************************
'*******************************************************************************************
' MonoBlock - A fast Binary Search with just one compare in loop
'*******************************************************************************************
Function MonoBlock(target As String, ByRef trow As Long, ByRef tarr As Long) As Boolean
Dim low As Long 'VBA: Long are signed 32 bit integers
Dim high As Long
Dim mid As Long
Dim tval As Long
'Dim ret As Long 'ALTERNATE TEST see #2
'*********************************************
'use only on sorted text arrays. No duplicates
'*********************************************
trow = -1
low = 0
high = tarr
MonoBlock = False 'initialise
'*******************************************
Do While high > 1
mid = high \ 2 'see #1
tval = low + mid
G_count = G_count + 1 'FOR TESTING: total up number of compares
'***********************************************
If target >= G_arr(tval) Then 'This is where processor time is eaten
low = tval
End If
' '************** ALTERNATE TEST see #2 ********************
' ret = StrComp(target, G_arr(tval))
'
' If ret = 0 Or ret = 1 Then 'means >= if StrComp had been used
' low = tval 'instead of
' End If 'target >= G_arr(tval)
'
' '**************************************************
high = high - mid
Loop
'*****************************************
G_count = G_count + 1 'equality compare
If G_arr(low) = target Then 'fast because avoids an equality
MonoBlock = True 'compare in loop but has fixed
trow = low 'loop count.
End If
End Function
' '************** ALTERNATE TEST see #2 ********************
' ret = StrComp(G_arr(low), target)
' If ret = 0 Then
' MonoBlock = True
' trow = low
' End If
' '**************************************************'
'End Function
' NOTE #2: ALTERNATE TEST
'
' For strings BinTri uses the function STRCOMP. To ensure its use doesn't give it an unfair speed
' advantage MonoBlock has been timed with and without its use. The speed matches in each case.
' CONCLUSION
' 1) Its the logic that makes BinTri faster NOT its use of STRCOMP.
' 2) The VBA compiler (which uses Microsoft P-Code) will be using the same underlying
' code whether >= or STRCOMP is used.
'
'*******************************************************************************************
' BinFind - Standard Binary Search
'*******************************************************************************************
Function BinFind(target As String, ByRef trow As Long, ByRef tarr As Long) As Boolean
Dim low As Long 'VBA: Long are signed 32 bit integers
Dim high As Long
Dim mid As Long
trow = -1
low = 0
high = tarr
BinFind = False
'*******************************************
Do While low <= high
mid = (low + high) \ 2 'VBA: an integer div eg 3 \ 2 = 1
'*****************************************
G_count = G_count + 2 'FOR TESTING: total up number of compares
If target = G_arr(mid) Then 'compare 1
BinFind = True
trow = mid
G_count = G_count - 1 'or too many counts on leaving
Exit Do
'*****************************************
ElseIf target < G_arr(mid) Then 'compare 2
high = mid - 1
'*****************************************
Else
low = mid + 1
End If
'*****************************************
Loop
End Function
'*******************************************************************************************
' BinTri - A Fast Binary Search
'*******************************************************************************************
Function BinTri(target As String, ByRef trow As Long, ByRef tarr As Long) As Boolean
Dim low As Long 'VBA: Long are signed 32 bit integers
Dim high As Long
Dim ret As Long
Dim mid As Long
trow = -1
low = 0
high = tarr
BinTri = False
'*******************************************
Do While low <= high
mid = (low + high) \ 2 'VBA: an integer div eg 3 \ 2 = 1
'*****************************************
G_count = G_count + 1 'FOR TESTING: total up number of compares
ret = StrComp(target, G_arr(mid)) ' SEE README (FOR STRINGS)
'ret = target - G_arr(mid) ' SEE README (FOR POSITIVE INTEGERS)
If ret = 0 Then 'A MATCH!
BinTri = True
trow = mid
Exit Do
'*****************************************
ElseIf ret < 0 Then 'target less than G_arr(mid)
high = mid - 1
'*****************************************
Else 'target more than G_arr(mid)
low = mid + 1
End If
'*****************************************
Loop
End Function
'*******************************************************************************************
'************************************************************************
'
' gen_arr - create a test array of random but incrementing and non dupicating
' strings or 32 bit positive signed integers.
' Strings are between 58 to 60 char length. last char being a
' unicode char (UTF16)
'
'************************************************************************
Sub gen_arr()
Dim a As Long
Dim trand As Long
Dim tnum As Long
Dim tbase As Long
tbase = 0
For a = 0 To UBound(G_arr)
Randomize
trand = Int(100 * Rnd) ' Generate random value between 0 and 99
tnum = tbase + trand
G_arr(a) = String(47, "*") & Format(tnum, String(10, "0")) & String(Int(3 * Rnd), "*") & ChrW(&HC9) 'Unicode capital E with acute 'STRINGS
'G_arr(a) = tnum 'INTEGERS
'Debug.Print G_arr (a)
tbase = tbase + 113 'up count high enough to avoid collision with the added random numbers
'Using a prime number for this to reduce a repeating pattern developing
Next a
Debug.Print "hello"
End Sub
'************************************************************************
'********
'
' MAIN() AAAA_test123 : Different tests have been REMed out.
'
'********
Sub AAAA_test123()
Dim tlook As String
'Dim tlook As Long
Dim trow As Long
Dim ret, a
Dim bitnum As Long
Dim tarr As Long
Dim trand As Long
Dim tupper As Long
Dim startTime As Variant
Dim tsecs As Double
Dim buf
'Dim starve_memory(10000000) As Long 'TEST
gen_arr
tarr = UBound(G_arr)
G_count = 0
startTime = Timer
G_count = 0 'TEST: used for counting compares. Can be removed later.
tupper = 10000000 '*** number of searches ****
For a = 1 To tupper 'all tests under a minute to run
Randomize ' Initialize random-number generator.
trand = Int(tarr * Rnd) ' grab a random element position number
tlook = G_arr(trand) 'grab it
'If a Mod 10 = 0 Then tlook = Replace(tlook, ChrW(&HC9), ChrW(&HE9)) 'STRING TEST: Uppercase Unicode E with accent to lowercase e with accent.
'If a Mod 10 = 0 Then tlook = Replace(tlook, ChrW(&HC9), "Z") 'STRING TEST: set so 10% of searches are NO finds
'tlook = tlook & "0" 'TEST: for no find
'***** *********************************************
'ret = MonoBlock(tlook, trow, tarr) 'fast binary (low fixed number of compares)
'**************************************************
'**************************************************
'ret = BinFind(tlook, trow, tarr) 'standard binary
'**************************************************
'**************************************************
ret = BinTri(tlook, trow, tarr) 'trinary (half the compares of standard binary)
'**************************************************
If ret = True Then 'REMed out lines were tests to check functions working ok
'Debug.Print a; " "; tlook; " "; trow; G_arr(trow); " "; G_count
Else
'MsgBox "ERROR a NO FIND on a search value:" & tlook
Debug.Print a; " "; tlook; " "; "N0 FIND"; " "; G_count 'leave UnREMed just in case
End If
Next
tsecs = Timer - startTime
If tsecs = 0 Then tsecs = 0.00000001 'stop 0 error
Debug.Print Round(tsecs, 2) & " secs"
Debug.Print Format(tupper / tsecs, "###,####,### searches per sec")
Debug.Print Round(G_count / tupper, 2) & " average compares per search"
End Sub