git subrepo clone git@github.com:Rconybea/xo-alloc.git xo-alloc
subrepo: subdir: "xo-alloc" merged: "fc656313" upstream: origin: "git@github.com:Rconybea/xo-alloc.git" branch: "main" commit: "fc656313" git-subrepo: version: "0.4.9" origin: "???" commit: "???"
This commit is contained in:
parent
d16545d815
commit
2c8faf6e43
49 changed files with 7196 additions and 0 deletions
49
xo-alloc/include/xo/alloc/Stack.hpp
Normal file
49
xo-alloc/include/xo/alloc/Stack.hpp
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
/* Stack.hpp
|
||||
*
|
||||
* author: Roland Conybeare, jul 2025
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace xo {
|
||||
namespace gc {
|
||||
/** Simple stack implementation
|
||||
**/
|
||||
template <typename T>
|
||||
class Stack {
|
||||
public:
|
||||
explicit Stack(std::size_t capacity) {
|
||||
this->contents_.reserve(capacity);
|
||||
}
|
||||
|
||||
bool is_empty() const { return contents_.empty(); }
|
||||
std::size_t available() const { return contents_.capacity() - contents_.size(); }
|
||||
void drop() { contents_.resize(contents_.size() - 1); }
|
||||
void push(const T & x) { contents_.push_back(x); }
|
||||
T pop() {
|
||||
T retval = contents_[contents_.size() - 1];
|
||||
this->drop();
|
||||
return retval;
|
||||
}
|
||||
const T & top() const {
|
||||
return this->lookup(0);
|
||||
}
|
||||
const T & lookup(std::size_t i) const {
|
||||
return contents_.at(contents_.size() - 1 - i);
|
||||
}
|
||||
void clear() { contents_.clear(); }
|
||||
void reset_to(std::size_t z) { contents_.resize(z); }
|
||||
|
||||
std::size_t n_elements() const { return contents_.size(); }
|
||||
std::size_t capacity() const { return contents_.capacity(); }
|
||||
|
||||
private:
|
||||
std::vector<T> contents_;
|
||||
};
|
||||
|
||||
} /*namespace gc*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end Stack.hpp */
|
||||
Loading…
Add table
Add a link
Reference in a new issue