Files
esengine/src/ECS/Utils/Bits.ts

253 lines
5.3 KiB
TypeScript
Raw Normal View History

/**
*
2025-06-16 09:36:36 +08:00
* BigInt实现
*/
export class Bits {
2025-06-16 09:36:36 +08:00
private _value: bigint = 0n;
2025-06-16 09:36:36 +08:00
constructor(initialValue: bigint = 0n) {
this._value = initialValue;
}
/**
* 1
*/
public set(index: number): void {
2025-06-16 09:36:36 +08:00
if (index < 0) {
throw new Error('Bit index cannot be negative');
}
2025-06-16 09:36:36 +08:00
this._value |= (1n << BigInt(index));
}
2021-05-07 16:23:15 +08:00
/**
* 0
*/
public clear(index: number): void {
2025-06-16 09:36:36 +08:00
if (index < 0) {
throw new Error('Bit index cannot be negative');
2021-05-07 16:23:15 +08:00
}
2025-06-16 09:36:36 +08:00
this._value &= ~(1n << BigInt(index));
}
2021-05-07 16:23:15 +08:00
/**
*
*/
public get(index: number): boolean {
2025-06-16 09:36:36 +08:00
if (index < 0) {
return false;
2021-05-07 16:23:15 +08:00
}
2025-06-16 09:36:36 +08:00
return (this._value & (1n << BigInt(index))) !== 0n;
2021-05-07 16:23:15 +08:00
}
/**
*
*/
public containsAll(other: Bits): boolean {
2025-06-16 09:36:36 +08:00
return (this._value & other._value) === other._value;
}
/**
*
*/
public intersects(other: Bits): boolean {
2025-06-16 09:36:36 +08:00
return (this._value & other._value) !== 0n;
}
/**
*
*/
public excludes(other: Bits): boolean {
return !this.intersects(other);
}
/**
*
*/
public clearAll(): void {
2025-06-16 09:36:36 +08:00
this._value = 0n;
}
/**
*
*/
public isEmpty(): boolean {
2025-06-16 09:36:36 +08:00
return this._value === 0n;
}
/**
*
*/
public cardinality(): number {
let count = 0;
2025-06-16 09:36:36 +08:00
let value = this._value;
while (value > 0n) {
if (value & 1n) {
count++;
}
value >>= 1n;
}
2025-06-16 09:36:36 +08:00
return count;
}
/**
2025-06-16 09:36:36 +08:00
*
*/
public and(other: Bits): Bits {
return new Bits(this._value & other._value);
}
/**
*
*/
2025-06-16 09:36:36 +08:00
public or(other: Bits): Bits {
return new Bits(this._value | other._value);
}
/**
*
*/
public xor(other: Bits): Bits {
return new Bits(this._value ^ other._value);
}
/**
*
*/
public not(maxBits: number = 64): Bits {
const mask = (1n << BigInt(maxBits)) - 1n;
return new Bits((~this._value) & mask);
}
/**
* Bits对象
*/
public copyFrom(other: Bits): void {
2025-06-16 09:36:36 +08:00
this._value = other._value;
}
/**
* Bits的副本
*/
public clone(): Bits {
2025-06-16 09:36:36 +08:00
return new Bits(this._value);
}
/**
* BigInt值
*/
public getValue(): bigint {
return this._value;
}
/**
* BigInt值
*/
public setValue(value: bigint): void {
this._value = value;
}
/**
*
*/
public toString(): string {
const bits: string[] = [];
let index = 0;
let value = this._value;
while (value > 0n) {
if (value & 1n) {
bits.push(index.toString());
}
value >>= 1n;
index++;
}
return `Bits[${bits.join(', ')}]`;
}
/**
*
*/
public toBinaryString(maxBits: number = 64): string {
let result = '';
for (let i = maxBits - 1; i >= 0; i--) {
result += this.get(i) ? '1' : '0';
if (i % 8 === 0 && i > 0) {
result += ' ';
}
}
return result;
}
/**
*
*/
public toHexString(): string {
return '0x' + this._value.toString(16).toUpperCase();
}
/**
* Bits
*/
public static fromBinaryString(binaryString: string): Bits {
const cleanString = binaryString.replace(/\s/g, '');
const value = BigInt('0b' + cleanString);
return new Bits(value);
}
/**
* Bits
*/
public static fromHexString(hexString: string): Bits {
const cleanString = hexString.replace(/^0x/i, '');
const value = BigInt('0x' + cleanString);
return new Bits(value);
}
/**
* Bits对象是否相等
*/
public equals(other: Bits): boolean {
return this._value === other._value;
}
/**
*
*/
public getHighestBitIndex(): number {
if (this._value === 0n) {
return -1;
}
let index = 0;
let value = this._value;
while (value > 1n) {
value >>= 1n;
index++;
}
return index;
}
/**
*
*/
public getLowestBitIndex(): number {
if (this._value === 0n) {
return -1;
}
let index = 0;
let value = this._value;
while ((value & 1n) === 0n) {
value >>= 1n;
index++;
}
return index;
}
2021-05-07 16:23:15 +08:00
}