Author Topic: Dictionary / Hash list  (Read 1879 times)

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 924
  • KodeSource
    • View Profile
    • KodeSource
Dictionary / Hash list
« on: 2012-Apr-24 »
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.
Code: GLBasic [Select]
//==============================================================================
//  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:
Code: GLBasic [Select]
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"
« Last Edit: 2012-Apr-25 by Slydog »
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10638
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Dictionary / Hash list
« Reply #1 on: 2012-Apr-24 »
Many thanks. Very useful.
In DDgui I use a sorting function, so the inserting for long lists is even quicker...


Code: GLBasic [Select]
// ------------------------------------------ //
//! Get index in ddgui_vals$[] for given string
//! New sorting algorithm for much faster access
// ------------------------------------------ //
FUNCTION DDgui_index%: ddgui_vals AS DDGUI_DLG, BYREF name$, create%
LOCAL up%, dn%, mid%
        up=0; dn=LEN(ddgui_vals.widgets[])-1
        WHILE up < dn
                mid = (up+dn) / 2
                IF ddgui_vals.widgets[mid].wid$ > name$
                        dn=MAX(mid-1, up)
                ELSE
                        IF ddgui_vals.widgets[mid].wid$ < name$
                                up=MIN(dn, mid+1)
                        ELSE
                                RETURN mid // not bigger, not smaller, guess what!?
                        ENDIF
                ENDIF
        WEND

        IF LEN(ddgui_vals.widgets[]) AND ddgui_vals.widgets[up].wid$ = name$ THEN RETURN up


        // not found. But must be at [up].
        IF create%
                dn = LEN(ddgui_vals.widgets[]) // one below last
                REDIM ddgui_vals.widgets[dn+1]
                FOR mid = dn TO up+1 STEP -1
                        ddgui_vals.widgets[mid] = ddgui_vals.widgets[mid-1]
                NEXT
                IF dn>0 AND ddgui_vals.widgets[up].wid$ < name$ THEN up=up+1
                LOCAL widg AS DDGUI_WDG
                widg.wid$ = name$
                ddgui_vals.widgets[up] = widg // copy a fresh on there, clear old junk


                RETURN up
        ENDIF
RETURN -1
ENDFUNCTION
 

This is the core function to this dictionary.
« Last Edit: 2012-Apr-24 by Kitty Hello »

Offline 8bitBoss

  • Mc. Print
  • *
  • Posts: 15
  • May the force be with you
    • View Profile
    • http://astutekitsune.tumblr.com
Re: Dictionary / Hash list
« Reply #2 on: 2012-Apr-25 »
Dude, this is a great idea!
One of my feature requests were dictionaries but I didn't think about crafting them myself. I will borrow your idea and see what I can come up with  :good:
i7 2700k
16GB DDR3 RAM
GeForce GTX 560 Ti
Windows 7 Ultimate 64Bit