 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Rex Henderson
Joined: 09 Mar 2008 Posts: 3
|
Posted: Thu Mar 13, 2008 9:11 am Post subject: New Scripting.Dictionary wrapper |
|
|
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)
}
|
|
|
| Back to top |
|
 |
_adam Guest
|
|
| Back to top |
|
 |
erictheturtle
Joined: 27 Jun 2007 Posts: 58 Location: California
|
Posted: Fri Mar 14, 2008 5:35 pm Post subject: |
|
|
It looks good. Thanks for posting it. _________________ -m35 |
|
| Back to top |
|
 |
Rex Henderson
Joined: 09 Mar 2008 Posts: 3
|
Posted: Sun Mar 16, 2008 5:45 am Post subject: tweaks |
|
|
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)
}
|
|
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|