1

Тема: AHK: Class OrderArray, что делает?

Нашел класс, не понимаю, в чем заключается его задача?

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
		}
	}
}
Win10: LTSC (21H2); AHK: ANSI (v1.1.36.02)

2

Re: AHK: Class OrderArray, что делает?

А где нашли? Похож на мой!

Разработка AHK-скриптов:
e-mail dfiveg@mail.ru
Telegram jollycoder

3 (изменено: Phoenixxx_Czar, 2022-07-03 15:23:39)

Re: AHK: Class OrderArray, что делает?

В библиотеках одного репозитория.

Win10: LTSC (21H2); AHK: ANSI (v1.1.36.02)

4

Re: AHK: Class OrderArray, что делает?

Этот класс создаёт псевдо-список, который при перечислении ключей-значений выводит их в порядке добавления:

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.ru
Telegram jollycoder

5

Re: AHK: Class OrderArray, что делает?

А какое у него практическое применение?

Win10: LTSC (21H2); AHK: ANSI (v1.1.36.02)

6

Re: AHK: Class OrderArray, что делает?

Выводить ключи-значения в изначальном порядке, когда это нужно:

people := {Mark: "15 years old", Pete: "30 years old", John: "50 years old"}
for name, age in people
   MsgBox, % "name: " . name . "`nage: " . age

mPeople := new CaseSenseList
mPeople.Mark := "15 years old"
mPeople.Pete := "30 years old"
mPeople.John := "50 years old"
for name, age in mPeople
   MsgBox, % "name: " . name . "`nage: " . age

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.ru
Telegram jollycoder

7

Re: AHK: Class OrderArray, что делает?

А когда это и в правду нужно? Есть конкретный пример?

Win10: LTSC (21H2); AHK: ANSI (v1.1.36.02)

8

Re: AHK: Class OrderArray, что делает?

Предыдущий пример вполне конкретный. Представьте, что вам нужно распечатать этот список, чтобы сохранился порядок по возрасту.

Разработка AHK-скриптов:
e-mail dfiveg@mail.ru
Telegram jollycoder

9

Re: AHK: Class OrderArray, что делает?

Ну, или, например, значения такого списка используются для каких-то промежуточных вычислений, их нужно подставлять в определённом порядке, а названия ключей не находятся в алфавитном порядке.

Разработка AHK-скриптов:
e-mail dfiveg@mail.ru
Telegram jollycoder