mores 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/mores/linked_list.rb +165 -0
- data/lib/mores/succession.rb +36 -0
- data/lib/mores/version.rb +1 -1
- data/lib/mores.rb +2 -1
- metadata +4 -3
- data/lib/mores/chainable.rb +0 -39
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3ece1af062ec702368bd3dc4c9566a79aa509135
|
4
|
+
data.tar.gz: 2e3ed10515d3c2994e284dcbe8216e9d8a7560c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88f5cdd6dd1e90e7369c44c933b6d4538409424fd7f67cdc0ac0724f31b5845ac19eefb91abf5a4938778ff72badf849b95d9900014bf58946a1b1d414a1c271
|
7
|
+
data.tar.gz: b541a546588535ad5615533f06248d23dbd8c9ced7c27a41b796c3924b68b7775f67b6e9e7a015427cc54e882d02418ceffe73ad95e6a003dd1319c41eec286e
|
@@ -0,0 +1,165 @@
|
|
1
|
+
module Mores
|
2
|
+
|
3
|
+
class LinkedList
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
NOMATCH = BasicObject.new
|
7
|
+
private_constant :NOMATCH
|
8
|
+
|
9
|
+
attr_reader :length, :head, :tail
|
10
|
+
alias :size :length
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@length = 0
|
14
|
+
end
|
15
|
+
|
16
|
+
def empty?
|
17
|
+
!@head
|
18
|
+
end
|
19
|
+
|
20
|
+
def count(*args)
|
21
|
+
return @length if args.empty? && !block_given?
|
22
|
+
super
|
23
|
+
end
|
24
|
+
|
25
|
+
def each
|
26
|
+
return to_enum(__callee__) { @length } unless block_given?
|
27
|
+
each_node { |n| yield n.value }
|
28
|
+
end
|
29
|
+
|
30
|
+
def reverse_each
|
31
|
+
return to_enum(__callee__) { @length } unless block_given?
|
32
|
+
reverse_each_node { |n| yield n.value }
|
33
|
+
end
|
34
|
+
|
35
|
+
def each_node
|
36
|
+
return to_enum(__callee__) { @length } unless block_given?
|
37
|
+
node = @head
|
38
|
+
until node.nil?
|
39
|
+
node = (cur = node).next
|
40
|
+
yield cur
|
41
|
+
end
|
42
|
+
self
|
43
|
+
end
|
44
|
+
|
45
|
+
def reverse_each_node
|
46
|
+
return to_enum(__callee__) { @length } unless block_given?
|
47
|
+
node = @tail
|
48
|
+
until node.nil?
|
49
|
+
node = (cur = node).prev
|
50
|
+
yield cur
|
51
|
+
end
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
def <<(value)
|
56
|
+
insert nil, @head, value
|
57
|
+
end
|
58
|
+
|
59
|
+
def >>(value)
|
60
|
+
insert @tail, nil, value
|
61
|
+
end
|
62
|
+
|
63
|
+
def clear
|
64
|
+
each_node { |n| n.send :clear }
|
65
|
+
@head = @tail = nil
|
66
|
+
@length = 0
|
67
|
+
self
|
68
|
+
end
|
69
|
+
|
70
|
+
def delete(value)
|
71
|
+
last = each_node.reduce(NOMATCH) { |v, n| n.value == value ? n.delete : v }
|
72
|
+
return last unless last == NOMATCH
|
73
|
+
yield value if block_given?
|
74
|
+
end
|
75
|
+
|
76
|
+
def delete_if
|
77
|
+
return to_enum(__callee__) { @length } unless block_given?
|
78
|
+
each_node { |n| n.delete if yield n.value }
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def insert(before, after, value)
|
84
|
+
node = Node.new(self, value)
|
85
|
+
|
86
|
+
unless before
|
87
|
+
@head = node
|
88
|
+
else
|
89
|
+
before.send :next=, node
|
90
|
+
node.send :prev=, before
|
91
|
+
end
|
92
|
+
|
93
|
+
unless after
|
94
|
+
@tail = node
|
95
|
+
else
|
96
|
+
after.send :prev=, node
|
97
|
+
node.send :next=, after
|
98
|
+
end
|
99
|
+
|
100
|
+
@length += 1
|
101
|
+
self
|
102
|
+
end
|
103
|
+
|
104
|
+
def remove(node)
|
105
|
+
before = node.prev
|
106
|
+
after = node.next
|
107
|
+
|
108
|
+
unless before
|
109
|
+
@head = after
|
110
|
+
else
|
111
|
+
before.send :next=, after
|
112
|
+
end
|
113
|
+
|
114
|
+
unless after
|
115
|
+
@tail = before
|
116
|
+
else
|
117
|
+
after.send :prev=, before
|
118
|
+
end
|
119
|
+
|
120
|
+
@length -= 1
|
121
|
+
self
|
122
|
+
end
|
123
|
+
|
124
|
+
class Node
|
125
|
+
attr_reader :list, :prev, :next
|
126
|
+
attr_accessor :value
|
127
|
+
|
128
|
+
def initialize(list, value)
|
129
|
+
@list = list
|
130
|
+
@value = value
|
131
|
+
end
|
132
|
+
|
133
|
+
def <(value)
|
134
|
+
list.send :insert, @prev, self, value
|
135
|
+
self
|
136
|
+
end
|
137
|
+
|
138
|
+
def >(value)
|
139
|
+
list.send :insert, self, @next, value
|
140
|
+
self
|
141
|
+
end
|
142
|
+
|
143
|
+
def delete
|
144
|
+
list.send :remove, self
|
145
|
+
clear
|
146
|
+
value
|
147
|
+
end
|
148
|
+
|
149
|
+
private
|
150
|
+
|
151
|
+
def prev=(node)
|
152
|
+
@prev = node
|
153
|
+
end
|
154
|
+
|
155
|
+
def next=(node)
|
156
|
+
@next = node
|
157
|
+
end
|
158
|
+
|
159
|
+
def clear
|
160
|
+
@list = @prev = @next = nil
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
3
|
+
module Mores
|
4
|
+
|
5
|
+
class Succession
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
def line(name, &default)
|
9
|
+
singleton_class.delegate name => :'__list.head.value'
|
10
|
+
__list.tail.value.define_singleton_method name, default || proc { |*| }
|
11
|
+
end
|
12
|
+
|
13
|
+
def >>(value)
|
14
|
+
value.extend __forwarder
|
15
|
+
node = (__list.tail < value).prev
|
16
|
+
value.define_singleton_method(:__node) { node }
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def __list
|
23
|
+
@__list ||= LinkedList.new >> Object.new
|
24
|
+
end
|
25
|
+
|
26
|
+
def __forwarder
|
27
|
+
@__forwarder ||= Module.new.module_exec(self) do |succession|
|
28
|
+
define_method(:method_missing) do |name, *args, &block|
|
29
|
+
return super(name, *args, &block) unless succession.singleton_methods.include? name
|
30
|
+
__node.next.value.public_send name, *args, &block
|
31
|
+
end
|
32
|
+
self; end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/lib/mores/version.rb
CHANGED
data/lib/mores.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mores
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Petter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10
|
11
|
+
date: 2016-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -77,7 +77,8 @@ files:
|
|
77
77
|
- LICENSE.txt
|
78
78
|
- README.md
|
79
79
|
- lib/mores.rb
|
80
|
-
- lib/mores/
|
80
|
+
- lib/mores/linked_list.rb
|
81
|
+
- lib/mores/succession.rb
|
81
82
|
- lib/mores/version.rb
|
82
83
|
- mores.gemspec
|
83
84
|
homepage: http://github.com/michaeljpetter/mores
|
data/lib/mores/chainable.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
module Mores
|
2
|
-
|
3
|
-
module Chainable
|
4
|
-
def self.included(mod)
|
5
|
-
mod.extend ClassMethods
|
6
|
-
end
|
7
|
-
|
8
|
-
def **(chain)
|
9
|
-
@__chain = chain
|
10
|
-
self
|
11
|
-
end
|
12
|
-
|
13
|
-
private
|
14
|
-
|
15
|
-
def __chain
|
16
|
-
@__chain ||= self.class.__send__(:__default_chain).new
|
17
|
-
end
|
18
|
-
|
19
|
-
module ClassMethods
|
20
|
-
def chain(name, &block)
|
21
|
-
__forwarder.__send__(:define_method, name) do |*args|
|
22
|
-
__chain.public_send(name, *args)
|
23
|
-
end
|
24
|
-
__default_chain.__send__(:define_method, name, block || proc { |*| })
|
25
|
-
end
|
26
|
-
|
27
|
-
private
|
28
|
-
|
29
|
-
def __forwarder
|
30
|
-
@__forwarder ||= superclass < Chainable ? superclass.__send__(:__forwarder) : Module.new.tap { |m| include m }
|
31
|
-
end
|
32
|
-
|
33
|
-
def __default_chain
|
34
|
-
@__default_chain ||= Class.new(superclass < Chainable ? superclass.__send__(:__default_chain) : Object)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|