array2hash 0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f3ff053477da70e0c4c1bb5122de7dc9bca8694d
4
+ data.tar.gz: a67a4d838a3a96d486805d868661608b9a9d78e1
5
+ SHA512:
6
+ metadata.gz: a3c28bd1975af4860747569c57991b3c266688c2aa002633e7358ac5bd1e74509fa4bf1bf4c0991a9cd019f72ab912fbfc0f98b9522c59e46cae04e961c94640
7
+ data.tar.gz: 9144eeb575f9e798aaaab975fab0c9984ef351bb26f135c8f1c432c1ee6e8227440d040b58d258591dc7e085643674cea7e565373a0387bf980956adc9ed3a13
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ array2hash (0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ racc (1.4.14)
10
+ rake (11.3.0)
11
+ rake-compiler (1.0.4)
12
+ rake
13
+ rubex (0.1)
14
+ racc (~> 1.4.14)
15
+ rake-compiler (~> 1.0.4)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ array2hash!
22
+ rake (~> 11.2)
23
+ rubex (~> 0.1)
24
+
25
+ BUNDLED WITH
26
+ 1.15.3
data/LICENSE ADDED
@@ -0,0 +1,29 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2017, Sameer Deshmukh
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ * Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ * Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ * Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,65 @@
1
+ # array2hash
2
+ Efficient conversion of a Ruby Array to a lookup table-like Hash written in Rubex.
3
+
4
+ This Ruby gem is a C extension written in [Rubex]() that can convert a Ruby Array to a Hash lookup table. Basically the following Array:
5
+ ``` ruby
6
+ ["1", 2, :three, "4"]
7
+ ```
8
+ ...will be converted into a Hash that looks like this:
9
+ ``` ruby
10
+ {
11
+ "1" => 0,
12
+ 2 => 1,
13
+ :three => 2,
14
+ "4" => 3
15
+ }
16
+ ```
17
+
18
+ The reason for making this gem is mainly speed. The conventional way of the above conversion would be as follows:
19
+ ``` ruby
20
+ a = ["1", 2, :three, "4"]
21
+ a.each_with_index.to_h
22
+ ```
23
+
24
+ This extension makes the above process upto 50% faster with a C extension. Here is the benchmarking code:
25
+ ``` ruby
26
+ require 'benchmark/ips'
27
+ a = (1..1000).to_a.map(&:to_s)
28
+ Benchmark.ips do |x|
29
+ x.report("convert") do
30
+ Array2Hash.convert a
31
+ end
32
+
33
+ x.report("each_with_index.to_h") do
34
+ a.each_with_index.to_h
35
+ end
36
+
37
+ x.compare!
38
+ end
39
+ ```
40
+
41
+ The results are as follows:
42
+ ```
43
+ Warming up --------------------------------------
44
+ convert 368.000 i/100ms
45
+ each_with_index.to_h 236.000 i/100ms
46
+ Calculating -------------------------------------
47
+ convert 3.488k (± 9.8%) i/s - 17.296k in 5.012260s
48
+ each_with_index.to_h 2.192k (± 8.3%) i/s - 11.092k in 5.097432s
49
+
50
+ Comparison:
51
+ convert: 3487.8 i/s
52
+ each_with_index.to_h: 2192.3 i/s - 1.59x slower
53
+ ```
54
+
55
+ # Usage
56
+
57
+ Install with `gem install array2hash`.
58
+
59
+ The use with the `convert` class method:
60
+ ``` ruby
61
+ require 'array2hash'
62
+
63
+ a = (1..1000).to_a.map(:&to_s)
64
+ Array2Hash.convert a
65
+ ```
@@ -0,0 +1,3 @@
1
+ require 'rubex/rake_task'
2
+
3
+ Rubex::RakeTask.new('array2hash')
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ $:.unshift File.expand_path("../lib", __FILE__)
3
+
4
+ require 'array2hash/version.rb'
5
+
6
+ Array2Hash::DESCRIPTION = <<MSG
7
+ Efficient conversion of a Ruby Array to a lookup table-like Hash written in Rubex via a C extension.
8
+ MSG
9
+
10
+ Gem::Specification.new do |spec|
11
+ spec.name = 'array2hash'
12
+ spec.version = Array2Hash::VERSION
13
+ spec.authors = ['Sameer Deshmukh']
14
+ spec.email = ['sameer.deshmukh93@gmail.com']
15
+ spec.summary = Array2Hash::DESCRIPTION
16
+ spec.description = Array2Hash::DESCRIPTION
17
+ spec.homepage = "http://github.com/v0dro/array2hash"
18
+ spec.license = 'BSD-3-Clause'
19
+
20
+ spec.files = `git ls-files -z`.split("\x0")
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = ["lib"]
24
+ spec.extensions = ['ext/array2hash/extconf.rb']
25
+
26
+ spec.add_development_dependency 'rubex', '~> 0.1'
27
+ spec.add_development_dependency 'rake', '~> 11.2'
28
+ end
@@ -0,0 +1,73 @@
1
+ /* C extension for array2hash.
2
+ This file in generated by Rubex::Compiler. Do not change!
3
+ File generation time: 2017-08-29 00:01:30 +0530.*/
4
+
5
+ #include <ruby.h>
6
+ #include <stdint.h>
7
+ #include <stdbool.h>
8
+ #include <ruby.h>
9
+
10
+ #define __rubex_INT2BOOL(arg) (arg ? Qtrue : Qfalse)
11
+
12
+
13
+ VALUE rb_cObject;
14
+ VALUE __rubex_rb_cls_Array2Hash;
15
+ static VALUE __rubex_rb_f_Array2Hash_convert (int argc,VALUE* argv,VALUE __rubex_arg_self);
16
+
17
+ VALUE __rubex_char2rubystr(char ch);
18
+ VALUE __rubex_char2rubystr(char ch)
19
+ {
20
+ char s[2];
21
+ s[0] = ch;
22
+ s[1] = '\0';
23
+ return rb_str_new2(s);
24
+ }
25
+
26
+
27
+ static VALUE __rubex_rb_f_Array2Hash_convert (int argc,VALUE* argv,VALUE __rubex_arg_self)
28
+ {
29
+ VALUE __rubex_arg_a;
30
+ long int __rubex_v_i;
31
+ long int __rubex_v_j;
32
+ VALUE __rubex_v_result;
33
+ VALUE __rubex_temp_1;
34
+ if (argc < 1)
35
+ {
36
+ rb_raise(rb_eArgError, "Need 1 args, not %d", argc);
37
+ }
38
+
39
+ __rubex_arg_a=argv[0];
40
+ __rubex_v_i = NUM2LONG(rb_funcall(__rubex_arg_a, rb_intern("size"), 0, NULL));
41
+ __rubex_v_j = 0;
42
+
43
+ /* Rubex file location: /home/sameer/gitrepos/array2hash/ext/array2hash/array2hash.rubex:10 */
44
+ __rubex_temp_1 = rb_hash_new();
45
+ __rubex_v_result = __rubex_temp_1;
46
+ __rubex_temp_1 = 0;
47
+ __rubex_temp_1 = 0;
48
+ while (( __rubex_v_j < __rubex_v_i ))
49
+ {
50
+
51
+ /* Rubex file location: /home/sameer/gitrepos/array2hash/ext/array2hash/array2hash.rubex:13 */
52
+ rb_hash_aset(__rubex_v_result,RARRAY_AREF(__rubex_arg_a,__rubex_v_j),LONG2NUM(__rubex_v_j));
53
+
54
+ /* Rubex file location: /home/sameer/gitrepos/array2hash/ext/array2hash/array2hash.rubex:14 */
55
+ __rubex_v_j = ( __rubex_v_j + 1 );
56
+ }
57
+
58
+
59
+ /* Rubex file location: /home/sameer/gitrepos/array2hash/ext/array2hash/array2hash.rubex:17 */
60
+ return __rubex_v_result;
61
+ }
62
+
63
+
64
+ void Init_array2hash ();
65
+ void Init_array2hash ()
66
+ {
67
+ VALUE __rubex_rb_cls_Array2Hash;
68
+
69
+ __rubex_rb_cls_Array2Hash = rb_define_class("Array2Hash", rb_cObject);
70
+
71
+ rb_define_singleton_method(__rubex_rb_cls_Array2Hash ,"convert", __rubex_rb_f_Array2Hash_convert, -1);
72
+ }
73
+
@@ -0,0 +1,19 @@
1
+ lib "<ruby.h>"
2
+ object rb_hash_aset(object, object, object)
3
+ object RARRAY_AREF(object, long int)
4
+ end
5
+
6
+ class Array2Hash
7
+ def self.convert(a)
8
+ long int i = a.size
9
+ long int j = 0
10
+ result = {}
11
+
12
+ while j < i do
13
+ rb_hash_aset(result, RARRAY_AREF(a, j), j)
14
+ j += 1
15
+ end
16
+
17
+ return result
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+ $libs += " "
3
+ create_makefile('ext/array2hash/array2hash')
@@ -0,0 +1 @@
1
+ require 'array2hash.so'
Binary file
@@ -0,0 +1,3 @@
1
+ class Array2Hash
2
+ VERSION = "0.1".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: array2hash
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Sameer Deshmukh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubex
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '11.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '11.2'
41
+ description: 'Efficient conversion of a Ruby Array to a lookup table-like Hash written
42
+ in Rubex via a C extension.
43
+
44
+ '
45
+ email:
46
+ - sameer.deshmukh93@gmail.com
47
+ executables: []
48
+ extensions:
49
+ - ext/array2hash/extconf.rb
50
+ extra_rdoc_files: []
51
+ files:
52
+ - ".gitignore"
53
+ - Gemfile
54
+ - Gemfile.lock
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - array2hash.gemspec
59
+ - ext/array2hash/array2hash.c
60
+ - ext/array2hash/array2hash.rubex
61
+ - ext/array2hash/extconf.rb
62
+ - lib/array2hash.rb
63
+ - lib/array2hash.so
64
+ - lib/array2hash/version.rb
65
+ homepage: http://github.com/v0dro/array2hash
66
+ licenses:
67
+ - BSD-3-Clause
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.5.2
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Efficient conversion of a Ruby Array to a lookup table-like Hash written
89
+ in Rubex via a C extension.
90
+ test_files: []