PureDIC library : associative array / dictionary

All PureFORM, JaPBe, Libs and useful code maintained by gnozal

Moderator: gnozal

gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

PureDIC library : associative array / dictionary

Post 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
Last edited by gnozal on Tue Aug 18, 2009 11:51 am, edited 1 time in total.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
Randy
User
User
Posts: 29
Joined: Sat Sep 30, 2006 11:36 pm
Location: Atlanta, Georgia

Getting a linking error

Post 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
Maybe it's just a bunch of stuff that happens -- Homer Simpson
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: Getting a linking error

Post 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).
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
Randy
User
User
Posts: 29
Joined: Sat Sep 30, 2006 11:36 pm
Location: Atlanta, Georgia

It was the threadsafe option

Post 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
Maybe it's just a bunch of stuff that happens -- Homer Simpson
User avatar
Randy
User
User
Posts: 29
Joined: Sat Sep 30, 2006 11:36 pm
Location: Atlanta, Georgia

Clearing The Table

Post 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
Maybe it's just a bunch of stuff that happens -- Homer Simpson
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: Clearing The Table

Post 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
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
Randy
User
User
Posts: 29
Joined: Sat Sep 30, 2006 11:36 pm
Location: Atlanta, Georgia

I noticed

Post 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
Maybe it's just a bunch of stuff that happens -- Homer Simpson
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Clearing The Table

Post 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
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: PureDIC library : associative array / dictionary

Post by IdeasVacuum »

or perhaps a spammer easing some posts in before an attack?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply