linked 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +76 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/linked/item.rb +313 -0
- data/lib/linked/list/eol.rb +82 -0
- data/lib/linked/list.rb +204 -0
- data/lib/linked/version.rb +3 -0
- data/lib/linked.rb +8 -0
- data/linked.gemspec +26 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: eec1f197c1ffec1b6a1e47edc5c2a1c75e0967b3
|
4
|
+
data.tar.gz: 826b07b940721256814a4e21613b88d26a485982
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a25ecac7d54d74faa3360b2e487287d3875b57633924be4d7f57d0cd9d0ce01ebe86d53dce49fe81db33b2b978fae9296f4f93006a67cf92abd16e145d68341a
|
7
|
+
data.tar.gz: 50e9953c0936dc3e2825106c4ce77e14ee161848ec451607a062662c3c7f799b131352e0348189bcfdfb83aacdf07e808c3148079c2a9199b3e8334b38d67675
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Sebastian Lindberg
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# Linked
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/seblindberg/ruby-linked.svg?branch=master)](https://travis-ci.org/seblindberg/ruby-linked)
|
4
|
+
[![Coverage Status](https://coveralls.io/repos/github/seblindberg/ruby-linked/badge.svg?branch=master)](https://coveralls.io/github/seblindberg/ruby-linked?branch=master)
|
5
|
+
[![Inline docs](http://inch-ci.org/github/seblindberg/ruby-linked.svg?branch=master)](http://inch-ci.org/github/seblindberg/ruby-linked)
|
6
|
+
|
7
|
+
Yet another Linked List implementation for Ruby (hence the somewhat awkward name). The library is still under development. The intention is to
|
8
|
+
|
9
|
+
1. nail down the functionality,
|
10
|
+
2. start porting the methods over to c and finally
|
11
|
+
3. try to optimize for speed, as much as possible.
|
12
|
+
|
13
|
+
The project is still in phase 1.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'linked'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install linked
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
A basic use case is show below. For more details, for now, see the docs.
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require 'linked'
|
37
|
+
|
38
|
+
# Include the List module in a class
|
39
|
+
class ListLike
|
40
|
+
include Linked::List
|
41
|
+
end
|
42
|
+
|
43
|
+
# Create a list
|
44
|
+
list = ListLike.new
|
45
|
+
|
46
|
+
# Append values
|
47
|
+
list << :value
|
48
|
+
list << 'value'
|
49
|
+
|
50
|
+
# Or create list items manually
|
51
|
+
item = Linked::Item.new 42
|
52
|
+
list.unshift item
|
53
|
+
|
54
|
+
# Remove items with #pop and #shift
|
55
|
+
list.pop.value # => 'value'
|
56
|
+
|
57
|
+
# The list behaves much like an Array
|
58
|
+
list.count # => 2
|
59
|
+
list.map(&:value) # => [42, :value]
|
60
|
+
```
|
61
|
+
|
62
|
+
## Development
|
63
|
+
|
64
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
65
|
+
|
66
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
70
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/linked.
|
71
|
+
|
72
|
+
|
73
|
+
## License
|
74
|
+
|
75
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
76
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "linked"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/linked/item.rb
ADDED
@@ -0,0 +1,313 @@
|
|
1
|
+
module Linked
|
2
|
+
# Item
|
3
|
+
#
|
4
|
+
# This class implements doubly linked list items, designed to work both on
|
5
|
+
# their own and as children of list.
|
6
|
+
#
|
7
|
+
# +- - - + +------+------+ +- - - +
|
8
|
+
# | Head | <--| prev | next |--> ... --> | Tail |
|
9
|
+
# + - - -+ +------+------+ + - - -+
|
10
|
+
# (optional) First Item N Items (optional)
|
11
|
+
#
|
12
|
+
# An object is considered a list if it responds to #head, #tail, #grow and
|
13
|
+
# #shrink. The latter facilitate counting of the items and will be called
|
14
|
+
# everytime an item is appended, prepended or deleted. #head and #tail are
|
15
|
+
# expected to return two objects that, respectivly
|
16
|
+
# a) responds to #next= and #append, or #prev= and #prepend and
|
17
|
+
# b) returns true for #nil?.
|
18
|
+
|
19
|
+
class Item
|
20
|
+
# Access the list (if any) that the item belongs to. Writing to this
|
21
|
+
# attribute is protected and should be avoided.
|
22
|
+
#
|
23
|
+
# Returns the item's list, or nil
|
24
|
+
|
25
|
+
attr_accessor :list
|
26
|
+
protected :list=
|
27
|
+
|
28
|
+
# The Item can hold an arbitrary object as its value and it will stay with
|
29
|
+
# the item.
|
30
|
+
|
31
|
+
attr_accessor :value
|
32
|
+
|
33
|
+
# Calling either #prev= or #next= directly is not recommended, since can
|
34
|
+
# corrupt the chain.
|
35
|
+
|
36
|
+
attr_writer :prev, :next
|
37
|
+
protected :prev=, :next=
|
38
|
+
|
39
|
+
# Creates a new item. If a list is given the item will be considered a part
|
40
|
+
# of that list and appended to the end of it.
|
41
|
+
#
|
42
|
+
# value - an arbitrary object to store with the item.
|
43
|
+
# list - an object responding to #head and #tail.
|
44
|
+
#
|
45
|
+
# Returns a new Item.
|
46
|
+
|
47
|
+
def initialize(value = nil, list: nil)
|
48
|
+
@value = value
|
49
|
+
@list = list
|
50
|
+
if list
|
51
|
+
list.tail.append self
|
52
|
+
else
|
53
|
+
@next = nil
|
54
|
+
@prev = nil
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Check if this is the first item in the list. It is crucial that tail#nil?
|
59
|
+
# returns true for the first item to be identified correctly.
|
60
|
+
#
|
61
|
+
# Returns true if no item come before this one.
|
62
|
+
|
63
|
+
def first?
|
64
|
+
@prev.nil?
|
65
|
+
end
|
66
|
+
|
67
|
+
# Check if this is the last item in the list. It is crucial that head#nil?
|
68
|
+
# returns true for the last item to be identified correctly.
|
69
|
+
#
|
70
|
+
# Returns true if no item come after this one.
|
71
|
+
|
72
|
+
def last?
|
73
|
+
@next.nil?
|
74
|
+
end
|
75
|
+
|
76
|
+
# Access the next item in the list. If this is the last one a StopIteration
|
77
|
+
# will be raised, so that items may be iterated over safely in a loop.
|
78
|
+
#
|
79
|
+
# Example
|
80
|
+
# loop do
|
81
|
+
# item = item.next
|
82
|
+
# end
|
83
|
+
#
|
84
|
+
# Returns the item that come after this.
|
85
|
+
|
86
|
+
def next
|
87
|
+
raise StopIteration if last?
|
88
|
+
@next
|
89
|
+
end
|
90
|
+
|
91
|
+
# Unsafe accessor of the next item in the list. It is preferable to use
|
92
|
+
# #next.
|
93
|
+
#
|
94
|
+
# Returns the item that come after this, or nil if this is the last one.
|
95
|
+
|
96
|
+
def next!
|
97
|
+
@next
|
98
|
+
end
|
99
|
+
|
100
|
+
# Access the previous item in the list. If this is the first one a
|
101
|
+
# StopIteration will be raised, so that items may be iterated over safely in
|
102
|
+
# a loop.
|
103
|
+
#
|
104
|
+
# Example
|
105
|
+
# loop do
|
106
|
+
# item = item.prev
|
107
|
+
# end
|
108
|
+
#
|
109
|
+
# Returns the item that come before this.
|
110
|
+
|
111
|
+
def prev
|
112
|
+
raise StopIteration if first?
|
113
|
+
@prev
|
114
|
+
end
|
115
|
+
|
116
|
+
alias previous prev
|
117
|
+
|
118
|
+
# Unsafe accessor of the previous item in the list. It is preferable to use
|
119
|
+
# #prev.
|
120
|
+
#
|
121
|
+
# Returns the item that come before this, or nil if this is the first one.
|
122
|
+
|
123
|
+
def prev!
|
124
|
+
@prev
|
125
|
+
end
|
126
|
+
|
127
|
+
alias previous! prev!
|
128
|
+
|
129
|
+
# Split the chain of items in two. If the chain belongs to a list this item
|
130
|
+
# and all that stay connected to it will continue to belong to it, while the
|
131
|
+
# rest are removed from it.
|
132
|
+
#
|
133
|
+
# By default all items followng this one will be kept together, but if given
|
134
|
+
# the argument after: true, the split will instead happen after this item
|
135
|
+
# and it will instead be kept with those before it.
|
136
|
+
#
|
137
|
+
# Example
|
138
|
+
#
|
139
|
+
# item_b.split(after: false) => ~item_a~ |> item_b item_c
|
140
|
+
# item_b.split(after: true) => item_a item_b <| ~item_c~
|
141
|
+
#
|
142
|
+
# after - determine wheter to split the chain before or after this item.
|
143
|
+
#
|
144
|
+
# Returns self.
|
145
|
+
|
146
|
+
def split after: false
|
147
|
+
if after
|
148
|
+
unless last?
|
149
|
+
if @list
|
150
|
+
item = self
|
151
|
+
count = 1 + loop.count do
|
152
|
+
item = item.next
|
153
|
+
item.list = nil
|
154
|
+
end
|
155
|
+
|
156
|
+
tail = @list.tail
|
157
|
+
tail.prev = self
|
158
|
+
@next = tail
|
159
|
+
@list.shrink count
|
160
|
+
else
|
161
|
+
@next.prev = nil
|
162
|
+
@next = nil
|
163
|
+
end
|
164
|
+
end
|
165
|
+
else
|
166
|
+
unless first?
|
167
|
+
if @list
|
168
|
+
item = self
|
169
|
+
count = 1 + loop.count do
|
170
|
+
item = item.prev
|
171
|
+
item.list = nil
|
172
|
+
end
|
173
|
+
|
174
|
+
head = @list.head
|
175
|
+
head.next = self
|
176
|
+
@prev = head
|
177
|
+
@list.shrink count
|
178
|
+
else
|
179
|
+
@prev.next = nil
|
180
|
+
@prev = nil
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
self
|
186
|
+
end
|
187
|
+
|
188
|
+
# Inserts the given item between this one and the one after it (if any). If
|
189
|
+
# the given item is part of a chain, all items following it will be moved to
|
190
|
+
# this one, and added to the list if one is set.
|
191
|
+
#
|
192
|
+
# Alternativly the argument can be an arbitrary object, in which case a new
|
193
|
+
# item will be created around it.
|
194
|
+
#
|
195
|
+
# If this item is part of a list #grow will be called on it with the
|
196
|
+
# number of added items as an argument. Should it also be the last item
|
197
|
+
# #prev= will be called on the list tail.
|
198
|
+
#
|
199
|
+
# sibling - the item to append, or an arbitrary object to be wraped in a new
|
200
|
+
# item.
|
201
|
+
#
|
202
|
+
# Returns the last item that was appended.
|
203
|
+
|
204
|
+
def append(sibling)
|
205
|
+
if sibling.is_a? Item
|
206
|
+
sibling.split
|
207
|
+
else
|
208
|
+
sibling = self.class.new sibling
|
209
|
+
end
|
210
|
+
|
211
|
+
sibling.prev = self
|
212
|
+
after_sibling = @next
|
213
|
+
@next = sibling
|
214
|
+
|
215
|
+
count = 1 + loop.count do
|
216
|
+
sibling.list = @list
|
217
|
+
sibling = sibling.next
|
218
|
+
end
|
219
|
+
|
220
|
+
@list.send :grow, count if @list
|
221
|
+
|
222
|
+
sibling.next = after_sibling
|
223
|
+
after_sibling.prev = sibling if after_sibling
|
224
|
+
sibling
|
225
|
+
end
|
226
|
+
|
227
|
+
# Inserts the given item between this one and the one before it (if any). If
|
228
|
+
# the given item is part of a chain, all items preceeding it will be moved
|
229
|
+
# to this one, and added to the list if one is set.
|
230
|
+
#
|
231
|
+
# Alternativly the argument can be an arbitrary object, in which case a new
|
232
|
+
# item will be created around it.
|
233
|
+
#
|
234
|
+
# If this item is part of a list #grow will be called on it with the
|
235
|
+
# number of added items as an argument. Should it also be the first item
|
236
|
+
# #next= will be called on the list head.
|
237
|
+
#
|
238
|
+
# sibling - the item to prepend. or an arbitrary object to be wraped in a
|
239
|
+
# new item.
|
240
|
+
#
|
241
|
+
# Returns the last item that was prepended.
|
242
|
+
|
243
|
+
def prepend(sibling)
|
244
|
+
if sibling.is_a? Item
|
245
|
+
sibling.split after: true
|
246
|
+
else
|
247
|
+
sibling = self.class.new sibling
|
248
|
+
end
|
249
|
+
|
250
|
+
sibling.next = self
|
251
|
+
before_sibling = @prev
|
252
|
+
@prev = sibling
|
253
|
+
|
254
|
+
count = 1 + loop.count do
|
255
|
+
sibling.list = @list
|
256
|
+
sibling = sibling.prev
|
257
|
+
end
|
258
|
+
|
259
|
+
@list.send :grow, count if @list
|
260
|
+
|
261
|
+
sibling.prev = before_sibling
|
262
|
+
before_sibling.next = sibling if before_sibling
|
263
|
+
sibling
|
264
|
+
end
|
265
|
+
|
266
|
+
# Remove an item from the chain. If this item is part of a list and is
|
267
|
+
# either first, last or both in that list, #next= and #prev= will be called
|
268
|
+
# on the list head and tail respectivly.
|
269
|
+
#
|
270
|
+
# If this item is part of a list #shrink will be called on it.
|
271
|
+
#
|
272
|
+
# Returns self.
|
273
|
+
|
274
|
+
def delete
|
275
|
+
@next.prev = @prev if @next
|
276
|
+
@prev.next = @next if @prev
|
277
|
+
@list.send :shrink if @list
|
278
|
+
|
279
|
+
@next = @prev = @list = nil
|
280
|
+
self
|
281
|
+
end
|
282
|
+
|
283
|
+
# Iterates over each item before this, in reverse order. If a block is not
|
284
|
+
# given an enumerator is returned.
|
285
|
+
|
286
|
+
def before
|
287
|
+
return to_enum(__callee__) unless block_given?
|
288
|
+
return if first?
|
289
|
+
|
290
|
+
item = self.prev
|
291
|
+
|
292
|
+
loop do
|
293
|
+
yield item
|
294
|
+
item = item.prev
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
# Iterates over each item after this. If a block is not given an enumerator
|
299
|
+
# is returned.
|
300
|
+
|
301
|
+
def after
|
302
|
+
return to_enum(__callee__) unless block_given?
|
303
|
+
return if last?
|
304
|
+
|
305
|
+
item = self.next
|
306
|
+
|
307
|
+
loop do
|
308
|
+
yield item
|
309
|
+
item = item.next
|
310
|
+
end
|
311
|
+
end
|
312
|
+
end
|
313
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Linked
|
2
|
+
module List
|
3
|
+
# End Of List
|
4
|
+
#
|
5
|
+
# This class implements a special list item that is placed at both the end
|
6
|
+
# and the beginning of a chain of regular items to form a list. The naming
|
7
|
+
# (end of list) comes from the fact that this object, by returning true for
|
8
|
+
# calls to #nil?, signifies the end of a list of Items. In both directions
|
9
|
+
# as a matter of fact, which is why the head and tail objects defined by
|
10
|
+
# Item is combined into one.
|
11
|
+
#
|
12
|
+
# In a nutshell, the structure looks something like this:
|
13
|
+
#
|
14
|
+
# +-------------------- EOL --------------------+
|
15
|
+
# | (head) (tail) |
|
16
|
+
# +---------------------------------------------+
|
17
|
+
# ^ +-- Item 1 ---+ +-- Item N ---+ ^
|
18
|
+
# +-| prev | next |<- ... ->| prev | next |-+
|
19
|
+
# +------+------+ +------+------+
|
20
|
+
|
21
|
+
class EOL < Item
|
22
|
+
private :value, :value=, :delete, :first?, :last?
|
23
|
+
|
24
|
+
def initialize(list:)
|
25
|
+
super()
|
26
|
+
@list = list
|
27
|
+
@prev = @next = self
|
28
|
+
end
|
29
|
+
|
30
|
+
# EOL objects will return true when asked if they are nil. This is
|
31
|
+
# foremost an implementation detail to comply with the requirements of the
|
32
|
+
# Item class, but also logical in the sense that end-of-list objects are
|
33
|
+
# not really part of the list, and should therefore be considered nil.
|
34
|
+
#
|
35
|
+
# Returns true.
|
36
|
+
|
37
|
+
def nil?
|
38
|
+
true
|
39
|
+
end
|
40
|
+
|
41
|
+
# Inserts one or more items at the end of the list. If the given object is
|
42
|
+
# not an Item, or a decendant of Item, it will be treated as a value.
|
43
|
+
# Depending on the state of the list the value will be
|
44
|
+
# a) wraped in a new instance of Item if the list is empty or
|
45
|
+
# b) wraped in an object of the same class as the last item in the list.
|
46
|
+
#
|
47
|
+
# sibling - the item or value to be appended.
|
48
|
+
#
|
49
|
+
# Returns the item that was appended. In case of a string of items the
|
50
|
+
# last one is returned.
|
51
|
+
|
52
|
+
def append(sibling)
|
53
|
+
if @prev == self
|
54
|
+
sibling = Item.new sibling unless sibling.is_a? Item
|
55
|
+
super sibling
|
56
|
+
else
|
57
|
+
@prev.append sibling
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Inserts one or more items at the beginning of the list. If the given
|
62
|
+
# object is not an Item, or a decendant of Item, it will be treated as a
|
63
|
+
# value. Depending on the state of the list the value will be
|
64
|
+
# a) wraped in a new instance of Item if the list is empty or
|
65
|
+
# b) wraped in an object of the same class as the last item in the list.
|
66
|
+
#
|
67
|
+
# sibling - the item or value to be prepended.
|
68
|
+
#
|
69
|
+
# Returns the item that was prepended. In case of a string of items the
|
70
|
+
# first one is returned.
|
71
|
+
|
72
|
+
def prepend(sibling)
|
73
|
+
if @next == self
|
74
|
+
sibling = Item.new sibling unless sibling.is_a? Item
|
75
|
+
super sibling
|
76
|
+
else
|
77
|
+
@next.prepend sibling
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/lib/linked/list.rb
ADDED
@@ -0,0 +1,204 @@
|
|
1
|
+
module Linked
|
2
|
+
# List
|
3
|
+
#
|
4
|
+
# This module can be included in any class to give it list like behaviour.
|
5
|
+
# Most importantly, the methods #head, #tail, #grow and #shrink are
|
6
|
+
# implemented to comply with the requirements defined by Item.
|
7
|
+
#
|
8
|
+
# Example
|
9
|
+
#
|
10
|
+
# class ListLike
|
11
|
+
# include Linked::List
|
12
|
+
#
|
13
|
+
# def initialize
|
14
|
+
# super
|
15
|
+
# ...
|
16
|
+
#
|
17
|
+
# A key implementation detail is the End-Of-List, or EOL object that sits
|
18
|
+
# between the list and the actual list items. It provides separation between
|
19
|
+
# the list and the actual list items.
|
20
|
+
|
21
|
+
module List
|
22
|
+
include Enumerable
|
23
|
+
|
24
|
+
# Private accessor method for the End-Of-List object.
|
25
|
+
#
|
26
|
+
# Returns a List::EOL object.
|
27
|
+
|
28
|
+
attr_reader :eol
|
29
|
+
private :eol
|
30
|
+
|
31
|
+
# Returns an object that responds to #next= and #prepend.
|
32
|
+
|
33
|
+
alias head eol
|
34
|
+
|
35
|
+
# Returns an object that responds to #prev= and #append.
|
36
|
+
|
37
|
+
alias tail eol
|
38
|
+
|
39
|
+
# Initializes the list by setting the two instance variable @item_count and
|
40
|
+
# @eol. It is important that this method be called during the initialization
|
41
|
+
# of the including class, and that the instance variables never be accessed
|
42
|
+
# directly.
|
43
|
+
|
44
|
+
def initialize(*)
|
45
|
+
super
|
46
|
+
|
47
|
+
@eol = EOL.new list: self
|
48
|
+
@item_count = 0
|
49
|
+
end
|
50
|
+
|
51
|
+
# Access the first n item(s) in the list.
|
52
|
+
#
|
53
|
+
# n - the number of items to return.
|
54
|
+
#
|
55
|
+
# Returns the first item, or an array of items if n > 1.
|
56
|
+
|
57
|
+
def first(*args)
|
58
|
+
if args.empty?
|
59
|
+
eol.next!
|
60
|
+
else
|
61
|
+
super
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Access the last n item(s) in the list. When n > 1 the resulting array of
|
66
|
+
# items will have their order preserved.
|
67
|
+
#
|
68
|
+
# When n is zero an empty array will be returned, in order to comply with
|
69
|
+
# the behaviour of #first. Negative values will raise an ArgumentError.
|
70
|
+
#
|
71
|
+
# n - the number of items to return.
|
72
|
+
#
|
73
|
+
# Returns the last item, or an array of items if n > 1.
|
74
|
+
|
75
|
+
def last(n = 1)
|
76
|
+
if n == 1
|
77
|
+
eol.prev!
|
78
|
+
else
|
79
|
+
raise ArgumentError, 'n cannot be negative' if n < 0
|
80
|
+
|
81
|
+
n = count if n > count
|
82
|
+
res = Array.new n
|
83
|
+
|
84
|
+
return res if n == 0
|
85
|
+
|
86
|
+
item = eol.prev!
|
87
|
+
loop do
|
88
|
+
n -= 1
|
89
|
+
res[n] = item
|
90
|
+
item = item.prev
|
91
|
+
end
|
92
|
+
|
93
|
+
res
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Overrides the Enumerable#count method when given no argument to provide a
|
98
|
+
# fast item count. Instead of iterating over each item, the internal item
|
99
|
+
# count is returned.
|
100
|
+
#
|
101
|
+
# args - see Enumerable#count
|
102
|
+
#
|
103
|
+
# Returns the number of items counted.
|
104
|
+
|
105
|
+
def count(*args)
|
106
|
+
if args.empty? && !block_given?
|
107
|
+
@item_count
|
108
|
+
else
|
109
|
+
super
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# Returns true if the list does not contain any items.
|
114
|
+
|
115
|
+
def empty?
|
116
|
+
@item_count == 0
|
117
|
+
end
|
118
|
+
|
119
|
+
# Insert an item at the end of the list. If the given object is not an Item,
|
120
|
+
# or a decendant of Item, it will be treated as a value. Depending on the
|
121
|
+
# state of the list the value will be
|
122
|
+
# a) wraped in a new instance of Item if the list is empty or
|
123
|
+
# b) wraped in an object of the same class as the last item in the list.
|
124
|
+
#
|
125
|
+
# item - the item to insert, or an arbitrary value.
|
126
|
+
#
|
127
|
+
# Returns self.
|
128
|
+
|
129
|
+
def push(item)
|
130
|
+
eol.append item
|
131
|
+
self
|
132
|
+
end
|
133
|
+
|
134
|
+
alias << push
|
135
|
+
|
136
|
+
# Pop the last item off the list.
|
137
|
+
#
|
138
|
+
# Returns the last item in the list, or nil if the list is empty.
|
139
|
+
|
140
|
+
def pop
|
141
|
+
return nil if empty?
|
142
|
+
last.delete
|
143
|
+
end
|
144
|
+
|
145
|
+
# Insert an item at the beginning of the list. If the given object is not an
|
146
|
+
# Item, or a decendant of Item, it will be treated as a value. Depending on
|
147
|
+
# the state of the list the value will be
|
148
|
+
# a) wraped in a new instance of Item if the list is empty or
|
149
|
+
# b) wraped in an object of the same class as the last item in the list.
|
150
|
+
#
|
151
|
+
# item - the item to insert, or an arbitrary value.
|
152
|
+
#
|
153
|
+
# Returns self.
|
154
|
+
|
155
|
+
def unshift(item)
|
156
|
+
eol.prepend item
|
157
|
+
self
|
158
|
+
end
|
159
|
+
|
160
|
+
# Shift the first item off the list.
|
161
|
+
#
|
162
|
+
# Returns the first item in the list, or nil if the list is empty.
|
163
|
+
|
164
|
+
def shift
|
165
|
+
return nil if empty?
|
166
|
+
first.delete
|
167
|
+
end
|
168
|
+
|
169
|
+
# Iterates over each item in the list, either in normal or reverse order. If
|
170
|
+
# a block is not given an enumerator is returned.
|
171
|
+
#
|
172
|
+
# reverse - flips the iteration order if true.
|
173
|
+
|
174
|
+
def each(reverse: false, &block)
|
175
|
+
if reverse
|
176
|
+
eol.before(&block)
|
177
|
+
else
|
178
|
+
eol.after(&block)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
# Internal method to grow the list with n elements. Never call this method
|
183
|
+
# without also inserting the n elements.
|
184
|
+
#
|
185
|
+
# n - the number of items that has been/will be added to the list.
|
186
|
+
#
|
187
|
+
# Returns updated the item count.
|
188
|
+
|
189
|
+
private def grow(n = 1)
|
190
|
+
@item_count += n
|
191
|
+
end
|
192
|
+
|
193
|
+
# Internal method to shrink the list with n elements. Never call this method
|
194
|
+
# without also deleting the n elements.
|
195
|
+
#
|
196
|
+
# n - the number of items that has been/will be removed from the list.
|
197
|
+
#
|
198
|
+
# Returns updated the item count.
|
199
|
+
|
200
|
+
private def shrink(n = 1)
|
201
|
+
@item_count -= n
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
data/lib/linked.rb
ADDED
data/linked.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'linked/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "linked"
|
8
|
+
spec.version = Linked::VERSION
|
9
|
+
spec.authors = ["Sebastian Lindberg"]
|
10
|
+
spec.email = ["seb.lindberg@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Yet another linked list implementation in Ruby.}
|
13
|
+
spec.description = %q{Yes, there are a lot of linked list implementaions out there but I wrote my own all the same. This library is ment to be subclassed/included by other objects that that behave like linked lists.}
|
14
|
+
spec.homepage = "https://github.com/seblindberg/ruby-linked"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
25
|
+
spec.add_development_dependency "coveralls", "~> 0.8"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: linked
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sebastian Lindberg
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coveralls
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.8'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.8'
|
69
|
+
description: Yes, there are a lot of linked list implementaions out there but I wrote
|
70
|
+
my own all the same. This library is ment to be subclassed/included by other objects
|
71
|
+
that that behave like linked lists.
|
72
|
+
email:
|
73
|
+
- seb.lindberg@gmail.com
|
74
|
+
executables: []
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- ".gitignore"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- lib/linked.rb
|
87
|
+
- lib/linked/item.rb
|
88
|
+
- lib/linked/list.rb
|
89
|
+
- lib/linked/list/eol.rb
|
90
|
+
- lib/linked/version.rb
|
91
|
+
- linked.gemspec
|
92
|
+
homepage: https://github.com/seblindberg/ruby-linked
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.5.1
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Yet another linked list implementation in Ruby.
|
116
|
+
test_files: []
|