PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/Lib/Array.ahk

http://7plus.googlecode.com/
AutoHotKey | 183 lines | 160 code | 6 blank | 17 comment | 30 complexity | 08dd2c3b0671746bcf0115b03c9745dc MD5 | raw file
  1; Array Lib - temp01 - http://www.autohotkey.com/forum/viewtopic.php?t=49736 
  2; Modified by Fragman
  3; Array is 1-based!!!
  4#include <CEnumerator>
  5Array(Params*){ 
  6	return new CArray(Params*)
  7}
  8IsArray(obj)
  9{
 10	return IsObject(obj) && obj.Is(CArray)
 11}
 12Array_CompareFunc(a, b, c){ 
 13   return a > b ? 1 : a = b ? 0 : -1 
 14} 
 15
 16Class CArray extends CRichObject
 17{
 18	__New(Params*)
 19	{
 20		for index, Param in Params
 21			this.Insert(Param)
 22	}
 23	_NewEnum()
 24	{
 25		return new CEnumerator(this)
 26	}
 27	InsertUnique(Params*)
 28	{
 29		if(Params.MaxIndex() && !this.IndexOf(Params[1]))
 30			this.Insert(Params*)
 31	}
 32	IndexOf(val, opts = "", startpos = 1)
 33	{
 34		if(IsObject(val))
 35		{
 36			for k, v in this
 37			  if(k >= startpos && v = val)
 38				 return k
 39			return 0
 40		}
 41		P := !!InStr(opts, "P"), C := !!InStr(opts, "C")
 42		If A := !!InStr(opts, "A")
 43			matches := Array()
 44		Loop % this.MaxIndex()
 45			If(A_Index>=startpos)
 46				If(match := InStr(this[A_Index], val, C)) and (P or StrLen(this[A_Index])=StrLen(val))
 47				{
 48					If A
 49						matches.Insert(A_Index)
 50					Else
 51						return A_Index
 52				}
 53		If A
 54		  return matches
 55		Else
 56		  return 0
 57	}
 58	IndexOfEqual(val, startpos = 0, key = "")
 59	{
 60		if(IsObject(val))
 61			Loop % this.MaxIndex()
 62				if(A_Index >= startpos && ((key && this[A_Index][key] = val[key]) || this[A_Index].Equals(val)))
 63					return A_Index
 64		else
 65			Loop % this.MaxIndex()
 66				if(A_Index >= startpos && this[A_Index] = val)
 67					return A_Index
 68	}
 69	ToString(Separator = "`n")
 70	{
 71		string := ""
 72		Loop % this.MaxIndex()
 73			string .= (A_Index = 1 ? "" : Separator) this[A_Index]
 74		return string
 75	}
 76	Contains(val)
 77	{
 78		Loop % this.MaxIndex()
 79			if(this[A_Index] = val)
 80				return true
 81		return false
 82	}
 83	Join(sep="`n"){ 
 84	   Loop, % this.MaxIndex() 
 85		  str .= this[A_Index] sep 
 86	   StringTrimRight, str, str, % StrLen(sep) 
 87	   return str 
 88	} 
 89	Copy(){ 
 90	   Return Array().extend(this) 
 91	} 
 92
 93	;~ Insert(index, Params*)
 94	;~ {
 95		;~ if(!Params.MaxIndex())
 96			;~ this._Insert(index)
 97		;~ else
 98			;~ for offset, param in Params
 99				;~ this._Insert(index + (offset-1), param) 
100		;~ Return this 
101	;~ }
102	Reverse(){ 
103	   arr2 := Array() 
104	   Loop, % len:=this.MaxIndex() 
105		  arr2[len-(A_Index-1)] := this[A_Index] 
106	   Return arr2 
107	} 
108	Sort(func="Array_CompareFunc"){ 
109	   n := this.MaxIndex(), swapped := true 
110	   while swapped { 
111		  swapped := false 
112		  Loop, % n-1 { 
113			 i := A_Index 
114			 if %func%(this[i], this[i+1], 1) > 0 ; standard ahk syntax for sort callout functions 
115				this.insert(i, this[i+1]).delete(i+2), swapped := true 
116		  } 
117		  n-- 
118	   } 
119	   Return this 
120	} 
121	Unique(func="Array_CompareFunc"){   ; by infogulch 
122	   i := 0 
123	   while ++i < this.MaxIndex(), j := i + 1 
124		  while j <= this.MaxIndex() 
125			 if !%func%(this[i], this[j], i-j) 
126				this.delete(j) ; j comes after 
127			 else 
128				j++ ; only increment to next element if not removing the current one 
129	   Return this 
130	} 
131
132	Extend(Params*){ 
133		for index, Param in Params
134		  If IsObject(Param) 
135			 for index2, value in Param
136				this.Insert(value)
137	   Return this 
138	} 
139	Pop(){ 
140	   Return this.delete(this.MaxIndex()) 
141	} 
142	Delete(Params*){ 
143		for index, Param in Params
144		{
145			if(IsObject(Param)) ;Arrays have no object keys
146				this._Remove(this.IndexOf(Param))
147			else
148				this._Remove(Param)
149		}
150	   Return this 
151	}
152
153	MaxIndex(){ 
154	   len := this._MaxIndex() 
155	   Return len="" ? 0 : len 
156	}
157
158	Swap(i,j){
159		if(this.MaxIndex()<i||this.MaxIndex()<j||i<1||j<1)
160			return 0
161		x:=this[i]
162		this[i]:=this[j]
163		this[j]:=x
164		return 1
165	}
166
167	Move(i,j)
168	{
169		if(i > this.MaxIndex() || j > this.MaxIndex())
170			return 0
171		if(i = j)
172			return 1
173		x := this[i]
174		this.Remove(i)
175		; I believe the following if is wrong, possibly revert if there are any array problems with the move function
176		; if(i<j)
177			this.Insert(j,x)
178		; else
179			; this.Insert(j-1,x)
180		; }
181		return 1
182	}
183}