-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stack and Queue should allow for random access in constant time #15922
Comments
Why have a stack at all then? (List could just have a few other methods added). Why enforce a constant-time-random-access implementation? |
Maybe one of Stack or Queue should never have existed, true. But now they are here. I don't see any other backing store coming for the same reason List will always be a simple array internally. |
It might be interesting to experiment with a linked-list of increasingly sized arrays. That approach could reduce the time taken for resizes, while still having good locality of reference within each block. |
It might. This is, indeed, only a good idea for Stack in its current form and not for List. Still, the Stack class always feels crippled when I use it. And what you propose does not change the asymptotic timings. I always feel the BCL could have had a really good collection library with multiple implementations for the same logical interface. Alas, this is apparently not the leading idea. Here, there could have been multiple stack implementations one of which would implement the idea you just brought up. Too late, now we are fixed on this design. |
logical/design problem: for both a stack and a queue, what does What is it you've been doing that you need indexed access into a stack/queue? I do think it would have been nice for Stack/Queue interfaces, though... |
Collection[0] is, in my mind clearly, what Peek would return. After each write the indexes would change. The use case is an expression parser and I need to peek into the current parsing stack. So I want the second-next element by saying stack[1]. I don't see any definition problem for queues. We are not talking about double-ended queues here as the BCL does not have such a thing. Maybe instead of an indexer we could have a |
I don't think it's a good idea to break the abstraction over the underlying representation (which this proposal would do) unless there's a clear benefit. What are the cases where you find yourself working with a My general feeling is that it's a bad idea to add APIs to |
👍 to @ellismg's comments that I don't think it is a good idea to break this abstraction. |
Because I logically wanted a stack and List does not have a Pop method. I would have needed to add it. I just did a survey how other platforms do it:
Maybe all that's needed is something like a |
@GSPP - But in your case you only wanted one extra level down, right? Were you not in a situation you'd be popping the top element off anyways? Note that in the case of stacks, there are two different ways to index them: root is 0 ("I'm on the nth level"), and head is 0 ("root is n from my current position"). |
@Clockwork-Muse sometimes I would pop one or multiple elements, but sometimes I would only peek and fail to match. |
Thanks for the suggestion, but from the above discussion it sounds like we won't end up taking this change. Closing. |
The
Stack<T>
andQueue<T>
classes algorithmically could allow for random access in constant time. At the moment this is not represented in the API surface of those types. I propose to lift that restriction.In fact most members of
List<T>
could be made available onStack<T>
andQueue<T>
as well. Really,Stack<T>
andQueue<T>
are mostly different ways to index and interpret an underlying array.There is no reason a
Stack<T>
could not have for exampleRemove(T)
or aRemoveAll(Func<T, bool>)
methods just likeList<T>
has.I propose in priority order:
get
indexers toStack<T>
andQueue<T>
Insert
,RemoveAt
and aset
indexer.List<T>
methods as seems appropriate toStack<T>
andQueue<T>
, copying them fromList<T>
. I don't see a reason why the set of methods to copy should be limited. We could go for feature parity.This issue seems like a good candidate for a community contribution.
The text was updated successfully, but these errors were encountered: