Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:55:03

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file corecel/cont/MiniStack.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Macros.hh"
0010 #include "corecel/Types.hh"
0011 
0012 namespace celeritas
0013 {
0014 //---------------------------------------------------------------------------//
0015 /*!
0016  * Helper class that provides the functionality of a stack on an underlying
0017  * container.
0018  */
0019 template<class T>
0020 class MiniStack
0021 {
0022   public:
0023     //!@{
0024     using size_type = ::celeritas::size_type;
0025     using value_type = T;
0026     //!@}
0027 
0028   public:
0029     //! Construct with underlying storage.
0030     CELER_FUNCTION explicit MiniStack(Span<T> storage)
0031         : data_(storage.data()), capacity_(storage.size())
0032     {
0033     }
0034 
0035     //! Insert a new element at the top of the stack
0036     CELER_FUNCTION void push(T element)
0037     {
0038         CELER_EXPECT(this->size() < this->capacity());
0039         data_[size_++] = element;
0040     }
0041 
0042     //! Remove and return the top element of the stack
0043     CELER_FUNCTION T pop()
0044     {
0045         CELER_EXPECT(!this->empty());
0046         return data_[--size_];
0047     }
0048 
0049     //! Whether there are any elements in the container
0050     CELER_FORCEINLINE_FUNCTION bool empty() const { return size_ == 0; }
0051 
0052     //! Get the number of elements
0053     CELER_FORCEINLINE_FUNCTION size_type size() const { return size_; }
0054 
0055     //! Get the number of elements that can fit in the allocated storage
0056     CELER_FORCEINLINE_FUNCTION size_type capacity() const { return capacity_; }
0057 
0058   private:
0059     T* data_;
0060     size_type size_{0};
0061     size_type capacity_;
0062 };
0063 
0064 //---------------------------------------------------------------------------//
0065 }  // namespace celeritas