PureDIC library : associative array / dictionary
Posted: Wed Feb 27, 2008 8:56 am
PureDIC library
Overview
Functions
PB4.xx : http://gnozal.ucoz.com/PureDIC_.htm
Example
Download
Only available for Purebasic Windows x86
PB4.0x-4.20 : http://gnozal.ucoz.com/PureDIC_.zip
PB4.3x : http://gnozal.ucoz.com/PureDIC_430.zip
PB4.4x : http://gnozal.ucoz.com/PureDIC_440.zip
PB4.5x : http://gnozal.ucoz.com/PureDIC_450.zip
PB4.6x : http://gnozal.ucoz.com/PureDIC_460.zip
PB5.0x : http://gnozal.ucoz.com/PureDIC_500.zip
Overview
Code: Select all
This is a purebasic associative array / dictionary library using some GDSL hash table functions.
Find more information about GDSL at http://directory.fsf.org/all/GDSL.html
An associative array is an abstract data type composed of a collection of keys and a collection of values, where each key is associated with one value. The operation of finding the value associated with a key is called a lookup or indexing, and this is the most important operation supported by an associative array. The relationship between a key and its value is sometimes called a mapping or binding. For example, if the value associated with the key "bob" is 7, we say that our array maps "bob" to 7.
PB4.xx : http://gnozal.ucoz.com/PureDIC_.htm
Example
Code: Select all
; Parsing callback - IMPORTANT : it MUST be a ProcedureC() !!!
ProcedureC ParseCB(*HTEObject.HTElement, NotUsed.l, *uData.HTInfo)
Protected StringKey.s, Value.l, UserValue.l, *HashTable
;
*HashTable = *uData\HT
StringKey = PeekS(*HTEObject\key)
Value = *HTEObject\uData
UserValue = *HTEObject\uData
;
Debug Str(*HashTable) + " -> '" + StringKey + "' : " + Str(Value) + " [" + Str(UserValue) + "]"
;
; Use #GDSL_MAP_STOP : if the parsing must be stopped
; Use #GDSL_MAP_CONT : if the parsing must continue
;
ProcedureReturn #GDSL_MAP_CONT
EndProcedure
;
; Create new dictionary
*HashTable = PureDIC_Create()
If *HashTable
Debug "Dictionary created, handle = " + Str(*HashTable)
; Add random elements
For i = 0 To 200
a$ = Chr(Random(255)) + Chr(Random(255)) + Chr(Random(255)) + Chr(Random(255)) + Chr(Random(255)) + Chr(Random(255))
;
PureDIC_Add(*HashTable, a$, i)
;
If i = 100
b$ = a$
EndIf
If i = 150
c$ = a$
EndIf
Next
; Count elements in dictionnary
Debug "Count = " + Str(PureDIC_Count(*HashTable))
; Parse elements in dictionnary
Debug "PARSE table"
PureDIC_Parse(*HashTable, @ParseCB())
Debug "END Parse"
; Get the 100th added element
Debug "Expected is 100 -> " + Str(PureDIC_Get(*HashTable, b$))
; Get the 150th added element
Debug "Expected is 150 -> " + Str(PureDIC_Get(*HashTable, c$))
; See if elements exists
d$ = "------"
Debug "Expected is 0 -> " + Str(PureDIC_Exist(*HashTable,d$))
Debug "Expected is 1 -> " + Str(PureDIC_Exist(*HashTable,c$))
; Close dictionary
PureDIC_Close(*HashTable)
EndIf
Only available for Purebasic Windows x86
PB4.0x-4.20 : http://gnozal.ucoz.com/PureDIC_.zip
PB4.3x : http://gnozal.ucoz.com/PureDIC_430.zip
PB4.4x : http://gnozal.ucoz.com/PureDIC_440.zip
PB4.5x : http://gnozal.ucoz.com/PureDIC_450.zip
PB4.6x : http://gnozal.ucoz.com/PureDIC_460.zip
PB5.0x : http://gnozal.ucoz.com/PureDIC_500.zip