-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merian-nodes: Graph: Profiler: Use ring buffer
- Loading branch information
Showing
2 changed files
with
55 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
namespace merian { | ||
|
||
/** | ||
* @brief A ring buffer that stores every element twice to allow for consecutive pointers | ||
* without iterators. | ||
* | ||
* @tparam T the type, must be compatibe with std::vector | ||
*/ | ||
template <typename T> class RingBuffer { | ||
|
||
public: | ||
RingBuffer(const std::size_t size, const T& value) : ring_size(size), buffer(size * 2, value) {} | ||
|
||
RingBuffer(const std::size_t size) : ring_size(size), buffer(size * 2) {} | ||
|
||
const std::size_t& size() { | ||
return ring_size; | ||
} | ||
|
||
const T& operator[](const std::size_t index) { | ||
return buffer[index % ring_size]; | ||
} | ||
|
||
void set(const std::size_t index, const T& value) { | ||
const std::size_t ring_index = index % ring_size; | ||
buffer[ring_index] = buffer[ring_index + ring_size] = value; | ||
} | ||
|
||
private: | ||
const std::size_t ring_size; | ||
std::vector<T> buffer; | ||
}; | ||
|
||
} // namespace merian |