ArrayUtils

 

/*
 Copyright aswing.org, see the LICENCE.txt.
*/
 
package com.xxxx.utils{
	
	/**
	 * Utils functions about Array.
	 * @author iiley
	 */
	public class ArrayUtils{
		
		/**
		 * Call the operation by pass each element of the array once.
		 * <p>
		 * for example:
		 * <pre>
		 * //hide all component in vector components
		 * ArrayUtils.each( 
		 *     components,
		 *     function(c:Component){
		 *         c.setVisible(false);
		 *     });
		 * <pre>
		 * @param arr the array for each element will be operated.
		 * @param the operation function for each element
		 * @see Vector#each
		 */
		public static function each(arr:Array, operation:Function):void{
			for(var i:int=0; i<arr.length; i++){
				operation(arr[i]);
			}
		}
		
		/**
		 * Sets the size of the array. If the new size is greater than the current size, 
		 * new undefined items are added to the end of the array. If the new size is less than 
		 * the current size, all components at index newSize and greater are removed.
		 * @param arr the array to resize
		 * @param size the new size of this vector 
		 */
		public static function setSize(arr:Array, size:int):void{
			//TODO test this method
			if(size < 0) size = 0;
			if(size == arr.length){
				return;
			}
			if(size > arr.length){
				arr[size - 1] = undefined;
			}else{
				arr.splice(size);
			}
		}
		
		/**
		 * Removes the object from the array and return the index.
		 * @return the index of the object, -1 if the object is not in the array
		 */
		public static function removeFromArray(arr:Array, obj:Object):int{
			var i:int = arr.indexOf(obj);
			if (i != -1)
				arr.splice(i, 1);
			return i;
		}
		
		public static function removeAllFromArray(arr:Array, obj:Object):void{
			for(var i:int=0; i<arr.length; i++){
				if(arr[i] == obj){
					arr.splice(i, 1);
					i--;
				}
			}
		}
		
		public static function removeAllBehindSomeIndex(array:Array , index:int):void{
			if(index <= 0){
				array.splice(0);
			}
			else {
				array.splice(index + 1);
			}
		}
		
		public static function cloneArray(arr:Array):Array{
			return arr.concat();
		}
		
		public static function deepClone(a:Array):Array {
			var r:Array = new Array();
			for each (var obj:Object in a) {
				if (obj.hasOwnProperty("clone")) {
					obj = obj.clone();
				}
				r.push(obj);
			}
			return r;
		}
		
		/**
		 * 
		 * 全闭区间 [start, end] 
		 * 
		 */	
		public static function copyArrayByRange(dst:Array, src:Array, start:int, end:int):Boolean {
			if (dst == null || src == null)
				return false;
			
			var l:int = src.length;
			if (l == 0 || l <= end)
				return false;
			
			dst.splice(0);
			for (var i:int = start; i <= end; ++i) {
				dst.push(src[i]);
			}
			return true;
		}
		
		/**
		 *mix the array 
		 * @param array
		 */	
		public static function mixArray(array:Array):void{
			array.sort(function(n1:*, n2:*):Number{
				var r:Number = Math.random()*1;
				return r>0.5?1:-1;
			});
		}
		
		public static function swap(array:Array, i:int, j:int):void {
			var t:Object = array[i];
			array[i] = array[j];
			array[j] = t;
		}
		
		public static function bubbleSort(array:Array, fnCompare:Function):void {
			var c:int = array.length - 1;
			for (var i:int = 0; i < c; ++i) {
				var bSwapped:Boolean = false;
				for (var j:int = 0; j < c - i; ++j) {
					if (fnCompare(array[j + 1], array[j])) {
						swap(array, j, j + 1);
						bSwapped = true;
					}
				}
				if (!bSwapped) {
					break;
				}
			}
		}
		
		public static function quickSort(array:Array, fnCompare:Function):void {
			doQuickSort(array, fnCompare, 0, array.length - 1);
		}
		
		private static function doQuickSort(array:Array, fnCompare:Function, bottom:int, top:int):void {
			if (top > bottom) {
				var piv:int = bottom;
				var k:int = bottom + 1;
				var r:int = top;
				var pivObj:Object = array[piv];
				while (k != r /*k < r*/) {
					if (fnCompare(array[k], pivObj)) {
						++k;
					}
					else {
						swap(array, k, r--);
					}
				}
				if (fnCompare(array[k], pivObj)) {
					swap(array, k, bottom);
					doQuickSort(array, fnCompare, bottom, k - 1);
					doQuickSort(array, fnCompare, r + 1, top);
				}
				else {
					if (top - bottom > 1) {
						--k;
						swap(array, k, bottom);
						doQuickSort(array, fnCompare, bottom, k - 1);
						doQuickSort(array, fnCompare, k + 1, top);
					}
				}
			}
		}
		
		public static function selectSort(array:Array, fnCompare:Function):void {
			var l:int = array.length;
			for (var i:int = 1; i < l; ++i) {
				var iMin:int = i - 1;
				var minCh:Object = array[iMin];
				for (var j:int = i; j < l; ++j) {
					var ch:Object = array[j];
					if (fnCompare(ch, minCh)) {
						iMin = j;
						minCh = ch;
					}
				}
				if (iMin != (i - 1)) {
					swap(array, iMin, i - 1);
				}
			}
		}
		
		public static function heapSort(array:Array, fnCompare:Function):void {
			for (var i:int = array.length - 1; i > 0; --i) {
				heapTree(array, fnCompare, i, i);
			}
		}
		
		private static function heapTree(array:Array, fnCompare:Function, iRootIndex:int, iTopIndex:int):void {
			if (iRootIndex > iTopIndex) {
				return;
			}
			
			var iLeftIndex:int = 0;
			var iRightIndex:int = 1;
			if (iRootIndex < iTopIndex) {
				iLeftIndex = (iRootIndex + 1) * 2;
				iRightIndex = iLeftIndex + 1;
			}
			
			if (iLeftIndex < iTopIndex) {
				var chRoot:Object = array[iRootIndex];
				var chLeft:Object = array[iLeftIndex];
				if (iRightIndex < iTopIndex) {
					var chRight:Object = array[iRightIndex];
					if (fnCompare(chRight, chLeft)) {
						if (fnCompare(chRoot, chLeft)) {
							swap(array, iLeftIndex, iRootIndex);
						}
					}
					else {
						if (fnCompare(chRoot, chRight)) {
							swap(array, iRightIndex, iRootIndex);
						}
					}
					
					heapTree(array, fnCompare, iLeftIndex, iTopIndex);
					heapTree(array, fnCompare, iRightIndex, iTopIndex);
					
					chRoot = array[iRootIndex];
					chLeft = array[iLeftIndex];
					chRight = array[iRightIndex];
					if (fnCompare(chRight, chLeft)) {
						if (fnCompare(chRoot, chLeft)) {
							swap(array, iLeftIndex, iRootIndex);
						}
					}
					else {
						if (fnCompare(chRoot, chRight)) {
							swap(array, iRightIndex, iRootIndex);
						}
					}
				}
				else {
					if (fnCompare(chRoot, chLeft)) {
						swap(array, iLeftIndex, iRootIndex);
					}
				}
			}
			else {
				
			}
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值