linked 0.1.2 → 0.1.3
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 +10 -0
- data/lib/linked/list.rb +45 -24
- data/lib/linked/version.rb +1 -1
- 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: b8501da277d54cd430b7caec75a20df3670cf0a7
|
4
|
+
data.tar.gz: f027fcee69636bd7c3cef7a9b1d34d54d69d104e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8699f64f9331749e745ed4f793f1cc9b372459ac2c6644cc34dee7ae8baf8150682c19934a9017dc03fc3cd50da3f2806631c38301b8627340afa20a0d37fd55
|
7
|
+
data.tar.gz: a70747dae1d050e30229393e2df5cc8fc0075ae29fcb969233772083447e1071bcd179243ab1f5ce64bb45e77c35efe4e446cd608919407b68e465f8ce2b7bfe
|
data/lib/linked/item.rb
CHANGED
@@ -89,6 +89,16 @@ module Linked
|
|
89
89
|
def last?
|
90
90
|
@next.nil?
|
91
91
|
end
|
92
|
+
|
93
|
+
# Check if the item is in the given list.
|
94
|
+
#
|
95
|
+
# list - any object.
|
96
|
+
#
|
97
|
+
# Returns true if the item is part of the given list.
|
98
|
+
|
99
|
+
def in?(list)
|
100
|
+
@list.equal? list
|
101
|
+
end
|
92
102
|
|
93
103
|
# Access the next item in the list. If this is the last one a StopIteration
|
94
104
|
# will be raised, so that items may be iterated over safely in a loop.
|
data/lib/linked/list.rb
CHANGED
@@ -79,7 +79,7 @@ module Linked
|
|
79
79
|
def first(n = 1)
|
80
80
|
raise ArgumentError, 'n cannot be negative' if n < 0
|
81
81
|
|
82
|
-
return first_item_after eol,
|
82
|
+
return first_item_after eol, n, count unless block_given?
|
83
83
|
|
84
84
|
item = eol
|
85
85
|
items_left = count
|
@@ -90,7 +90,7 @@ module Linked
|
|
90
90
|
items_left -= 1
|
91
91
|
end
|
92
92
|
|
93
|
-
first_item_after item,
|
93
|
+
first_item_after item, n, items_left
|
94
94
|
end
|
95
95
|
|
96
96
|
# Access the last n item(s) in the list. The items will retain thier order.
|
@@ -109,7 +109,7 @@ module Linked
|
|
109
109
|
def last(n = 1)
|
110
110
|
raise ArgumentError, 'n cannot be negative' if n < 0
|
111
111
|
|
112
|
-
return last_item_before eol,
|
112
|
+
return last_item_before eol, n, count unless block_given?
|
113
113
|
|
114
114
|
item = eol
|
115
115
|
items_left = count
|
@@ -120,7 +120,7 @@ module Linked
|
|
120
120
|
items_left -= 1
|
121
121
|
end
|
122
122
|
|
123
|
-
last_item_before item,
|
123
|
+
last_item_before item, n, items_left
|
124
124
|
end
|
125
125
|
|
126
126
|
# Overrides the Enumerable#count method when given no argument to provide a
|
@@ -194,6 +194,18 @@ module Linked
|
|
194
194
|
return nil if empty?
|
195
195
|
first.delete
|
196
196
|
end
|
197
|
+
|
198
|
+
# Check if an item is in the list.
|
199
|
+
#
|
200
|
+
# item - Item, or any object that may be in the list.
|
201
|
+
#
|
202
|
+
# Returns true if the given item is in the list, otherwise false.
|
203
|
+
|
204
|
+
def include?(item)
|
205
|
+
item.in? self
|
206
|
+
rescue NoMethodError
|
207
|
+
false
|
208
|
+
end
|
197
209
|
|
198
210
|
# Iterates over each item in the list, either in normal or reverse order. If
|
199
211
|
# a block is not given an enumerator is returned.
|
@@ -273,59 +285,68 @@ module Linked
|
|
273
285
|
@item_count -= n
|
274
286
|
end
|
275
287
|
|
276
|
-
#
|
277
|
-
# item,
|
278
|
-
#
|
288
|
+
# Protected helper method that returns the first n items, starting just
|
289
|
+
# after item, given that there are items_left items left. Knowing the exact
|
290
|
+
# number of items left is not cruicial but does impact speed. The number
|
291
|
+
# should not be lower than the actual ammount. The following must
|
292
|
+
# hold for the output to be valid:
|
279
293
|
# a) n > 0
|
280
294
|
# b) there are at least items_left items left
|
281
295
|
#
|
282
|
-
# item - the Item just before the item to start from
|
283
|
-
# items_left - the number of items left.
|
296
|
+
# item - the Item just before the item to start from.
|
284
297
|
# n - the number of items to return.
|
298
|
+
# items_left - the number of items left.
|
285
299
|
#
|
286
300
|
# Returns, for different values of n:
|
287
301
|
# n == 0) nil
|
288
302
|
# n == 1) an item if items_left > 0 or nil
|
289
303
|
# n > 1) an array of items if items_left > 0 or an empty array
|
290
304
|
|
291
|
-
|
305
|
+
protected def first_item_after(item, n, items_left = @item_count)
|
292
306
|
# Optimize for these cases
|
293
307
|
return nil if n == 0
|
308
|
+
return n > 1 ? [] : nil if item.next!.nil?
|
294
309
|
return item.next if n == 1
|
310
|
+
|
311
|
+
n = items_left if n > items_left
|
295
312
|
|
296
|
-
|
313
|
+
arr = Array.new n
|
314
|
+
n.times { |i| arr[i] = item = item.next }
|
315
|
+
arr
|
297
316
|
rescue StopIteration
|
298
|
-
|
317
|
+
arr.compact! || arr
|
299
318
|
end
|
300
319
|
|
301
|
-
#
|
302
|
-
# item, given that there are items_left items left.
|
303
|
-
#
|
320
|
+
# Protected helper method that returns the last n items, ending just before
|
321
|
+
# item, given that there are items_left items left. Knowing the exact
|
322
|
+
# number of items left is not cruicial but does impact speed. The number
|
323
|
+
# should not be lower than the actual ammount. The following must hold for
|
324
|
+
# the output to be valid:
|
304
325
|
# a) n > 0
|
305
326
|
# b) there are at least items_left items left
|
306
327
|
#
|
307
328
|
# item - the Item just after the item to start from.
|
308
|
-
# items_left - the number of items left.
|
309
329
|
# n - the number of items to return.
|
330
|
+
# items_left - the number of items left.
|
310
331
|
#
|
311
332
|
# Returns, for different values of n:
|
312
333
|
# n == 0) nil
|
313
334
|
# n == 1) an item if items_left > 0 or nil
|
314
335
|
# n > 1) an array of items if items_left > 0 or an empty array
|
315
336
|
|
316
|
-
|
337
|
+
protected def last_item_before(item, n, items_left = @item_count)
|
317
338
|
# Optimize for these cases
|
318
339
|
return nil if n == 0
|
340
|
+
return n > 1 ? [] : nil if item.prev!.nil?
|
319
341
|
return item.prev if n == 1
|
320
342
|
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
(n - 1).downto(0)
|
325
|
-
|
326
|
-
end
|
343
|
+
n = items_left if n > items_left
|
344
|
+
|
345
|
+
arr = Array.new n
|
346
|
+
(n - 1).downto(0) { |i| arr[i] = item = item.prev }
|
347
|
+
arr
|
327
348
|
rescue StopIteration
|
328
|
-
|
349
|
+
arr.compact! || arr
|
329
350
|
end
|
330
351
|
end
|
331
352
|
end
|
data/lib/linked/version.rb
CHANGED