// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. #pragma once #include "../Types/BaseTypes.h" #include "Engine/Platform/Platform.h" /// /// Small buffer for samples used to calculate min/max/avg values. /// template class SamplesBuffer { protected: int32 _count = 0; T _data[Size]; public: /// /// Gets amount of elements in the collection. /// FORCE_INLINE int32 Count() const { return _count; } /// /// Gets amount of elements that can be added to the collection. /// FORCE_INLINE int32 Capacity() const { return Size; } /// /// Returns true if collection has any elements added. /// FORCE_INLINE bool HasItems() const { return _count > 0; } /// /// Returns true if collection is empty. /// FORCE_INLINE bool IsEmpty() const { return _count < 1; } /// /// Gets pointer to the first element in the collection. /// FORCE_INLINE T* Get() const { return _data; } /// /// Gets or sets element at the specified index. /// /// The index. /// The element. FORCE_INLINE T& At(int32 index) const { ASSERT(index >= 0 && index < _count); return _data[index]; } /// /// Gets or sets element at the specified index. /// /// The index. /// The element. FORCE_INLINE T& operator[](int32 index) { ASSERT(index >= 0 && index < _count); return _data[index]; } /// /// Gets or sets element at the specified index. /// /// The index. /// The element. FORCE_INLINE const T& operator[](int32 index) const { ASSERT(index >= 0 && index < _count); return _data[index]; } /// /// Gets the first element value. /// FORCE_INLINE T First() const { ASSERT(HasItems()); return _data[0]; } /// /// Gets last element value. /// FORCE_INLINE T Last() const { ASSERT(HasItems()); return _data[_count - 1]; } public: /// /// Adds the specified value to the buffer. /// /// The value to add. void Add(const T& value) { if (_count != Size) _count++; if (_count > 1) { for (int32 i = _count - 1; i > 0; i--) _data[i] = _data[i - 1]; } _data[0] = value; } /// /// Sets all elements to the given value. /// /// The value. void SetAll(const T value) { for (int32 i = 0; i < _count; i++) _data[i] = value; } /// /// Clears this collection. /// FORCE_INLINE void Clear() { _count = 0; } public: /// /// Gets the minimum value in the buffer. /// T Minimum() const { ASSERT(HasItems()); T result = _data[0]; for (int32 i = 1; i < _count; i++) { if (_data[i] < result) result = _data[i]; } return result; } /// /// Gets the maximum value in the buffer. /// T Maximum() const { ASSERT(HasItems()); T result = _data[0]; for (int32 i = 1; i < _count; i++) { if (_data[i] > result) result = _data[i]; } return result; } /// /// Gets the average value in the buffer. /// T Average() const { ASSERT(HasItems()); T result = _data[0]; for (int32 i = 1; i < _count; i++) { result += _data[i]; } return result / _count; } };