Browse Source

add new (not cache friendly) ExecuteOnAllEntities function (all the entities regardless the group)

tags/Rel25b
sebas77 6 years ago
parent
commit
ec97b0c2ea
7 changed files with 69 additions and 22 deletions
  1. +1
    -1
      Svelto.Common
  2. +3
    -2
      Svelto.ECS/DataStructures/TypeSafeDictionary.cs
  3. +2
    -2
      Svelto.ECS/EnginesRoot.Engines.cs
  4. +8
    -4
      Svelto.ECS/EnginesRoot.Entities.cs
  5. +8
    -5
      Svelto.ECS/EnginesRoot.Submission.cs
  6. +36
    -2
      Svelto.ECS/EntitiesDB.cs
  7. +11
    -6
      Svelto.ECS/IEntitiesDB.cs

+ 1
- 1
Svelto.Common

@@ -1 +1 @@
Subproject commit a1f5f55c8b28f66ed033fa02c425dfa16c3c30e6
Subproject commit 4f7d4d96407f3ce390a23b930ba235a2447729ec

+ 3
- 2
Svelto.ECS/DataStructures/TypeSafeDictionary.cs View File

@@ -88,7 +88,8 @@ namespace Svelto.ECS.Internal
Dictionary<Type, FasterList<IHandleEntityViewEngineAbstracted>>
entityViewEnginesDB)
{
var fasterValuesBuffer = GetFasterValuesBuffer();
int count;
var fasterValuesBuffer = GetFasterValuesBuffer(out count);
var valueIndex = GetValueIndex(fromEntityGid.entityID);

if (entityViewEnginesDB != null)
@@ -101,7 +102,7 @@ namespace Svelto.ECS.Internal
toGroupCasted.Add(fromEntityGid.entityID, ref fasterValuesBuffer[valueIndex]);
if (entityViewEnginesDB != null)
AddEntityViewToEngines(entityViewEnginesDB, ref toGroupCasted.GetFasterValuesBuffer()[toGroupCasted.GetValueIndex(fromEntityGid.entityID)]);
AddEntityViewToEngines(entityViewEnginesDB, ref toGroupCasted.GetFasterValuesBuffer(out count)[toGroupCasted.GetValueIndex(fromEntityGid.entityID)]);
}

Remove(fromEntityGid.entityID);


+ 2
- 2
Svelto.ECS/EnginesRoot.Engines.cs View File

@@ -43,10 +43,10 @@ namespace Svelto.ECS
{
[ExclusiveGroup.StandardEntitiesGroup] = new Dictionary<Type, ITypeSafeDictionary>()
};
//_groupedGroups = new Dictionary<Type, FasterDictionary<int, int>>();
_groupedGroups = new Dictionary<Type, FasterDictionary<int, ITypeSafeDictionary>>();
_groupedEntityToAdd = new DoubleBufferedEntityViews<Dictionary<int, Dictionary<Type, ITypeSafeDictionary>>>();

_DB = new entitiesDB(_groupEntityDB);
_DB = new entitiesDB(_groupEntityDB, _groupedGroups);

_scheduler = entityViewScheduler;
_scheduler.Schedule(new WeakAction(SubmitEntityViews));


+ 8
- 4
Svelto.ECS/EnginesRoot.Entities.cs View File

@@ -138,14 +138,18 @@ namespace Svelto.ECS
{
safeDictionary = fromTypeSafeDictionary.Create();
toGroup.Add(entityType, safeDictionary);
//_groupedGroups[entityType] = new FasterDictionary<int, ITypeSafeDictionary>();
_groupedGroups[entityType] = new FasterDictionary<int, ITypeSafeDictionary>();
}

_groupedGroups[entityType][toGroupID] = safeDictionary;
}

fromTypeSafeDictionary.MoveEntityFromDictionaryAndEngines(fromEntityGID, toGroupID, safeDictionary, _entityEngines);

if (fromTypeSafeDictionary.Count == 0) //clean up
{
_groupedGroups[entityType].Remove(toGroupID);

fromGroup.Remove(entityType);
}
@@ -188,7 +192,6 @@ namespace Svelto.ECS
readonly entitiesDB _DB;
readonly DoubleBufferedEntityViews<Dictionary<int, Dictionary<Type, ITypeSafeDictionary>>> _groupedEntityToAdd;

static readonly Type _typeEntityInfoView = typeof(EntityInfoView);
}

@@ -205,8 +208,9 @@ namespace Svelto.ECS
var typeSafeDictionary = (TypeSafeDictionary<T>) _current[typeof(T)];

initializer.ID = _id;
typeSafeDictionary.GetFasterValuesBuffer()[typeSafeDictionary.FindElementIndex(_id.entityID)] = initializer;

int count;
typeSafeDictionary.GetFasterValuesBuffer(out count)[typeSafeDictionary.FindElementIndex(_id.entityID)] = initializer;
}

readonly Dictionary<Type, ITypeSafeDictionary> _current;


+ 8
- 5
Svelto.ECS/EnginesRoot.Submission.cs View File

@@ -63,15 +63,18 @@ namespace Svelto.ECS
foreach (var entityViewTypeSafeDictionary in groupOfEntitiesToSubmit.Value)
{
ITypeSafeDictionary dbDic;
FasterDictionary<int, ITypeSafeDictionary> groupedGroup = null;
if (groupDB.TryGetValue(entityViewTypeSafeDictionary.Key, out dbDic) == false)
{
dbDic = groupDB[entityViewTypeSafeDictionary.Key] = entityViewTypeSafeDictionary.Value.Create();
//_groupedGroups[entityViewTypeSafeDictionary.Key] = new FasterDictionary<int, ITypeSafeDictionary>();
if (_groupedGroups.TryGetValue(entityViewTypeSafeDictionary.Key, out groupedGroup) == false)
groupedGroup = _groupedGroups[entityViewTypeSafeDictionary.Key] = new FasterDictionary<int, ITypeSafeDictionary>();
}

//type safe copy
dbDic.FillWithIndexedEntities(entityViewTypeSafeDictionary.Value);
// _groupedGroups[entityViewTypeSafeDictionary.Key][groupID] = dbDic;
groupedGroup[groupID] = dbDic;
}
}

@@ -92,8 +95,8 @@ namespace Svelto.ECS
//to the FasterDictionary capabilitiies OR it's possible to get a specific entityView indexed by
//ID. This ID doesn't need to be the EGID, it can be just the entityID
readonly Dictionary<int, Dictionary<Type, ITypeSafeDictionary>> _groupEntityDB;
// readonly Dictionary<Type, FasterDictionary<int, ITypeSafeDictionary>> _groupedGroups; //yes I am being sarcastic
readonly EntitySubmissionScheduler _scheduler;
readonly Dictionary<int, Dictionary<Type, ITypeSafeDictionary>> _groupEntityDB;
readonly Dictionary<Type, FasterDictionary<int, ITypeSafeDictionary>> _groupedGroups; //yes I am being sarcastic
readonly EntitySubmissionScheduler _scheduler;
}
}

+ 36
- 2
Svelto.ECS/EntitiesDB.cs View File

@@ -1,15 +1,18 @@
using System;
using System.Collections.Generic;
using Svelto.DataStructures;
using Svelto.DataStructures.Experimental;
using Svelto.Utilities;

namespace Svelto.ECS.Internal
{
class entitiesDB : IEntitiesDB
{
internal entitiesDB(Dictionary<int, Dictionary<Type, ITypeSafeDictionary>> groupEntityViewsDB)
internal entitiesDB(Dictionary<int, Dictionary<Type, ITypeSafeDictionary>> groupEntityViewsDB,
Dictionary<Type, FasterDictionary<int, ITypeSafeDictionary>> groupedGroups)
{
_groupEntityViewsDB = groupEntityViewsDB;
_groupedGroups = groupedGroups;
}

public ReadOnlyCollectionStruct<T> QueryEntityViews<T>() where T:class, IEntityStruct
@@ -159,7 +162,7 @@ namespace Svelto.ECS.Internal
public void ExecuteOnEntities<T, W>(int groupID, ref W value, ActionRef<T, W> action) where T : IEntityStruct
{
int count;
var entities = QueryEntities<T>(out count);
var entities = QueryEntities<T>(groupID, out count);

for (int i = 0; i < count; i++)
action(ref entities[i], ref value);
@@ -174,6 +177,36 @@ namespace Svelto.ECS.Internal
action(ref entities[i], ref value);
}

public void ExecuteOnAllEntities<T>(ActionRef<T> action) where T : IEntityStruct
{
int count;
var typeSafeDictionaries = _groupedGroups[typeof(T)].GetFasterValuesBuffer(out count);
for (int j = 0; j < count; j++)
{
int count2;
var safedic = typeSafeDictionaries[j];
TypeSafeDictionary<T> casted = safedic as TypeSafeDictionary<T>;
var entities = casted.GetFasterValuesBuffer(out count2);
for (int i = 0; i < count2; i++)
action(ref entities[i]);
}
}

public void ExecuteOnAllEntities<T, W>(ref W value, ActionRef<T, W> action) where T : IEntityStruct
{
int count;
var typeSafeDictionaries = _groupedGroups[typeof(T)].GetFasterValuesBuffer(out count);
for (int j = 0; j < count; j++)
{
int count2;
var safedic = typeSafeDictionaries[j];
TypeSafeDictionary<T> casted = safedic as TypeSafeDictionary<T>;
var entities = casted.GetFasterValuesBuffer(out count2);
for (int i = 0; i < count2; i++)
action(ref entities[i], ref value);
}
}

public bool Exists<T>(EGID entityGID) where T : IEntityStruct
{
TypeSafeDictionary<T> casted;
@@ -257,5 +290,6 @@ namespace Svelto.ECS.Internal
//grouped set of entity views, this is the standard way to handle entity views
readonly Dictionary<int, Dictionary<Type, ITypeSafeDictionary>> _groupEntityViewsDB;
Dictionary<Type, FasterDictionary<int, ITypeSafeDictionary>> _groupedGroups;
}
}

+ 11
- 6
Svelto.ECS/IEntitiesDB.cs View File

@@ -38,21 +38,26 @@ namespace Svelto.ECS
bool TryQueryEntitiesAndIndex<T>(EGID entityGid, out uint index, out T[] array) where T : IEntityStruct;

//to use with EntityViews, EntityStructs and EntityViewStructs
void ExecuteOnEntity<T, W>(EGID egid, ref W value, ActionRef<T, W> action) where T : IEntityStruct;
void ExecuteOnEntity<T>(EGID egid, ActionRef<T> action) where T : IEntityStruct;
void ExecuteOnEntity<T>(int id, ActionRef<T> action) where T : IEntityStruct;
void ExecuteOnEntity<T>(int id, int groupid, ActionRef<T> action) where T : IEntityStruct;
void ExecuteOnEntity<T, W>(int id, ref W value, ActionRef<T, W> action) where T : IEntityStruct;
void ExecuteOnEntity<T, W>(int id, int groupid, ref W value, ActionRef<T, W> action) where T : IEntityStruct;

void ExecuteOnEntities<T>(int groupID, ActionRef<T> action) where T : IEntityStruct;
void ExecuteOnEntities<T>(ActionRef<T> action) where T : IEntityStruct;

void ExecuteOnEntity<T, W>(EGID egid, ref W value, ActionRef<T, W> action) where T : IEntityStruct;

void ExecuteOnEntity<T, W>(int id, ref W value, ActionRef<T, W> action) where T : IEntityStruct;
void ExecuteOnEntity<T, W>(int id, int groupid, ref W value, ActionRef<T, W> action) where T : IEntityStruct;
void ExecuteOnEntities<T, W>(int groupID, ref W value, ActionRef<T, W> action) where T : IEntityStruct;
void ExecuteOnEntities<T, W>(ref W value, ActionRef<T, W> action) where T : IEntityStruct;

void ExecuteOnAllEntities<T>(ActionRef<T> action) where T : IEntityStruct;
void ExecuteOnAllEntities<T, W>(ref W value, ActionRef<T, W> action) where T : IEntityStruct;

bool Exists<T>(EGID egid) where T : IEntityStruct;
bool HasAny<T>() where T:IEntityStruct;


Loading…
Cancel
Save