fast_slice 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 76c299152c97820445861f419759fef22f36cd79
4
- data.tar.gz: c1ab82924be78c347b0be49fa06053c8637ef7e0
3
+ metadata.gz: ad4a9a197c0d531c6a12eb2ba3dcb8b6dbe677b6
4
+ data.tar.gz: c34e41dc8b0989a9db29a725256e0a061132eb29
5
5
  SHA512:
6
- metadata.gz: d9fdb9db080a35d97169ec64f5b78a97086f5678e01d0262728928bccccb632b5fa4f0bab68a9300200ad0c27c0dc2299aec6556b1710170a6bcb3a002956fdd
7
- data.tar.gz: 4cbdd2934cec23b7d1ea3db75ce4a8dc81a4bf00aabbc07ea3efd99f762c6249a172090ea8e32588afa9315f45a1d1cb7854ca982856b55daf5c0c75f36f1346
6
+ metadata.gz: 8f79eaef84b1910772c27923b348346dcf318bfdb5b607c73b5290a86af8df77acf3f02940354325e24970ee80b08a8bceff8a1f2f5a0c4f67dfd0c70b2e3ec2
7
+ data.tar.gz: 51ac330d592cea1611037e9d4e6629f3924606b5210b7b6914c0489a804c7c754fbc1318d410d7e2844a83fe52b973d12ac027f6b5ab48301eca54aa2ef38a14
@@ -1,9 +1,11 @@
1
1
  #include <ruby.h>
2
2
 
3
+ static VALUE FastSlice = Qnil;
4
+
3
5
  // Effectively a copy and paste from hash.c
4
6
  // but replacing RHASH(hash)->tbl with RHASH_TBL (RHASH is not public)
5
- VALUE
6
- __rb_hash_has_key(VALUE hash, VALUE key)
7
+ static VALUE
8
+ rb_hash_has_key(VALUE hash, VALUE key)
7
9
  {
8
10
  if (!RHASH_TBL(hash))
9
11
  return Qfalse;
@@ -13,12 +15,9 @@ __rb_hash_has_key(VALUE hash, VALUE key)
13
15
  return Qfalse;
14
16
  }
15
17
 
16
- VALUE
17
- rb_fast_slice(int argc, VALUE *argv, VALUE self)
18
+ static VALUE
19
+ fast_slice(VALUE hash, VALUE keys)
18
20
  {
19
- VALUE hash, args;
20
- rb_scan_args(argc, argv, "1*", &hash, &args);
21
-
22
21
  if (TYPE(hash) != T_HASH) {
23
22
  rb_raise(rb_eTypeError, "expected a Hash");
24
23
  return Qfalse;
@@ -29,11 +28,11 @@ rb_fast_slice(int argc, VALUE *argv, VALUE self)
29
28
  result = rb_hash_new();
30
29
 
31
30
  int i;
32
- for (i=0; i < RARRAY_LEN(args); i++) {
33
- VALUE arg;
34
- arg = rb_ary_entry(args, i);
35
- if(__rb_hash_has_key(hash, arg) == Qtrue) {
36
- rb_hash_aset(result, arg, rb_hash_aref(hash, arg));
31
+ for (i=0; i < RARRAY_LEN(keys); i++) {
32
+ VALUE key;
33
+ key = rb_ary_entry(keys, i);
34
+ if(rb_hash_has_key(hash, key) == Qtrue) {
35
+ rb_hash_aset(result, key, rb_hash_aref(hash, key));
37
36
  }
38
37
  }
39
38
 
@@ -43,20 +42,33 @@ rb_fast_slice(int argc, VALUE *argv, VALUE self)
43
42
  return rb_hash_dup(hash);
44
43
  }
45
44
 
46
- // Add the `slice` instance method to Hash
47
- // TODO figure out how to do this
48
- VALUE rb_define_hash_slice(int argc, VALUE *argv, VALUE self)
45
+ VALUE
46
+ rb_fast_slice_m(int argc, VALUE *argv, VALUE self)
49
47
  {
50
- rb_define_method(rb_cHash, "slice", rb_fast_slice, -1);
48
+ VALUE hash, args;
49
+ rb_scan_args(argc, argv, "1*", &hash, &args);
50
+ return fast_slice(hash, args);
51
+ }
51
52
 
53
+ VALUE
54
+ rb_fast_slice(int argc, VALUE *argv, VALUE self)
55
+ {
56
+ VALUE args;
57
+ rb_scan_args(argc, argv, "0*", &args);
58
+ return fast_slice(self, args);
59
+ }
60
+
61
+ static VALUE
62
+ rb_define_hash_slice_m(void)
63
+ {
64
+ rb_define_method(rb_cHash, "slice", rb_fast_slice, -1);
52
65
  return Qtrue;
53
66
  }
54
67
 
55
68
  void Init_fast_slice( void )
56
69
  {
57
- VALUE FastSlice = Qnil;
58
- FastSlice = rb_define_class_under(rb_cObject, "FastSlice", rb_cObject);
59
-
60
- rb_define_singleton_method(FastSlice, "slice", rb_fast_slice, -1);
61
- // rb_define_singleton_method(FastSlice, "define_on_hash", rb_define_hash_slice, 0);
70
+ FastSlice = rb_define_module_under(rb_cObject, "FastSlice");
71
+ rb_define_singleton_method(FastSlice, "slice", rb_fast_slice_m, -1);
72
+ rb_define_method(FastSlice, "slice", rb_fast_slice, -1);
73
+ rb_define_singleton_method(FastSlice, "define_on_hash", rb_define_hash_slice_m, 0);
62
74
  }
@@ -0,0 +1,3 @@
1
+ module FastSlice
2
+ VERSION = "1.0.1"
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_slice
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Cook
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-14 00:00:00.000000000 Z
11
+ date: 2017-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -47,6 +47,7 @@ extra_rdoc_files: []
47
47
  files:
48
48
  - ext/fast_slice/extconf.rb
49
49
  - ext/fast_slice/fast_slice.c
50
+ - lib/fast_slice/version.rb
50
51
  homepage:
51
52
  licenses: []
52
53
  metadata: {}