queuehash 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/queuehash.rb +51 -0
- data/lib/queuehash.rb~ +45 -0
- metadata +40 -0
data/lib/queuehash.rb
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# A QueueHash is an ordered hash: Keys are ordered according to when they were
|
|
2
|
+
# inserted.
|
|
3
|
+
#
|
|
4
|
+
# The RubyForge project page is at http://rubyforge.org/projects/queuehash .
|
|
5
|
+
|
|
6
|
+
require 'delegate'
|
|
7
|
+
|
|
8
|
+
class QueueHash < DelegateClass( Array )
|
|
9
|
+
Version = '0.1.0'
|
|
10
|
+
|
|
11
|
+
# Creates a QueueHash with all the elements in <tt>array</tt> as keys, and
|
|
12
|
+
# each value initially set to be the same as the corresponding key.
|
|
13
|
+
def self.new_from_array(array)
|
|
14
|
+
new( *( ( array.map { |elt| [ elt, elt ] } ).flatten ) )
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Takes an even number of arguments, and sets each odd-numbered argument to
|
|
18
|
+
# correspond to the argument immediately afterward. For example:
|
|
19
|
+
# queueHash = QueueHash.new (1, 2, 3, 4)
|
|
20
|
+
# queueHash[1] => 2
|
|
21
|
+
# queueHash[3] => 4
|
|
22
|
+
def initialize(*values)
|
|
23
|
+
@pairs = []
|
|
24
|
+
0.step(values.size-1, 2) { |i| @pairs << [ values[i], values[i+1] ] }
|
|
25
|
+
super( @pairs )
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def ==( otherObj )
|
|
29
|
+
if otherObj.class == QueueHash && otherObj.size == size
|
|
30
|
+
( 0..size ).all? { |i|
|
|
31
|
+
keys[i] == otherObj.keys[i] && values[i] == otherObj.values[i]
|
|
32
|
+
}
|
|
33
|
+
else
|
|
34
|
+
false
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def [](key)
|
|
39
|
+
( pair = @pairs.find { |pair| pair[0] == key } ) ? pair.last : nil
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def []=(key, value); @pairs << [key, value]; end
|
|
43
|
+
|
|
44
|
+
def each; @pairs.each { |pair| yield pair[0], pair[1] }; end
|
|
45
|
+
|
|
46
|
+
def keys; @pairs.map { |pair| pair[0] }; end
|
|
47
|
+
|
|
48
|
+
def values; @pairs.map { |pair| pair[1] }; end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
|
data/lib/queuehash.rb~
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require 'delegate'
|
|
2
|
+
|
|
3
|
+
# An ordered hash: Keys are ordered according to when they were inserted.
|
|
4
|
+
class QueueHash < DelegateClass( Array )
|
|
5
|
+
# Creates a QueueHash with all the elements in <tt>array</tt> as keys, and
|
|
6
|
+
# each value initially set to be the same as the corresponding key.
|
|
7
|
+
def self.new_from_array(array)
|
|
8
|
+
new( *( ( array.map { |elt| [ elt, elt ] } ).flatten ) )
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Takes an even number of arguments, and sets each odd-numbered argument to
|
|
12
|
+
# correspond to the argument immediately afterward. For example:
|
|
13
|
+
# queueHash = QueueHash.new (1, 2, 3, 4)
|
|
14
|
+
# queueHash[1] => 2
|
|
15
|
+
# queueHash[3] => 4
|
|
16
|
+
def initialize(*values)
|
|
17
|
+
@pairs = []
|
|
18
|
+
0.step(values.size-1, 2) { |i| @pairs << [ values[i], values[i+1] ] }
|
|
19
|
+
super( @pairs )
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def ==( otherObj )
|
|
23
|
+
if otherObj.class == QueueHash && otherObj.size == size
|
|
24
|
+
( 0..size ).all? { |i|
|
|
25
|
+
keys[i] == otherObj.keys[i] && values[i] == otherObj.values[i]
|
|
26
|
+
}
|
|
27
|
+
else
|
|
28
|
+
false
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def [](key)
|
|
33
|
+
( pair = @pairs.find { |pair| pair[0] == key } ) ? pair.last : nil
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def []=(key, value); @pairs << [key, value]; end
|
|
37
|
+
|
|
38
|
+
def each; @pairs.each { |pair| yield pair[0], pair[1] }; end
|
|
39
|
+
|
|
40
|
+
def keys; @pairs.map { |pair| pair[0] }; end
|
|
41
|
+
|
|
42
|
+
def values; @pairs.map { |pair| pair[1] }; end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
|
metadata
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
rubygems_version: 0.8.6
|
|
3
|
+
specification_version: 1
|
|
4
|
+
name: queuehash
|
|
5
|
+
version: !ruby/object:Gem::Version
|
|
6
|
+
version: 0.1.0
|
|
7
|
+
date: 2005-06-08
|
|
8
|
+
summary: "A QueueHash is an ordered hash: Keys are ordered according to when they were
|
|
9
|
+
inserted."
|
|
10
|
+
require_paths:
|
|
11
|
+
- lib
|
|
12
|
+
email: sera@fhwang.net
|
|
13
|
+
homepage: http://queuehash.rubyforge.org/
|
|
14
|
+
rubyforge_project:
|
|
15
|
+
description: "A QueueHash is an ordered hash: Keys are ordered according to when they were
|
|
16
|
+
inserted."
|
|
17
|
+
autorequire: queuehash
|
|
18
|
+
default_executable:
|
|
19
|
+
bindir: bin
|
|
20
|
+
has_rdoc: false
|
|
21
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
-
|
|
24
|
+
- ">"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 0.0.0
|
|
27
|
+
version:
|
|
28
|
+
platform: ruby
|
|
29
|
+
authors:
|
|
30
|
+
- Francis Hwang
|
|
31
|
+
files:
|
|
32
|
+
- lib/queuehash.rb
|
|
33
|
+
- lib/queuehash.rb~
|
|
34
|
+
test_files: []
|
|
35
|
+
rdoc_options: []
|
|
36
|
+
extra_rdoc_files: []
|
|
37
|
+
executables: []
|
|
38
|
+
extensions: []
|
|
39
|
+
requirements: []
|
|
40
|
+
dependencies: []
|