UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on April 22, 2023, 07:03:09 AM

Title: Alphabet Bit Mask - String Compares - String Hash
Post by: kevin on April 22, 2023, 07:03:09 AM

This block of code generates a mask of used characters (restricted to the alphabet) based on a given input string. The technique employed involves using bits 0 to 25 of a 32-bit integer to represent a flag for the presence of each character in the alphabet.

If you have a requirement to compare user input with a dictionary of words, you could store the bitmasks of the words as a hash. If two words share the same mask, they at least contain the same set of letters. Then, you can compare the actual strings to determine if they are an exact match.


[pbcode]
   /*

     This block of code generates a mask of used characters
    (restricted to the alphabet) based on a given input string.
    The technique employed involves using bits 0 to 25 of a
    32-bit integer to represent a flag for the presence of
    each character in the alphabet.

      If you have a requirement to compare user input with a
    dictionary of words, you could store the bitmasks of the
    words as a hash. If two words share the same mask, they at
    least contain the same set of letters. Then, you can compare
    the actual strings to determine if they are an exact match.

   */


   for lp=asc("A") to asc("Z")
      print bin$(Alphabet_BitMASK(Chr$(lp)))
      s$+=chr$(lp)
   next

   print bin$(AlphaBet_BitMASK(s$))

   Sync
   waitkey
   
   


Function Alphabet_BitMASK(ThisString$)
         
         ThisString$=upper$(ThisString$)
         
         For lp=1 to Len(ThisString$)
               ThisCHR = mid(ThisString$,lp)-asc("A")
               BitMASK |= 1 << ThisCHR
         next
   
EndFunction BitMASK




[/pbcode]