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.4KB

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