BinaryHashRec

Returns a pointer to a record in a binary buffer.

Syntax:

BinaryHashRec( handle, rec-size, key-offset, key-size, key-value)

Parameters:

(i) handle: handle of buffer.

(i) rec-size: specifies the fixed length of each record.

(i) key-offset: the offset within a record of the key field (where the first byte in the record is 0).

(i) key-size: specifies the size of the key field, in bytes.

(s) key-value: the value of the key field to be searched for.

Returns:

(i)  the starting position of a record in a binary buffer.

 

This function uses a hashing algorithm to calculate a hash value for the specified "key value", which provides an offset into the binary buffer. It starts searching at that offset for either (1) a record with a key field whose first byte is a 0, or (2) a record with a key field whose value is the same as "key value". For case (1), it stores "key value" in the key field of the found record, and returns the offset of the beginning of that record. For case (2), the offset of the record is returned but not stored. If an appropriate record cannot be found (i.e., if the buffer is full), it returns an error.

 

Note: The binary buffer must consist of fixed-length records. Each record must contain a fixed-length key field in a fixed position.

 

Example:


; In this example, we are going to choose a bunch of fruits at random,
; look the fruit up in a hash table, increment how many times each
; fruit occurs, sort the table when we are done, and write a report to
; a file.
fruits="apple pear banana apricot kiwi orange peach grape grapefruit"
fruitcount=ItemCount(fruits," ")-1
namesize=20
countsize=4
recsize=namesize+countsize
nameoffset=0
countoffset=20
tableentries=100 ; note hash tables should be 20-40 percent bigger than your data
tablesize=tableentries*recsize
hash=BinaryAlloc(tablesize)
For x=1 To 1000
   afruit=ItemExtract(Random(fruitcount)+1,fruits," ")
   offset=BinaryHashRec(hash,recsize,nameoffset,namesize,afruit)
   BinaryIncr4(hash,offset+countoffset)
Next
BinarySort(hash,recsize,countoffset,countsize,@WORD4|@ASCENDING)
offset=0
Report=""
While offset<tablesize
   If offset==0
      If BinaryPeek(hash,0)==0
         offset=BinaryIndex(hash,recsize,"",@FWDSCAN)
      EndIf
   Else
      offset=BinaryIndex(hash,offset,"",@FWDSCAN)
   EndIf
   If offset==0 Then Break
   afruit=BinaryPeekStr(hash,offset,namesize)
   acount=BinaryPeek4(hash,offset+countoffset)
   offset=offset+recsize
   Report=StrCat(report,afruit," ",acount,@CRLF)
EndWhile
BinaryFree(hash)
Message("Random Fruit Report",Report)
See Also:

BinarySort, BinaryIncr, BinaryPeek, BinaryPoke