ArrayUtil.as 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. Copyright (c) 2008, Adobe Systems Incorporated
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Adobe Systems Incorporated nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package com.adobe.utils
  28. {
  29. /**
  30. * Class that contains static utility methods for manipulating and working
  31. * with Arrays.
  32. *
  33. * Note that all APIs assume that they are working with well formed arrays.
  34. * i.e. they will only manipulate indexed values.
  35. *
  36. * @langversion ActionScript 3.0
  37. * @playerversion Flash 9.0
  38. * @tiptext
  39. */
  40. public class ArrayUtil
  41. {
  42. /**
  43. * Determines whether the specified array contains the specified value.
  44. *
  45. * @param arr The array that will be checked for the specified value.
  46. *
  47. * @param value The object which will be searched for within the array
  48. *
  49. * @return True if the array contains the value, False if it does not.
  50. *
  51. * @langversion ActionScript 3.0
  52. * @playerversion Flash 9.0
  53. * @tiptext
  54. */
  55. public static function arrayContainsValue(arr:Array, value:Object):Boolean
  56. {
  57. return (arr.indexOf(value) != -1);
  58. }
  59. /**
  60. * Remove all instances of the specified value from the array,
  61. *
  62. * @param arr The array from which the value will be removed
  63. *
  64. * @param value The object that will be removed from the array.
  65. *
  66. * @langversion ActionScript 3.0
  67. * @playerversion Flash 9.0
  68. * @tiptext
  69. */
  70. public static function removeValueFromArray(arr:Array, value:Object):void
  71. {
  72. var len:uint = arr.length;
  73. for(var i:Number = len; i > -1; i--)
  74. {
  75. if(arr[i] === value)
  76. {
  77. arr.splice(i, 1);
  78. }
  79. }
  80. }
  81. /**
  82. * Create a new array that only contains unique instances of objects
  83. * in the specified array.
  84. *
  85. * Basically, this can be used to remove duplication object instances
  86. * from an array
  87. *
  88. * @param arr The array which contains the values that will be used to
  89. * create the new array that contains no duplicate values.
  90. *
  91. * @return A new array which only contains unique items from the specified
  92. * array.
  93. *
  94. * @langversion ActionScript 3.0
  95. * @playerversion Flash 9.0
  96. * @tiptext
  97. */
  98. public static function createUniqueCopy(a:Array):Array
  99. {
  100. var newArray:Array = new Array();
  101. var len:Number = a.length;
  102. var item:Object;
  103. for (var i:uint = 0; i < len; ++i)
  104. {
  105. item = a[i];
  106. if(ArrayUtil.arrayContainsValue(newArray, item))
  107. {
  108. continue;
  109. }
  110. newArray.push(item);
  111. }
  112. return newArray;
  113. }
  114. /**
  115. * Creates a copy of the specified array.
  116. *
  117. * Note that the array returned is a new array but the items within the
  118. * array are not copies of the items in the original array (but rather
  119. * references to the same items)
  120. *
  121. * @param arr The array that will be copies
  122. *
  123. * @return A new array which contains the same items as the array passed
  124. * in.
  125. *
  126. * @langversion ActionScript 3.0
  127. * @playerversion Flash 9.0
  128. * @tiptext
  129. */
  130. public static function copyArray(arr:Array):Array
  131. {
  132. return arr.slice();
  133. }
  134. /**
  135. * Compares two arrays and returns a boolean indicating whether the arrays
  136. * contain the same values at the same indexes.
  137. *
  138. * @param arr1 The first array that will be compared to the second.
  139. *
  140. * @param arr2 The second array that will be compared to the first.
  141. *
  142. * @return True if the arrays contains the same values at the same indexes.
  143. False if they do not.
  144. *
  145. * @langversion ActionScript 3.0
  146. * @playerversion Flash 9.0
  147. * @tiptext
  148. */
  149. public static function arraysAreEqual(arr1:Array, arr2:Array):Boolean
  150. {
  151. if(arr1.length != arr2.length)
  152. {
  153. return false;
  154. }
  155. var len:Number = arr1.length;
  156. for(var i:Number = 0; i < len; i++)
  157. {
  158. if(arr1[i] !== arr2[i])
  159. {
  160. return false;
  161. }
  162. }
  163. return true;
  164. }
  165. }
  166. }