See
![Arrow :arrow:](./images/smilies/icon_arrow.gif)
Cheers.
Helgef wrote: ↑30 Mar 2020, 09:39To allow storing values at any depth, eg, myMap[1,2,3] := myValue.
Seegithub for source and example. Please report any problems.
Code: Select all
MyMap := MapN()
MyMap["One","Two"] := "OneTwo"
MyMap["Three","Four", "Five"] := "ThreeFourFive"
MsgBox MyMap["One","Two"]
MsgBox MyMap["Three", "Four", "Five"]
MsgBox st_printArr(MyMap)
MyMap["One","Two","Three"] := "OneTwoThree"
MsgBox MyMap["One","Two","Three"]
MsgBox st_printArr(MyMap)
; String Things for testing
st_printArr(array, depth:=5, indentLevel:="")
{
for k,v in Array
{
list.= indentLevel "[" k "]"
if (IsObject(v) && depth>1)
list.="`n" st_printArr(v, depth-1, indentLevel . " ")
Else
list.=" => " v
list.="`n"
}
return rtrim(list)
}
; Helgef: An "n-dimensional" map.
class MapN extends map {
static __new()
=> this.prototype.class := this
__item[k1, p*] {
get => p.length ? super[k1][p*] : super[k1]
set {
if p.length && (!super.has(k1) || !(super[k1] is this.class))
super[k1] := (this.class)()
( p.length ) ? super[k1][p*] := value : super[k1] := value
}
}
}
Helgef wrote: ↑30 Mar 2020, 09:39To allow storing values at any depth, eg, myMap[1,2,3] := myValue.
Seegithub for source and example. Please report any problems.
FanaticGuru wrote: ↑08 May 2023, 15:23Could you show me how to convert this so that it modifies the Map base object through prototype other than extending it with MapN?
Code: Select all
class MultiDimMap {
static __New() {
__set := Map.Prototype.GetOwnPropDesc("__Item").set
__get := Map.Prototype.GetOwnPropDesc("__Item").get
Map.Prototype.DefineProp("__Item", {
get:(self, k1, p*) => p.Length
? __get(self, k1)[p*]
: __get(self, k1),
set:(self, value, k1, p*) => (p.Length && (
!(Map.Prototype.Has)(self, k1)
|| !(__get(self, k1) is Map)
) && __set(self, (m:=Map(), m.CaseSense:=self.CaseSense, m), k1),
p.Length
? __get(self, k1)[p*] := value
: __set(self, value, k1))
})
}
}
m := Map()
m[1,2,3] := 123
m["a","b","c"] := "abc"
msgbox m[1,2,3]
msgbox m["a","b","c"]
@ntepa this is some super cool mind bending code. After staring at it for about 30 minutes (after already understanding Helgef code), I understand how the proto stuff is working but I was pretty far from creating it myself. This kind of code is kind of tough to figure out a piece at a time. This does continue to flesh out my baseline knowledge of working with prototypes that will hopefully make it where I am better at this stuff on my own in the future.ntepa wrote: ↑08 May 2023, 19:25FanaticGuru wrote: ↑08 May 2023, 15:23Could you show me how to convert this so that it modifies the Map base object through prototype other than extending it with MapN?Code: Select all
class MultiDimMap { static __New() { __set := Map.Prototype.GetOwnPropDesc("__Item").set __get := Map.Prototype.GetOwnPropDesc("__Item").get Map.Prototype.DefineProp("__Item", { get:(self, k1, p*) => p.Length ? __get(self, k1)[p*] : __get(self, k1), set:(self, value, k1, p*) => (p.Length && ( !(Map.Prototype.Has)(self, k1) || !(__get(self, k1) is Map) ) && __set(self, (m:=Map(), m.CaseSense:=self.CaseSense, m), k1), p.Length ? __get(self, k1)[p*] := value : __set(self, value, k1)) }) } } m := Map() m[1,2,3] := 123 m["a","b","c"] := "abc" msgbox m[1,2,3] msgbox m["a","b","c"]
Code: Select all
; Helgef: An "n-dimensional" map; ntepa: Map.Prototype Class; FanaticGuru: fat arrow Function
; https://www.autohotkey.com/boards/viewtopic.php?p=520860#p520860
MapN() => (__set := Map.Prototype.GetOwnPropDesc("__Item").set, __get := Map.Prototype.GetOwnPropDesc("__Item").get, Map.Prototype.DefineProp("__Item", { get: (self, k1, p*) => p.Length ? __get(self, k1)[p*] : __get(self, k1), set: (self, value, k1, p*) => (p.Length && (!(Map.Prototype.Has)(self, k1) || !(__get(self, k1) is Map)) && __set(self, (m := Map(), m.CaseSense := self.CaseSense, m), k1), p.Length ? __get(self, k1)[p*] := value : __set(self, value, k1)) }))
MapN ; Initialize in Script before using Map
m := Map()
m[1, 2, 3] := 123
m["a", "b", "c"] := "abc"
MsgBox m[1, 2, 3]
MsgBox m["a", "b", "c"]
Code: Select all
map.prototype := map_n.prototype
Code: Select all
multi_d_key := [4,2,8]
if multi_d_array[multi_d_key*]
{
; path exists
}