Some typed container classes implemented as hashtables. A bag is a unique collection of values. Typically you would add values to the bag and later test if they exist. A map is a collection of unique keys mapped to values. You might use a map to translate say a window handle to a class or find an array index given a key. These are also often referred to as dictionaries.

For new types, copy an existing map or bag and change the types. Strings use a slightly different hash function.

The following bags are included:

  • DWord - Bag of unique dword values
  • Long - Bag of unique long values
  • String - Bag of unique string values

The following maps are included:

  • DWord/Long - maps a dword value to a long value
  • DWord/String - maps a dword value to a string value
  • Long/Long - maps a long value to a long value
  • Long/String - maps a long value to a string value
  • String/Long - maps a string value to a long value

It's implemented in two phases. Bag.inc provides the general hashtable code. Each implementation uses that to provide a specific typed bag or map.

To use, copy Bag.inc and the bag or map .inc file you want to use. For the following example, you would copy Bag.inc, LongBag.inc, and place the source in say Test.bas.

#Compiler PBWin 10
#Compile Exe
#Dim All
#Include "LongBag.inc"
Function PBMain () As Long 
  Local i As Long 
  Dim b As iLongBag 
  Call NewLongBag( b ): ' Create a new bag 
  For i = 1 To 10 
    b.Add( i ): ' Add 10 values 
  Next 
  For i = 1 To 10 
    If b.Contains( i ) Then 
	  'If the values exist, do something 
	End If 
  Next
End Function