Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Looking up values in an array
Message
From
29/08/2001 02:45:46
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00550067
Message ID:
00550166
Views:
19
Is there any reason you did not do the following?

SELECT DISTINCT (bdate) FROM payroll!payroll_tbl WHERE bdate >= bd into array a__gen1


If there is, do this:

SELECT DISTINCT (bdate) FROM payroll!payroll_tbl into array a__gen1 ORDER WHERE bdate

followed by a binary search

binary search example:
clear

for lnElementCount = 1000 to 65000 step 1000

	lnKeyCount = 1000
	
	dimension laElements[lnElementCount]
	dimension laKeys[lnKeyCount]
	
	for j = 1 to lnElementCount
		laElements[j] = j
	endfor

	for j = 1 to lnKeyCount
		laKeys[j] = int(rand() * lnElementCount)
	endfor

	lnSeconds = seconds()
	for j = 1 to lnKeyCount
		=ascan(laElements, laKeys[j])
	endfor
	lnSeconds = seconds() - lnSeconds
	? "ASCAN()"
	?? "Total Elements = "
	?? lnElementCount
	?? " : Keys Sought = "
	?? lnKeyCount
	?? " : Time = "
	?? lnSeconds
	
	lnSeconds = seconds()
	for j = 1 to lnKeyCount
		=aBinScan(@laElements, laKeys[j])
	endfor
	lnSeconds = seconds() - lnSeconds
	? "aBinScan()"
	?? "Total Elements = "
	?? lnElementCount
	?? " : Keys Sought = "
	?? lnKeyCount
	?? " : Time = "
	?? lnSeconds
	?
	
endfor

**
** aBinScan (taArray, tKey)
**	author:  Albert Ballinger
**	parameters:
**		taArray as sorted array
**		tKey as sought for element
**	returns:
**		-1 if tKey less than all elements
**		(1, alen(taArray)) if tKey found, index of element
**		(-1, -alen(taArray)) if tKey not found, index of first 
**			element greater than tKey
**		-(alen(taArray) + 1) if tKey not found, no elements greater
**			than tKey
**
function aBinScan (taArray, tKey)

	return aBinRecurse(@taArray, tKey, 1, alen(taArray))
	
endfunc

**
** aBinScan (taArray, tKey)
**	author:  Albert Ballinger
**	see aBinScan (taArray, tKey)
**
function aBinRecurse (taArray, tKey, tnLPartition, tnRPartition)

	local lnMidPoint
	lnMidPoint = int(tnLPartition + (tnRPartition - tnLPartition) / 2)
	
	if taArray[lnMidPoint] = tKey
		return lnMidPoint	&& found it
	endif
	
	if lnMidPoint = tnLPartition
		* not found
		if alen(taArray) = lnMidPoint	&& at upper boundary?
			if taArray[lnMidPoint] < tKey
				return -(lnMidPoint + 1)	&& out of range
			else
				return -lnMidPoint	&& less than last element
			endif
		else
			return -lnMidPoint
		endif
	endif
	
	if tKey < taArray[lnMidPoint]
		return aBinRecurse(@taArray, tKey, tnLPartition, lnMidPoint)
	else
		return aBinRecurse(@taArray, tKey, lnMidPoint + 1, tnRPartition)
	endif

endfunc
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform