<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK: Class OrderArray, что делает?]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=17222&amp;type=atom" />
	<updated>2022-07-03T12:43:00Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=17222</id>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class OrderArray, что делает?]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153752#p153752" />
			<content type="html"><![CDATA[<p>Ну, или, например, значения такого списка используются для каких-то промежуточных вычислений, их нужно подставлять в определённом порядке, а названия ключей не находятся в алфавитном порядке.</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2022-07-03T12:43:00Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153752#p153752</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class OrderArray, что делает?]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153751#p153751" />
			<content type="html"><![CDATA[<p>Предыдущий пример вполне конкретный. Представьте, что вам нужно распечатать этот список, чтобы сохранился порядок по возрасту.</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2022-07-03T12:39:49Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153751#p153751</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class OrderArray, что делает?]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153750#p153750" />
			<content type="html"><![CDATA[<p>А когда это и в правду нужно? Есть конкретный пример?</p>]]></content>
			<author>
				<name><![CDATA[Phoenixxx_Czar]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34426</uri>
			</author>
			<updated>2022-07-03T12:36:39Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153750#p153750</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class OrderArray, что делает?]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153749#p153749" />
			<content type="html"><![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>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2022-07-03T12:27:41Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153749#p153749</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class OrderArray, что делает?]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153748#p153748" />
			<content type="html"><![CDATA[<p>А какое у него практическое применение?</p>]]></content>
			<author>
				<name><![CDATA[Phoenixxx_Czar]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34426</uri>
			</author>
			<updated>2022-07-03T12:09:42Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153748#p153748</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class OrderArray, что делает?]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153747#p153747" />
			<content type="html"><![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>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2022-07-03T11:52:38Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153747#p153747</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class OrderArray, что делает?]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153746#p153746" />
			<content type="html"><![CDATA[<p>В библиотеках одного <a href="https://github.com/CzarOfScripts/ahk-libs">репозитория</a>.</p>]]></content>
			<author>
				<name><![CDATA[Phoenixxx_Czar]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34426</uri>
			</author>
			<updated>2022-07-03T11:23:18Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153746#p153746</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Class OrderArray, что делает?]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153745#p153745" />
			<content type="html"><![CDATA[<p>А где нашли? Похож на мой! <img src="//forum.script-coding.com/img/smilies/smile.png" width="15" height="15" /></p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2022-07-03T10:54:44Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153745#p153745</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[AHK: Class OrderArray, что делает?]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153744#p153744" />
			<content type="html"><![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>]]></content>
			<author>
				<name><![CDATA[Phoenixxx_Czar]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34426</uri>
			</author>
			<updated>2022-07-03T10:22:46Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153744#p153744</id>
		</entry>
</feed>
