tmp commit

This commit is contained in:
Mikhail 2023-11-23 00:37:03 +08:00
parent b07256f020
commit db863bf8bf
5 changed files with 25 additions and 15 deletions

View File

@ -142,7 +142,7 @@ namespace DCFApixels.DragonECS
Dictionary<int, int> r = new Dictionary<int, int>();
foreach (var id in maskInc)
{
var bit = EcsMaskBit.FromPoolID(id);
var bit = EcsMaskBit.FromID(id);
if(!r.TryAdd(bit.chankIndex, bit.mask))
r[bit.chankIndex] = r[bit.chankIndex] | bit.mask;
}
@ -150,7 +150,7 @@ namespace DCFApixels.DragonECS
r.Clear();
foreach (var id in maskExc)
{
var bit = EcsMaskBit.FromPoolID(id);
var bit = EcsMaskBit.FromID(id);
if (!r.TryAdd(bit.chankIndex, bit.mask))
r[bit.chankIndex] = r[bit.chankIndex] | bit.mask;
}
@ -218,6 +218,10 @@ namespace DCFApixels.DragonECS
#region Mask
public readonly struct EcsMaskBit
{
private const int BITS = 32;
private const int DIV_SHIFT = 5;
private const int MOD_MASK = BITS - 1;
public readonly int chankIndex;
public readonly int mask;
public EcsMaskBit(int chankIndex, int mask)
@ -225,11 +229,10 @@ namespace DCFApixels.DragonECS
this.chankIndex = chankIndex;
this.mask = mask;
}
public static EcsMaskBit FromPoolID(int id)
public static EcsMaskBit FromID(int id)
{
return new EcsMaskBit(id / 32, 1 << (id % 32));
return new EcsMaskBit(id >> DIV_SHIFT, 1 << (id & MOD_MASK)); //аналогично new EcsMaskBit(id / BITS, 1 << (id % BITS)) но быстрее
}
public override string ToString()
{
return $"bit({chankIndex}, {mask})";

View File

@ -273,14 +273,14 @@ namespace DCFApixels.DragonECS
internal void IncrementEntityComponentCount(int entityID, int componentID)
{
_componentCounts[entityID]++;
EcsMaskBit bit = EcsMaskBit.FromPoolID(componentID);
EcsMaskBit bit = EcsMaskBit.FromID(componentID);
_entitiesComponentMasks[entityID][bit.chankIndex] |= bit.mask;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void DecrementEntityComponentCount(int entityID, int componentID)
{
var count = --_componentCounts[entityID];
EcsMaskBit bit = EcsMaskBit.FromPoolID(componentID);
EcsMaskBit bit = EcsMaskBit.FromID(componentID);
_entitiesComponentMasks[entityID][bit.chankIndex] &= ~bit.mask;
if (count == 0 && _allEntites.Has(entityID))

View File

@ -140,7 +140,7 @@ namespace DCFApixels.DragonECS
_source = world;
_componentID = componentID;
_maskBit = EcsMaskBit.FromPoolID(componentID);
_maskBit = EcsMaskBit.FromID(componentID);
const int capacity = 512;

View File

@ -187,7 +187,14 @@ namespace DCFApixels.DragonECS
if (!Has(key: key)) EcsPoolThrowHalper.ThrowNotHaveComponent<T>(unchecked((int)key));
#endif
_listeners.InvokeOnGet(unchecked((int)key));
return ref _entries[FindEntry(key)].value;
//return ref _entries[FindEntry(key)].value;
for (int i = _buckets[unchecked((int)key & _modBitMask)]; i >= 0; i = _entries[i].next)
if (_entries[i].key == key) return ref _entries[i].value; // return i;
#pragma warning disable CS0251 // Èíäåêñàöèÿ ìàññèâà ñ îòðèöàòåëüíûì èíäåêñîì
return ref _entries[-1].value; //ïðè íîðìàëüíîé ðàáîòå ýòî íåäîñòèæèìûé êîä.
#pragma warning restore CS0251 // Èíäåêñàöèÿ ìàññèâà ñ îòðèöàòåëüíûì èíäåêñîì
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@ -25,15 +25,15 @@ namespace DCFApixels.DragonECS.Utils
private int _modBitMask;
#region Properties
public TValue this[long keyX, long keyY]
public ref TValue this[long keyX, long keyY]
{
get => _entries[FindEntry(keyX + (keyY << 32))].value;
set => Insert(keyX + (keyY << 32), value);
get => ref _entries[FindEntry(keyX + (keyY << 32))].value;
//set => Insert(keyX + (keyY << 32), value);
}
public TValue this[long key]
public ref TValue this[long key]
{
get => _entries[FindEntry(key)].value;
set => Insert(key, value);
get => ref _entries[FindEntry(key)].value;
//set => Insert(key, value);
}
public int Count => _count;