Class: SknUtils::Lists::LinkedCommons
- Inherits:
-
Object
- Object
- SknUtils::Lists::LinkedCommons
- Defined in:
- lib/skn_utils/lists/linked_commons.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#size ⇒ Object
Returns the value of attribute size.
Class Method Summary collapse
-
.call(*vargs, &compare_key_proc) ⇒ Object
Initialize and return first node if nodes are available.
Instance Method Summary collapse
-
#append(value) ⇒ Object
return new size.
-
#at_index(index) ⇒ Object
return node at positive index from head.
-
#clear ⇒ Object
return number cleared.
- #current ⇒ Object
-
#each(&block) ⇒ Object
perform each() or return enumerator.
- #empty? ⇒ Boolean
-
#first ⇒ Object
return values and position current to last node accessed prevent @current from nil assignment.
-
#initialize(*vargs, &compare_key_proc) ⇒ LinkedCommons
constructor
A new instance of LinkedCommons.
-
#insert(value) ⇒ Object
return new size.
- #last ⇒ Object
- #next ⇒ Object
-
#prepend(value) ⇒ Object
return new size.
-
#sort!(direction_sym = :default, &compare_sort_proc) ⇒ Object
block format: sort condition : {|a_obj,b_obj| a_obj >= b_obj}.
-
#to_a ⇒ Object
convert self to a value array.
Constructor Details
#initialize(*vargs, &compare_key_proc) ⇒ LinkedCommons
Returns a new instance of LinkedCommons.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 23 def initialize(*vargs, &compare_key_proc) @current = nil @head = nil @tail = nil @size = 0 @match_value = block_given? ? compare_key_proc : lambda {|obj| obj } @sort_ascending = lambda {|a_obj,b_obj| @match_value.call(a_obj) >= @match_value.call(b_obj)} @sort_descending = lambda {|a_obj,b_obj| @match_value.call(a_obj) <= @match_value.call(b_obj)} @sort_condition = @sort_ascending vargs.each {|value| insert(value) } first if vargs.size > 1 end |
Instance Attribute Details
#size ⇒ Object
Returns the value of attribute size.
14 15 16 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 14 def size @size end |
Class Method Details
.call(*vargs, &compare_key_proc) ⇒ Object
Initialize and return first node if nodes are available
17 18 19 20 21 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 17 def self.call(*vargs, &compare_key_proc) target = self.new(*vargs, &compare_key_proc) return target.instance_variable_get(:@current) if vargs.size > 1 target end |
Instance Method Details
#append(value) ⇒ Object
return new size
100 101 102 103 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 100 def append(value) temp = self.tail.value rescue nil insert_after(temp, value) end |
#at_index(index) ⇒ Object
return node at positive index from head
61 62 63 64 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 61 def at_index(index) find_by_index(index) current end |
#clear ⇒ Object
return number cleared
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 71 def clear rc = 0 node = self.head position = node while node do node = node.remove! rc += 1 break if position.equal?(node) end @current = nil self.head = nil self.tail = nil self.size = 0 rc end |
#current ⇒ Object
51 52 53 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 51 def current @current.value rescue nil end |
#each(&block) ⇒ Object
perform each() or return enumerator
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 110 def each(&block) @current = self.head position = self.head if block_given? while position do block.call( position.value.dup ) position = position.next break if position.equal?(@current) end else Enumerator.new do |yielder| while position do yielder << position.value.dup position = position.next break if position.equal?(@current) end end end end |
#empty? ⇒ Boolean
66 67 68 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 66 def empty? self.size == 0 end |
#first ⇒ Object
return values and position current to last node accessed prevent @current from nil assignment
41 42 43 44 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 41 def first @current = self.head if self.head @current.value rescue nil end |
#insert(value) ⇒ Object
return new size
89 90 91 92 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 89 def insert(value) temp = @current.value rescue nil insert_after(temp, value) end |
#last ⇒ Object
55 56 57 58 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 55 def last @current = self.tail if self.tail @current.value rescue nil end |
#next ⇒ Object
46 47 48 49 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 46 def next @current = @current.next if @current and @current.next @current.value rescue nil end |
#prepend(value) ⇒ Object
return new size
95 96 97 98 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 95 def prepend(value) temp = self.head.value rescue nil insert_before(temp, value) end |
#sort!(direction_sym = :default, &compare_sort_proc) ⇒ Object
block format: sort condition : {|a_obj,b_obj| a_obj >= b_obj}
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 144 def sort!(direction_sym=:default, &compare_sort_proc) @active_sort_condition = block_given? ? compare_sort_proc : case direction_sym when :asc @sort_ascending when :desc @sort_descending else @sort_condition end sorted = merge_sort(self.to_a) clear sorted.each {|item| insert(item) } self.size end |
#to_a ⇒ Object
convert self to a value array
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 131 def to_a @current = self.head position = self.head result = [] while position do result << position.value.dup position = position.next break if position.equal?(@current) end result end |