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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea4e0ba7b896f4e64d885d73066e65e4e08b49ea
4
- data.tar.gz: 7913edd077d33d5a7b36982568d5b3cb46cc7c1e
3
+ metadata.gz: b8501da277d54cd430b7caec75a20df3670cf0a7
4
+ data.tar.gz: f027fcee69636bd7c3cef7a9b1d34d54d69d104e
5
5
  SHA512:
6
- metadata.gz: 1467066f7e5af4ddc7eca82013d94777b956d7c93497502977fd776523e570bb7ae0d441940cb9a275f567c48750a50cff80881de98e1ca89c1592b7a5aff599
7
- data.tar.gz: fac1a3393906af1c00f788fbc4e5eb30ab2b48e042190f898b43944a6ae4718baec62b9c0e0193537cc4f484f4a518f435abf0a7611bb5f28215d6aa8128b54f
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, count, n unless block_given?
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, items_left, n
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, count, n unless block_given?
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, items_left, n
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
- # Private helper method that returns the first n items, starting just after
277
- # item, given that there are items_left items left. The following must hold
278
- # for the output to be valid:
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
- private def first_item_after(item, items_left, n)
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
- (n > items_left ? items_left : n).times.map { item = item.next }
313
+ arr = Array.new n
314
+ n.times { |i| arr[i] = item = item.next }
315
+ arr
297
316
  rescue StopIteration
298
- n > 1 ? [] : nil
317
+ arr.compact! || arr
299
318
  end
300
319
 
301
- # Private helper method that returns the last n items, ending just before
302
- # item, given that there are items_left items left. The following must hold
303
- # for the output to be valid:
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
- private def last_item_before(item, items_left, n)
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
- # Truncate n if it is larger than the number of items
322
- # left
323
- n = (n > items_left ? items_left : n)
324
- (n - 1).downto(0).with_object(Array.new n) do |i, arr|
325
- arr[i] = item = item.prev
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
- n > 1 ? [] : nil
349
+ arr.compact! || arr
329
350
  end
330
351
  end
331
352
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Linked
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.3'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linked
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Lindberg