Long ago - and I am pretty sure the code has been lost to time -
in v2.2.3 (I think) I wrote code to do binary searching on a selection, 
as at that time Search Selection (the old command name) was sequential, 
but sorting the selection was indexed.

Even with the overhead of managing the pointers, and stepping through 
records to find all matching records in the selection, as memory 
serves, it was faster (interpretedly) to use the binary search on a 
selection size of beginning at 6-8 records.

For those who care, an outline of a binary search (on records in 
selection)
The following was written off the stop of my head, in the email editor,
BUT it gets to the basics of a binary search on a selection in 4D.

- get size of selection ($Selection_Size).
- make sure it is sorted on the field you want to search on.

repeat
 $Current_record:= int($Selection_Size/2)

   repeat
    goto selected record([table];$Current_record)
     case of
      :($Current_record>=$Selection_Size) or ($Current_record<=1)
      $Not_Found:=true
      :([table]field = Value)  // found it create collection of 
matching records
       add to set([table];"Matching")
       $previous:=$Current_record
       $Next:=$Current_record

        repeat
          $previous:= previous-1
          goto selected record([table];$previous)
         
          if ([table]field = Value)  // found it create collection of 
matching records
            add to set([table];"Matching")
          else
            $previous:=0
          end if
        until($previous=0)

        repeat
          $$Next:= $Next+1
          goto selected record([table];$Next)
         
          if ([table]field = Value)  // found it create collection of 
matching records
            add to set([table];"Matching")
          else
            $Next:=Selection_Size
          end if
        until($Next= Selection_Size)
        $Mo_More_Matches = true
      :([table]field > Value)  // Not found current record value > 
search value
       $Current_record:=int((1+$Current_record)/2)
      :([table]field < Value)  // Not found current record value < 
search value
       $Current_record:=int((Selection_Size+1+$Current_record)/2)
      end case
until ($Not_Found)) or ($No_More_Mathes)

At this point the set "Matching" contains all records matching the 
criteria, or nothing.

On Tue, 18 Jul 2017 22:16:18 +1000, David Adams via 4D_Tech wrote:
> 
> And, lest anyone forget, you can use binary search logic on sorted
> selections with GOTO SELECTED RECORD. Why not?
---------------
Gas is for washing parts
Alcohol is for drinkin'
Nitromethane is for racing 
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**********************************************************************

Reply via email to