Class: OrderedSet
- Inherits:
-
Object
- Object
- OrderedSet
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/ordered_set.rb
Instance Method Summary collapse
- #<<(item) ⇒ Object
- #add!(items) ⇒ Object (also: #concat)
- #clear ⇒ Object
- #delete(item) ⇒ Object
- #delete_at(index, &block) ⇒ Object
- #include?(item) ⇒ Boolean
- #index(item) ⇒ Object
-
#initialize(items = []) ⇒ OrderedSet
constructor
A new instance of OrderedSet.
- #intersect!(items) ⇒ Object
- #limit!(limit, offset = 0) ⇒ Object
- #reorder!(items) ⇒ Object
- #replace(items) ⇒ Object
- #reverse_reorder!(items) ⇒ Object
- #select! ⇒ Object
- #shuffle! ⇒ Object
-
#slice!(*args) ⇒ Object
Array#slice! is somewhat inconsistent with the other bang methods, and this emulates that.
- #subtract!(items) ⇒ Object
- #to_ary ⇒ Object
- #unshift(item) ⇒ Object
- #unshift!(items) ⇒ Object
Methods included from Enumerable
Constructor Details
#initialize(items = []) ⇒ OrderedSet
Returns a new instance of OrderedSet.
11 12 13 14 15 |
# File 'lib/ordered_set.rb', line 11 def initialize(items = []) @order = [] @position = {} replace(items) end |
Instance Method Details
#<<(item) ⇒ Object
37 38 39 40 41 |
# File 'lib/ordered_set.rb', line 37 def <<(item) return if include?(item) @position[item] = @order.size @order << item end |
#add!(items) ⇒ Object Also known as: concat
44 45 46 47 48 49 |
# File 'lib/ordered_set.rb', line 44 def add!(items) items.each do |item| self << item end self end |
#clear ⇒ Object
28 29 30 31 |
# File 'lib/ordered_set.rb', line 28 def clear @order.clear @position.clear end |
#delete(item) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/ordered_set.rb', line 105 def delete(item) @order.delete(item) do return block_given? ? yield : nil end reindex item end |
#delete_at(index, &block) ⇒ Object
113 114 115 |
# File 'lib/ordered_set.rb', line 113 def delete_at(index, &block) delete(self[index], &block) end |
#include?(item) ⇒ Boolean
33 34 35 |
# File 'lib/ordered_set.rb', line 33 def include?(item) @position.has_key?(item) end |
#index(item) ⇒ Object
101 102 103 |
# File 'lib/ordered_set.rb', line 101 def index(item) @position[item] end |
#intersect!(items) ⇒ Object
75 76 77 78 79 |
# File 'lib/ordered_set.rb', line 75 def intersect!(items) @order.replace(self.to_a & items.to_a) reindex self end |
#limit!(limit, offset = 0) ⇒ Object
140 141 142 143 144 |
# File 'lib/ordered_set.rb', line 140 def limit!(limit, offset = 0) new_order = @order.slice(offset, limit) || [] replace(new_order) self end |
#reorder!(items) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/ordered_set.rb', line 81 def reorder!(items) new_order = (items.to_a & self.to_a) + self.to_a @order.replace(new_order.uniq) reindex self end |
#replace(items) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/ordered_set.rb', line 20 def replace(items) clear items.each do |item| self << item end self end |
#reverse_reorder!(items) ⇒ Object
92 93 94 95 |
# File 'lib/ordered_set.rb', line 92 def reverse_reorder!(items) return self if items.empty? reverse!.reorder!(items).reverse! end |
#select! ⇒ Object
127 128 129 |
# File 'lib/ordered_set.rb', line 127 def select! reject! {|item| not yield(item)} end |
#shuffle! ⇒ Object
88 89 90 |
# File 'lib/ordered_set.rb', line 88 def shuffle! sort_by! { rand } end |
#slice!(*args) ⇒ Object
Array#slice! is somewhat inconsistent with the other bang methods, and this emulates that. For the more consistent behavior use limit!
133 134 135 136 137 |
# File 'lib/ordered_set.rb', line 133 def slice!(*args) removed_slice = @order.slice!(*args) reindex removed_slice end |
#subtract!(items) ⇒ Object
68 69 70 71 72 |
# File 'lib/ordered_set.rb', line 68 def subtract!(items) @order.replace(self.to_a - items.to_a) reindex self end |
#to_ary ⇒ Object
97 98 99 |
# File 'lib/ordered_set.rb', line 97 def to_ary @order.dup end |
#unshift(item) ⇒ Object
63 64 65 |
# File 'lib/ordered_set.rb', line 63 def unshift(item) unshift!([item]) end |
#unshift!(items) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ordered_set.rb', line 52 def unshift!(items) need_reindex = false items.each do |item| next if include?(item) @order.unshift(item) need_reindex = true # Need to recalculate the positions. end reindex if need_reindex self end |