<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; AHK: Class OrderArray, что делает?]]></title>
		<link>https://forum.script-coding.com/viewtopic.php?id=17222</link>
		<atom:link href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=17222&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «AHK: Class OrderArray, что делает?».]]></description>
		<lastBuildDate>Sun, 03 Jul 2022 12:43:00 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: AHK: Class OrderArray, что делает?]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153752#p153752</link>
			<description><![CDATA[<p>Ну, или, например, значения такого списка используются для каких-то промежуточных вычислений, их нужно подставлять в определённом порядке, а названия ключей не находятся в алфавитном порядке.</p>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Sun, 03 Jul 2022 12:43:00 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153752#p153752</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class OrderArray, что делает?]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153751#p153751</link>
			<description><![CDATA[<p>Предыдущий пример вполне конкретный. Представьте, что вам нужно распечатать этот список, чтобы сохранился порядок по возрасту.</p>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Sun, 03 Jul 2022 12:39:49 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153751#p153751</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class OrderArray, что делает?]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153750#p153750</link>
			<description><![CDATA[<p>А когда это и в правду нужно? Есть конкретный пример?</p>]]></description>
			<author><![CDATA[null@example.com (Phoenixxx_Czar)]]></author>
			<pubDate>Sun, 03 Jul 2022 12:36:39 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153750#p153750</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class OrderArray, что делает?]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153749#p153749</link>
			<description><![CDATA[<p>Выводить ключи-значения в изначальном порядке, когда это нужно:<br /></p><div class="codebox"><pre><code>people := {Mark: &quot;15 years old&quot;, Pete: &quot;30 years old&quot;, John: &quot;50 years old&quot;}
for name, age in people
   MsgBox, % &quot;name: &quot; . name . &quot;`nage: &quot; . age

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

class CaseSenseList
{
   __New() {
      ObjRawSet(this, &quot;_list_&quot;, [])
   }
   
   __Delete() {
      this.SetCapacity(&quot;_list_&quot;, 0)
      ObjRawSet(this, &quot;_list_&quot;, &quot;&quot;)
   }
   
   __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 == &quot;_list_&quot;)
         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 := &quot;&quot;) {
         if ++this.i &lt;= 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 &amp;&amp; keyExist := true
      Return keyExist ? this._list_.RemoveAt(k)[2] : &quot;&quot;
   }
   
   HasKey(key) {
      for k, v in this._list_
         if (v[1] == key)
            Return true
      Return false
   }
}</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Sun, 03 Jul 2022 12:27:41 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153749#p153749</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class OrderArray, что делает?]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153748#p153748</link>
			<description><![CDATA[<p>А какое у него практическое применение?</p>]]></description>
			<author><![CDATA[null@example.com (Phoenixxx_Czar)]]></author>
			<pubDate>Sun, 03 Jul 2022 12:09:42 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153748#p153748</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class OrderArray, что делает?]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153747#p153747</link>
			<description><![CDATA[<p>Этот класс создаёт псевдо-список, который при перечислении ключей-значений выводит их в порядке добавления:<br /></p><div class="codebox"><pre><code>usualList := {}
usualList.b := &quot;b&quot;
usualList.a := &quot;a&quot;

for k, v in usualList
   MsgBox, % v

modifiedList := new OrderArray
modifiedList.b := &quot;b&quot;
modifiedList.a := &quot;a&quot;

for k, v in modifiedList
   MsgBox, % v

Class OrderArray {
   __New() {
      ObjRawSet(this, &quot;__Data&quot;, {})
      ObjRawSet(this, &quot;__Order&quot;, [])
   }

   __Get(args*) {
      return this.__Data[args*]
   }

   __Set(args*) {
      key := args[1]
      val := args.Pop()
      if(args.Length() &lt; 2 &amp;&amp; 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 := &quot;&quot;) {
         res := this.oEnum.next(, key)
         val := this.Data[key]
         return res
      }
   }
}</code></pre></div><p>Но мой лучше, он ещё и регистр поддерживает:<br /></p><div class="codebox"><pre><code>usualList := {}
usualList.b := &quot;b&quot;
usualList.A := &quot;A&quot;
usualList.a := &quot;a&quot;

str := &quot;&quot;
for k, v in usualList
   str .= &quot;key: &quot; . k . &quot;; value: &quot; . v . &quot;`n&quot;
MsgBox,, Usual List, % str

modifiedList := new OrderArray
modifiedList.b := &quot;b&quot;
modifiedList.A := &quot;A&quot;
modifiedList.a := &quot;a&quot;

str := &quot;&quot;
for k, v in modifiedList
   str .= &quot;key: &quot; . k . &quot;; value: &quot; . v . &quot;`n&quot;
MsgBox,, Modified List, % str

teadrinkerList := new CaseSenseList
teadrinkerList.b := &quot;b&quot;
teadrinkerList.A := &quot;A&quot;
teadrinkerList.a := &quot;a&quot;

str := &quot;&quot;
for k, v in teadrinkerList
   str .= &quot;key: &quot; . k . &quot;; value: &quot; . v . &quot;`n&quot;
MsgBox,, Teadrinker&#039;s List, % str


Class OrderArray {
   __New() {
      ObjRawSet(this, &quot;__Data&quot;, {})
      ObjRawSet(this, &quot;__Order&quot;, [])
   }

   __Get(args*) {
      return this.__Data[args*]
   }

   __Set(args*) {
      key := args[1]
      val := args.Pop()
      if(args.Length() &lt; 2 &amp;&amp; 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 := &quot;&quot;) {
         res := this.oEnum.next(, key)
         val := this.Data[key]
         return res
      }
   }
}

class CaseSenseList
{
   __New() {
      ObjRawSet(this, &quot;_list_&quot;, [])
   }
   
   __Delete() {
      this.SetCapacity(&quot;_list_&quot;, 0)
      ObjRawSet(this, &quot;_list_&quot;, &quot;&quot;)
   }
   
   __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 == &quot;_list_&quot;)
         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 := &quot;&quot;) {
         if ++this.i &lt;= 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 &amp;&amp; keyExist := true
      Return keyExist ? this._list_.RemoveAt(k)[2] : &quot;&quot;
   }
   
   HasKey(key) {
      for k, v in this._list_
         if (v[1] == key)
            Return true
      Return false
   }
}</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Sun, 03 Jul 2022 11:52:38 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153747#p153747</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class OrderArray, что делает?]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153746#p153746</link>
			<description><![CDATA[<p>В библиотеках одного <a href="https://github.com/CzarOfScripts/ahk-libs">репозитория</a>.</p>]]></description>
			<author><![CDATA[null@example.com (Phoenixxx_Czar)]]></author>
			<pubDate>Sun, 03 Jul 2022 11:23:18 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153746#p153746</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Class OrderArray, что делает?]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153745#p153745</link>
			<description><![CDATA[<p>А где нашли? Похож на мой! <img src="//forum.script-coding.com/img/smilies/smile.png" width="15" height="15" /></p>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Sun, 03 Jul 2022 10:54:44 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153745#p153745</guid>
		</item>
		<item>
			<title><![CDATA[AHK: Class OrderArray, что делает?]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=153744#p153744</link>
			<description><![CDATA[<p>Нашел класс, не понимаю, в чем заключается его задача?</p><div class="codebox"><pre><code>Class OrderArray {
	__New() {
		ObjRawSet(this, &quot;__Data&quot;, {})
		ObjRawSet(this, &quot;__Order&quot;, [])
	}

	__Get(args*) {
		return this.__Data[args*]
	}

	__Set(args*) {
		key := args[1]
		val := args.Pop()
		if(args.Length() &lt; 2 &amp;&amp; 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 := &quot;&quot;) {
			res := this.oEnum.next(, key)
			val := this.Data[key]
			return res
		}
	}
}</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (Phoenixxx_Czar)]]></author>
			<pubDate>Sun, 03 Jul 2022 10:22:46 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=153744#p153744</guid>
		</item>
	</channel>
</rss>
