comparison bindings/as3/ext/com/adobe/utils/ArrayUtil.as @ 732:3a0b9700b3d2

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