3#include "elementa/license.inc"
4#include "elementa/checks.inc"
6#ifndef ELEMENTA_ADTS_COMPRESSEDSTACK_H
7#define ELEMENTA_ADTS_COMPRESSEDSTACK_H
61 HeldData(Obj && o):
object{std::move(o)} {}
97 using Base = std::stack<CSHolder<Obj>>;
109 {
return(Base::empty()); }
112 typename Base::size_type
size(
void)
const
119 void push(
const Obj & o);
135 typename Base::size_type size_{0};
157 if (Base::size() <= 1)
return(1.0);
158 return(
static_cast<double>(size()-Base::size()) /
159 static_cast<double>(size()));
166 if ( (Base::empty())||(top().interesting) ) Base::emplace();
167 else ++(
const_cast<typename Base::reference
>(Base::top()).
170 ELE_CODE_TRACE({},
"Pushed un-interesting element in stack."
171 " Stack acc size = " << size() <<
172 " (in memory: " << Base::size() <<
")");
181 ELE_CODE_TRACE({},
"Pushed interesting element in stack."
182 " Stack acc size = " << size() <<
183 " (in memory: " << Base::size() <<
")");
190 Base::emplace(std::move(o));
192 ELE_CODE_TRACE({},
"Moved interesting element into stack."
193 " Stack acc size = " << size() <<
194 " (in memory: " << Base::size() <<
")");
201 if (!top().interesting)
203 ELE_CODE_TRACE({},
"Popping un-interesting element from stack");
204 --(
const_cast<typename Base::reference
>(top()).held.howmanyunint);
205 if (Base::top().held.howmanyunint == 0) Base::pop();
209 ELE_CODE_TRACE({},
"Popping interesting element from stack");
CSHolder(const Obj &o)
Construct an interesting element.
CSHolder(void)
Construct an un-interesting element.
Base::size_type size(void) const
Return the total size of the stack by counting all, not compressed.
const bool interesting
TRUE if it holds an interesting element.
std::stack< CSHolder< Obj > > Base
Shortcut.
union elementa::adts::CSHolder::HeldData held
Data held by the holder.
A compressed stack that only stores some objects and not others.
double comprRatio(void) const noexcept
Return the compression ratio in [0,1].
void pop(void)
Pop an element from stack.
const CSHolder< Obj > & top(void) const
Consult the top.
void push(void)
Push an un-interesting element without storing anything.
Holder for elements in a compressed stack.
#define ELE_CODE_TRACE_OFF
Place this inside local scope (e.g., routine) to deactivate traces there.
#define ELE_CODE_INVSTATE(expl)
To throw an invalid-state exception with an explanation.
Obj object
Interesting element.
size_t howmanyunint
Number of compressed un-interested elems.