SlidingMax.cs 955 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. namespace Leap.Unity.Graphing
  3. {
  4. public class SlidingMax
  5. {
  6. public SlidingMax(int history)
  7. {
  8. this._history = history;
  9. this._count = 0;
  10. }
  11. public void AddValue(float value)
  12. {
  13. while (this._buffer.Count != 0 && this._buffer.Front.value <= value)
  14. {
  15. this._buffer.PopFront();
  16. }
  17. this._buffer.PushFront(new SlidingMax.IndexValuePair(this._count, value));
  18. this._count++;
  19. while (this._buffer.Back.index < this._count - this._history)
  20. {
  21. this._buffer.PopBack();
  22. }
  23. }
  24. public float Max
  25. {
  26. get
  27. {
  28. return this._buffer.Back.value;
  29. }
  30. }
  31. private int _history;
  32. private int _count;
  33. private RingBuffer<SlidingMax.IndexValuePair> _buffer = new RingBuffer<SlidingMax.IndexValuePair>(8);
  34. private struct IndexValuePair
  35. {
  36. public IndexValuePair(int index, float value)
  37. {
  38. this.index = index;
  39. this.value = value;
  40. }
  41. public int index;
  42. public float value;
  43. }
  44. }
  45. }