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...

A OOP class for implementing stacks -massively upgraded

Post by srod »

Update : 22nd November 2007.
Have added a new method, namely; Peek().

This allows you to examine an element from a stack without affecting the stack at all; that is no kind of 'pop' action is performed.

Note that the index supplied to this method is zero-based and thus must be a value within the range 0 to NumberOfElementsPushed()-1 etc.

The user manual has been updated.

Download


Bug fix : 8th November 2007.

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

Download


Upgraded : 1st November 2007.

Hi,

I have restructured the class so that we now have two stack classes inheriting ( :wink: ) from a single base class. The first class gives a basic stack (as previous) in which stack objects can be created dynamically and 32-bit values pushed and popped at will.

A second class (sharing the same method names to give some kind of polymorphism!) allows you to push and pop any kind of structure, even those containing string fields. No need to worry about memory leaks as all strings are carefully freed etc.

It is important to understand exactly how these more complex stack objects function :
When the developer pushes a structured variable, the entire structure is copied (including string fields) and placed on the underlying stack. This means that, having pushed such a variable onto a structured stack, the developer is then free to modify the original variable, safe in the knowledge that the original fields are preserved on the stack.

The only proviso is that all string fields must be placed before all other fields.

The download includes a couple of examples + a very short user guide. Don't worry if you're not up to scratch with OOP terminology as this little library is very simple to use. :)

Download



Hi,

I'm rewriting some old utility routines of mine, but this time in the form of simple OOP classes as this is the way I seem to be heading lately. And why not indeed? Basic OOP is a breeze in Purebasic! :wink:

This first utility allows the developer to easily create stack objects with all the usual functionality etc.

Create a stack object with something like :

Code: Select all

MyStackObject.StackObject
MyStackObject = NewStack(100)  ;For a stack of size 100 etc.
Push a value with :

Code: Select all

MyStackObject\Push(25)  ;Pushes the value 25 for example.
and so on.

REMAINDER OF POST REMOVED.
Last edited by srod on Thu Nov 22, 2007 2:27 pm, edited 4 times in total.
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

REMOVED.
Last edited by srod on Fri Nov 02, 2007 1:01 am, edited 1 time in total.
I may look like a mule, but I'm not a complete ass.
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Post by #NULL »

i just have a question: where are the limits for linked lists to be used as
stack? is it just a speed thing or is there something else?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

There are no linked lists in this code!

You are mistaking method calls for linked list functions.
I may look like a mule, but I'm not a complete ass.
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Post by citystate »

I think #NULL is asking how a stack using a linked list compares to your code re speed - I mean, apart from the obvious coolness of using OOP in PB, is there a reason to reinvent the wheel?

incidently, nice code :)
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

citystate wrote:I think #NULL is asking how a stack using a linked list compares to your code re speed - I mean, apart from the obvious coolness of using OOP in PB, is there a reason to reinvent the wheel?

incidently, nice code :)
A linked list would be over-kill for implementing a stack in my opinion, not really a natural way of doing it. The method given here, aside from the usual overheads when using OOP (indirect function calling etc.) is about as direct as it gets.

As for reinventing the wheel... if this code helps anyone or simply fits in with their way of doing things then all is well and good. Otherwise, just ignore it.

As I said, I am moving more and more towards OOP now for my own reasons and am more than happy to do so using PB.
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 »

Thanks for the code.

Maybe when you have a spare moment ( :lol: ) you can write an "'OOP in PureBasic' for the brain dead" tutorial.

As the #1 qualifier among your potential reader base I look forward to reading it!
:D
Dare2 cut down to size
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

Post by milan1612 »

Nice code, could be useful in one of my projects :P
Windows 7 & PureBasic 4.4
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Post by #NULL »

i find the code interesting too, nice work i can learn from, thanks.
and about my own question (citystate understood right): one can use your code to get stacks dynamically and you can use pointers in structures to it. as far as i know you couldn't do that with PB-LLs, so that would be an advantage.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Thanks all.

@#NULL : still not sure I understand, but sure you could use a stack to hold pointers into a linked list etc. However, keeping such code threadsafe would involve a little work. I prefer to instead directly allocate memory for each structure and hold a pointer to it in the stack etc. Have to take care with freeing strings this way, but as I say it is my preferred option.

@Dare : you must be kidding lad; 'spare moment'? Who do you think I am, the Deli Lama? :wink: Anyhow I think there are already a couple of tutorials floating around. Are you asking about using objects / classes in your own code or creating them with interfaces etc?

**EDIT : @#NULL : ah now I understand what you are saying! Doh, we Brits are a little slow in the head sometimes. The only time I work up a head of steam is when the pubs open! :wink: You are correct in that you can embed a stack object within any kind of structure; dynamic or otherwise. As long as you remember to use the Destroy() method immediately prior to the structure losing focus etc. as there are no destructors being called automatically.
I may look like a mule, but I'm not a complete ass.
User avatar
IceSoft
Addict
Addict
Posts: 1695
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Post by IceSoft »

srod wrote:As I said, I am moving more and more towards OOP now for my own reasons and am more than happy to do so using PB.
Yes. You are on the right way.
OOP is more and more important for reusable sources.
Belive! C++ version of Puzzle of Mystralia
Bug Planet
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

IceSoft wrote:
srod wrote:As I said, I am moving more and more towards OOP now for my own reasons and am more than happy to do so using PB.
Yes. You are on the right way.
OOP is more and more important for reusable sources.
sshhhhh, (srod whispers) we don't want to start a debate about that old war-horse again! :wink:

Actually, it's a debate which has always puzzled me a little; if people want to use OOP then PB has all the necessary ingredients required to do so. Okay, it is lacking some of the 'heavier' elements required for what some would describe as 'true oop', but to my mind that is a good thing! As it stands PB allows for clean and very efficient use of basic oop techniques without all of the clutter and bloat. I would say that the only thing missing of any real consequence are constructors and destructors, but even this isn't really a big deal. No, PB has a lightweight and very easy to use means of employing simple oop and I think offers a great balance between functionality, ease of use and without the outright absurdties that comes with many other implementations.
I may look like a mule, but I'm not a complete ass.
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

Nice code.
Thank you very much for sharing.

Best regards

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

Post by srod »

You're welcome wolfie. :)
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Upgraded : 1st November 2007.

Hi,

I have restructured the class so that we now have two stack classes inheriting ( :wink: ) from a single base class. The first class gives a basic stack (as previous) in which stack objects can be created dynamically and 32-bit values pushed and popped at will.

A second class (sharing the same method names to give some kind of polymorphism!) allows you to push and pop any kind of structure, even those containing string fields. No need to worry about memory leaks as all strings are carefully freed etc.

It is important to understand exactly how these more complex stack objects function :
When the developer pushes a structured variable, the entire structure is copied (including string fields) and placed on the underlying stack. This means that, having pushed such a variable onto a structured stack, the developer is then free to modify the original variable, safe in the knowledge that the original fields are preserved on the stack.

The only proviso is that all string fields must be placed before all other fields.
See the first post for the download link.
I may look like a mule, but I'm not a complete ass.
Post Reply