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