|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- using System;
-
- namespace Svelto.ECS
- {
- public readonly ref struct DoubleEntitiesEnumerator<T1> where T1 : struct, IEntityComponent
- {
- public DoubleEntitiesEnumerator(GroupsEnumerable<T1> groupsEnumerable) { _groupsEnumerable = groupsEnumerable; }
-
- public EntityGroupsIterator GetEnumerator() { return new EntityGroupsIterator(_groupsEnumerable); }
-
- readonly GroupsEnumerable<T1> _groupsEnumerable;
-
- public ref struct EntityGroupsIterator
- {
- public EntityGroupsIterator(GroupsEnumerable<T1> groupsEnumerable) : this()
- {
- _groupsEnumerableA = groupsEnumerable.GetEnumerator();
- _groupsEnumerableA.MoveNext();
- _groupsEnumerableB = _groupsEnumerableA;
- _indexA = 0;
- _indexB = 0;
- }
-
- public bool MoveNext()
- {
- //once GroupEnumerables complete, they reset. If they reset and moveNext doesn't happen, they are invalid
- while (_groupsEnumerableA.isValid)
- {
- var (buffersA, _) = _groupsEnumerableA.Current;
- var (buffersB, _) = _groupsEnumerableB.Current;
-
- //current index A must iterate as long as is less than the current A group count
- while (_indexA < buffersA.count)
- {
- //current index B must iterate as long as is less than the current B group count
- if (++_indexB < buffersB.count)
- {
- return true;
- }
-
- //if B iteration is over, move to the next group
- if (_groupsEnumerableB.MoveNext() == false)
- {
- //if there is no valid next groups, we reset B and we need to move to the next A element
- _groupsEnumerableB = _groupsEnumerableA;
- (buffersB, _) = _groupsEnumerableB.Current;
- ++_indexA; //next A element
- _indexB = _indexA;
- }
- else
- //otherwise the current A will be checked against the new B group. IndexB must be reset
- //to work on the new group
- {
- _indexB = -1;
- }
- }
-
- //the current group A iteration is done, so we move to the next A group
- if (_groupsEnumerableA.MoveNext() == true)
- {
- //there is a new group, we reset the iteration
- _indexA = 0;
- _indexB = 0;
- _groupsEnumerableB = _groupsEnumerableA;
- }
- else
- return false;
- }
-
- return false;
- }
-
- public void Reset() { throw new Exception(); }
-
- public ValueRef Current
- {
- get
- {
- var valueRef = new ValueRef(_groupsEnumerableA.Current, _indexA, _groupsEnumerableB.Current
- , _indexB);
- return valueRef;
- }
- }
-
- public void Dispose() { }
-
- GroupsEnumerable<T1>.GroupsIterator _groupsEnumerableA;
- GroupsEnumerable<T1>.GroupsIterator _groupsEnumerableB;
- int _indexA;
- int _indexB;
- }
-
- public ref struct ValueRef
- {
- public readonly GroupsEnumerable<T1>.RefCurrent _current;
- public readonly int _indexA;
- public readonly GroupsEnumerable<T1>.RefCurrent _refCurrent;
- public readonly int _indexB;
-
- public ValueRef
- (GroupsEnumerable<T1>.RefCurrent current, int indexA, GroupsEnumerable<T1>.RefCurrent refCurrent
- , int indexB)
- {
- _current = current;
- _indexA = indexA;
- _refCurrent = refCurrent;
- _indexB = indexB;
- }
-
- public void Deconstruct
- (out EntityCollection<T1> buffers, out int indexA, out EntityCollection<T1> refCurrent, out int indexB)
- {
- buffers = _current._buffers;
- indexA = _indexA;
- refCurrent = _refCurrent._buffers;
- indexB = _indexB;
- }
-
- public void Deconstruct
- (out EntityCollection<T1> buffers, out int indexA, out ExclusiveGroupStruct groupA
- , out EntityCollection<T1> refCurrent, out int indexB, out ExclusiveGroupStruct groupB)
- {
- buffers = _current._buffers;
- indexA = _indexA;
- refCurrent = _refCurrent._buffers;
- indexB = _indexB;
- groupA = _current._group;
- groupB = _refCurrent._group;
- }
- }
- }
-
- public readonly ref struct DoubleIterationEnumerator<T1, T2> where T1 : struct, IEntityComponent
- where T2 : struct, IEntityComponent
- {
- public DoubleIterationEnumerator(GroupsEnumerable<T1, T2> groupsEnumerable)
- {
- _groupsEnumerable = groupsEnumerable;
- }
-
- public EntityGroupsIterator GetEnumerator() { return new EntityGroupsIterator(_groupsEnumerable); }
-
- readonly GroupsEnumerable<T1, T2> _groupsEnumerable;
-
- public ref struct EntityGroupsIterator
- {
- public EntityGroupsIterator(GroupsEnumerable<T1, T2> groupsEnumerable) : this()
- {
- _groupsEnumerableA = groupsEnumerable.GetEnumerator();
- _groupsEnumerableA.MoveNext();
- _groupsEnumerableB = _groupsEnumerableA;
- _indexA = 0;
- _indexB = 0;
- }
-
- public bool MoveNext()
- {
- //once GroupEnumerables complete, they reset. If they reset and moveNext doesn't happen, they are invalid
- while (_groupsEnumerableA.isValid)
- {
- var (buffersA, _) = _groupsEnumerableA.Current;
- var (buffersB, _) = _groupsEnumerableB.Current;
-
- //current index A must iterate as long as is less than the current A group count
- while (_indexA < buffersA.count)
- {
- //current index B must iterate as long as is less than the current B group count
- if (++_indexB < buffersB.count)
- {
- return true;
- }
-
- //if B iteration is over, move to the next group
- if (_groupsEnumerableB.MoveNext() == false)
- {
- //if there is no valid next groups, we reset B and we need to move to the next A element
- _groupsEnumerableB = _groupsEnumerableA;
- (buffersB, _) = _groupsEnumerableB.Current;
- ++_indexA; //next A element
- _indexB = _indexA;
- }
- else
- //otherwise the current A will be checked against the new B group. IndexB must be reset
- //to work on the new group
- {
- _indexB = -1;
- }
- }
-
- //the current group A iteration is done, so we move to the next A group
- if (_groupsEnumerableA.MoveNext() == true)
- {
- //there is a new group, we reset the iteration
- _indexA = 0;
- _indexB = 0;
- _groupsEnumerableB = _groupsEnumerableA;
- }
- else
- return false;
- }
-
- return false;
- }
-
- public void Reset() { throw new Exception(); }
-
- public ValueRef Current
- {
- get
- {
- var valueRef = new ValueRef(_groupsEnumerableA.Current, _indexA, _groupsEnumerableB.Current
- , _indexB);
- return valueRef;
- }
- }
-
- public void Dispose() { }
-
- GroupsEnumerable<T1, T2>.GroupsIterator _groupsEnumerableA;
- GroupsEnumerable<T1, T2>.GroupsIterator _groupsEnumerableB;
- int _indexA;
- int _indexB;
- }
-
- public ref struct ValueRef
- {
- public readonly GroupsEnumerable<T1, T2>.RefCurrent _current;
- public readonly int _indexA;
- public readonly GroupsEnumerable<T1, T2>.RefCurrent _refCurrent;
- public readonly int _indexB;
-
- public ValueRef
- (GroupsEnumerable<T1, T2>.RefCurrent current, int indexA, GroupsEnumerable<T1, T2>.RefCurrent refCurrent
- , int indexB)
- {
- _current = current;
- _indexA = indexA;
- _refCurrent = refCurrent;
- _indexB = indexB;
- }
-
- public void Deconstruct(out EntityCollection<T1, T2> buffers, out int indexA,
- out EntityCollection<T1, T2> refCurrent, out int indexB)
- {
- buffers = _current._buffers;
- indexA = _indexA;
- refCurrent = _refCurrent._buffers;
- indexB = _indexB;
- }
-
- public void Deconstruct
- (out EntityCollection<T1, T2> buffers, out int indexA, out ExclusiveGroupStruct groupA
- , out EntityCollection<T1, T2> refCurrent, out int indexB, out ExclusiveGroupStruct groupB)
- {
- buffers = _current._buffers;
- indexA = _indexA;
- refCurrent = _refCurrent._buffers;
- indexB = _indexB;
- groupA = _current._group;
- groupB = _refCurrent._group;
- }
- }
- }
-
- /// <summary>
- /// Special Enumerator to iterate a group of entities against themselves with complexity n*(n+1)/2 (skips already tested couples)
- /// </summary>
- /// <typeparam name="T1"></typeparam>
- /// <typeparam name="T2"></typeparam>
- /// <typeparam name="T3"></typeparam>
- public readonly ref struct DoubleEntitiesEnumerator<T1, T2, T3> where T1 : struct, IEntityComponent
- where T2 : struct, IEntityComponent
- where T3 : struct, IEntityComponent
- {
- public DoubleEntitiesEnumerator(GroupsEnumerable<T1, T2, T3> groupsEnumerable)
- {
- _groupsEnumerable = groupsEnumerable;
- }
-
- public EntityGroupsIterator GetEnumerator() { return new EntityGroupsIterator(_groupsEnumerable); }
-
- readonly GroupsEnumerable<T1, T2, T3> _groupsEnumerable;
-
- public ref struct EntityGroupsIterator
- {
- public EntityGroupsIterator(GroupsEnumerable<T1, T2, T3> groupsEnumerable) : this()
- {
- _groupsEnumerableA = groupsEnumerable.GetEnumerator();
- _groupsEnumerableA.MoveNext();
- _groupsEnumerableB = _groupsEnumerableA;
- _indexA = 0;
- _indexB = 0;
- }
-
- public bool MoveNext()
- {
- //once GroupEnumerables complete, they reset. If they reset and moveNext doesn't happen, they are invalid
- while (_groupsEnumerableA.isValid)
- {
- var (buffersA, _) = _groupsEnumerableA.Current;
- var (buffersB, _) = _groupsEnumerableB.Current;
-
- //current index A must iterate as long as is less than the current A group count
- while (_indexA < buffersA.count)
- {
- //current index B must iterate as long as is less than the current B group count
- if (++_indexB < buffersB.count)
- {
- return true;
- }
-
- //if B iteration is over, move to the next group
- if (_groupsEnumerableB.MoveNext() == false)
- {
- //if there is no valid next groups, we reset B and we need to move to the next A element
- _groupsEnumerableB = _groupsEnumerableA;
- (buffersB, _) = _groupsEnumerableB.Current;
- ++_indexA; //next A element
- _indexB = _indexA;
- }
- else
- //otherwise the current A will be checked against the new B group. IndexB must be reset
- //to work on the new group
- {
- _indexB = -1;
- }
- }
-
- //the current group A iteration is done, so we move to the next A group
- if (_groupsEnumerableA.MoveNext() == true)
- {
- //there is a new group, we reset the iteration
- _indexA = 0;
- _indexB = 0;
- _groupsEnumerableB = _groupsEnumerableA;
- }
- else
- return false;
- }
-
- return false;
- }
-
- public void Reset() { throw new Exception(); }
-
- public ValueRef Current
- {
- get
- {
- var valueRef = new ValueRef(_groupsEnumerableA.Current, _indexA, _groupsEnumerableB.Current
- , _indexB);
- return valueRef;
- }
- }
-
- public void Dispose() { }
-
- GroupsEnumerable<T1, T2, T3>.GroupsIterator _groupsEnumerableA;
- GroupsEnumerable<T1, T2, T3>.GroupsIterator _groupsEnumerableB;
- int _indexA;
- int _indexB;
- }
-
- public ref struct ValueRef
- {
- public readonly GroupsEnumerable<T1, T2, T3>.RefCurrent _current;
- public readonly int _indexA;
- public readonly GroupsEnumerable<T1, T2, T3>.RefCurrent _refCurrent;
- public readonly int _indexB;
-
- public ValueRef
- (GroupsEnumerable<T1, T2, T3>.RefCurrent current, int indexA
- , GroupsEnumerable<T1, T2, T3>.RefCurrent refCurrent, int indexB)
- {
- _current = current;
- _indexA = indexA;
- _refCurrent = refCurrent;
- _indexB = indexB;
- }
-
- public void Deconstruct
- (out EntityCollection<T1, T2, T3> buffers, out int indexA, out EntityCollection<T1, T2, T3> refCurrent
- , out int indexB)
- {
- buffers = _current._buffers;
- indexA = _indexA;
- refCurrent = _refCurrent._buffers;
- indexB = _indexB;
- }
-
- public void Deconstruct
- (out EntityCollection<T1, T2, T3> buffers, out int indexA, out ExclusiveGroupStruct groupA
- , out EntityCollection<T1, T2, T3> refCurrent, out int indexB, out ExclusiveGroupStruct groupB)
- {
- buffers = _current._buffers;
- indexA = _indexA;
- refCurrent = _refCurrent._buffers;
- indexB = _indexB;
- groupA = _current._group;
- groupB = _refCurrent._group;
- }
- }
- }
-
- /// <summary>
- /// Special Enumerator to iterate a group of entities against themselves with complexity n*(n+1)/2 (skips already tested couples)
- /// </summary>
- /// <typeparam name="T1"></typeparam>
- /// <typeparam name="T2"></typeparam>
- /// <typeparam name="T3"></typeparam>
- /// <typeparam name="T4"></typeparam>
- public readonly ref struct DoubleEntitiesEnumerator<T1, T2, T3, T4> where T1 : struct, IEntityComponent
- where T2 : struct, IEntityComponent
- where T3 : struct, IEntityComponent
- where T4 : struct, IEntityComponent
- {
- public DoubleEntitiesEnumerator(GroupsEnumerable<T1, T2, T3, T4> groupsEnumerable)
- {
- _groupsEnumerable = groupsEnumerable;
- }
-
- public EntityGroupsIterator GetEnumerator() { return new EntityGroupsIterator(_groupsEnumerable); }
-
- readonly GroupsEnumerable<T1, T2, T3, T4> _groupsEnumerable;
-
- public ref struct EntityGroupsIterator
- {
- public EntityGroupsIterator(GroupsEnumerable<T1, T2, T3, T4> groupsEnumerable) : this()
- {
- _groupsEnumerableA = groupsEnumerable.GetEnumerator();
- _groupsEnumerableA.MoveNext();
- _groupsEnumerableB = _groupsEnumerableA;
- _indexA = 0;
- _indexB = 0;
- }
-
- public bool MoveNext()
- {
- //once GroupEnumerables complete, they reset. If they reset and moveNext doesn't happen, they are invalid
- while (_groupsEnumerableA.isValid)
- {
- var (buffersA, _) = _groupsEnumerableA.Current;
- var (buffersB, _) = _groupsEnumerableB.Current;
-
- //current index A must iterate as long as is less than the current A group count
- while (_indexA < buffersA.count)
- {
- //current index B must iterate as long as is less than the current B group count
- if (++_indexB < buffersB.count)
- {
- return true;
- }
-
- //if B iteration is over, move to the next group
- if (_groupsEnumerableB.MoveNext() == false)
- {
- //if there is no valid next groups, we reset B and we need to move to the next A element
- _groupsEnumerableB = _groupsEnumerableA;
- (buffersB, _) = _groupsEnumerableB.Current;
- ++_indexA; //next A element
- _indexB = _indexA;
- }
- else
- //otherwise the current A will be checked against the new B group. IndexB must be reset
- //to work on the new group
- {
- _indexB = -1;
- }
- }
-
- //the current group A iteration is done, so we move to the next A group
- if (_groupsEnumerableA.MoveNext() == true)
- {
- //there is a new group, we reset the iteration
- _indexA = 0;
- _indexB = 0;
- _groupsEnumerableB = _groupsEnumerableA;
- }
- else
- return false;
- }
-
- return false;
- }
-
- public void Reset() { throw new Exception(); }
-
- public ValueRef Current
- {
- get
- {
- var valueRef = new ValueRef(_groupsEnumerableA.Current, _indexA, _groupsEnumerableB.Current
- , _indexB);
- return valueRef;
- }
- }
-
- public void Dispose() { }
-
- GroupsEnumerable<T1, T2, T3, T4>.GroupsIterator _groupsEnumerableA;
- GroupsEnumerable<T1, T2, T3, T4>.GroupsIterator _groupsEnumerableB;
- int _indexA;
- int _indexB;
- }
-
- public ref struct ValueRef
- {
- public readonly GroupsEnumerable<T1, T2, T3, T4>.RefCurrent _current;
- public readonly int _indexA;
- public readonly GroupsEnumerable<T1, T2, T3, T4>.RefCurrent _refCurrent;
- public readonly int _indexB;
-
- public ValueRef
- (GroupsEnumerable<T1, T2, T3, T4>.RefCurrent current, int indexA
- , GroupsEnumerable<T1, T2, T3, T4>.RefCurrent refCurrent, int indexB)
- {
- _current = current;
- _indexA = indexA;
- _refCurrent = refCurrent;
- _indexB = indexB;
- }
-
- public void Deconstruct
- (out EntityCollection<T1, T2, T3, T4> buffers, out int indexA, out EntityCollection<T1, T2, T3, T4> refCurrent
- , out int indexB)
- {
- buffers = _current._buffers;
- indexA = _indexA;
- refCurrent = _refCurrent._buffers;
- indexB = _indexB;
- }
-
- public void Deconstruct
- (out EntityCollection<T1, T2, T3, T4> buffers, out int indexA, out ExclusiveGroupStruct groupA
- , out EntityCollection<T1, T2, T3, T4> refCurrent, out int indexB, out ExclusiveGroupStruct groupB)
- {
- buffers = _current._buffers;
- indexA = _indexA;
- refCurrent = _refCurrent._buffers;
- indexB = _indexB;
- groupA = _current._group;
- groupB = _refCurrent._group;
- }
- }
- }
- }
|