// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. namespace FlaxEditor.Windows.Profiler { /// /// Profiler samples storage buffer. Support recording new frame samples. /// /// Single sample data type. public class SamplesBuffer { private T[] _data; private int _count; /// /// Gets the amount of samples in the buffer. /// public int Count => _count; /// /// Gets the last sample value. Check buffer before calling this property. /// public T Last => _data[_count - 1]; /// /// Gets or sets the sample value at the specified index. /// /// The index. /// The sample value. public T this[int index] { get => _data[index]; set => _data[index] = value; } /// /// Initializes a new instance of the class. /// /// The maximum buffer capacity. public SamplesBuffer(int capacity = ProfilerMode.MaxSamples) { _data = new T[capacity]; _count = 0; } /// /// Gets the sample at the specified index or the last sample if index is equal to -1. /// /// The index. /// The sample value public T Get(int index) { if (index >= _data.Length || _data.Length == 0) return default; return index == -1 ? _data[_count - 1] : _data[index]; } /// /// Clears this buffer. /// public void Clear() { _count = 0; } /// /// Adds the specified sample to the buffer. /// /// The sample. public void Add(T sample) { // Remove first sample if no space if (_count == _data.Length) { for (int i = 1; i < _count; i++) { _data[i - 1] = _data[i]; } _count--; } _data[_count++] = sample; } /// /// Adds the specified sample to the buffer. /// /// The sample. public void Add(ref T sample) { // Remove first sample if no space if (_count == _data.Length) { for (int i = 1; i < _count; i++) { _data[i - 1] = _data[i]; } _count--; } _data[_count++] = sample; } } }