This class can be thought of in two ways. You can see it as a vector of bits or as a set of non-negative integers. The name BitSet is a bit misleading. It is implemented by a bit vector, but its equally possible to see it as set of non-negative integer; each integer in the set is represented by a set bit at the corresponding index. The size of this structure is determined by the highest integer in the set. You can union, intersect and build (symmetric) remainders, by invoking the logical operations and, or, andNot, resp. xor. This implementation is NOT synchronized against concurrent access from multiple threads. Specifically, if one thread is reading from a bitset while another thread is simultaneously modifying it, the results are undefined. author Jochen Hoenicke author Tom Tromey ([email protected]) author Eric Blake ([email protected]) status updated to 1.4
예제 #1
0
파일: Matcher.cs 프로젝트: RastaCow/Nez
		public bool isInterested( BitSet componentBits )
		{
			// Check if the entity possesses ALL of the components defined in the aspect.
			if( !allSet.isEmpty() )
			{
				for( int i = allSet.nextSetBit( 0 ); i >= 0; i = allSet.nextSetBit( i + 1 ) )
				{
					if( !componentBits.get( i ) )
					{
						return false;
					}
				}
			}

			// If we are STILL interested,
			// Check if the entity possesses ANY of the exclusion components, if it does then the system is not interested.
			if( !exclusionSet.isEmpty() && exclusionSet.intersects( componentBits ) )
			{
				return false;
			}

			// If we are STILL interested,
			// Check if the entity possesses ANY of the components in the oneSet. If so, the system is interested.
			if( !oneSet.isEmpty() && !oneSet.intersects( componentBits ) )
			{
				return false;
			}

			return true;
		}
예제 #2
0
		public static IEnumerable<Type> getTypesFromBits( BitSet bits )
		{
			foreach( var keyValuePair in _componentTypesMask )
			{
				if( bits.get( keyValuePair.Value ) )
					yield return keyValuePair.Key;
			}   
		}
예제 #3
0
파일: Matcher.cs 프로젝트: RastaCow/Nez
		static void appendTypes( StringBuilder builder, string headerMessage, BitSet typeBits )
		{
			var firstType = true;
			builder.Append( headerMessage );
			foreach( var type in ComponentTypeManager.getTypesFromBits( typeBits ) )
			{
				if( !firstType )
					builder.Append( ", " );
				builder.Append( type.Name );

				firstType = false;
			}

			builder.AppendLine();
		}
예제 #4
0
파일: BitSet.cs 프로젝트: RastaCow/Nez
		// This is used by EnumSet for efficiency.
		public bool containsAll( BitSet other )
		{
			for( int i = other.bits.Length - 1; i >= 0; i-- )
			{
				if( ( bits[i] & other.bits[i] ) != other.bits[i] )
					return false;
			}

			return true;
		}
예제 #5
0
파일: BitSet.cs 프로젝트: RastaCow/Nez
		/// <summary>
		/// Performs the logical XOR operation on this bit set and the
		/// given <code>set</code>.  This means it builds the symmetric
		/// remainder of the two sets (the elements that are in one set,
		/// but not in the other).  The result is stored into this bit set,
		/// which grows as necessary.
		/// </summary>
		/// <param name="bs">the second bit set</param>
		public void xor( BitSet bs )
		{
			ensure( bs.bits.Length - 1 );
			for( int i = bs.bits.Length - 1; i >= 0; i-- )
				bits[i] ^= bs.bits[i];
		}
예제 #6
0
파일: BitSet.cs 프로젝트: RastaCow/Nez
		/// <summary>
		/// Performs the logical OR operation on this bit set and the
		/// given <code>set</code>.  This means it builds the union
		/// of the two sets.  The result is stored into this bit set, which
		/// grows as necessary.
		/// </summary>
		/// <param name="bs">the second bit set</param>
		public void or( BitSet bs )
		{
			ensure( bs.bits.Length - 1 );
			for( var i = bs.bits.Length - 1; i >= 0; i-- )
				bits[i] |= bs.bits[i];
		}
예제 #7
0
파일: BitSet.cs 프로젝트: RastaCow/Nez
		/// <summary>
		/// Returns true if the specified BitSet and this one share at least one
		/// common true bit.
		/// </summary>
		/// <param name="set">the set to check for intersection</param>
		/// <returns>true if the sets intersect</returns>
		public bool intersects( BitSet set )
		{
			var i = Math.Min( bits.Length, set.bits.Length );
			while( --i >= 0 )
			{
				if( ( bits[i] & set.bits[i] ) != 0 )
					return true;
			}
			return false;
		}
예제 #8
0
파일: BitSet.cs 프로젝트: RastaCow/Nez
		/// <summary>
		/// Returns a new <code>BitSet</code> composed of a range of bits from
		/// this one.
		/// </summary>
		/// <param name="from">the low index (inclusive)</param>
		/// <param name="to">the high index (exclusive)</param>
		/// <returns></returns>
		public BitSet get( int from, int to )
		{
			if( from < 0 || from > to )
				throw new ArgumentOutOfRangeException();
			
			var bs = new BitSet( to - from );
			var lo_offset = (uint)from >> 6;
			if( lo_offset >= bits.Length || to == from )
				return bs;

			var lo_bit = from & LONG_MASK;
			var hi_offset = (uint)to >> 6;
			if( lo_bit == 0 )
			{
				var len = Math.Min( hi_offset - lo_offset + 1, (uint)bits.Length - lo_offset );
				Array.Copy( bits, (int)lo_offset, bs.bits, 0, (int)len );
				if( hi_offset < bits.Length )
					bs.bits[hi_offset - lo_offset] &= ( 1L << to ) - 1;
				return bs;
			}

			var len2 = Math.Min( hi_offset, (uint)bits.Length - 1 );
			var reverse = 64 - lo_bit;
			int i;
			for( i = 0; lo_offset < len2; lo_offset++, i++ )
				bs.bits[i] = ( ( bits[lo_offset] >> lo_bit ) | ( bits[lo_offset + 1] << reverse ) );
			
			if( ( to & LONG_MASK ) > lo_bit )
				bs.bits[i++] = bits[lo_offset] >> lo_bit;
			
			if( hi_offset < bits.Length )
				bs.bits[i - 1] &= ( 1L << ( to - from ) ) - 1;
			
			return bs;
		}
예제 #9
0
파일: BitSet.cs 프로젝트: RastaCow/Nez
		/// <summary>
		/// Create a clone of this bit set, that is an instance of the same
		/// class and contains the same elements.  But it doesn't change when
		/// this bit set changes.
		/// </summary>
		/// <returns>the clone of this object.</returns>
		public object clone()
		{
			try
			{
				var bs = new BitSet();
				bs.bits = (long[])bits.Clone();
				return bs;
			}
			catch
			{
				// Impossible to get here.
				return null;
			}
		}
예제 #10
0
파일: BitSet.cs 프로젝트: RastaCow/Nez
		/// <summary>
		/// Performs the logical AND operation on this bit set and the
		/// complement of the given <code>bs</code>.  This means it
		/// selects every element in the first set, that isn't in the
		/// second set.  The result is stored into this bit set and is
		/// effectively the set difference of the two.
		/// </summary>
		/// <param name="bs">the second bit set</param>
		public void andNot( BitSet bs )
		{
			var i = Math.Min( bits.Length, bs.bits.Length );
			while( --i >= 0 )
				bits[i] &= ~bs.bits[i];
		}
예제 #11
0
파일: BitSet.cs 프로젝트: RastaCow/Nez
		/// <summary>
		/// Performs the logical AND operation on this bit set and the
		/// given <code>set</code>.  This means it builds the intersection
		/// of the two sets.  The result is stored into this bit set.
		/// </summary>
		/// <param name="bs">the second bit set</param>
		public void and( BitSet bs )
		{
			var max = Math.Min( bits.Length, bs.bits.Length );
			int i;
			for( i = 0; i < max; ++i )
				bits[i] &= bs.bits[i];
			
			while( i < bits.Length )
				bits[i++] = 0;
		}