Here's a quick dictionary / hash library I created when first starting with GLBasic.
I have never used it (changed my mind!), and I don't think it was ever tested.
It was created for simplicity, and perhaps should not be used too much in your main game loop.
I haven't tested it so I don't know if it works, or how fast it is:
Oh, and it only works for storing numerical values (floats) against a string.
It could easily be update for strings.
//==============================================================================
// D I C T I O N A R Y
//==============================================================================
TYPE TDictionaryItem
name$
value
ENDTYPE
TYPE TDictionary
table[] AS TDictionaryItem
FUNCTION Clear:
DIM self.table[0]
ENDFUNCTION
FUNCTION Add: name$, value
// If 'name$' already exists, call 'Set' instead
IF self.Exists(name$)
self.Set(name$, value)
RETURN
ENDIF
LOCAL item AS TDictionaryItem
item.name$ = name$
item.value = value
DIMPUSH self.table[], item
ENDFUNCTION
FUNCTION Get: name$, defaultValue=-1
LOCAL rv# = defaultValue
FOREACH item IN self.table[]
IF item.name$ = name$
rv = item.value
BREAK
ENDIF
NEXT
RETURN rv
ENDFUNCTION
FUNCTION Set%: name$, value
// If the 'name$' doesn't exist in the table, call 'Add' instead
IF NOT self.Exists(name$)
self.Add(name$, value)
RETURN
ENDIF
FOREACH item IN self.table[]
IF item.name$ = name$
item.value = value
BREAK
ENDIF
NEXT
ENDFUNCTION
FUNCTION Exists%: name$
FOREACH item IN self.table[]
IF item.name$ = name$
RETURN TRUE
ENDIF
NEXT
RETURN FALSE
ENDFUNCTION
ENDTYPE
Usage:GLOBAL salaries AS TDictionary
salaries.Clear()
salaries.Add("Peter", 49000)
salaries.Add("Paul", 58000)
salaries.Add("Mary", 63000)
DEBUG "Paul makes $" + salaries.Get("Paul") + " per year\n"
salaries.Set("Paul", 67000)
DEBUG "Paul now makes $" + salaries.Get("Paul") + " per year\n"
IF NOT salaries.Exists("Slydog") THEN DEBUG "Slydog doesn't exist in employee table"