CatStack and CatQueue

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
TechnoCat
Inner party member
Posts: 1612
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

CatStack and CatQueue

Post by TechnoCat »

CatStack and CatQueue are stack and queue implementations I made in Lua.
It is pretty self explanatory, most commands you see in other languages are there. I also think it is a very simple example of OO with Lua for people to look at.

So without further adieu:
Download the library from bitbucket! (comes with a rudimentary unit-test too)

I understand this is a fairly mundane library, but stacks and queues have many uses. And, I'd like some peer review on the style/performance of this code.

I also made sure there was a way to deep-copy tables when you peek/top your stack for happy viewing.
Oh, and they have pretty prints

Code: Select all

CatStack
6: MATHEMATICAL!
5: adventure time!
4: kittens
3: 5
2: 5
1: 5

Code: Select all

CatQueue
     .
out / \
     |
0: 5
1: 5
2: 5
3: kittens
4: adventure time!
5: MATHEMATICAL!
     .
in  / \
     |
Last edited by TechnoCat on Tue Aug 16, 2011 2:00 am, edited 6 times in total.
User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: CatStack and CatQueue

Post by kraftman »

What are the uses of this? I've never dealt with stacks/queues before.
User avatar
TechnoCat
Inner party member
Posts: 1612
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: CatStack and CatQueue

Post by TechnoCat »

kraftman wrote:What are the uses of this? I've never dealt with stacks/queues before.
Graph theory.
Reverse Polish Notation.
Process Scheduling.
Simple Data Management.
Games like snake.
Networking IO (or more generally, buffering). ie. queuing up packets.
Wikipedia: Stacks Queues
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: CatStack and CatQueue

Post by Robin »

Nice. :D

CatStack:empty() could be shorter:

Code: Select all

function CatStack:empty()
        return self:getSize() == 0
end
And the same goes for CatQueue:empty().
Help us help you: attach a .love.
User avatar
TechnoCat
Inner party member
Posts: 1612
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: CatStack and CatQueue

Post by TechnoCat »

Robin wrote:Nice. :D

CatStack:empty() could be shorter:

Code: Select all

function CatStack:empty()
        return self:getSize() == 0
end
And the same goes for CatQueue:empty().
good call.
User avatar
ivan
Party member
Posts: 1918
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: CatStack and CatQueue

Post by ivan »

I have used STL in the past, so I can see where you are coming from.
However, Lua tables already come with more functionality offered by stacks, arrays and lists. :)
I'm not trying to discredit your lib but if I were a beginner to Lua it might be wiser for me to study the 'table' object itself rather than an extra container library.
User avatar
TechnoCat
Inner party member
Posts: 1612
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: CatStack and CatQueue

Post by TechnoCat »

ivan wrote:I have used STL in the past, so I can see where you are coming from.
However, Lua tables already come with more functionality offered by stacks, arrays and lists. :)
I'm not trying to discredit your lib but if I were a beginner to Lua it might be wiser for me to study the 'table' object itself rather than an extra container library.
http://www.lua.org/pil/11.4.html
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 4 guests