Chromium Code Reviews| Index: cc/ring_buffer.h |
| diff --git a/cc/ring_buffer.h b/cc/ring_buffer.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8740a39058af9bc666f564beec62a0d855ec68e6 |
| --- /dev/null |
| +++ b/cc/ring_buffer.h |
| @@ -0,0 +1,53 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CC_RING_BUFFER_H_ |
| +#define CC_RING_BUFFER_H_ |
| + |
| +namespace cc { |
| + |
| +template<typename T, unsigned int size> |
| +class RingBuffer { |
| + public: |
| + explicit RingBuffer() |
| + : current_index_(0) { |
| + } |
| + |
| + unsigned int BufferSize() const { |
| + return size; |
| + } |
| + |
| + unsigned int CurrentIndex() const { |
| + return current_index_; |
| + } |
| + |
| + // tests if a value was saved to this index |
| + bool IsFilledIndex(unsigned int n) const { |
|
shawnsingh
2013/01/10 21:13:01
I just noticed that the style guide asks us never
danakj
2013/01/10 23:31:40
What, are you sure it doesn't mean to use "unsigne
|
| + return BufferIndex(n) < current_index_; |
| + } |
| + |
| + // n = 0 returns the oldest value and |
| + // n = bufferSize() - 1 returns the most recent value. |
| + T ReadBuffer(unsigned int n) const { |
| + return buffer_[BufferIndex(n)]; |
|
shawnsingh
2013/01/10 21:13:01
Can we DCHECK here that n is always >= 0 and stric
|
| + } |
| + |
| + void SaveToBuffer(T value) |
| + { |
| + buffer_[BufferIndex(0)] = value; |
| + current_index_++; |
| + } |
| + |
| + private: |
| + inline int BufferIndex(int n) const { |
| + return (current_index_ + n) % size; |
| + } |
| + |
| + T buffer_[size]; |
| + unsigned int current_index_; |
| +}; |
| + |
| +} // namespace cc |
| + |
| +#endif // CC_RING_BUFFER_H_ |