www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Does D has built-in stack structure?

reply "Assembly" <jckj33 gmail.com> writes:
Does D has built-in stack structure (if so, which module?) or 
should I implement it myself?
Jun 21 2015
parent reply "Adrian Matoga" <epi atari8.info> writes:
On Monday, 22 June 2015 at 06:09:48 UTC, Assembly wrote:
 Does D has built-in stack structure (if so, which module?) or 
 should I implement it myself?
AFAIK there's no built-in, but std.array.Appender could be easily wrapped in an interface that makes thinking of it as stack easier: struct Stack(T) { import std.array: Appender, appender; Appender!(T[]) _app; property ref inout(T) top() inout { return _app.data[$ - 1]; }; property bool empty() const { return _app.data.length == 0; } void pop() { _app.shrinkTo(_app.data.length - 1); } void push(T t) { _app.put(t); } }
Jun 22 2015
parent "Assembly" <jckj33 gmail.com> writes:
On Monday, 22 June 2015 at 08:18:08 UTC, Adrian Matoga wrote:
 On Monday, 22 June 2015 at 06:09:48 UTC, Assembly wrote:
 Does D has built-in stack structure (if so, which module?) or 
 should I implement it myself?
AFAIK there's no built-in, but std.array.Appender could be easily wrapped in an interface that makes thinking of it as stack easier: struct Stack(T) { import std.array: Appender, appender; Appender!(T[]) _app; property ref inout(T) top() inout { return _app.data[$ - 1]; }; property bool empty() const { return _app.data.length == 0; } void pop() { _app.shrinkTo(_app.data.length - 1); } void push(T t) { _app.put(t); } }
thanks!
Jun 22 2015