Skip to main content
NetworkAdminKB Logo

VBScript Library

Go Search
NetworkAdminKB.com
Knowledge Base
VBScript Library
Utilities
  
NetworkAdminKB.com > VBScript Library > Knowledge Base > Components > Arrays > FastIndexFind  

FastIndexFind

Article Content

'********************************************************************

'*

'* Function FastIndexFind

'*

'*   Author: NetworkAdminKB.com

'*  Created: 2005-01-13

'* Modified: 2009-01-11

'*

'* Purpose: Search for and Return the Index of an element in an 

'*             unorder/unsorted Array.

'*

'*   Input: aryAny = The unorder/unsorted Array to Search.

'*          anyVar = The item to search for in the Array

'*         blnCase = A Boolean indicating a Case Sensitve Search.

'*                    True  = Case Sensitive

'*                    False = Case InSensitive

'*

'*  Output: Return the index of the location of the element, or Return

'*            Empty if the item is not found.

'*

'*   Notes: Assumes all items are unique.  If they are not unique the

'*            first item encountered is returned, this can be from

'*            either end, or the middle of the Array as both ends and

'*            the middle are searched simultaneously.

'*          This search process folds the array in half and searches

'*            from the ends towards the middle of the two halfs.

'*            Thus 4 searches are going on simultaneously.

'*          A Constant minimum of minEls Elements is required to preform,

'*            the Fast Search otherwise a sequential search is performed.

'*

'* Calls:

'*   blnStrComp

'*

'* Changes:

'* 2009-01-11: Changed all comparisons to use blnStrComp to simplify

'*               procedure.

'********************************************************************

Function FastIndexFind(ByVal aryAny, ByVal anyVar, ByVal blnCase)

  'Version 2.0 2009-01-11

  Dim intOdd, maxLoop, intUbound, intMid, x

  'The minimum elements in the array required to perform

  'the FastIndexFind on, otherwise use a standard sequential search.

  Const minEls = 5

 

  FastIndexFind = Empty

 

  intUBound = UBound(aryAny)

 

  If (UBound(aryAny) Mod 2) <> 0 Then

    maxLoop = Int(intUBound/2)

    intMid = maxLoop + 1

  Else

    intOdd = Int(intUBound/2)

    maxLoop = intOdd - 1

    intMid = intOdd + 1

  End If '(UBound(aryAny) Mod 2) <> 0

 

  If intUBound =< minEls Then

    'Perform a sequential search on smaller arrays.

    For x = 0 to intUBound     

      If blnStrComp(anyVar, "=", aryAny(x), blnCase) Then

        FastIndexFind = x

        Exit For

      End If 'blnStrComp(anyVar, "=", aryAny(x), blnCase)

    Next 'x

  Else

    If Not IsEmpty(intOdd) Then

      If blnStrComp(anyVar, "=", aryAny(intOdd), blnCase) Then

        FastIndexFind = intOdd

        Exit Function

      End If 'blnStrComp(anyVar, "=", aryAny(intOdd), blnCase)

    End If 'Not IsEmpty(intOdd)

    'If the item is not found at intOdd search the rest of

    'the array.

    For x = 0 to maxLoop

 

      If blnStrComp(anyVar, "=", aryAny(x), blnCase) Then

        FastIndexFind = x

        Exit For

      End If 'blnStrComp(anyVar, "=", aryAny(x), blnCase)

       

      If blnStrComp(aryAny(maxLoop - x), "=", anyVar, blnCase) Then

        FastIndexFind = maxLoop - x

        Exit For

      End If 'blnStrComp(aryAny(maxLoop - x), "=", anyVar, blnCase)

       

      If blnStrComp(aryAny(intMid + x), "=", anyVar, blnCase) Then

        FastIndexFind = intMid + x

        Exit For

      End If 'blnStrComp(aryAny(intMid + x), "=", anyVar, blnCase)

       

      If blnStrComp(aryAny(intUBound - x), "=", anyVar, blnCase) Then

        FastIndexFind = intUBound - x

       Exit For

      End If 'aryAny(maxLoop - x) = anyVar        

 

      If x > Int(maxLoop/2) Then

        'We only need to process half of these values because

        'we are searching from both ends.

        Exit For

      End If 'x > Int(maxLoop/2)

    Next 'x

  End If 'intUBound =< minEls

End Function 'FastIndexFind

Keywords
Array Search 
Related Articles
blnStrComp.aspx 
Last modified at 1/11/2009 1:33 PM  by TEMPLATE\kbadmin 
 
 NetworkAdminKB.com
 Copyright © 2008 NetworkAdminKB.com, All rights reserved. Terms of Use | Contact US