extruding-hash 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/.rspec ADDED
File without changes
data/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ This work is licensed under the Creative Commons Attribution 3.0 Unported License.
2
+
3
+ To view a copy of this license, visit :
4
+
5
+ http://creativecommons.org/licenses/by/3.0/
6
+
7
+ Or send a letter to :
8
+
9
+ Creative Commons,
10
+ 444 Castro Street,
11
+ Suite 900,
12
+ Mountain View,
13
+ California, 94041,
14
+ USA.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ extruding-hash
2
+ ==============
3
+
4
+ `extruding-hash` provdes a data structure, based on a Hash of Arrays,
5
+ that provides a method or two to gather data together in what can be
6
+ considered _bins_. The Hash functionality provides these _bins_, while
7
+ the data values themselves are contained in Arrays, providing the
8
+ concept of _columns_.
9
+
10
+ The `extruding-hash` object has methods where if an added _column_ (in
11
+ the form of a Hash) has any _bins_ that the object does not already
12
+ contain, the _bin_ will be added, populated with **nil** values for any
13
+ _columns_ that had not had a value for that _bin_ - see figure 1:
14
+
15
+ Init. State (extruding-hash object)
16
+ Bin Columns
17
+ --- -------
18
+ a1|b1 [ 1, 2, 3, 5 ]
19
+ a3|b3 [ 2, 3, 5, 8 ]
20
+ a4|b4 [ 3, 5, 8, 13 ]
21
+
22
+ New column (Hash) to be added, note second bin not in extruding-hash object already.
23
+ Bins Values
24
+ --- -------
25
+ a1|b1 21
26
+ a2|b2 34
27
+ a4|b4 55
28
+
29
+ End. State (extruding-hash object), after column introduced.
30
+ Bin Columns
31
+ --- -------
32
+ a1|b1 [ 1, 2, 3, 5, 21 ]
33
+ a2|b2 [ nil, nil, nil, nil, 34 ]
34
+ a3|b3 [ 2, 3, 5, 8, nil ]
35
+ a4|b4 [ 3, 5, 8, 13 55 ]
36
+
37
+ Figure 1
38
+
39
+ License
40
+ -------
41
+
42
+ `extruding-hash` is licensed under the Creative Commons 3.0 License.
43
+ Details can be found in the file LICENSE.
44
+
45
+ License-file referencing and other doc. formatting taken from
46
+ [damiendallimore](https://github.com/damiendallimore "damiendallimore on GitHub").
47
+
48
+ Install
49
+ -------
50
+
51
+ gem install extruding-hash
52
+
53
+ Usage
54
+ -----
55
+ ```ruby
56
+ require 'extruding-hash'
57
+ data = ExtrudingHash.new
58
+ data = { "a1|b1" => [ 1, 2, 3, 5 ], "a3|b3" => [ 2, 3, 5, 8 ],
59
+ "a4|b4" => [ 3, 5, 8, 13 ] }
60
+ data << { "a1|b1" => 21, "a2|b2" => 34, "a4|b4" => 55 }
61
+ data
62
+ => {"a1|b1"=>[1, 2, 3, 5, 21], "a2|b2"=>[nil, nil, nil, 34],
63
+ "a3|b3"=>[2, 3, 5, 8, nil], "a4|b4"=>[ 3, 5, 8, 13, 55]}
64
+ ```
65
+
66
+ Contribute
67
+ ----------
68
+
69
+ Please fork the GitHub project (https://github.com/aburnheimer/extruding-hash),
70
+ make any changes, commit and push to GitHub, and submit a pull request.
71
+ Including tests for your changes would be greatly appreciated!
72
+
73
+ Contact
74
+ -------
75
+
76
+ This project was initiated by Andrew Burnheimer.
77
+
78
+ * Email:
79
+ * aburnheimer@gmail.com
80
+ * Twitter:
81
+ * @aburnheimer
82
+ * Github:
83
+ * https://github.com/aburnheimer/
@@ -0,0 +1,21 @@
1
+ Gem::Specification.new do |s|
2
+ s.platform = Gem::Platform::RUBY
3
+ s.name = 'extruding-hash'
4
+ s.version = '0.9.0'
5
+ s.summary = %q{Hash structure for histograms}
6
+
7
+ s.description = %q{Data structure, based on a Hash of Arrays, that
8
+ provides a method or two to gather data together in what can be
9
+ considered "bins".}
10
+
11
+ s.license = 'CC-BY-3.0'
12
+
13
+ s.author = 'Andrew Burnheimer'
14
+ s.email = 'Andrew_Burnheimer@cable.comcast.com'
15
+ s.homepage = 'https://github.com/aburnheimer/extruding-hash'
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+
20
+ # no dependencies
21
+ end
@@ -0,0 +1,6 @@
1
+ class Array
2
+ def last=(arg)
3
+ self.pop
4
+ self << arg
5
+ end
6
+ end
@@ -0,0 +1,71 @@
1
+ # `extruding-hash` provdes a data structure, based on a Hash of Arrays,
2
+ # that provides a method or two to gather data together in what can be
3
+ # considered _bins_. The Hash functionality provides these _bins_,
4
+ # while the data values themselves are contained in Arrays, providing
5
+ # the concept of _columns_.
6
+ #
7
+ # The `extruding-hash` object has methods where if an added _column_ (in
8
+ # the form of a Hash) has any _bins_ that the object does not already
9
+ # contain, the _bin_ will be added, populated with *nil* values for any
10
+ # _columns_ that had not had a value for that _bin_ - see README.md for
11
+ # more info.
12
+ #
13
+ # Author:: Andrew Burnheimer (mailto:aburnheimer@gmail.com)
14
+ # Copyright:: Copyright (c) 2012
15
+ # License:: Creative Commons 3.0 License. Details can be found in the
16
+ # file LICENSE.
17
+ require File.expand_path File.join(File.dirname(__FILE__), 'array')
18
+
19
+ class ExtrudingHash < Hash
20
+
21
+ # Set this sub-class of hash to the passed hash.
22
+ def set_from_hash( right )
23
+ right.each { |k,v| self[k] = v }
24
+ return self
25
+ end
26
+
27
+ # Add a column into the structure. Respect any _bins_ already in the
28
+ # structure, and add nil values to any existing columns that haven't
29
+ # included the _bins_ of the newly passed-in column.
30
+ def <<( right )
31
+ right.each_key do |key|
32
+ self[key] = Array.new if self[key].nil?
33
+ end
34
+ self.normalize!
35
+
36
+ right.each do |key, value|
37
+ self[key] << value
38
+ end
39
+ self.normalize!
40
+
41
+ return self
42
+ end
43
+
44
+ # The (largest) number of columns among all _bins_ of the Hash.
45
+ def columns
46
+ ret = -1
47
+ self.each do |key, value|
48
+ ret = value.count if value.count > ret
49
+ end
50
+
51
+ if ret < 0
52
+ nil
53
+ else
54
+ ret
55
+ end
56
+ end
57
+
58
+ # Grow any extent short rows to the same width as the longest
59
+ def normalize!
60
+ max = self.columns
61
+
62
+ self.each do |key, value|
63
+ value << nil while value.count < max
64
+ end
65
+
66
+ max
67
+ end
68
+
69
+ end
70
+
71
+ # vim:set et ts=2 sts=2 sw=2 tw=72 wm=72 ai:
@@ -0,0 +1 @@
1
+ require 'extruding-hash/extruding_hash'
@@ -0,0 +1,13 @@
1
+ require File.expand_path File.join(File.dirname(__FILE__), '../lib/extruding-hash/array')
2
+
3
+ describe Array, "#last=" do
4
+ it "sets the last element to a new value" do
5
+ array = Array.new
6
+ array = [ 1, 3, 5 ]
7
+ array.last = 4
8
+ array.last.should eq(4)
9
+ end
10
+ end
11
+
12
+ # vim:set et ts=2 sts=2 sw=2 tw=72 wm=72 ai:
13
+
@@ -0,0 +1,47 @@
1
+ require File.expand_path File.join(File.dirname(__FILE__), '../lib/extruding-hash/extruding_hash')
2
+
3
+ describe ExtrudingHash, "#<<" do
4
+
5
+ it "returns the number of columns contained" do
6
+ test_value = ExtrudingHash.new.set_from_hash( { "a1|b1" => [ 1, 2, 3, 5 ],
7
+ "a3|b3" => [ 2, 3, 5, 8 ], "a4|b4" => [ 3, 5, 8, 13 ] } )
8
+
9
+ test_value.columns.should eq(4)
10
+ end
11
+
12
+ it "can normalize itself such that all bins' value-arrays are as wide as the widest" do
13
+ test_value = ExtrudingHash.new.set_from_hash( { "a1|b1" => [ 1, 2, 3 ],
14
+ "a3|b3" => [ 2, 3, 5, 8 ] } )
15
+ test_value.normalize!
16
+
17
+ expected_value = { "a1|b1" => [ 1, 2, 3, nil], "a3|b3" => [ 2, 3, 5, 8 ] }
18
+
19
+ test_value.should eq(expected_value)
20
+ end
21
+
22
+ it "adds a column where all bins correspond" do
23
+ test_value = ExtrudingHash.new.set_from_hash( { "a1|b1" => [ 1, 2, 3, 5 ],
24
+ "a3|b3" => [ 2, 3, 5, 8 ], "a4|b4" => [ 3, 5, 8, 13 ] } )
25
+
26
+ test_value << { "a1|b1" => 21, "a3|b3" => 34, "a4|b4" => 55 }
27
+
28
+ expected_value = {"a1|b1"=>[1, 2, 3, 5, 21],
29
+ "a3|b3"=>[2, 3, 5, 8, 34], "a4|b4"=>[ 3, 5, 8, 13, 55]}
30
+
31
+ test_value.should eq(expected_value)
32
+ end
33
+
34
+ it "adds a column where all bins do not correspond" do
35
+ test_value = ExtrudingHash.new.set_from_hash( { "a1|b1" => [ 1, 2, 3, 5 ],
36
+ "a3|b3" => [ 2, 3, 5, 8 ], "a4|b4" => [ 3, 5, 8, 13 ] } )
37
+
38
+ test_value << { "a1|b1" => 21, "a2|b2" => 34, "a4|b4" => 55 }
39
+
40
+ expected_value = {"a1|b1"=>[1, 2, 3, 5, 21],
41
+ "a2|b2"=>[nil, nil, nil, nil, 34], "a3|b3"=>[2, 3, 5, 8, nil],
42
+ "a4|b4"=>[ 3, 5, 8, 13, 55]}
43
+
44
+ test_value.should eq(expected_value)
45
+ end
46
+
47
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: extruding-hash
3
+ version: !ruby/object:Gem::Version
4
+ hash: 59
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 9
9
+ - 0
10
+ version: 0.9.0
11
+ platform: ruby
12
+ authors:
13
+ - Andrew Burnheimer
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-10-17 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: |-
22
+ Data structure, based on a Hash of Arrays, that
23
+ provides a method or two to gather data together in what can be
24
+ considered "bins".
25
+ email: Andrew_Burnheimer@cable.comcast.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - .gitignore
34
+ - .rspec
35
+ - LICENSE
36
+ - README.md
37
+ - extruding-hash.gemspec
38
+ - lib/extruding-hash.rb
39
+ - lib/extruding-hash/array.rb
40
+ - lib/extruding-hash/extruding_hash.rb
41
+ - spec/array_spec.rb
42
+ - spec/extruding_hash_spec.rb
43
+ homepage: https://github.com/aburnheimer/extruding-hash
44
+ licenses:
45
+ - CC-BY-3.0
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.10
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Hash structure for histograms
76
+ test_files:
77
+ - spec/array_spec.rb
78
+ - spec/extruding_hash_spec.rb