RCR: Stack, Queue alias methods in Array

[snipped lots of good args by kent and martin]

maybe we can name methods by the way we access them (and not on its form).
top/bottom are too generic/ambiguous since we can turn array upside-down
anytime :slight_smile:

try fifo/lifo. so we have fifo#in, fifo#out, lifo#in, lifo#outā€¦

[snipped lots of good args by kent and martin]

maybe we can name methods by the way we access them (and not on its form).
top/bottom are too generic/ambiguous since we can turn array upside-down
anytime :slight_smile:

true :slight_smile:

try fifo/lifo. so we have fifo#in, fifo#out, lifo#in, lifo#outā€¦

stack and queue are the more usual name, though. and the method names
should be the usual names for operations on stacks and queues. the
problem is, ā€˜peek at the top elementā€™ is not (as far as i know, anyway)
technically a valid stack operation - strictly, you should use
ā€˜stack.push(a = stack.pop)ā€™.

does any language with a Stack class define this method? the literature
usually refers to the top of a stack, and the head and tail of a queue,
hence my proposed method names.

martin

Ā·Ā·Ā·

ā€œPe?a, Botpā€ botp@delmonte-phil.com wrote:

The C++ standard explicity disallows retrieving an element using pop().
Instead, pop() takes an element of the stack (and returns void), while
top() returns the element last pushed onto the stack.

The reason is for exception safety. We might implement pop() like this:

template
T Stack::Pop()
{
T result; // if empty, return default-constructed T
if( vused_ > 0)
{
result = v_[ā€“vused_];
}
return result;
}

but this would not be exception-safe, becase an exception could be
thrown in the last line (return result;) when the element being returned
is copy constructed. If that happens, then itā€™s too late; weā€™ve already
returned from pop(), so thereā€™s no chance for the function to roll back
the changes it made to the stack, and the element that was on the top of
the stack is now permanently lost. I stole this example from Herb
Sutter; see GotW #8: CHALLENGE EDITION: Exception Safety in case you want to read the
whole explanation.

Ruby doesnā€™t have this problem, because all containers in Ruby hold
references to objects instead of objects. If the stack mentioned above
were a stack of pointers (say T=Foo*), then there is no problem, because
the copy constructor cannot throw an exception. In Ruby, we can safely
write:

class Stack
def pop
@v = vused[-1]
vused[-1].delete_at(-1)
return @v
end
end

and there are no exception-safety problems to worry about.

Paul

Ā·Ā·Ā·

On Sat, Nov 30, 2002 at 03:57:28AM +0900, Martin DeMello wrote:

stack and queue are the more usual name, though. and the method names
should be the usual names for operations on stacks and queues. the
problem is, ā€˜peek at the top elementā€™ is not (as far as i know, anyway)
technically a valid stack operation - strictly, you should use
ā€˜stack.push(a = stack.pop)ā€™.

does any language with a Stack class define this method? the literature
usually refers to the top of a stack, and the head and tail of a queue,
hence my proposed method names.