Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:46

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