Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TypeSafeDictionary.cs 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using Svelto.DataStructures;
  2. using System.Collections.Generic;
  3. namespace Svelto.ECS.Internal
  4. {
  5. /// <summary>
  6. /// This is just a place holder at the moment
  7. /// I always wanted to create my own Dictionary
  8. /// data structure as excercise, but never had the
  9. /// time to. At the moment I need the custom interface
  10. /// wrapped though.
  11. /// </summary>
  12. public interface ITypeSafeDictionary
  13. {
  14. void FillWithIndexedEntityViews(ITypeSafeList entityViews);
  15. bool Remove(int entityId);
  16. IEntityView GetIndexedEntityView(int entityID);
  17. }
  18. class TypeSafeDictionary<TValue> : Dictionary<int, TValue>, ITypeSafeDictionary where TValue:IEntityView
  19. {
  20. internal static readonly ReadOnlyDictionary<int, TValue> Default =
  21. new ReadOnlyDictionary<int, TValue>(new Dictionary<int, TValue>());
  22. public void FillWithIndexedEntityViews(ITypeSafeList entityViews)
  23. {
  24. int count;
  25. var buffer = FasterList<TValue>.NoVirt.ToArrayFast((FasterList<TValue>) entityViews, out count);
  26. for (int i = 0; i < count; i++)
  27. {
  28. var entityView = buffer[i];
  29. Add(entityView.ID, entityView);
  30. }
  31. }
  32. new public bool Remove(int entityId)
  33. {
  34. base.Remove(entityId);
  35. return this.Count > 0;
  36. }
  37. public IEntityView GetIndexedEntityView(int entityID)
  38. {
  39. return this[entityID];
  40. }
  41. }
  42. }