source: code/Website/open-flash-chart/com/adobe/utils/ArrayUtil.as@ 7937

Last change on this file since 7937 was 7849, checked in by dennisw, 16 years ago
File size: 5.5 KB
Line 
1/*
2 Adobe Systems Incorporated(r) Source Code License Agreement
3 Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
4
5 Please read this Source Code License Agreement carefully before using
6 the source code.
7
8 Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive,
9 no-charge, royalty-free, irrevocable copyright license, to reproduce,
10 prepare derivative works of, publicly display, publicly perform, and
11 distribute this source code and such derivative works in source or
12 object code form without any attribution requirements.
13
14 The name "Adobe Systems Incorporated" must not be used to endorse or promote products
15 derived from the source code without prior written permission.
16
17 You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
18 against any loss, damage, claims or lawsuits, including attorney's
19 fees that arise or result from your use or distribution of the source
20 code.
21
22 THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT
23 ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
24 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF
26 NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA
27 OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
33 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36package com.adobe.utils
37{
38
39 /**
40 * Class that contains static utility methods for manipulating and working
41 * with Arrays.
42 *
43 * Note that all APIs assume that they are working with well formed arrays.
44 * i.e. they will only manipulate indexed values.
45 *
46 * @langversion ActionScript 3.0
47 * @playerversion Flash 9.0
48 * @tiptext
49 */
50 public class ArrayUtil
51 {
52
53 /**
54 * Determines whether the specified array contains the specified value.
55 *
56 * @param arr The array that will be checked for the specified value.
57 *
58 * @param value The object which will be searched for within the array
59 *
60 * @return True if the array contains the value, False if it does not.
61 *
62 * @langversion ActionScript 3.0
63 * @playerversion Flash 9.0
64 * @tiptext
65 */
66 public static function arrayContainsValue(arr:Array, value:Object):Boolean
67 {
68 return (arr.indexOf(value) != -1);
69 }
70
71 /**
72 * Remove all instances of the specified value from the array,
73 *
74 * @param arr The array from which the value will be removed
75 *
76 * @param value The object that will be removed from the array.
77 *
78 * @langversion ActionScript 3.0
79 * @playerversion Flash 9.0
80 * @tiptext
81 */
82 public static function removeValueFromArray(arr:Array, value:Object):void
83 {
84 var len:uint = arr.length;
85
86 for(var i:Number = len; i > -1; i--)
87 {
88 if(arr[i] === value)
89 {
90 arr.splice(i, 1);
91 }
92 }
93 }
94
95 /**
96 * Create a new array that only contains unique instances of objects
97 * in the specified array.
98 *
99 * Basically, this can be used to remove duplication object instances
100 * from an array
101 *
102 * @param arr The array which contains the values that will be used to
103 * create the new array that contains no duplicate values.
104 *
105 * @return A new array which only contains unique items from the specified
106 * array.
107 *
108 * @langversion ActionScript 3.0
109 * @playerversion Flash 9.0
110 * @tiptext
111 */
112 public static function createUniqueCopy(a:Array):Array
113 {
114 var newArray:Array = new Array();
115
116 var len:Number = a.length;
117 var item:Object;
118
119 for (var i:uint = 0; i < len; ++i)
120 {
121 item = a[i];
122
123 if(ArrayUtil.arrayContainsValue(newArray, item))
124 {
125 continue;
126 }
127
128 newArray.push(item);
129 }
130
131 return newArray;
132 }
133
134 /**
135 * Creates a copy of the specified array.
136 *
137 * Note that the array returned is a new array but the items within the
138 * array are not copies of the items in the original array (but rather
139 * references to the same items)
140 *
141 * @param arr The array that will be copies
142 *
143 * @return A new array which contains the same items as the array passed
144 * in.
145 *
146 * @langversion ActionScript 3.0
147 * @playerversion Flash 9.0
148 * @tiptext
149 */
150 public static function copyArray(arr:Array):Array
151 {
152 return arr.slice();
153 }
154
155 /**
156 * Compares two arrays and returns a boolean indicating whether the arrays
157 * contain the same values at the same indexes.
158 *
159 * @param arr1 The first array that will be compared to the second.
160 *
161 * @param arr2 The second array that will be compared to the first.
162 *
163 * @return True if the arrays contains the same values at the same indexes.
164 False if they do not.
165 *
166 * @langversion ActionScript 3.0
167 * @playerversion Flash 9.0
168 * @tiptext
169 */
170 public static function arraysAreEqual(arr1:Array, arr2:Array):Boolean
171 {
172 if(arr1.length != arr2.length)
173 {
174 return false;
175 }
176
177 var len:Number = arr1.length;
178
179 for(var i:Number = 0; i < len; i++)
180 {
181 if(arr1[i] !== arr2[i])
182 {
183 return false;
184 }
185 }
186
187 return true;
188 }
189 }
190}
Note: See TracBrowser for help on using the repository browser.