Class: Impromptu::OrderedSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/impromptu/ordered_set.rb

Direct Known Subclasses

ComponentSet

Instance Method Summary collapse

Constructor Details

#initializeOrderedSet

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

Returns:

  • (Boolean)


45
46
47
# File 'lib/impromptu/ordered_set.rb', line 45

def empty?
  @items_list.empty?
end

#include?(item) ⇒ Boolean

Returns:

  • (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

#sizeObject



41
42
43
# File 'lib/impromptu/ordered_set.rb', line 41

def size
  @items_list.size
end

#to_aObject



33
34
35
# File 'lib/impromptu/ordered_set.rb', line 33

def to_a
  @items_list.dup
end