class SillyList::UniqLifo
Class UniqLifo
designed to manage Last In First Out array (stack) It ensures that items are uniq.
The array can have a max size or be illimited (then max_size
must be 0)
@example
my_array = SillyList::UniqLifo.new([1,2,3], max_size: 2) my_array.list #=> [1,2] my_array.add(3) my_array.list #=> [3,1] my_array.add([4,5]) my_array.list #=> [4,5] my_array.remove #=> 4 my_array.list #=> [5]
Attributes
Public Class Methods
# File lib/silly_list/uniq_lifo.rb, line 20 def initialize(list=[], max_size: 0) @list = init_list(list, max_size) @max_size = max_size end
Public Instance Methods
Adds an item to the list
@param [<Object, Array>] object to be added @return [UniqLifo]
# File lib/silly_list/uniq_lifo.rb, line 36 def add(item) return unless item delete(item) add_at_beginning(item) remove_last if size_exceeded? self end
Checks if list is empty
@return [Boolean]
# File lib/silly_list/uniq_lifo.rb, line 28 def empty? list.empty? end
Deletes the first item of the list
@return [Object] removed item
# File lib/silly_list/uniq_lifo.rb, line 49 def remove list.delete_at(0) end
Private Instance Methods
Insert item at the beginning of the list
@param [Object] item to be added @return [Array<Object>] list
# File lib/silly_list/uniq_lifo.rb, line 84 def add_at_beginning(item) list.unshift(*item) end
Delete all the occurence of item in the list
@param [Object] item to be deleted @return [Object, nil] deleted item or nil
# File lib/silly_list/uniq_lifo.rb, line 76 def delete(item) list.delete(item) end
Ensures that the list does not exceed the max size
@param [Array<Object>] list to be initialized @param [Integer] max size @return [Array<Object>] list
# File lib/silly_list/uniq_lifo.rb, line 68 def init_list(list, max_size) list[0..max_size-1] end
Removes the excess of items
@return [Array<Object>] array of removed item
# File lib/silly_list/uniq_lifo.rb, line 58 def remove_last index = list.size - max_size list.pop(index) end
Checks if list have exceeded the max size
@return [Boolean]
# File lib/silly_list/uniq_lifo.rb, line 91 def size_exceeded? return false if max_size.zero? list.size > max_size end