Этот класс создаёт псевдо-список, который при перечислении ключей-значений выводит их в порядке добавления:
usualList := {}
usualList.b := "b"
usualList.a := "a"
for k, v in usualList
MsgBox, % v
modifiedList := new OrderArray
modifiedList.b := "b"
modifiedList.a := "a"
for k, v in modifiedList
MsgBox, % v
Class OrderArray {
__New() {
ObjRawSet(this, "__Data", {})
ObjRawSet(this, "__Order", [])
}
__Get(args*) {
return this.__Data[args*]
}
__Set(args*) {
key := args[1]
val := args.Pop()
if(args.Length() < 2 && this.__Data.HasKey(key)) {
this.Delete(key)
}
if(!this.__Data.HasKey(key)) {
this.__Order.Push(key)
}
this.__Data[args*] := val
return val
}
InsertAt(pos, key, val) {
this.__Order.InsertAt(pos, key)
this.__Data[key] := val
}
RemoveAt(pos) {
val := this.__Data[this.__Order[pos]]
this.__Data.Delete(this.__Order[pos])
this.__Order.RemoveAt(pos)
return val
}
Delete(key) {
for i, v in this.__Order {
if(key == v) {
return this.RemoveAt(i)
}
}
}
Length() {
return this.__Order.Length()
}
HasKey(key) {
return this.__Data.HasKey(key)
}
_NewEnum() {
return new OrderArray.Enum(this.__Data, this.__Order)
}
Class Enum {
__New(Data, Order) {
this.Data := Data
this.oEnum := Order._NewEnum()
}
Next(ByRef key, ByRef val := "") {
res := this.oEnum.next(, key)
val := this.Data[key]
return res
}
}
}
Но мой лучше, он ещё и регистр поддерживает:
usualList := {}
usualList.b := "b"
usualList.A := "A"
usualList.a := "a"
str := ""
for k, v in usualList
str .= "key: " . k . "; value: " . v . "`n"
MsgBox,, Usual List, % str
modifiedList := new OrderArray
modifiedList.b := "b"
modifiedList.A := "A"
modifiedList.a := "a"
str := ""
for k, v in modifiedList
str .= "key: " . k . "; value: " . v . "`n"
MsgBox,, Modified List, % str
teadrinkerList := new CaseSenseList
teadrinkerList.b := "b"
teadrinkerList.A := "A"
teadrinkerList.a := "a"
str := ""
for k, v in teadrinkerList
str .= "key: " . k . "; value: " . v . "`n"
MsgBox,, Teadrinker's List, % str
Class OrderArray {
__New() {
ObjRawSet(this, "__Data", {})
ObjRawSet(this, "__Order", [])
}
__Get(args*) {
return this.__Data[args*]
}
__Set(args*) {
key := args[1]
val := args.Pop()
if(args.Length() < 2 && this.__Data.HasKey(key)) {
this.Delete(key)
}
if(!this.__Data.HasKey(key)) {
this.__Order.Push(key)
}
this.__Data[args*] := val
return val
}
InsertAt(pos, key, val) {
this.__Order.InsertAt(pos, key)
this.__Data[key] := val
}
RemoveAt(pos) {
val := this.__Data[this.__Order[pos]]
this.__Data.Delete(this.__Order[pos])
this.__Order.RemoveAt(pos)
return val
}
Delete(key) {
for i, v in this.__Order {
if(key == v) {
return this.RemoveAt(i)
}
}
}
Length() {
return this.__Order.Length()
}
HasKey(key) {
return this.__Data.HasKey(key)
}
_NewEnum() {
return new OrderArray.Enum(this.__Data, this.__Order)
}
Class Enum {
__New(Data, Order) {
this.Data := Data
this.oEnum := Order._NewEnum()
}
Next(ByRef key, ByRef val := "") {
res := this.oEnum.next(, key)
val := this.Data[key]
return res
}
}
}
class CaseSenseList
{
__New() {
ObjRawSet(this, "_list_", [])
}
__Delete() {
this.SetCapacity("_list_", 0)
ObjRawSet(this, "_list_", "")
}
__Set(key, value) {
for k, v in this._list_ {
if !(v[1] == key)
continue
v[2] := value
keyExist := true
} until keyExist
if !keyExist
this._list_.Push([key, value])
Return value
}
__Get(key) {
if (key == "_list_")
Return
for k, v in this._list_ {
if (v[1] == key)
Return v[2]
}
}
_NewEnum() {
Return new this._CustomEnum_(this._list_)
}
class _CustomEnum_
{
__New(list) {
this.i := 0
this.list := list
}
Next(ByRef k, ByRef v := "") {
if ++this.i <= this.list.Length() {
k := this.list[this.i, 1]
v := this.list[this.i, 2]
Return true
}
}
}
Count() {
Return this._list_.Length()
}
Delete(key) {
for k, v in this._list_
continue
until v[1] == key && keyExist := true
Return keyExist ? this._list_.RemoveAt(k)[2] : ""
}
HasKey(key) {
for k, v in this._list_
if (v[1] == key)
Return true
Return false
}
}
Разработка AHK-скриптов:
e-mail
dfiveg@mail.ruTelegram
jollycoder