Class Bag
Bag for managing values.
The semantics of a bag is to pop the oldest entry pushed into the bag. This class follows the pattern from Lua classes.
Metamethods
Bag:__index (key) | Lookup of missing class members. |
Methods
Bag:create (...) | Create a new instance. |
Bag:_init (...) | Initialize a new instance. |
Bag:isEmpty () | Is the bag empty. |
Bag:depth () | What is the depth of the internal structure. |
Bag:layout () | Get the layout of the bag. |
Bag:bottom () | Get a reference to the bottommost item in the bag. |
Bag:top () | Get a reference to the topmost item in the bag. |
Bag:push (...) | Push a value on the bag. |
Bag:unshift (...) | Unshift a value into the bag. |
Bag:pop ([num=1]) | Pop the last num values pushed on the bag. |
Bag:shift ([num=1]) | Shift the first num values unshifted into the bag. |
Bag:drop ([num=1]) | Drop the topmost num values of the Bag. |
Bag:get ([idx=1]) | Get the indexed entry. |
Bag:export () | Export a list of all the contents. |
Bag:flush () | Flush all the contents. |
Metamethods
- Bag:__index (key)
-
Lookup of missing class members.
Parameters:
- key string lookup of member
Returns:
anyRaises:
on wrong arguments
Methods
- Bag:create (...)
-
Create a new instance.
Parameters:
- ...
vararg
forwarded to
_init()
Returns:
self - ...
vararg
forwarded to
- Bag:_init (...)
-
Initialize a new instance.
Parameters:
- ... vararg pushed on the bag
Returns:
self - Bag:isEmpty ()
-
Is the bag empty.
Note that the internal structure is non-empty even if a nil
is pushed on the bag.
Returns:
boolean whether the internal structure has length zero - Bag:depth ()
-
What is the depth of the internal structure.
Note that the internal structure has a depth even if a nil
is pushed on the bag.
Returns:
number how deep is the internal structure - Bag:layout ()
-
Get the layout of the bag.
This method is used for testing to inspect which types of objects exists in the bag.
Returns:
table description of the bag - Bag:bottom ()
-
Get a reference to the bottommost item in the bag.
The bottommost item can also be described as the first item to be handled.
This method leaves the item on the bag.
Nicknames:
-
first
Returns:
any item that can be put on the bag - Bag:top ()
-
Get a reference to the topmost item in the bag.
The topmost item can also be described as the last item to be handled.
This method leaves the item on the bag.
Nicknames:
-
last
Returns:
any item that can be put on the bag - Bag:push (...)
-
Push a value on the bag.
This is stack-like semantics.
Parameters:
- ...
Returns:
self facilitate chaining - Bag:unshift (...)
-
Unshift a value into the bag.
This is queue-like semantics.
Parameters:
- ...
Returns:
self facilitate chaining - Bag:pop ([num=1])
-
Pop the last num values pushed on the bag.
This is stack-like semantics.
Note that this will remove the last pushed (topmost) value.
Parameters:
- num nil or number items to drop (default 1)
Returns:
any item that can be put on the bagRaises:
on wrong arguments - Bag:shift ([num=1])
-
Shift the first num values unshifted into the bag.
This is queue-like semantics.
Note that this will remove the first unshifted (topmost) value.
Parameters:
- num nil or number items to drop (default 1)
Returns:
any item that can be put on the bagRaises:
on wrong arguments - Bag:drop ([num=1])
-
Drop the topmost num values of the Bag.
Note that this will remove the topmost values.
Parameters:
- num nil or number items to drop (default 1)
Returns:
self facilitate chainingRaises:
on wrong arguments - Bag:get ([idx=1])
-
Get the indexed entry.
Accessing this will not change stored values.
Parameters:
- idx nil or number entry from top, negative count from bottom (default 1)
Returns:
any item that can be put in the bagRaises:
on wrong arguments - Bag:export ()
-
Export a list of all the contents.
Returns:
table list of values - Bag:flush ()
-
Flush all the contents.
Note that this clears the internal storage.
Returns:
table list of values