A OOP class for implementing stacks -massively upgraded

Share your advanced PureBasic knowledge/code with the community.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Dare wrote:
srod wrote:Bug fix : 8th November 2007.

A bug with popping structures containing string fields has been fixed. :)

Download link is in the first post.
A useful but boring post.

We expect better from you. At least a few unsubstantiated innuendos and malicious accusations are required.
:lol:

Well funny you should say that, because I did hear a rumour regarding that fellow Dare2 and his strange absence from these forums. Apparently, as the word on the street would have it, he got romantically involved with a dolphin and a tin of sardines and was caught by a Russian trawler just off the coast of Finland wearing just a pair of slippers and a false moustache. He was eventually sold to a Greek circus owner on a no win no fee basis and now spends the majority of his time balancing on one leg whilst waiting for his other leg to open a small business in the south of France...

The guy's a flipping dreamer! :wink:
I may look like a mule, but I'm not a complete ass.
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

That rumour is totally wrong! The moustache is real!
Dare2 cut down to size
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

:lol:
I may look like a mule, but I'm not a complete ass.
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Post by Foz »

You have Push and Pop, yet you can't Peek.

Is that one method you forgot to add? Or is that just an unnecessary extra?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Sorry, how does Peek fit into a stack? I'm not sure I understand your request.
I may look like a mule, but I'm not a complete ass.
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Post by Foz »

Push obviously adds the value
Pop returns and removes the value

Peek is a function similar to Pop where you can retreive the information of like Pop, but it *isn't* removed from the stack. Very useful if you want to check certain values before letting it through the Pop.

Of course, Peek can work currently by calling Pop and then Pushing it back on the stack. But that's a lot of work when you can just return what is currently on the stack without actually removing and adding it again.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Well, no offence, but I do know what a Peek operation is, I just don't see how it's important for a stack - certainly I've never seen such a function added to a stack utility before! :)

The truth is that I certainly have no need for such a function and so I will not be adding it. However, beings as the utility is in source code form, please feel free to modify it in any way you wish.
I may look like a mule, but I'm not a complete ass.
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Post by Foz »

No problem, it was just something that I personally have used in the past, and just thought I'd ask.

The good thing about open source is if I need it in the future I'll add it in. :)

Thanks,
Foz
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

:)

Perhaps my oop class for dynamic arrays will help which is a few posts down in this forum section. You could easily adapt this to give the functionality of a stack and because it is based upon an array, you immediately have the equivalent to a Peek() function.

The two classes share a common code base anyhow.
I may look like a mule, but I'm not a complete ass.
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

Hey srod, are these stacks dynamic? Can I create them with 10 elements then push 12 and have it resize if there is not enough space?

Edit: Don't feel you need to add it if it doesn't, as I probably won't be doing anything other than testing with it now. Wouldn't want to you go to any trouble adding a feature that doesn't get used :)

Edit: never mind, tried it and it didn't work.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Hi Tinman,

you can use the ResizeStack() method to resize the stack. Combine this with the SizeOfStack() method and you can easily either implement dynamic growth yourself or easily adapt the code so that the stack does indeed grow dynamically as you push more elements etc.

Either way should, as I say, be quite straight forward. :)
I may look like a mule, but I'm not a complete ass.
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Post by eesau »

srod wrote:Well, no offence, but I do know what a Peek operation is, I just don't see how it's important for a stack - certainly I've never seen such a function added to a stack utility before! :)
I've always had the impression that it is a widely known and used stack-function. It's really useful, and essential for example in making a stack-based shunting yard algorithm (more here). Needed it myself the other day while I was building an expression evaluator -- I would've used your stack code but decided to code my own since yours didn't have the functionality I needed.

But I'll be sure to use your code later, it really seems easy and powerful. So thanks! :wink:
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Hi,

I know Dijkstra's algorithm for converting infix expressions to reverse Polish very well and nowhere does the algorithm specify a need to peek at values below the top of the stack. Indeed, I have a utility in these forums somewhere for evaluating expressions based upon this algorithm, and whilst it is heavily reliant upon a stack, it most certainly does nothing but push and pop! Indeed, it would cease to be a stack in my opinion if the data structure in question is being used in other ways! :wink: In fact you'd perhaps be better off with an array if you do need to peek around at elements lying below the top of the stack etc.

Still, I'm not questionning your reasons for needing to peek and poke around the elements of a stack etc. I'm just trying to find an excuse not to add it to my code as I didna have the time at the moment! :) It would be an easy addition however.
I may look like a mule, but I'm not a complete ass.
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Post by eesau »

srod wrote:I know Dijkstra's algorithm for converting infix expressions to reverse Polish very well and nowhere does the algorithm specify a need to peek at values below the top of the stack. Indeed, I have a utility in these forums somewhere for evaluating expressions based upon this algorithm, and whilst it is heavily reliant upon a stack, it most certainly does nothing but push and pop! Indeed, it would cease to be a stack in my opinion if the data structure in question is being used in other ways! :wink: In fact you'd perhaps be better off with an array if you do need to peek around at elements lying below the top of the stack etc.

Still, I'm not questionning your reasons for needing to peek and poke around the elements of a stack etc. I'm just trying to find an excuse not to add it to my code as I didna have the time at the moment! :) It would be an easy addition however.
No no, maybe I wasn't being clear. I didn't mean that I'd need to peek (or poke for that matter!) at values *below* the top of the stack, only to return the value of the element that lies at the top of the stack, without popping it. That's normal stack functionality to me at least, but I could be wrong.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Ah now I understand! :) Yes, I can see how that might be useful for Djikstra's algorithm.

I will add a GetTopOfStack() method then. Just need to chuck the old laundry outside and refill my barrell of coffee.

Consider it done! :wink:
I may look like a mule, but I'm not a complete ass.
Post Reply