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, else class instance.
Instance Method Summary collapse
-
#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.
- #last ⇒ Object
- #next ⇒ Object
-
#nodes ⇒ Object
Access to current node as anchor for Node based operations if initialized without vargs, this is only way to access a node.
-
#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.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 41 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.
32 33 34 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 32 def size @size end |
Class Method Details
.call(*vargs, &compare_key_proc) ⇒ Object
Initialize and return first node if nodes are available, else class instance
35 36 37 38 39 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 35 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
#at_index(index) ⇒ Object
return node at positive index from head
79 80 81 82 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 79 def at_index(index) find_by_index(index) current end |
#clear ⇒ Object
return number cleared
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 95 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
69 70 71 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 69 def current @current.value rescue nil end |
#each(&block) ⇒ Object
perform each() or return enumerator
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 123 def each(&block) @current = self.head position = @current 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
90 91 92 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 90 def empty? self.size == 0 end |
#first ⇒ Object
return values and position current to last node accessed prevent @current from nil assignment
59 60 61 62 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 59 def first @current = self.head if self.head @current.value rescue nil end |
#last ⇒ Object
73 74 75 76 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 73 def last @current = self.tail if self.tail @current.value rescue nil end |
#next ⇒ Object
64 65 66 67 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 64 def next @current = @current.next if @current and @current.next @current.value rescue nil end |
#nodes ⇒ Object
Access to current node as anchor for Node based operations if initialized without vargs, this is only way to access a node
86 87 88 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 86 def nodes @current end |
#prepend(value) ⇒ Object
return new size
113 114 115 116 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 113 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}
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 157 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
144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/skn_utils/lists/linked_commons.rb', line 144 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 |