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