Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi TObjectStack<T> How to access Items other than top?

In Delphi XE2, I need a generic object stack in which my calling code can access not just the top (via Peek), but Top-1, Top-2 etc. More of an "ObjectStackList" perhaps.

I haven't spotted a simple way to achieve that using the existing System.Generics.Collections classes. And TObjectStack's FItems is private in TStack, so no straightforward way to create a TObjectStack descendant which can access it.

I realize I could use ToArray to access items by index, but that creates a separate array which seems a waste of time and space just to access a single item in FItems.

Any ideas?

like image 510
gwideman Avatar asked Oct 03 '22 21:10

gwideman


1 Answers

Just use TObjectList instead. You can access all the elements you want, and you can modify it at whichever end you want to treat as the top of a stack.

like image 51
Rob Kennedy Avatar answered Oct 09 '22 09:10

Rob Kennedy