From 6367268b255ff6abd45d8c8eca23296301d15c0c Mon Sep 17 00:00:00 2001 From: Jonathan Lifflander Date: Tue, 6 Sep 2022 15:11:37 -0700 Subject: [PATCH] #1950: utils: implement circular buffer with fixed size --- src/vt/utils/container/circular_buffer.h | 116 +++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 src/vt/utils/container/circular_buffer.h diff --git a/src/vt/utils/container/circular_buffer.h b/src/vt/utils/container/circular_buffer.h new file mode 100644 index 0000000000..9e79570da3 --- /dev/null +++ b/src/vt/utils/container/circular_buffer.h @@ -0,0 +1,116 @@ +/* +//@HEADER +// ***************************************************************************** +// +// circular_buffer.h +// DARMA/vt => Virtual Transport +// +// Copyright 2019-2021 National Technology & Engineering Solutions of Sandia, LLC +// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. +// Government retains certain rights in this software. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from this +// software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// Questions? Contact darma@sandia.gov +// +// ***************************************************************************** +//@HEADER +*/ + +#if !defined INCLUDED_VT_UTILS_CONTAINER_CIRCULAR_BUFFER_H +#define INCLUDED_VT_UTILS_CONTAINER_CIRCULAR_BUFFER_H + +#include "vt/config.h" + +namespace vt { namespace util { namespace container { + +template +struct CircularBuffer { + + CircularBuffer() = default; + +private: + int getNextEntry() const { + int next_entry = head_ + 1; + if (next_entry == size) { + next_entry = 0; + } + return next_entry; + } + +public: + template + void emplace(Args&&... args) { + int const next_entry = getNextEntry(); + elms_[head_] = T{std::forward(args)...}; + head_ = next_entry; + } + + void push(T&& t) { + int const next_entry = getNextEntry(); + elms_[head_] = std::move(t); + head_ = next_entry; + } + + void push(T const& t) { + int const next_entry = getNextEntry(); + elms_[head_] = t; + head_ = next_entry; + } + + T pop() { + T elm = std::move(elms_[tail_]); + ++tail_; + if (tail_ == size) { + tail_ = 0; + } + return elm; + } + + int numFree() const { + if (head_ == tail_) { + return size - 1; + } else if (head_ < tail_) { + return tail_ - head_ - 1; + } else { + return size + tail_ - head_ - 1; + } + } + + bool empty() const { return head_ == tail_; } + bool full() const { return numFree() == 0; } + int len() const { return size - 1 - numFree(); } + +private: + int head_ = 0; + int tail_ = 0; + std::array elms_; +}; + +}}} /* end namespace vt::util::container */ + +#endif /*INCLUDED_VT_UTILS_CONTAINER_CIRCULAR_BUFFER_H*/