⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitvector.as

📁 一个2D基于verlet的Flash物理引擎。它用AS3编写而成。Fisix的目标是应用到游戏等计算量很大的实时应用中。尽管flash比c/c++要慢,很棒的物理引擎
💻 AS
字号:
/**
 * DATA STRUCTURES FOR GAME PROGRAMMERS
 * Copyright (c) 2007 Michael Baczynski, http://www.polygonal.de
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package de.polygonal.ds
{
	/**
	 * A bitvector is meant to condense bit values (or booleans) into
	 * an array as close as possible so that no space is wasted.
	 */
	public class BitVector
	{
		private var _bits:Array;
		private var _arrSize:int;
		private var _bitSize:int;
		
		/**
		 * Creates a bitvector with a given number of bits.
		 * Each cell holds a 31-bit signed integer.
		 * 
		 * @param bits The total number of bits.
		 */
		public function BitVector(bits:int)
		{
			_bits = [];
			_arrSize = 0;
			
			resize(bits);
		}
		
		/**
		 * The total number of bits.
		 */
		public function get bitCount():int
		{
			return _arrSize * 31;
		}
		
		/**
		 * The total number of cells.
		 */
		public function get cellCount():int
		{
			return _arrSize;
		}
		
		/**
		 * Gets a bit from a given index.
		 * 
		 * @param index The index of the bit.
		 */
		public function getBit(index:int):int
		{
			var bit:int = index % 31;
			return (_bits[(index / 31) >> 0] & (1 << bit)) >> bit;
		}
		
		/**
		 * Sets a bit at a given index.
		 * 
		 * @param index The index of the bit.
		 * @param b     The boolean flag to set.
		 */
		public function setBit(index:int, b:Boolean):void
		{
			var cell:int = index / 31;
			var mask:int = 1 << index % 31;
			_bits[cell] = b ? (_bits[cell] | mask) : (_bits[cell] & (~mask));
		}
		
		/**
		 * Resizes the bitvector to an appropriate number of bits.
		 * 
		 * @param size The total number of bits.
		 */
		public function resize(size:int):void
		{
			if (size == _bitSize) return;
			_bitSize = size;
			
			//convert the bit-size to integer-size
			if (size % 31 == 0)
				size /= 31;
			else
				size = (size / 31) + 1;
			
			if (size < _arrSize)
			{
				_bits.splice(size);
				_arrSize = size;
			}
			else
			{
				_bits = _bits.concat(new Array(size - _arrSize));
				_arrSize = _bits.length;
			}
		}
		
		/**
		 * Resets all bits to 0;
		 */
		public function clear():void
		{
			var k:int = _bits.length;
			for (var i:int = 0; i < k; i++)
				_bits[i] = 0;
		}
		
		/**
		 * Sets each bit to 1.
		 */
		public function setAll():void
		{
			var k:int = _bits.length;
			for (var i:int = 0; i < k; i++)
				_bits[i] = int.MAX_VALUE;
		}
		
		/**
		 * Returns a string representing the current object.
		 */
		public function toString():String
		{
			return "[BitVector, size=" + _bitSize + "]";
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -