linked 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/linked/item.rb +35 -0
- data/lib/linked/list.rb +49 -2
- data/lib/linked/version.rb +3 -1
- data/lib/linked.rb +2 -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: 513dbae22fea14a370f2181ef4da797264121631
|
4
|
+
data.tar.gz: 685eb16cb3380aec648af578118b54416af20772
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b3cfa2318674d45841c7a075250cb88d1095a5df6c56a270eda3bf60bce06cd41db8612f1d1df520cd9ab32ded2f73aedb36f71e1fb410c7d875e77f4d128e62
|
7
|
+
data.tar.gz: 6557225296323c2ff767766bde4ea13067e39103bc84a9e3fb43229991ca65156373475ad7091412174608c11c4b7bdf889274d96d975e9fee3bd493a1f8aab0
|
data/lib/linked/item.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Linked
|
2
4
|
# Item
|
3
5
|
#
|
@@ -54,6 +56,21 @@ module Linked
|
|
54
56
|
@prev = nil
|
55
57
|
end
|
56
58
|
end
|
59
|
+
|
60
|
+
# Calling #dup on an item returns a copy that is no longer connected to the
|
61
|
+
# original item chain, or the list. The value will also be copied.
|
62
|
+
#
|
63
|
+
# Returns a new Item.
|
64
|
+
|
65
|
+
def initialize_dup(source)
|
66
|
+
@next = @prev = @list = nil
|
67
|
+
@value = begin
|
68
|
+
source.value.dup
|
69
|
+
rescue TypeError
|
70
|
+
source.value
|
71
|
+
end
|
72
|
+
super
|
73
|
+
end
|
57
74
|
|
58
75
|
# Check if this is the first item in the list. It is crucial that tail#nil?
|
59
76
|
# returns true for the first item to be identified correctly.
|
@@ -309,5 +326,23 @@ module Linked
|
|
309
326
|
item = item.next
|
310
327
|
end
|
311
328
|
end
|
329
|
+
|
330
|
+
# Freezes the value, as well as making the list item itself immutable.
|
331
|
+
|
332
|
+
def freeze
|
333
|
+
value.freeze
|
334
|
+
super
|
335
|
+
end
|
336
|
+
|
337
|
+
# The default #inspect method becomes very cluttered the moment you start
|
338
|
+
# liking objects together. This implementation fixes that and only shows the
|
339
|
+
# class name, object id and value (if set).
|
340
|
+
|
341
|
+
def inspect
|
342
|
+
return yield self if block_given?
|
343
|
+
|
344
|
+
output = format '%s:0x%0x', self.class.name, object_id
|
345
|
+
value ? output + " value=#{value.inspect}" : output
|
346
|
+
end
|
312
347
|
end
|
313
348
|
end
|
data/lib/linked/list.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Linked
|
2
4
|
# List
|
3
5
|
#
|
@@ -42,10 +44,23 @@ module Linked
|
|
42
44
|
# directly.
|
43
45
|
|
44
46
|
def initialize(*)
|
47
|
+
@eol = EOL.new list: self
|
48
|
+
@item_count = 0
|
49
|
+
|
45
50
|
super
|
46
|
-
|
51
|
+
end
|
52
|
+
|
53
|
+
# When copying a list its entire item chain needs to be copied as well.
|
54
|
+
# Therefore #dup will be called on each of the original lists items, making
|
55
|
+
# this operation quite expensive.
|
56
|
+
|
57
|
+
def initialize_dup(source)
|
47
58
|
@eol = EOL.new list: self
|
48
59
|
@item_count = 0
|
60
|
+
|
61
|
+
source.each_item { |item| push item.dup }
|
62
|
+
|
63
|
+
super
|
49
64
|
end
|
50
65
|
|
51
66
|
# Access the first n item(s) in the list.
|
@@ -171,13 +186,45 @@ module Linked
|
|
171
186
|
#
|
172
187
|
# reverse - flips the iteration order if true.
|
173
188
|
|
174
|
-
def
|
189
|
+
def each_item(reverse: false, &block)
|
175
190
|
if reverse
|
176
191
|
eol.before(&block)
|
177
192
|
else
|
178
193
|
eol.after(&block)
|
179
194
|
end
|
180
195
|
end
|
196
|
+
|
197
|
+
alias each each_item
|
198
|
+
|
199
|
+
# Calls #freeze on all items in the list, as well as the head and the tail
|
200
|
+
# (eol).
|
201
|
+
|
202
|
+
def freeze
|
203
|
+
eol.freeze
|
204
|
+
each_item(&:freeze)
|
205
|
+
super
|
206
|
+
end
|
207
|
+
|
208
|
+
# Overrides the default inspect method to provide a more useful view of the
|
209
|
+
# list.
|
210
|
+
#
|
211
|
+
# Importantly this implementation supports nested lists and will return a
|
212
|
+
# tree like structure.
|
213
|
+
|
214
|
+
def inspect(&block)
|
215
|
+
# Get the parents inspect output
|
216
|
+
res = [super]
|
217
|
+
|
218
|
+
each_item do |item|
|
219
|
+
lines = item.inspect(&block).split "\n"
|
220
|
+
|
221
|
+
res.push (item.last? ? '└─╴' : '├─╴') + lines.shift
|
222
|
+
padding = item.last? ? ' ' : '│ '
|
223
|
+
lines.each { |line| res.push padding + line }
|
224
|
+
end
|
225
|
+
|
226
|
+
res.join("\n")
|
227
|
+
end
|
181
228
|
|
182
229
|
# Internal method to grow the list with n elements. Never call this method
|
183
230
|
# without also inserting the n elements.
|
data/lib/linked/version.rb
CHANGED
data/lib/linked.rb
CHANGED