using System;

namespace Leap.Unity.Graphing
{
	public class SlidingMax
	{
		public SlidingMax(int history)
		{
			this._history = history;
			this._count = 0;
		}

		public void AddValue(float value)
		{
			while (this._buffer.Count != 0 && this._buffer.Front.value <= value)
			{
				this._buffer.PopFront();
			}
			this._buffer.PushFront(new SlidingMax.IndexValuePair(this._count, value));
			this._count++;
			while (this._buffer.Back.index < this._count - this._history)
			{
				this._buffer.PopBack();
			}
		}

		public float Max
		{
			get
			{
				return this._buffer.Back.value;
			}
		}

		private int _history;

		private int _count;

		private RingBuffer<SlidingMax.IndexValuePair> _buffer = new RingBuffer<SlidingMax.IndexValuePair>(8);

		private struct IndexValuePair
		{
			public IndexValuePair(int index, float value)
			{
				this.index = index;
				this.value = value;
			}

			public int index;

			public float value;
		}
	}
}
