skn_utils 3.3.1 → 3.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/skn_utils/lists/circular_linked_list.rb +56 -3
- data/lib/skn_utils/lists/doubly_linked_list.rb +56 -3
- data/lib/skn_utils/lists/linked_list.rb +56 -3
- data/lib/skn_utils/version.rb +1 -1
- data/spec/lib/skn_utils/lists/Circular_linked_list_spec.rb +35 -0
- data/spec/lib/skn_utils/lists/doubly_linked_list_spec.rb +35 -0
- data/spec/lib/skn_utils/lists/linked_list_spec.rb +35 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2f654cc636fb8d2694432b991fc8c145dd81d01
|
4
|
+
data.tar.gz: 3f7a28e3be152907b0b25949a371855cb739de2b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e5e65ca3ada647ad23c5a26d224b025b2e14a00e63e0f15d2ce35823c7df69e77720d43fa53c40af0abf6e5297346c98b321dd2bc244f20ed012b3633296a8bf
|
7
|
+
data.tar.gz: e68c4b224458eb5aa669fd776177c433812705e54006f0fcf12c5b122d1f4a1b9ba1f9fc5744800cb83d158bdb587492ac2eebf88fb4fedf848afe3115afb630
|
@@ -7,13 +7,18 @@ module SknUtils
|
|
7
7
|
class CircularLinkedList
|
8
8
|
attr_accessor :size
|
9
9
|
|
10
|
-
def initialize(*
|
10
|
+
def initialize(*vargs, &block)
|
11
11
|
@current = nil
|
12
12
|
@head = nil
|
13
13
|
@tail = nil
|
14
14
|
@size = 0
|
15
|
-
|
16
|
-
|
15
|
+
|
16
|
+
@sort_ascending = lambda {|a_obj,b_obj| a_obj >= b_obj}
|
17
|
+
@sort_descending = lambda {|a_obj,b_obj| a_obj <= b_obj}
|
18
|
+
@sort_condition = block_given? ? block : @sort_ascending
|
19
|
+
|
20
|
+
vargs.each {|value| insert(value) }
|
21
|
+
first if vargs.size > 1
|
17
22
|
end
|
18
23
|
|
19
24
|
#
|
@@ -189,6 +194,25 @@ module SknUtils
|
|
189
194
|
result
|
190
195
|
end
|
191
196
|
|
197
|
+
# block format: sort condition : {|a_obj,b_obj| a_obj >= b_obj}
|
198
|
+
def sort!(direction_sym=:default, &block)
|
199
|
+
active_sort_condition = block_given? ? block :
|
200
|
+
(
|
201
|
+
case direction_sym
|
202
|
+
when :asc
|
203
|
+
@sort_ascending
|
204
|
+
when :desc
|
205
|
+
@sort_descending
|
206
|
+
else
|
207
|
+
@sort_condition
|
208
|
+
end
|
209
|
+
)
|
210
|
+
sorted = merge_sort(to_a, active_sort_condition)
|
211
|
+
clear
|
212
|
+
sorted.each {|item| insert(item) }
|
213
|
+
size
|
214
|
+
end
|
215
|
+
|
192
216
|
private
|
193
217
|
|
194
218
|
attr_accessor :head, :tail
|
@@ -213,6 +237,35 @@ module SknUtils
|
|
213
237
|
node
|
214
238
|
end
|
215
239
|
|
240
|
+
# Merged Sort via Ref: http://rubyalgorithms.com/merge_sort.html
|
241
|
+
# arr is Array to be sorted, sort_cond is Proc expecting a/b params returning true/false
|
242
|
+
def merge_sort(arr, sort_cond)
|
243
|
+
return arr if arr.size < 2
|
244
|
+
|
245
|
+
middle = arr.size / 2
|
246
|
+
|
247
|
+
left = merge_sort(arr[0...middle], sort_cond)
|
248
|
+
right = merge_sort(arr[middle..arr.size], sort_cond)
|
249
|
+
|
250
|
+
merge(left, right, sort_cond)
|
251
|
+
end
|
252
|
+
|
253
|
+
def merge(left, right, sort_cond)
|
254
|
+
sorted = []
|
255
|
+
|
256
|
+
while left.any? && right.any?
|
257
|
+
|
258
|
+
if sort_cond.call(left.first, right.first) # replace this condition with a proc
|
259
|
+
sorted.push right.shift
|
260
|
+
else
|
261
|
+
sorted.push left.shift
|
262
|
+
end
|
263
|
+
|
264
|
+
end
|
265
|
+
|
266
|
+
sorted + left + right
|
267
|
+
end
|
268
|
+
|
216
269
|
end # end class
|
217
270
|
end # module
|
218
271
|
end # end module
|
@@ -8,13 +8,18 @@ module SknUtils
|
|
8
8
|
class DoublyLinkedList
|
9
9
|
attr_accessor :size
|
10
10
|
|
11
|
-
def initialize(*
|
11
|
+
def initialize(*vargs, &block)
|
12
12
|
@current = nil
|
13
13
|
@head = nil
|
14
14
|
@tail = nil
|
15
15
|
@size = 0
|
16
|
-
|
17
|
-
|
16
|
+
|
17
|
+
@sort_ascending = lambda {|a_obj,b_obj| a_obj >= b_obj}
|
18
|
+
@sort_descending = lambda {|a_obj,b_obj| a_obj <= b_obj}
|
19
|
+
@sort_condition = block_given? ? block : @sort_ascending
|
20
|
+
|
21
|
+
vargs.each {|value| insert(value) }
|
22
|
+
first if vargs.size > 1
|
18
23
|
end
|
19
24
|
|
20
25
|
#
|
@@ -187,6 +192,25 @@ module SknUtils
|
|
187
192
|
result
|
188
193
|
end
|
189
194
|
|
195
|
+
# block format: sort condition : {|a_obj,b_obj| a_obj >= b_obj}
|
196
|
+
def sort!(direction_sym=:default, &block)
|
197
|
+
active_sort_condition = block_given? ? block :
|
198
|
+
(
|
199
|
+
case direction_sym
|
200
|
+
when :asc
|
201
|
+
@sort_ascending
|
202
|
+
when :desc
|
203
|
+
@sort_descending
|
204
|
+
else
|
205
|
+
@sort_condition
|
206
|
+
end
|
207
|
+
)
|
208
|
+
sorted = merge_sort(to_a, active_sort_condition)
|
209
|
+
clear
|
210
|
+
sorted.each {|item| insert(item) }
|
211
|
+
size
|
212
|
+
end
|
213
|
+
|
190
214
|
private
|
191
215
|
|
192
216
|
attr_accessor :head, :tail
|
@@ -211,6 +235,35 @@ module SknUtils
|
|
211
235
|
node
|
212
236
|
end
|
213
237
|
|
238
|
+
# Merged Sort via Ref: http://rubyalgorithms.com/merge_sort.html
|
239
|
+
# arr is Array to be sorted, sort_cond is Proc expecting a/b params returning true/false
|
240
|
+
def merge_sort(arr, sort_cond)
|
241
|
+
return arr if arr.size < 2
|
242
|
+
|
243
|
+
middle = arr.size / 2
|
244
|
+
|
245
|
+
left = merge_sort(arr[0...middle], sort_cond)
|
246
|
+
right = merge_sort(arr[middle..arr.size], sort_cond)
|
247
|
+
|
248
|
+
merge(left, right, sort_cond)
|
249
|
+
end
|
250
|
+
|
251
|
+
def merge(left, right, sort_cond)
|
252
|
+
sorted = []
|
253
|
+
|
254
|
+
while left.any? && right.any?
|
255
|
+
|
256
|
+
if sort_cond.call(left.first, right.first) # replace this condition with a proc
|
257
|
+
sorted.push right.shift
|
258
|
+
else
|
259
|
+
sorted.push left.shift
|
260
|
+
end
|
261
|
+
|
262
|
+
end
|
263
|
+
|
264
|
+
sorted + left + right
|
265
|
+
end
|
266
|
+
|
214
267
|
end # end class
|
215
268
|
end # module
|
216
269
|
end # end module
|
@@ -8,13 +8,19 @@ module SknUtils
|
|
8
8
|
class LinkedList
|
9
9
|
attr_accessor :size
|
10
10
|
|
11
|
-
def initialize(*
|
11
|
+
def initialize(*vargs, &block)
|
12
|
+
@sort_condition = nil
|
12
13
|
@current = nil
|
13
14
|
@head = nil
|
14
15
|
@tail = nil
|
15
16
|
@size = 0
|
16
|
-
|
17
|
-
|
17
|
+
|
18
|
+
@sort_ascending = lambda {|a_obj,b_obj| a_obj >= b_obj}
|
19
|
+
@sort_descending = lambda {|a_obj,b_obj| a_obj <= b_obj}
|
20
|
+
@sort_condition = block_given? ? block : @sort_ascending
|
21
|
+
|
22
|
+
vargs.each {|value| insert(value) }
|
23
|
+
first if vargs.size > 1
|
18
24
|
end
|
19
25
|
|
20
26
|
#
|
@@ -171,6 +177,24 @@ module SknUtils
|
|
171
177
|
result
|
172
178
|
end
|
173
179
|
|
180
|
+
# block format: sort condition : {|a_obj,b_obj| a_obj >= b_obj}
|
181
|
+
def sort!(direction_sym=:default, &block)
|
182
|
+
active_sort_condition = block_given? ? block :
|
183
|
+
case direction_sym
|
184
|
+
when :asc
|
185
|
+
@sort_ascending
|
186
|
+
when :desc
|
187
|
+
@sort_descending
|
188
|
+
else
|
189
|
+
@sort_condition
|
190
|
+
end
|
191
|
+
|
192
|
+
sorted = merge_sort(to_a, active_sort_condition)
|
193
|
+
clear
|
194
|
+
sorted.each {|item| insert(item) }
|
195
|
+
size
|
196
|
+
end
|
197
|
+
|
174
198
|
private
|
175
199
|
|
176
200
|
attr_accessor :head, :tail
|
@@ -195,6 +219,35 @@ module SknUtils
|
|
195
219
|
node
|
196
220
|
end
|
197
221
|
|
222
|
+
# Merged Sort via Ref: http://rubyalgorithms.com/merge_sort.html
|
223
|
+
# arr is Array to be sorted, sort_cond is Proc expecting a/b params returning true/false
|
224
|
+
def merge_sort(arr, sort_cond)
|
225
|
+
return arr if arr.size < 2
|
226
|
+
|
227
|
+
middle = arr.size / 2
|
228
|
+
|
229
|
+
left = merge_sort(arr[0...middle], sort_cond)
|
230
|
+
right = merge_sort(arr[middle..arr.size], sort_cond)
|
231
|
+
|
232
|
+
merge(left, right, sort_cond)
|
233
|
+
end
|
234
|
+
|
235
|
+
def merge(left, right, sort_cond)
|
236
|
+
sorted = []
|
237
|
+
|
238
|
+
while left.any? && right.any?
|
239
|
+
|
240
|
+
if sort_cond.call(left.first, right.first) # replace this condition with a proc
|
241
|
+
sorted.push right.shift
|
242
|
+
else
|
243
|
+
sorted.push left.shift
|
244
|
+
end
|
245
|
+
|
246
|
+
end
|
247
|
+
|
248
|
+
sorted + left + right
|
249
|
+
end
|
250
|
+
|
198
251
|
end # end class
|
199
252
|
end # end module
|
200
253
|
end # end module
|
data/lib/skn_utils/version.rb
CHANGED
@@ -236,7 +236,42 @@ RSpec.describe SknUtils::Lists::CircularLinkedList, "Circular LinkedList " do
|
|
236
236
|
expect(only.nth(1)).to be nil
|
237
237
|
expect(only.nth(-1)).to be nil
|
238
238
|
end
|
239
|
+
end
|
240
|
+
|
241
|
+
context "Sort Feature" do
|
242
|
+
let(:num_list) { described_class.new(100, 50, 10, 40, 80, 30, 60, 90, 70, 20, 110) }
|
243
|
+
let(:alpha_list) { described_class.new('Z', 'K', 'S', 'n', 's', 'z', 'k', 'N', 'o', 'A') }
|
244
|
+
let(:hash_list) { described_class.new({key: 'Z'}, {key: 'K'}, {key: 'S'}, {key: 'n'}, {key: 's'},
|
245
|
+
{key: 'z'}, {key: 'k'}, {key: 'N'}, {key: 'o'}, {key: 'A'}
|
246
|
+
) {|a,b| a[:key] >= b[:key] }
|
247
|
+
}
|
239
248
|
|
249
|
+
it "#sort! redefines numeric list in asending order" do
|
250
|
+
expect(num_list.sort!).to eq(11)
|
251
|
+
expect(num_list.to_a).to eq([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110])
|
252
|
+
end
|
253
|
+
it "#sort!(:desc) redefines numeric list in descending order" do
|
254
|
+
expect(num_list.sort!(:desc)).to eq(11)
|
255
|
+
expect(num_list.to_a).to eq([110, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10])
|
256
|
+
end
|
257
|
+
it "#sort! redefines alpha numeric list in asending order" do
|
258
|
+
expect(alpha_list.sort!).to eq(10)
|
259
|
+
expect(alpha_list.to_a).to eq(["A", "K", "N", "S", "Z", "k", "n", "o", "s", "z"])
|
260
|
+
end
|
261
|
+
it "#sort!(:desc) redefines alpha numeric list in descending order" do
|
262
|
+
expect(alpha_list.sort!(:desc)).to eq(10)
|
263
|
+
expect(alpha_list.to_a).to eq(["z", "s", "o", "n", "k", "Z", "S", "N", "K", "A"])
|
264
|
+
end
|
265
|
+
it "#sort!() redefines hash object values in default order" do
|
266
|
+
expect(hash_list.sort!).to eq(10)
|
267
|
+
expect(hash_list.to_a).to eq([{:key=>"A"}, {:key=>"K"}, {:key=>"N"}, {:key=>"S"}, {:key=>"Z"},
|
268
|
+
{:key=>"k"}, {:key=>"n"}, {:key=>"o"}, {:key=>"s"}, {:key=>"z"}])
|
269
|
+
end
|
270
|
+
it "#sort!() lambda overrides sort_condifiton and sorts hash object values in custom order" do
|
271
|
+
expect(hash_list.sort!() {|a,b| a[:key] <= b[:key] }).to eq(10)
|
272
|
+
expect(hash_list.to_a).to eq([{:key=>"z"}, {:key=>"s"}, {:key=>"o"}, {:key=>"n"}, {:key=>"k"},
|
273
|
+
{:key=>"Z"}, {:key=>"S"}, {:key=>"N"}, {:key=>"K"}, {:key=>"A"}])
|
274
|
+
end
|
240
275
|
end
|
241
276
|
|
242
277
|
end
|
@@ -236,7 +236,42 @@ RSpec.describe SknUtils::Lists::DoublyLinkedList, "Double-Ended LinkedList " do
|
|
236
236
|
expect(only.nth(1)).to be nil
|
237
237
|
expect(only.nth(-1)).to be nil
|
238
238
|
end
|
239
|
+
end
|
240
|
+
|
241
|
+
context "Sort Feature" do
|
242
|
+
let(:num_list) { described_class.new(100, 50, 10, 40, 80, 30, 60, 90, 70, 20, 110) }
|
243
|
+
let(:alpha_list) { described_class.new('Z', 'K', 'S', 'n', 's', 'z', 'k', 'N', 'o', 'A') }
|
244
|
+
let(:hash_list) { described_class.new({key: 'Z'}, {key: 'K'}, {key: 'S'}, {key: 'n'}, {key: 's'},
|
245
|
+
{key: 'z'}, {key: 'k'}, {key: 'N'}, {key: 'o'}, {key: 'A'}
|
246
|
+
) {|a,b| a[:key] >= b[:key] }
|
247
|
+
}
|
239
248
|
|
249
|
+
it "#sort! redefines numeric list in asending order" do
|
250
|
+
expect(num_list.sort!).to eq(11)
|
251
|
+
expect(num_list.to_a).to eq([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110])
|
252
|
+
end
|
253
|
+
it "#sort!(:desc) redefines numeric list in descending order" do
|
254
|
+
expect(num_list.sort!(:desc)).to eq(11)
|
255
|
+
expect(num_list.to_a).to eq([110, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10])
|
256
|
+
end
|
257
|
+
it "#sort! redefines alpha numeric list in asending order" do
|
258
|
+
expect(alpha_list.sort!).to eq(10)
|
259
|
+
expect(alpha_list.to_a).to eq(["A", "K", "N", "S", "Z", "k", "n", "o", "s", "z"])
|
260
|
+
end
|
261
|
+
it "#sort!(:desc) redefines alpha numeric list in descending order" do
|
262
|
+
expect(alpha_list.sort!(:desc)).to eq(10)
|
263
|
+
expect(alpha_list.to_a).to eq(["z", "s", "o", "n", "k", "Z", "S", "N", "K", "A"])
|
264
|
+
end
|
265
|
+
it "#sort!() redefines hash object values in default order" do
|
266
|
+
expect(hash_list.sort!).to eq(10)
|
267
|
+
expect(hash_list.to_a).to eq([{:key=>"A"}, {:key=>"K"}, {:key=>"N"}, {:key=>"S"}, {:key=>"Z"},
|
268
|
+
{:key=>"k"}, {:key=>"n"}, {:key=>"o"}, {:key=>"s"}, {:key=>"z"}])
|
269
|
+
end
|
270
|
+
it "#sort!() lambda overrides sort_condifiton and sorts hash object values in custom order" do
|
271
|
+
expect(hash_list.sort!() {|a,b| a[:key] <= b[:key] }).to eq(10)
|
272
|
+
expect(hash_list.to_a).to eq([{:key=>"z"}, {:key=>"s"}, {:key=>"o"}, {:key=>"n"}, {:key=>"k"},
|
273
|
+
{:key=>"Z"}, {:key=>"S"}, {:key=>"N"}, {:key=>"K"}, {:key=>"A"}])
|
274
|
+
end
|
240
275
|
end
|
241
276
|
|
242
277
|
end
|
@@ -229,7 +229,42 @@ RSpec.describe SknUtils::Lists::LinkedList, "Singular LinkedList " do
|
|
229
229
|
expect(only.nth(1)).to be nil
|
230
230
|
expect(only.nth(-1)).to be nil
|
231
231
|
end
|
232
|
+
end
|
233
|
+
|
234
|
+
context "Sort Feature" do
|
235
|
+
let(:num_list) { described_class.new(100, 50, 10, 40, 80, 30, 60, 90, 70, 20, 110) }
|
236
|
+
let(:alpha_list) { described_class.new('Z', 'K', 'S', 'n', 's', 'z', 'k', 'N', 'o', 'A') }
|
237
|
+
let(:hash_list) { described_class.new({key: 'Z'}, {key: 'K'}, {key: 'S'}, {key: 'n'}, {key: 's'},
|
238
|
+
{key: 'z'}, {key: 'k'}, {key: 'N'}, {key: 'o'}, {key: 'A'}
|
239
|
+
) {|a,b| a[:key] >= b[:key] }
|
240
|
+
}
|
232
241
|
|
242
|
+
it "#sort! redefines numeric list in asending order" do
|
243
|
+
expect(num_list.sort!).to eq(11)
|
244
|
+
expect(num_list.to_a).to eq([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110])
|
245
|
+
end
|
246
|
+
it "#sort!(:desc) redefines numeric list in descending order" do
|
247
|
+
expect(num_list.sort!(:desc)).to eq(11)
|
248
|
+
expect(num_list.to_a).to eq([110, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10])
|
249
|
+
end
|
250
|
+
it "#sort! redefines alpha numeric list in asending order" do
|
251
|
+
expect(alpha_list.sort!).to eq(10)
|
252
|
+
expect(alpha_list.to_a).to eq(["A", "K", "N", "S", "Z", "k", "n", "o", "s", "z"])
|
253
|
+
end
|
254
|
+
it "#sort!(:desc) redefines alpha numeric list in descending order" do
|
255
|
+
expect(alpha_list.sort!(:desc)).to eq(10)
|
256
|
+
expect(alpha_list.to_a).to eq(["z", "s", "o", "n", "k", "Z", "S", "N", "K", "A"])
|
257
|
+
end
|
258
|
+
it "#sort!() redefines hash object values in default order" do
|
259
|
+
expect(hash_list.sort!).to eq(10)
|
260
|
+
expect(hash_list.to_a).to eq([{:key=>"A"}, {:key=>"K"}, {:key=>"N"}, {:key=>"S"}, {:key=>"Z"},
|
261
|
+
{:key=>"k"}, {:key=>"n"}, {:key=>"o"}, {:key=>"s"}, {:key=>"z"}])
|
262
|
+
end
|
263
|
+
it "#sort!() lambda overrides sort_condifiton and sorts hash object values in custom order" do
|
264
|
+
expect(hash_list.sort!() {|a,b| a[:key] <= b[:key] }).to eq(10)
|
265
|
+
expect(hash_list.to_a).to eq([{:key=>"z"}, {:key=>"s"}, {:key=>"o"}, {:key=>"n"}, {:key=>"k"},
|
266
|
+
{:key=>"Z"}, {:key=>"S"}, {:key=>"N"}, {:key=>"K"}, {:key=>"A"}])
|
267
|
+
end
|
233
268
|
end
|
234
269
|
|
235
270
|
end
|