// Copyright (c) 2012-2021 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; T _data[Size]; // Note: first element is _data[0] public: /// /// Initializes a new instance of the class. /// SamplesBuffer() : _count(0) { } public: /// /// Gets amount of elements in the collection. /// /// The collection size. FORCE_INLINE int32 Count() const { return _count; } /// /// Gets amount of elements that can be added to the collection. /// /// The collection capacity. FORCE_INLINE int32 Capacity() const { return Size; } /// /// Returns true if collection has any elements added. /// /// True if collection has any elements added, otherwise false. FORCE_INLINE bool HasItems() const { return _count > 0; } /// /// Returns true if collection is empty. /// /// True if collection is empty, otherwise false. FORCE_INLINE bool IsEmpty() const { return _count < 1; } /// /// Gets pointer to the first element in the collection. /// /// 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. /// /// The first element. FORCE_INLINE T First() const { ASSERT(HasItems()); return _data[0]; } /// /// Gets last element value. /// /// The last element. 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) { // Move previous elements for (int32 i = _count - 1; i > 0; i--) _data[i] = _data[i - 1]; // Update elements count if (_count != Size) _count++; // Insert element _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. /// /// The minimum value. 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. /// /// The maximum value. 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. /// /// The average value. T Average() const { ASSERT(HasItems()); T result = _data[0]; for (int32 i = 1; i < _count; i++) { result += _data[i]; } return result / _count; } };