AutoHotkey Community

It is currently May 26th, 2012, 4:36 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: March 13th, 2008, 9:11 am 
Offline

Joined: March 9th, 2008, 7:12 am
Posts: 3
Here's an alternative approach to the Dictionary.ahk/CoHelper.ahk/Com.ahk stuff which I have not been able to get functioning correctly.

It makes use of the AWESOME! ws4ahk.ahk work by EricTheTurtle to wrap instances of the Scripting.Dictionary.

Code:
#include ws4ahk.ahk
WS_Initialize() ;Scripting support needed for our dictionary
InitDictionary() ;Establish dictionary wrapper code

;dic = Session      ;A dictionary named Session
;CreateDictionary(dic)
;
;mode = 3
;CompareMode(dic,mode)   ;Set CompareMode
;
;AddItem(dic,"Key1","Value1")        ;Simple adds
;AddItem(dic,"Key2","Value2")
;
;GetKeypairs(dic,items,"`n","/")          ;enumerate to ahk parsable string
;msgbox % items
;
;replacement = ValueNew
;ReplaceItem(dic,"Key1",replacement)   ;insert new value, return old
;
;msgbox % "old value is " replacement
;
;SetItem(dic,"Key2","SomeValue")      ;or just overwrite old value
;GetItem(dic,"Key2",item)
;
;msgbox % item
;
;RemoveKey(dic,"Key1",removed)           ;remove key, return value
;
;msgbox % "Removed " removed
;
;ReplaceKey(dic,"Key2","Key3")          ;replace key (that exists or not!)
;
;SetItem(dic,"UnknownKey","Oops")      ;key does not exist, so creates it
;GetKeys(dic,keys)                        ;keys with default delimiter
;
;msgbox % "Keys " keys
;
;GetItems(dic,items,"/")                ;items with explicit delimiter
;msgbox % "Items " items
;
;GetKeypairs(dic,items)            ;keypairs with default delimiters
;msgbox % items
;
;dic2 = Session2                     ; new instance
;CreateDictionary(dic2)
;
;GetCount(dic,CountDic)
;GetCount(dic2,CountDic2)
;
;msgbox % "Counts are " CountDic " and " CountDic2
;
;InvalidKey = DoesNotExist
;KeyExists(dic2,InvalidKey)          ;return empty string if key did not exist
;if(!InvalidKey)
;        msgbox Key does not exist!
;WS_Uninitialize()

InitDictionary()
{
   Code =
   (LTrim
      Function ReplaceItem(DName,DKey,DItem,NoExist)
         if DName.Exists(DKey) then
            ReplaceItem = DName.Item(DKey)
         else
            ReplaceItem = NoExist
         end if
         DName.Item(DKey) = DItem
      End Function

      Function RemoveKey(DName,DKey,NoExist)
         if DName.Exists(DKey) then
            RemoveKey = DName.Item(DKey)
         else
            RemoveKey = NoExist
         end if
         DName.Remove(DKey)
      End Function

      Function GetItemsOrKeys(DName,Delim,ItemsOrKeys)
         if ItemsOrKeys = 0 then
            IorK = DName.Items
         else
            IorK = DName.Keys
         end if
         set ccat =  new StrConCatArray
         ub = ubound(IorK)
         for i = 0 to ub
            if i < ub then ccat.Add(IorK(i) & Delim) else ccat.Add(IorK(i))
         next
         GetItemsOrKeys = ccat.Value
         ccat.Clear
      End Function

      Function GetKeypairs(DName,DelimKP,DelimKV)
         Keys = DName.Keys
         set ccat = new StrConCatArray
         ub = ubound(Keys)
         for i = 0 to ub
            Keypair = Keys(i) & DelimKV & DName.Item(Keys(i))
            if i < ub then ccat.Add(Keypair & DelimKP) else ccat.Add(Keypair)
         next
         GetKeypairs = ccat.Value
         ccat.Clear
      End Function

      '-------------------------------------------
      'String concatenation class (using an array)
      'MULTITUDES performance over native vb concatenation
      '-------------------------------------------
      'Written by Marcus Tucker, July 2004
      'http://marcustucker.com
      '-------------------------------------------
      Class StrConCatArray
         Private StringCounter
         Private StringArray()
         Private StringLength

         'called at creation of instance
         Private Sub Class_Initialize()
            StringCounter = 0
            InitStringLength = 128
            ReDim StringArray(InitStringLength - 1)
            StringLength = InitStringLength
         End Sub

         Private Sub Class_Terminate()
            Erase StringArray
         End Sub

         'add new string to array
         Public Sub Add(byref NewString)
            StringArray(StringCounter) = NewString
            StringCounter = StringCounter + 1

            'ReDim array if necessary
            If StringCounter MOD StringLength = 0 Then
               'redimension
               ReDim Preserve StringArray(StringCounter + StringLength - 1)

               'double the size of the array next time
               StringLength = StringLength * 2
            End If
         End Sub

         'return the concatenated string
         Public Property Get Value
            Value = Join(StringArray, "")
         End Property

         'resets array
         Public Function Clear()
            StringCounter = 0

            Redim StringArray(InitStringLength - 1)
            StringLength = InitStringLength
         End Function
      End Class
   )
   return WS_Exec(Code)
}

CreateDictionary(DName)
{
   Code =
   (LTrim
      dim %DName%
      set %DName% = CreateObject("Scripting.Dictionary")
   )
   return WS_Exec(Code)
}

CompareMode(Dname,ByRef Mode)
{
   If(Mode < 0 || Mode = "")   ;Return the current value of Mode
   {
      Code=
      (LTrim
         %DName%.CompareMode
      )
      return WS_Exec(Code)
   }
   else            ;Set the CompareMode and return the previous mode
   {
      Code =
      (LTrim
         ret = %DName%.CompareMode
         %DName%.CompareMode = %Mode%
         ReturnValue(ret)
      )
      return WS_Eval(Mode,Code)
   }
}

AddItem(DName,DKey,DItem)
{
   DKey := ScriptStr(DKey)
   DItem := ScriptStr(DItem)
   Code=
   (LTrim
      %DName%.Add %DKey%,%DItem%
   )
   return WS_Exec(Code)
}

GetItem(DName,DKey,ByRef DItem)
{
   DKey := ScriptStr(DKey)
   Code=
   (LTrim
      %DName%.Item(%DKey%)
   )
   return WS_Eval(DItem,Code)
}

SetItem(DName,DKey,DItem="")      ;replace an item
{
   DKey := ScriptStr(DKey)
   DItem := ScriptStr(DItem)
   NoExist := ScriptStr("")
   Code=
   (LTrim
      ReplaceItem(%DName%,%DKey%,%DItem%,%NoExist%)
   )
   return WS_Eval(DItem,Code)
}
ReplaceItem(DName,DKey,byref DItem,NoExist = "")      ;replace an item and return the old item
{
   DKey := ScriptStr(DKey)
   DItem := ScriptStr(DItem)
   NoExist := ScriptStr(NoExist)      ;Optional special return marker to indicate no pre-existing item
   Code=
   (LTrim
      ReplaceItem(%DName%,%DKey%,%DItem%,%NoExist%)
   )
   return WS_Eval(DItem,Code)
}

RemoveKey(DName,DKey,byref DItem = "",NoExist = "")         ;Remove and optionally return an item
{
   DKey := ScriptStr(DKey)
   DItem := ScriptStr(DItem)
   NoExist := ScriptStr(NoExist)      ;Optional special return marker to indicate no pre-existing item
   Code=
   (LTrim
      RemoveKey (%DName%,%DKey%,%NoExist%)
   )
   return WS_Eval(DItem,Code)
}

ReplaceKey(DName,DKey,DNKey)
{
   DKey := ScriptStr(DKey)
   DNKey := ScriptStr(DNKey)
   Code =
   (LTrim
      %Dname%.Key(%DKey%) = %DNKey%
   )
   return WS_Exec(Code)
}

KeyExists(DName,byref DKey)
{
   WSDKey := ScriptStr(DKey)
   Code =
   (LTrim
      %DName%.Exists(%WSDKey%)
   )
   ret := WS_Eval(exists,Code)
   msgbox % ":" exists ":"
   if(!exists)
      DKey =
   return ret
}

RemoveAll(DName)
{
   Code=
   (LTrim
      %DName%.RemoveAll
   )
   return WS_Exec(Code)
}

GetItems(DName,ByRef Items,Delim="|")
{
   Delim := ScriptStr(Delim)
   Code=
   (LTrim
      GetItemsOrKeys(%DName%,%Delim%,0)
   )
   return WS_Eval(Items,Code)
}

GetKeys(DName,ByRef Keys,Delim="|")
{
   Delim := ScriptStr(Delim)
   Code=
   (LTrim
      GetItemsOrKeys(%DName%,%Delim%,1)
   )
   return WS_Eval(Keys,Code)
}

GetKeypairs(DName,ByRef Keypairs,DelimKP = "|",DelimKV = "=")
{
   DelimKP := ScriptStr(DelimKP)      ;delimits keypairs
   DelimKV := ScriptStr(DelimKV)      ;delimits a key and value
   Code=
   (LTrim
      GetKeypairs(%DName%,%DelimKP%,%DelimKV%)
   )
   return WS_Eval(Keypairs,Code)
}

GetCount(DName,byref Count)
{
   Code=
   (LTrim
      %DName%.Count
   )
   return WS_Eval(Count,Code)
}



Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 13th, 2008, 10:25 am 
hey man. you forgot a link to the include

http://www.autohotkey.net/~easycom/ws4ahk.ahk

:D


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2008, 5:35 pm 
Offline

Joined: June 27th, 2007, 9:07 pm
Posts: 101
Location: California
It looks good. Thanks for posting it.

_________________
-m35


Report this post
Top
 Profile  
Reply with quote  
 Post subject: tweaks
PostPosted: March 16th, 2008, 5:45 am 
Offline

Joined: March 9th, 2008, 7:12 am
Posts: 3
removed a debug MsgBox from KeyExists()
added DictionaryExists()
added DestroyDictionary()
added CompareMode parameter to CreateDictionary()


Code:
#Include ws4ahk.ahk

InitDictionary()
{
   Code =
   (LTrim
      Function ReplaceItem(DName,DKey,DItem,NoExist)
         if DName.Exists(DKey) then
            ReplaceItem = DName.Item(DKey)
         else
            ReplaceItem = NoExist
         end if
         DName.Item(DKey) = DItem
      End Function

      Function RemoveKey(DName,DKey,NoExist)
         if DName.Exists(DKey) then
            RemoveKey = DName.Item(DKey)
         else
            RemoveKey = NoExist
         end if
         DName.Remove(DKey)
      End Function

      Function GetItemsOrKeys(DName,Delim,ItemsOrKeys)
         if ItemsOrKeys = 0 then
            IorK = DName.Items
         else
            IorK = DName.Keys
         end if
         set ccat =  new StrConCatArray
         ub = ubound(IorK)
         for i = 0 to ub
            if i < ub then ccat.Add(IorK(i) & Delim) else ccat.Add(IorK(i))
         next
         GetItemsOrKeys = ccat.Value
         ccat.Clear
      End Function

      Function GetKeypairs(DName,DelimKP,DelimKV)
         Keys = DName.Keys
         set ccat = new StrConCatArray
         ub = ubound(Keys)
         for i = 0 to ub
            Keypair = Keys(i) & DelimKV & DName.Item(Keys(i))
            if i < ub then ccat.Add(Keypair & DelimKP) else ccat.Add(Keypair)
         next
         GetKeypairs = ccat.Value
         ccat.Clear
      End Function

      Function DictionaryExists(ADictionary)
         DictionaryExists = False
         if TypeName(ADictionary) = "Dictionary" then DictionaryExists = True
      End Function

      '-------------------------------------------
      'String concatenation class (using an array)
      'MULTITUDES performance over native vb concatenation
      '-------------------------------------------
      'Written by Marcus Tucker, July 2004
      'http://marcustucker.com
      '-------------------------------------------
      Class StrConCatArray
         Private StringCounter
         Private StringArray()
         Private StringLength

         'called at creation of instance
         Private Sub Class_Initialize()
            StringCounter = 0
            InitStringLength = 128
            ReDim StringArray(InitStringLength - 1)
            StringLength = InitStringLength
         End Sub

         Private Sub Class_Terminate()
            Erase StringArray
         End Sub

         'add new string to array
         Public Sub Add(byref NewString)
            StringArray(StringCounter) = NewString
            StringCounter = StringCounter + 1

            'ReDim array if necessary
            If StringCounter MOD StringLength = 0 Then
               'redimension
               ReDim Preserve StringArray(StringCounter + StringLength - 1)

               'double the size of the array next time
               StringLength = StringLength * 2
            End If
         End Sub

         'return the concatenated string
         Public Property Get Value
            Value = Join(StringArray, "")
         End Property

         'resets array
         Public Function Clear()
            StringCounter = 0

            Redim StringArray(InitStringLength - 1)
            StringLength = InitStringLength
         End Function
      End Class
   )
   return WS_Exec(Code)
}

CreateDictionary(DName,Mode=2)
{
   Code =
   (LTrim
      dim %DName%
      set %DName% = CreateObject("Scripting.Dictionary")
      %DName%.SetCompareMode(2)
   )
   return WS_Exec(Code)
}

CompareMode(Dname,ByRef Mode)
{
   If(Mode < 0 || Mode = "")   ;Return the current value of Mode
   {
      Code=
      (LTrim
         %DName%.CompareMode
      )
      return WS_Exec(Code)
   }
   else            ;Set the CompareMode and return the previous mode
   {
      Code =
      (LTrim
         ret = %DName%.CompareMode
         %DName%.CompareMode = %Mode%
         ReturnValue(ret)
      )
      return WS_Eval(Mode,Code)
   }
}

AddItem(DName,DKey,DItem)
{
   DKey := ScriptStr(DKey)
   DItem := ScriptStr(DItem)
   Code=
   (LTrim
      %DName%.Add %DKey%,%DItem%
   )
   return WS_Exec(Code)
}

GetItem(DName,DKey,ByRef DItem)
{
   DKey := ScriptStr(DKey)
   Code=
   (LTrim
      %DName%.Item(%DKey%)
   )
   return WS_Eval(DItem,Code)
}

SetItem(DName,DKey,DItem="")      ;replace an item
{
   DKey := ScriptStr(DKey)
   DItem := ScriptStr(DItem)
   NoExist := ScriptStr("")
   Code=
   (LTrim
      ReplaceItem(%DName%,%DKey%,%DItem%,%NoExist%)
   )
   return WS_Eval(DItem,Code)
}
ReplaceItem(DName,DKey,byref DItem,NoExist = "")      ;replace an item and return the old item
{
   DKey := ScriptStr(DKey)
   DItem := ScriptStr(DItem)
   NoExist := ScriptStr(NoExist)      ;Optional special return marker to indicate no pre-existing item
   Code=
   (LTrim
      ReplaceItem(%DName%,%DKey%,%DItem%,%NoExist%)
   )
   return WS_Eval(DItem,Code)
}

RemoveKey(DName,DKey,byref DItem = "",NoExist = "")         ;Remove and optionally return an item
{
   DKey := ScriptStr(DKey)
   DItem := ScriptStr(DItem)
   NoExist := ScriptStr(NoExist)      ;Optional special return marker to indicate no pre-existing item
   Code=
   (LTrim
      RemoveKey (%DName%,%DKey%,%NoExist%)
   )
   return WS_Eval(DItem,Code)
}

ReplaceKey(DName,DKey,DNKey)
{
   DKey := ScriptStr(DKey)
   DNKey := ScriptStr(DNKey)
   Code =
   (LTrim
      %Dname%.Key(%DKey%) = %DNKey%
   )
   return WS_Exec(Code)
}

KeyExists(DName,byref DKey)
{
   WSDKey := ScriptStr(DKey)
   Code =
   (LTrim
      %DName%.Exists(%WSDKey%)
   )
   ret := WS_Eval(exists,Code)
   if(!exists)
      DKey =
   return ret
}

RemoveAll(DName)
{
   Code=
   (LTrim
      %DName%.RemoveAll
   )
   return WS_Exec(Code)
}

GetItems(DName,ByRef Items,Delim="|")
{
   Delim := ScriptStr(Delim)
   Code=
   (LTrim
      GetItemsOrKeys(%DName%,%Delim%,0)
   )
   return WS_Eval(Items,Code)
}

GetKeys(DName,ByRef Keys,Delim="|")
{
   Delim := ScriptStr(Delim)
   Code=
   (LTrim
      GetItemsOrKeys(%DName%,%Delim%,1)
   )
   return WS_Eval(Keys,Code)
}

GetKeypairs(DName,ByRef Keypairs,DelimKP = "|",DelimKV = "=")
{
   DelimKP := ScriptStr(DelimKP)      ;delimits keypairs
   DelimKV := ScriptStr(DelimKV)      ;delimits a key and value
   Code=
   (LTrim
      GetKeypairs(%DName%,%DelimKP%,%DelimKV%)
   )
   return WS_Eval(Keypairs,Code)
}

GetCount(DName,byref Count)
{
   Code=
   (LTrim
      %DName%.Count
   )
   return WS_Eval(Count,Code)
}

DictionaryExists(DName,byref Exists)
{
   Code=
   (LTrim
      DictionaryExists(%DName%)
   )

    return WS_Eval(Exists,Code)
}

DestroyDictionary(DName)
{
   Code=
   (LTrim
      %DName% = Nothing
   )
                return WS_Exec(Code)

}


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: JamixZol and 12 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group