PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Zip.ahk

http://github.com/infogulch/Zip
AutoHotKey | 133 lines | 23 code | 5 blank | 105 comment | 8 complexity | 3c3cb8914ac3bcaf4632d9b723d008f9 MD5 | raw file
  1. /*!
  2. Library: Zip, version 1.0
  3. Zip arrays.
  4. Supports AutoHotkey_L and AutoHotkey v2 Alpha
  5. Author: infogulch
  6. Version: 1.0
  7. */
  8. /*!
  9. Function: Zip( [mode,] array1 [..., arrayN] )
  10. Parameters:
  11. mode - (Optional) If the first arg is a string it is interpreted as the mode specifier
  12. which modifies the function's behavior when arrays have differing sizes.
  13. array1...arrayN - Arrays to zip.
  14. Returns:
  15. An array of arrays where the *i*-th array contains the *i*-th element from all of the parameter arrays.
  16. E.g. passing arrays `[1,2,3]` and `["a","b","c"]` would return `[[1,"a"],[2,"b"],[3,"c"]]`
  17. Throws:
  18. Non object arg passed.
  19. Extra:
  20. ## Remarks
  21. ### Behavior when arrays have different sizes
  22. This is changed by specifying the `mode` parameter.
  23. * default (omitted or when options param is any string other than `all` or `copy`)
  24. When one array is longer than another, truncate the longer array to the length
  25. of the shorter array.
  26. See example A
  27. * all
  28. When one array is shorter than another it is extended with empty values (skipped).
  29. This can result in sparsely populated return arrays like: `{3: "c", 5: "e"}`
  30. See example B
  31. Since nothing is truncated in this mode the result is 100% reversible. (Example C)
  32. * copy
  33. When one array is shorter than another, the last value in the
  34. array is copied to extended it.
  35. See example D
  36. ### Unzipping
  37. zip() in conjunction with the \* operator can be used to unzip zipped arrays.
  38. See example C
  39. ### Examples
  40. These are referred to in the examples.
  41. When they're lined up like this, you can look down the columns to see the values it will return.
  42. > a1 := ["a","b","c"]
  43. > a2 := ["A","B","C"]
  44. > a3 := [ 1 , 2 , 3 , 4] ; this one has 4 elements
  45. A:
  46. > zipped := zip(a1, a2, a3)
  47. > ; [["a", "A", 1], ["b", "B", 2], ["c", "C", 3]]
  48. > ; notice that the value 4 has been omitted entirely
  49. B:
  50. > zipped := zip("all", a1, a2, a3)
  51. > ; [["a", "A", 1], ["b", "B", 2], ["c", "C", 3], {3: 4}]
  52. > ; notice the sparsely-populated fourth array
  53. C:
  54. > unzipped := zip(zipped*)
  55. > ; [["a","b","c"],["A","B","C"],[1,2,3,4]]
  56. > ; using zip again unzips and returns the originals
  57. D:
  58. > zipped := zip("copy", a1, a2, a3)
  59. > ; [["a", "A", 1], ["b", "B", 2], ["c", "C", 3], ["c", "C", 4]]
  60. > ; notice where "all" produced a sparsely-populated fourth array, this copies
  61. > ; the last value of the smaller arrays to fill in missing indexes
  62. E: side-effects of "copy" mode
  63. > unzipped := zip(zipped*)
  64. > ; [["a", "b", "c", "c"], ["A", "B", "C", "C"], [1, 2, 3, 4]]
  65. > ; notice the extra "c" and "C" where they were too short.
  66. ### See also
  67. [AutoHotkey Forum topic](http://www.autohotkey.com/forum/viewtopic.php?t=77832)
  68. The default mode is similar to [Python's zip](http://docs.python.org/library/functions.html#zip)
  69. ### License
  70. > Copyright (c) 2011, infogulch
  71. > All rights reserved.
  72. >
  73. > Redistribution and use in source and binary forms, with or without modification, are permitted
  74. > provided that the following conditions are met:
  75. >
  76. > * Redistributions of source code must retain the above copyright notice, this list of
  77. > conditions and the following disclaimer.
  78. > * Redistributions in binary form must reproduce the above copyright notice, this list
  79. > of conditions and the following disclaimer in the documentation and/or other materials
  80. > provided with the distribution.
  81. >
  82. > THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  83. > IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  84. > AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  85. > CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  86. > CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  87. > SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  88. > THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  89. > OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  90. > POSSIBILITY OF SUCH DAMAGE.
  91. */
  92. Zip( arrays* )
  93. {
  94. if !IsObject(arrays[1])
  95. mod := arrays.remove(1), mod := (mod = "all") + (mod = "copy")*2
  96. count := arrays[1].MaxIndex()
  97. loop % arrays.MaxIndex()
  98. {
  99. if !IsObject(arrays[A_Index])
  100. throw Exception("Zip() only accepts arrays", -1)
  101. c := arrays[A_Index].MaxIndex()
  102. if (mod ? c > count : c < count)
  103. count := c
  104. }
  105. ret := []
  106. i := 0
  107. loop % count
  108. loop % (arrays.MaxIndex(), i++)
  109. if (arrays[A_Index].HasKey(i))
  110. ret[i,A_Index] := arrays[A_Index, i]
  111. else if (mod == 2 && i > arrays[A_Index].MaxIndex())
  112. ret[i,A_Index] := arrays[A_Index, arrays[A_Index].MaxIndex()]
  113. return ret
  114. }