root/as3/Utils/src/org/libspark/utils/ArrayUtil.as

リビジョン 804, 13.9 kB (コミッタ: michi, コミット時期: 6 ヶ月 前)

asdoc追加

  • svn:executable 属性の設定値: *
Line 
1 /*
2  * Copyright(c) 2006-2007 the Spark project.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific language
14  * governing permissions and limitations under the License.
15  */
16
17 package org.libspark.utils
18 {
19     /**
20      * Arrayのためのユーティリティクラスです
21      */
22     public class ArrayUtil
23     {
24         /**
25          * 配列の末尾に指定された要素を追加します。
26          *
27          * @param element 追加する要素
28          * @param source 操作対象の配列
29          * @return 追加された要素
30          * @author yossy
31          */
32         public static function addElement(element:*, source:Array):*
33         {
34             source.push(element);
35             return element;
36         }
37        
38         /**
39          * 配列の指定された位置に指定された要素を追加します。
40          *
41          * @param element 追加する要素
42          * @param index 要素を追加する位置。負の値の場合、末尾からの位置になります
43          * @param source 操作対象の配列
44          * @return 追加された要素
45          * @throws RangeError indexが範囲外の場合
46          * @author yossy
47          */
48         public static function addElementAt(element:*, index:int, source:Array):*
49         {
50             if (index < 0) {
51                 index += source.length;
52             }
53            
54             if (index < 0 || source.length < index) {
55                 throw new RangeError('index');
56             }
57            
58             source.splice(index, 0, element);
59            
60             return element;
61         }
62        
63         /**
64          * 配列の指定された位置に存在する要素を取得します。
65          *
66          * @param index 取得する位置。負の値の場合、末尾からの位置になります
67          * @param source 操作対象の配列
68          * @return 指定された位置に存在する要素
69          * @throws RangeError indexが範囲外の場合
70          * @author yossy
71          */
72         public static function getElementAt(index:int, source:Array):*
73         {
74             if (index < 0) {
75                 index += source.length;
76             }
77            
78             if (index < 0 || source.length <= index) {
79                 throw new RangeError('index');
80             }
81            
82             return source[index];
83         }
84        
85         /**
86          * 指定された要素の配列内での位置を取得します。
87          *
88          * @param element 位置を取得する要素
89          * @param source 操作対象の配列
90          * @return 指定された要素の位置。配列無いに存在しない場合は-1
91          * @author yossy
92          */
93         public static function getElementIndex(element:*, source:Array):int
94         {
95             return source.indexOf(element);
96         }
97        
98         /**
99          * 指定された要素を配列内から削除します。
100          *
101          * @param element 削除する要素
102          * @param source 操作対象の配列
103          * @return 削除された要素
104          * @throws ArgumentError 配列内に要素が存在しない場合
105          * @atuhor yossy
106          */
107         public static function removeElement(element:*, source:Array):*
108         {
109             var index:int = source.indexOf(element);
110             if (index < 0) {
111                 throw new ArgumentError('element');
112             }
113            
114             source.splice(index, 1);
115            
116             return element;
117         }
118        
119         /**
120          * 配列内の指定された位置に存在する要素を削除します。
121          *
122          * @param index 削除する要素の位置。負の値の場合、末尾からの位置になります
123          * @param source 操作対象の配列
124          * @return 削除された要素
125          * @throws RangeError indexが範囲外の場合
126          * @author yossy
127          */
128         public static function removeElementAt(index:int, source:Array):*
129         {
130             if (index < 0) {
131                 index += source.length;
132             }
133            
134             if (index < 0 || source.length <= index) {
135                 throw new RangeError('index');
136             }
137            
138             return source.splice(index, 1)[0];
139         }
140        
141         /**
142          * 配列内の指定された要素を指定された位置に移動します。
143          *
144          * @param element 移動する要素
145          * @param index 移動先の位置。負の値の場合、末尾からの位置になります
146          * @param source 操作対象の配列
147          * @throws ArgumentError 配列内に要素が存在しない場合
148          * @throws RangeError indexが範囲外の場合
149          * @author yossy
150          */
151         public static function setElementIndex(element:*, index:int, source:Array):void
152         {
153             if (index < 0) {
154                 index += source.length;
155             }
156            
157             if (index < 0 || source.length <= index) {
158                 throw new RangeError('index');
159             }
160            
161             var oldIndex:int = source.indexOf(element);
162             if (oldIndex < 0) {
163                 throw new ArgumentError('element');
164             }
165            
166             source.splice(oldIndex, 1);
167             source.splice(index, 0, element);
168         }
169        
170         /**
171          * 配列内の指定された二つの要素の位置を入れ替えます。
172          *
173          * @param element1 入れ替える要素
174          * @param element2 入れ替える要素
175          * @param source 操作対象の配列
176          * @throws ArgumentError 配列内に要素が存在しない場合
177          * @author yossy
178          */
179         public static function swapElements(element1:*, element2:*, source:Array):void
180         {
181             var index1:int = source.indexOf(element1);
182             if (index1 < 0) {
183                 throw new ArgumentError('element1');
184             }
185            
186             var index2:int = source.indexOf(element2);
187             if (index2 < 0) {
188                 throw new ArgumentError('element2');
189             }
190            
191             source[index1] = element2;
192             source[index2] = element1;
193         }
194        
195         /**
196          * 配列内の指定された二つの位置に存在する要素を入れ替えます。
197          *
198          * @param index1 入れ替える要素の位置。負の値の場合、末尾からの位置になります
199          * @param index2 入れ替える要素の位置。負の値の場合、末尾からの位置になります
200          * @param source 操作対象の配列
201          * @throws RangeError indexが範囲外の場合
202          * @author yossy
203          */
204         public static function swapElementsAt(index1:int, index2:int, source:Array):void
205         {
206             if (index1 < 0) {
207                 index1 += source.length;
208             }
209             if (index1 < 0 || source.length <= index1) {
210                 throw new RangeError('index1');
211             }
212            
213             if (index2 < 0) {
214                 index2 += source.length;
215             }
216             if (index2 < 0 || source.length <= index2) {
217                 throw new RangeError('index2');
218             }
219            
220             var temp:* = source[index1];
221             source[index1] = source[index2];
222             source[index2] = temp;
223         }
224        
225        
226         /**
227          * 同じデータで構成された配列を作成します
228          *
229          * @param   param 配列にセットする値
230          * @param   len 配列の数
231          * @return  新しい配列を返します
232          * @author  michi at seyself.com
233          */
234         public static function identicalArray( param:* , len:uint ):Array
235         {
236             var a:Array = [];
237             for(var i:uint=0;i<len;i++) a.push(param);
238             return a;
239         }
240        
241         /**
242          * 連続する数値で構成された配列を作成します
243          *
244          * @param       len 作成する配列の要素数
245          * @param       firstValue 配列の最初の要素に含まれる数値
246          * @param       step 1要素ごとに加算(減算)されていく数値
247          * @return  新たしい配列を返します
248          * @author  michi at seyself.com
249          */
250         public static function numericArray(len:int, firstValue:Number=0, step:Number=1.0 ):Array
251         {
252             var a:Array = new Array(len);
253             return a.map(function(v:Number, i:int, a:Array):Number { return firstValue+i*step; });
254         }
255        
256         /**
257          * 配列内の要素がすべて同じクラスのインスタンスであるかを確認します
258          *
259          * @param       array 配列
260          * @param       theClass 判定対象となるクラス。指定がない場合は配列の1つ目の要素のコンストラクタから判定します。
261          * @return  異なるクラスのインスタンスが含まれている場合はfalseが返されます
262          * @author  michi at seyself.com
263          */
264         public static function instanceOfEquals( array:Array, theClass:Class=null ):Boolean
265         {
266             var n:uint = array.length;
267            
268             if (n == 0) throw new ArgumentError("空の配列をチェックすることはできません。");
269            
270             theClass = theClass || array[0].constructor;
271            
272             for (var i:int = 0; i < n; i++ )
273                 if ( !(array[i] is theClass) ) return false;
274             return true;
275         }
276        
277         /**
278          * 数値のみで構成される配列内の要素すべてに加算します
279          *
280          * @param       numericArray 数値のみで構成された配列
281          * @param       value 加算する数値
282          * @return 新たしい配列を返します
283          * @author  michi at seyself.com
284          */
285         public static function addNumber( numericArray:Array, value:Number ):Array
286         {
287             return numericArray.map(function(v:Number, i:int, a:Array):Number { return v + value; } );
288         }
289        
290         /**
291          * 数値のみで構成される配列内の要素すべてから減算します
292          *
293          * @param       numericArray 数値のみで構成された配列
294          * @param       value 減算する数値
295          * @return 新たしい配列を返します
296          * @author  michi at seyself.com
297          */
298         public static function subtractNumber( numericArray:Array, value:Number ):Array
299         {
300             return numericArray.map(function(v:Number, i:int, a:Array):Number { return v - value; } );
301         }
302        
303         /**
304          * 数値のみで構成される配列内の要素すべてに乗算します
305          *
306          * @param       numericArray 数値のみで構成された配列
307          * @param       value 乗算する数値
308          * @return  新たしい配列を返します
309          * @author  michi at seyself.com
310          */
311         public static function multipleNumber( numericArray:Array, value:Number ):Array
312         {
313             return numericArray.map(function(v:Number, i:int, a:Array):Number { return v * value; } );
314         }
315        
316         /**
317          * 数値のみで構成される配列内の要素すべてに除算します
318          *
319          * @param       numericArray 数値のみで構成された配列
320          * @param       value 除算する数値
321          * @return  新たしい配列を返します
322          * @author  michi at seyself.com
323          */
324         public static function divideNumber( numericArray:Array, value:Number ):Array
325         {
326             return numericArray.map(function(v:Number, i:int, a:Array):Number { return v / value; } );
327         }
328        
329         /**
330          * 配列をランダムに並び替えます
331          *
332          * @param   array 並び替えを行う配列
333          * @return  新しい配列を返します
334          * @author  michi at seyself.com
335          */
336         public static function shuffle( array:Array ):Array
337         {
338             var c:Array = array.concat();
339             var n:int, i:int = c.length - 1, t:*;
340             for( ; i; --i ){
341                 n = Math.random() * i >> 0;
342                 t = c[i];
343                 c[i] = c[n];
344                 c[n] = t;
345             }
346             return c;
347         }
348        
349         /**
350          * 指定配列の中から比較対象の配列内に同じ値が含まれているものだけを抜き出した新しい配列を作ります
351          *
352          * @param       array 比較元となる配列
353          * @param       compareTarget 比較対象の配列
354          * @return  新たしい配列を返します
355          * @author  michi at seyself.com
356          */
357         public static function matches(array:Array, compareTarget:Array):Array
358         {
359             var len1:uint = array.length;
360             var len2:uint = compareTarget.length;
361             var res:Array = [];
362             for (var i:int = 0; i < len1; i++ ) {
363                 for (var j:int = 0; j < len2; j++ ) {
364                     if( array[i] == compareTarget[j] ) {
365                         res.push( array[i] );
366                         break;
367                     }
368                 }
369             }
370             return res;
371         }
372        
373        
374     }
375 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。