Page 1 of 1

CatStack and CatQueue

Posted: Tue Aug 16, 2011 12:43 am
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  / \
     |

Re: CatStack and CatQueue

Posted: Tue Aug 16, 2011 1:44 am
by kraftman
What are the uses of this? I've never dealt with stacks/queues before.

Re: CatStack and CatQueue

Posted: Tue Aug 16, 2011 1:49 am
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

Re: CatStack and CatQueue

Posted: Tue Aug 16, 2011 6:47 am
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().

Re: CatStack and CatQueue

Posted: Tue Aug 16, 2011 12:23 pm
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.

Re: CatStack and CatQueue

Posted: Tue Aug 16, 2011 3:05 pm
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.

Re: CatStack and CatQueue

Posted: Tue Aug 16, 2011 3:23 pm
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