BatchGroup.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Veldrid.Common
  7. {
  8. public class BatchGroup
  9. {
  10. private BatchItem[] _items;
  11. public BatchGroup()
  12. {
  13. _items = new BatchItem[64];
  14. }
  15. public int Count { get; private set; }
  16. public ref BatchItem Add()
  17. {
  18. if (Count >= _items.Length)
  19. {
  20. var lastSize = _items.Length;
  21. var newSize = (lastSize + lastSize / 2 + 63) & (~63);
  22. Array.Resize(ref _items, newSize);
  23. }
  24. return ref _items[Count++];
  25. }
  26. public void Clear()
  27. {
  28. Count = 0;
  29. }
  30. public ReadOnlySpan<BatchItem> GetSpan() => new ReadOnlySpan<BatchItem>(_items, 0, Count);
  31. public Enumerator GetEnumerator() => new Enumerator(_items, Count);
  32. public struct Enumerator
  33. {
  34. private readonly BatchItem[] _items;
  35. private readonly int _count;
  36. private int _index;
  37. internal Enumerator(BatchItem[] items, int count)
  38. {
  39. _items = items;
  40. _count = count;
  41. _index = -1;
  42. }
  43. public BatchItem Current => _items[_index];
  44. public bool MoveNext()
  45. {
  46. if (_index >= _count)
  47. return false;
  48. _index++;
  49. return true;
  50. }
  51. public void Reset()
  52. {
  53. _index = -1;
  54. }
  55. }
  56. }
  57. }