Class: Impromptu::OrderedSet
- Inherits:
-
Object
- Object
- Impromptu::OrderedSet
- Includes:
- Enumerable
- Defined in:
- lib/impromptu/ordered_set.rb
Direct Known Subclasses
Instance Method Summary collapse
- #<<(item) ⇒ Object (also: #push)
- #delete(item) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #include?(item) ⇒ Boolean
-
#initialize ⇒ OrderedSet
constructor
A new instance of OrderedSet.
- #merge(items) ⇒ Object
- #size ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize ⇒ OrderedSet
Returns a new instance of OrderedSet.
5 6 7 8 |
# File 'lib/impromptu/ordered_set.rb', line 5 def initialize @items_hash = {} @items_list = [] end |
Instance Method Details
#<<(item) ⇒ Object Also known as: push
10 11 12 13 14 15 16 |
# File 'lib/impromptu/ordered_set.rb', line 10 def <<(item) unless self.include?(item) @items_hash[item] = item @items_list << item end @items_hash[item] end |
#delete(item) ⇒ Object
23 24 25 26 27 |
# File 'lib/impromptu/ordered_set.rb', line 23 def delete(item) self.include?(item) or return nil @items_hash.delete(item) @items_list.delete(item) end |
#each(&block) ⇒ Object
37 38 39 |
# File 'lib/impromptu/ordered_set.rb', line 37 def each(&block) @items_list.each {|item| yield item} end |
#empty? ⇒ Boolean
45 46 47 |
# File 'lib/impromptu/ordered_set.rb', line 45 def empty? @items_list.empty? end |
#include?(item) ⇒ Boolean
29 30 31 |
# File 'lib/impromptu/ordered_set.rb', line 29 def include?(item) @items_hash.has_key?(item) end |
#merge(items) ⇒ Object
19 20 21 |
# File 'lib/impromptu/ordered_set.rb', line 19 def merge(items) items.each {|item| self.<< item} end |
#size ⇒ Object
41 42 43 |
# File 'lib/impromptu/ordered_set.rb', line 41 def size @items_list.size end |
#to_a ⇒ Object
33 34 35 |
# File 'lib/impromptu/ordered_set.rb', line 33 def to_a @items_list.dup end |