hashery 1.5.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.ruby +30 -17
- data/.yardopts +1 -0
- data/Config.rb +28 -0
- data/{QED.rdoc → DEMO.rdoc} +0 -0
- data/HISTORY.rdoc +37 -0
- data/LICENSE.txt +26 -0
- data/NOTICE.txt +46 -0
- data/README.rdoc +10 -7
- data/lib/hashery.rb +6 -6
- data/lib/hashery.yml +30 -17
- data/lib/hashery/association.rb +169 -109
- data/lib/hashery/casting_hash.rb +128 -135
- data/lib/hashery/core_ext.rb +89 -61
- data/lib/hashery/crud_hash.rb +365 -0
- data/lib/hashery/dictionary.rb +545 -345
- data/lib/hashery/fuzzy_hash.rb +177 -125
- data/lib/hashery/ini_hash.rb +321 -0
- data/lib/hashery/key_hash.rb +54 -179
- data/lib/hashery/linked_list.rb +245 -191
- data/lib/hashery/lru_hash.rb +292 -202
- data/lib/hashery/open_cascade.rb +133 -78
- data/lib/hashery/open_hash.rb +127 -61
- data/lib/hashery/ordered_hash.rb +128 -122
- data/lib/hashery/path_hash.rb +238 -0
- data/lib/hashery/property_hash.rb +144 -80
- data/lib/hashery/query_hash.rb +85 -29
- data/lib/hashery/stash.rb +7 -3
- data/lib/hashery/static_hash.rb +46 -41
- data/test/case_association.rb +65 -4
- data/test/case_dictionary.rb +149 -5
- data/test/{case_keyhash.rb → case_key_hash.rb} +20 -14
- data/test/case_lru_hash.rb +162 -0
- data/test/{case_opencascade.rb → case_open_cascade.rb} +4 -8
- data/test/case_open_hash.rb +87 -0
- data/test/case_query_hash.rb +226 -0
- data/test/helper.rb +8 -0
- metadata +33 -63
- data/COPYING.rdoc +0 -45
- data/lib/hashery/basic_object.rb +0 -74
- data/lib/hashery/basic_struct.rb +0 -288
- data/lib/hashery/basicobject.rb +0 -1
- data/lib/hashery/basicstruct.rb +0 -1
- data/lib/hashery/castinghash.rb +0 -1
- data/lib/hashery/fuzzyhash.rb +0 -1
- data/lib/hashery/ini.rb +0 -268
- data/lib/hashery/keyhash.rb +0 -1
- data/lib/hashery/linkedlist.rb +0 -1
- data/lib/hashery/lruhash.rb +0 -1
- data/lib/hashery/memoizer.rb +0 -64
- data/lib/hashery/open_object.rb +0 -1
- data/lib/hashery/opencascade.rb +0 -1
- data/lib/hashery/openhash.rb +0 -1
- data/lib/hashery/openobject.rb +0 -1
- data/lib/hashery/orderedhash.rb +0 -1
- data/lib/hashery/ostructable.rb +0 -186
- data/lib/hashery/propertyhash.rb +0 -1
- data/lib/hashery/queryhash.rb +0 -1
- data/lib/hashery/statichash.rb +0 -1
- data/qed/01_openhash.rdoc +0 -57
- data/qed/02_queryhash.rdoc +0 -21
- data/qed/03_castinghash.rdoc +0 -13
- data/qed/04_statichash.rdoc +0 -22
- data/qed/05_association.rdoc +0 -59
- data/qed/06_opencascade.rdoc +0 -58
- data/qed/07_fuzzyhash.rdoc +0 -141
- data/qed/08_properyhash.rdoc +0 -38
- data/qed/09_ostructable.rdoc +0 -56
- data/qed/applique/ae.rb +0 -1
- data/test/case_basicstruct.rb +0 -192
- data/test/case_openhash.rb +0 -22
data/lib/hashery/key_hash.rb
CHANGED
@@ -1,186 +1,61 @@
|
|
1
|
-
require 'hashery/
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
# s =
|
14
|
-
# s[:x]
|
15
|
-
# s[
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
|
39
|
-
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
def self.[](*hash)
|
48
|
-
s = new
|
49
|
-
super(*hash).each{ |k,v| s[k] = v }
|
50
|
-
s
|
51
|
-
end
|
52
|
-
|
53
|
-
#
|
54
|
-
def [](key)
|
55
|
-
super(convert_key(key))
|
56
|
-
end
|
57
|
-
|
58
|
-
#
|
59
|
-
def []=(key,value)
|
60
|
-
super(convert_key(key), value)
|
61
|
-
end
|
62
|
-
|
63
|
-
#
|
64
|
-
def <<(other)
|
65
|
-
case other
|
66
|
-
when Hash
|
67
|
-
super(other.rekey{ |key| convert_key(key) })
|
68
|
-
when Array
|
69
|
-
self[other[0]] = other[1]
|
70
|
-
else
|
71
|
-
raise ArgumentError
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
#
|
76
|
-
def fetch(key)
|
77
|
-
super(convert_key(key))
|
78
|
-
end
|
79
|
-
|
80
|
-
#
|
81
|
-
def store(key, value)
|
82
|
-
super(convert_key(key), value)
|
83
|
-
end
|
84
|
-
|
85
|
-
#
|
86
|
-
def key?(key)
|
87
|
-
super(convert_key(key))
|
88
|
-
end
|
89
|
-
|
90
|
-
#
|
91
|
-
def has_key?(key)
|
92
|
-
super(convert_key(key))
|
93
|
-
end
|
94
|
-
|
95
|
-
#
|
96
|
-
def include?(key)
|
97
|
-
super(convert_key(key))
|
98
|
-
end
|
99
|
-
|
100
|
-
#
|
101
|
-
def member?(key)
|
102
|
-
super(convert_key(key))
|
103
|
-
end
|
104
|
-
|
105
|
-
# Synonym for #rekey, but modifies the receiver in place (and returns it).
|
106
|
-
#
|
107
|
-
# foo = { :name=>'Gavin', :wife=>:Lisa }.to_stash
|
108
|
-
# foo.rekey!{ |k| k.upcase } #=> { "NAME"=>"Gavin", "WIFE"=>:Lisa }
|
109
|
-
# foo.inspect #=> { "NAME"=>"Gavin", "WIFE"=>:Lisa }
|
110
|
-
#
|
111
|
-
def rekey!(*args, &block)
|
112
|
-
# for backward comptability (TODO: DEPRECATE?).
|
113
|
-
block = args.pop.to_sym.to_proc if args.size == 1
|
114
|
-
if args.empty?
|
115
|
-
block = lambda{|k| k} unless block
|
116
|
-
keys.each do |k|
|
117
|
-
nk = block[k]
|
118
|
-
self[nk.to_s]=delete(k) #if nk
|
119
|
-
end
|
120
|
-
else
|
121
|
-
raise ArgumentError, "3 for 2" if block
|
122
|
-
to, from = *args
|
123
|
-
self[to] = delete(from) if has_key?(from)
|
1
|
+
require 'hashery/crud_hash'
|
2
|
+
|
3
|
+
module Hashery
|
4
|
+
|
5
|
+
# The KeyHash class is a Hash class which accepts a block for
|
6
|
+
# normalizing keys.
|
7
|
+
#
|
8
|
+
# The KeyHash class is essentially the same as a normal Hash.
|
9
|
+
# But notice the significant distinction of indifferent key
|
10
|
+
# access.
|
11
|
+
#
|
12
|
+
# s = KeyHash.new
|
13
|
+
# s[:x] = 1
|
14
|
+
# s[:x] #=> 1
|
15
|
+
# s['x'] #=> 1
|
16
|
+
#
|
17
|
+
# We can see that internally the key has indeed been converted
|
18
|
+
# to a String.
|
19
|
+
#
|
20
|
+
# s.to_h #=> {'x'=>1 }
|
21
|
+
#
|
22
|
+
# By default all keys are converted to strings. This has two advantages
|
23
|
+
# over a regular Hash is many usecases. First it means hash entries have
|
24
|
+
# indifferent access. <tt>1</tt>, <tt>"1"</tt> and <tt>:1</tt> are all
|
25
|
+
# equivalent --any object that defines <tt>#to_s</tt> can be used as a key.
|
26
|
+
# Secondly, since strings are garbage collected so will default KeyHash
|
27
|
+
# objects.
|
28
|
+
#
|
29
|
+
# But keys can be normalized by any function. Theses functions can be quite
|
30
|
+
# unique.
|
31
|
+
#
|
32
|
+
# h = KeyHash.new(0){ |k| k.to_i }
|
33
|
+
# h[1.34] += 1
|
34
|
+
# h[1.20] += 1
|
35
|
+
# h[1.00] += 1
|
36
|
+
# h #=> { 1 => 3 }
|
37
|
+
#
|
38
|
+
class KeyHash < CRUDHash
|
39
|
+
|
40
|
+
#
|
41
|
+
# Unlike a regular Hash, a KeyHash's block sets the `key_proc` rather
|
42
|
+
# than the `default_proc`.
|
43
|
+
#
|
44
|
+
def initialize(*default, &block)
|
45
|
+
super(*default)
|
46
|
+
@key_proc = block || Proc.new{ |k| k.to_s }
|
124
47
|
end
|
125
|
-
self
|
126
|
-
end
|
127
|
-
|
128
|
-
#
|
129
|
-
def rekey(*args, &block)
|
130
|
-
dup.rekey!(*args, &block)
|
131
|
-
end
|
132
|
-
|
133
|
-
#
|
134
|
-
def delete(key)
|
135
|
-
super(convert_key(key))
|
136
|
-
end
|
137
|
-
|
138
|
-
#
|
139
|
-
def update(other)
|
140
|
-
super(other.rekey{ |key| convert_key(key) })
|
141
|
-
end
|
142
|
-
|
143
|
-
# Same as #update.
|
144
|
-
def merge!(other)
|
145
|
-
super(other.rekey{ |key| convert_key(key) })
|
146
|
-
end
|
147
48
|
|
148
|
-
#
|
149
|
-
def merge(other)
|
150
|
-
super(other.rekey{ |key| convert_key(key) })
|
151
|
-
end
|
152
|
-
|
153
|
-
#
|
154
|
-
def replace(other)
|
155
|
-
super(other.rekey{ |key| convert_key(key) })
|
156
|
-
end
|
157
|
-
|
158
|
-
#
|
159
|
-
def values_at(*keys)
|
160
|
-
super(*keys.map{ |key| convert_key(key) })
|
161
|
-
end
|
162
|
-
|
163
|
-
#
|
164
|
-
def to_hash
|
165
|
-
h = {}
|
166
|
-
each{ |k,v| h[k] = v }
|
167
|
-
h
|
168
49
|
end
|
169
50
|
|
170
|
-
alias_method :to_h, :to_hash
|
171
|
-
|
172
|
-
private
|
173
|
-
|
174
|
-
def convert_key(key)
|
175
|
-
key.to_s
|
176
|
-
end
|
177
|
-
|
178
51
|
end
|
179
52
|
|
180
|
-
class Hash
|
181
|
-
#
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
53
|
+
#class Hash
|
54
|
+
# #
|
55
|
+
# # Convert a Hash to a KeyHash object.
|
56
|
+
# #
|
57
|
+
# def to_keyhash
|
58
|
+
# Hashery::KeyHash[self]
|
59
|
+
# end
|
60
|
+
#end
|
186
61
|
|
data/lib/hashery/linked_list.rb
CHANGED
@@ -1,195 +1,249 @@
|
|
1
|
-
# LinkedList
|
2
|
-
#
|
3
|
-
# Copyright (C) 2006 Kirk Haines <khaines@enigo.com>.
|
4
|
-
#
|
5
|
-
# General Public License (GPL)
|
6
|
-
#
|
7
|
-
# Permission is hereby granted, free of charge, to any person obtaining
|
8
|
-
# a copy of this software and associated documentation files (the
|
9
|
-
# "Software"), to deal in the Software without restriction, including
|
10
|
-
# without limitation the rights to use, copy, modify, merge, publish,
|
11
|
-
# distribute, sublicense, and/or sell copies of the Software, and to
|
12
|
-
# permit persons to whom the Software is furnished to do so, subject to
|
13
|
-
# the following conditions:
|
14
|
-
#
|
15
|
-
# The above copyright notice and this permission notice shall be
|
16
|
-
# included in all copies or substantial portions of the Software.
|
17
|
-
#
|
18
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19
|
-
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
20
|
-
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21
|
-
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
22
|
-
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
23
|
-
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
24
|
-
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
25
|
-
|
26
1
|
require 'enumerator'
|
27
2
|
|
28
|
-
|
29
|
-
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
3
|
+
module Hashery
|
4
|
+
|
5
|
+
# LinkedList implements a simple doubly linked list with efficient
|
6
|
+
# hash-like element access.
|
7
|
+
#
|
8
|
+
# This is a simple linked-list implementation with efficient random
|
9
|
+
# access of data elements. It was inspired by George Moscovitis'
|
10
|
+
# LRUCache implementation found in Facets 1.7.30, but unlike the
|
11
|
+
# linked-list in that cache, this one does not require the use of a
|
12
|
+
# mixin on any class to be stored. The linked-list provides the
|
13
|
+
# push, pop, shift, unshift, first, last, delete and length methods
|
14
|
+
# which work just like their namesakes in the Array class, but it
|
15
|
+
# also supports setting and retrieving values by key, just like a
|
16
|
+
# hash.
|
17
|
+
#
|
18
|
+
# LinkedList was ported from the original in Kirk Hanes IOWA web framework.
|
19
|
+
#
|
20
|
+
# == Acknowledgements
|
21
|
+
#
|
22
|
+
# LinkedList is based on the LinkedList library by Kirk Haines.
|
23
|
+
#
|
24
|
+
# Copyright (C) 2006 Kirk Haines <khaines@enigo.com>.
|
25
|
+
#
|
26
|
+
class LinkedList
|
27
|
+
|
28
|
+
include Enumerable
|
29
|
+
|
30
|
+
# Represents a single node of the linked list.
|
31
|
+
#
|
32
|
+
class Node
|
33
|
+
attr_accessor :key, :value, :prev_node, :next_node
|
34
|
+
|
35
|
+
def initialize(key=nil,value=nil,prev_node=nil,next_node=nil)
|
36
|
+
@key = key
|
37
|
+
@value = value
|
38
|
+
@prev_node = prev_node
|
39
|
+
@next_node = next_node
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# Initialize new LinkedList instance.
|
45
|
+
#
|
46
|
+
def initialize
|
47
|
+
@head = Node.new
|
48
|
+
@tail = Node.new
|
49
|
+
@lookup = Hash.new
|
50
|
+
|
51
|
+
node_join(@head,@tail)
|
52
|
+
end
|
53
|
+
|
54
|
+
#
|
55
|
+
# Lookup entry by key.
|
56
|
+
#
|
57
|
+
def [](key)
|
58
|
+
@lookup[key].value
|
59
|
+
end
|
60
|
+
|
61
|
+
#
|
62
|
+
# Add node to linked list.
|
63
|
+
#
|
64
|
+
def []=(k,v)
|
65
|
+
if @lookup.has_key?(k)
|
66
|
+
@lookup[k].value = v
|
67
|
+
else
|
68
|
+
n = Node.new(k,v,@head,@head.next_node)
|
69
|
+
node_join(n,@head.next_node)
|
70
|
+
node_join(@head,n)
|
71
|
+
@lookup[k] = n
|
72
|
+
end
|
73
|
+
v
|
74
|
+
end
|
75
|
+
|
76
|
+
#
|
77
|
+
# Is linked list empty?
|
78
|
+
#
|
79
|
+
def empty?
|
80
|
+
@lookup.empty?
|
81
|
+
end
|
82
|
+
|
83
|
+
#
|
84
|
+
# Remove node idenified by key.
|
85
|
+
#
|
86
|
+
def delete(key)
|
87
|
+
n = @lookup.delete(key)
|
88
|
+
v = n ? node_purge(n) : nil
|
89
|
+
v
|
90
|
+
end
|
91
|
+
|
92
|
+
#
|
93
|
+
# Get value of first node.
|
94
|
+
#
|
95
|
+
def first
|
96
|
+
@head.next_node.value
|
97
|
+
end
|
98
|
+
|
99
|
+
#
|
100
|
+
# Get value of last node.
|
101
|
+
#
|
102
|
+
def last
|
103
|
+
@tail.prev_node.value
|
104
|
+
end
|
105
|
+
|
106
|
+
#
|
107
|
+
#
|
108
|
+
#
|
109
|
+
def shift
|
110
|
+
k = @head.next_node.key
|
111
|
+
n = @lookup.delete(k)
|
112
|
+
node_delete(n) if n
|
113
|
+
end
|
114
|
+
|
115
|
+
#
|
116
|
+
#
|
117
|
+
#
|
118
|
+
def unshift(v)
|
119
|
+
if @lookup.has_key?(v)
|
120
|
+
n = @lookup[v]
|
121
|
+
node_delete(n)
|
122
|
+
node_join(n,@head.next_node)
|
123
|
+
node_join(@head,n)
|
124
|
+
else
|
125
|
+
n = Node.new(v,v,@head,@head.next_node)
|
126
|
+
node_join(n,@head.next_node)
|
127
|
+
node_join(@head,n)
|
128
|
+
@lookup[v] = n
|
129
|
+
end
|
130
|
+
v
|
131
|
+
end
|
132
|
+
|
133
|
+
#
|
134
|
+
#
|
135
|
+
#
|
136
|
+
def pop
|
137
|
+
k = @tail.prev_node.key
|
138
|
+
n = @lookup.delete(k)
|
139
|
+
node_delete(n) if n
|
140
|
+
end
|
141
|
+
|
142
|
+
#
|
143
|
+
#
|
144
|
+
#
|
145
|
+
def push(v)
|
146
|
+
if @lookup.has_key?(v)
|
147
|
+
n = @lookup[v]
|
148
|
+
node_delete(n)
|
149
|
+
node_join(@tail.prev_node,n)
|
150
|
+
node_join(n,@tail)
|
151
|
+
else
|
152
|
+
n = Node.new(v,v,@tail.prev_node,@tail)
|
153
|
+
node_join(@tail.prev_node,n)
|
154
|
+
node_join(n,@tail)
|
155
|
+
@lookup[v] = n
|
156
|
+
end
|
157
|
+
v
|
158
|
+
end
|
159
|
+
|
160
|
+
alias :<< :push
|
161
|
+
|
162
|
+
#
|
163
|
+
# Produces an Array of key values.
|
164
|
+
#
|
165
|
+
# Returns [Array].
|
166
|
+
#
|
167
|
+
def queue
|
168
|
+
r = []
|
169
|
+
n = @head
|
170
|
+
while (n = n.next_node) and n != @tail
|
171
|
+
r << n.key
|
172
|
+
end
|
173
|
+
r
|
174
|
+
end
|
175
|
+
|
176
|
+
#
|
177
|
+
# Converts to an Array of node values.
|
178
|
+
#
|
179
|
+
# Returns [Array].
|
180
|
+
#
|
181
|
+
def to_a
|
182
|
+
r = []
|
183
|
+
n = @head
|
184
|
+
while (n = n.next_node) and n != @tail
|
185
|
+
r << n.value
|
186
|
+
end
|
187
|
+
r
|
188
|
+
end
|
189
|
+
|
190
|
+
#
|
191
|
+
# Number of nodes.
|
192
|
+
#
|
193
|
+
def length
|
194
|
+
@lookup.length
|
195
|
+
end
|
196
|
+
|
197
|
+
alias size length
|
198
|
+
|
199
|
+
#
|
200
|
+
# Iterate over nodes, starting with the head node
|
201
|
+
# and ending with the tail node.
|
202
|
+
#
|
203
|
+
def each
|
204
|
+
n = @head
|
205
|
+
while (n = n.next_node) and n != @tail
|
206
|
+
yield(n.key,n.value)
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
private
|
211
|
+
|
212
|
+
#
|
213
|
+
# Delete a node.
|
214
|
+
#
|
215
|
+
# n - A node.
|
216
|
+
#
|
217
|
+
def node_delete(n)
|
218
|
+
node_join(n.prev_node,n.next_node)
|
219
|
+
v = n.value
|
220
|
+
end
|
221
|
+
|
222
|
+
#
|
223
|
+
# Purge a node.
|
224
|
+
#
|
225
|
+
# n - A node.
|
226
|
+
#
|
227
|
+
def node_purge(n)
|
228
|
+
node_join(n.prev_node,n.next_node)
|
229
|
+
v = n.value
|
230
|
+
n.value = nil
|
231
|
+
n.key = nil
|
232
|
+
n.next_node = nil
|
233
|
+
n.prev_node = nil
|
234
|
+
v
|
235
|
+
end
|
236
|
+
|
237
|
+
# Join two nodes.
|
238
|
+
#
|
239
|
+
# a - A node.
|
240
|
+
# b - A node.
|
241
|
+
#
|
242
|
+
def node_join(a,b)
|
243
|
+
a.next_node = b
|
244
|
+
b.prev_node = a
|
245
|
+
end
|
246
|
+
|
247
|
+
end
|
193
248
|
|
194
249
|
end
|
195
|
-
|