Page 1 of 1

PureDIC library : associative array / dictionary

Posted: Wed Feb 27, 2008 8:56 am
by gnozal
PureDIC library

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.
Functions
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
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

Getting a linking error

Posted: Tue Aug 11, 2009 3:03 pm
by Randy
Hi Gnozal,

I'm using 4.31 on Windows XP and I'm getting a linking error. I've installed Pure_DIC 4.30.

The error is:
POLINK: error: Unresolved external symbol '_PB_StringBasePosition'
POLINK: fatal error: 1 unresolved external(s).
Any ideas as to what I'm doing wrong? I've tried the example program, the one in the help file, and even just a one line *test = PureDic_Create().

Thanks,

Randy

Re: Getting a linking error

Posted: Tue Aug 11, 2009 3:30 pm
by gnozal
Hi Randy,
Randy wrote:The error is:
POLINK: error: Unresolved external symbol '_PB_StringBasePosition'
POLINK: fatal error: 1 unresolved external(s).
You have probably unicode and/or threasafe enabled.
- threadsafe : you need to activate the appropriate subsytem ; have a look at the help file PureDIC.chm ('threadsafe' topic).
- unicode : not supported (I didn't need it) ; you may add the unicode support yourself (see the included sources).

It was the threadsafe option

Posted: Tue Aug 11, 2009 3:44 pm
by Randy
I didn't realize I had the thing checked nor that it was even an option. :oops: Anyway, all is well. Thanks for the quick response!

Randy

Clearing The Table

Posted: Thu Aug 13, 2009 5:53 pm
by Randy
Hi again Gnozal,

If you decide to work or enhance this library, would it be possible to add something to clear the table? Right now I'm closing and reopening which works. It's just that I thought it would be a neater solution to have something like PureDIC_Clear(*HashTable).

Thanks,

Randy

Re: Clearing The Table

Posted: Fri Aug 14, 2009 1:36 pm
by gnozal
Randy wrote:If you decide to work or enhance this library, would it be possible to add something to clear the table? Right now I'm closing and reopening which works. It's just that I thought it would be a neater solution to have something like PureDIC_Clear(*HashTable).
Just add this to your code :

Code: Select all

ImportC ""
  gdsl_hash_flush(*HashTable)
EndImport 
Procedure PureDIC_Clear(*HashTable) ; Clear hashtable
  ProcedureReturn gdsl_hash_flush(*HashTable) 
EndProcedure
Also note that PB4.40 now has a genuine hash table library (Map) : http://www.purebasic.fr/english/viewtopic.php?t=38503

I noticed

Posted: Fri Aug 14, 2009 2:50 pm
by Randy
Thanks Gnozal,

It was a little while later that I spotted the beta version and downloaded it. The internal map system seems to work so far and had this request inside it.

I don't know when 4.40 becomes final so I'm keeping it and 4.31 just in case. Since 4.31 doesn't have mapping, your routines are still in place so this will help.

Thanks again,

Randy

Re: Clearing The Table

Posted: Wed Dec 03, 2014 8:13 am
by TI-994A
A curious case of deja vu! :lol:

(on Fri Aug 14, 2009)
Randy wrote:If you decide to work or enhance this library, would it be possible to add something to clear the table? Right now I'm closing and reopening which works. It's just that I thought it would be a neater solution to have something like PureDIC_Clear(*HashTable).
(on Wed Dec 03, 2014)
maya112 wrote:If you decide to work or enhance this library, would it be possible to add something to clear the table? Right now I'm closing and reopening which works. It's just that I thought it would be a neater solution to have something like PureDIC_Clear(*HashTable).
@maya112 (if you're not a figment of my imagination, as your name suggests), gnozal had already posted the solution:
gnozal wrote:Just add this to your code :

Code: Select all

ImportC ""
  gdsl_hash_flush(*HashTable)
EndImport 
Procedure PureDIC_Clear(*HashTable) ; Clear hashtable
  ProcedureReturn gdsl_hash_flush(*HashTable) 
EndProcedure

Re: PureDIC library : associative array / dictionary

Posted: Wed Dec 03, 2014 8:14 pm
by IdeasVacuum
or perhaps a spammer easing some posts in before an attack?