comprehend 0.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,96 @@
1
+ #include <ruby.h>
2
+
3
+ #define RARRAY_SHARED_ROOT_FLAG FL_USER5
4
+ #define ARY_SHARED_ROOT_P(ary) (FL_TEST((ary), RARRAY_SHARED_ROOT_FLAG))
5
+ # define ARY_EMBED_P(ary) \
6
+ FL_TEST((ary), RARRAY_EMBED_FLAG)!=0
7
+ #define ARY_SET_EMBED_LEN(ary, n) do { \
8
+ long tmp_n = (n); \
9
+ RBASIC(ary)->flags &= ~RARRAY_EMBED_LEN_MASK; \
10
+ RBASIC(ary)->flags |= (tmp_n) << RARRAY_EMBED_LEN_SHIFT; \
11
+ } while (0)
12
+ #define ARY_SET_HEAP_LEN(ary, n) do { \
13
+ RARRAY(ary)->as.heap.len = (n); \
14
+ } while (0)
15
+ #define ARY_SET_LEN(ary, n) do { \
16
+ if (ARY_EMBED_P(ary)) { \
17
+ ARY_SET_EMBED_LEN((ary), (n)); \
18
+ } \
19
+ else { \
20
+ ARY_SET_HEAP_LEN((ary), (n)); \
21
+ } \
22
+ } while (0)
23
+ #define ARY_CAPA(ary) (ARY_EMBED_P(ary) ? RARRAY_EMBED_LEN_MAX : \
24
+ ARY_SHARED_ROOT_P(ary) ? RARRAY_LEN(ary) : RARRAY(ary)->as.heap.aux.capa)
25
+ #define ARY_DEFAULT_SIZE 16
26
+ #define ARY_SET_CAPA(ary, n) do { \
27
+ RARRAY(ary)->as.heap.aux.capa = (n); \
28
+ } while (0)
29
+
30
+ static void ary_resize_capa(VALUE ary, long capacity)
31
+ {
32
+ if (capacity > RARRAY_EMBED_LEN_MAX) {
33
+ if (ARY_EMBED_P(ary)) {
34
+ long len = ARY_EMBED_LEN(ary);
35
+ VALUE *ptr = ALLOC_N(VALUE, (capacity));
36
+ MEMCPY(ptr, ARY_EMBED_PTR(ary), VALUE, len);
37
+ FL_UNSET_EMBED(ary);
38
+ ARY_SET_PTR(ary, ptr);
39
+ ARY_SET_HEAP_LEN(ary, len);
40
+ }
41
+ else {
42
+ REALLOC_N(RARRAY(ary)->as.heap.ptr, VALUE, (capacity));
43
+ }
44
+ ARY_SET_CAPA(ary, (capacity));
45
+ }
46
+ else {
47
+ if (!ARY_EMBED_P(ary)) {
48
+ long len = RARRAY_LEN(ary);
49
+ VALUE *ptr = RARRAY_PTR(ary);
50
+ if (len > capacity) len = capacity;
51
+ MEMCPY(RARRAY(ary)->as.ary, ptr, VALUE, len);
52
+ FL_SET_EMBED(ary);
53
+ ARY_SET_LEN(ary, len);
54
+ xfree(ptr);
55
+ }
56
+ }
57
+ }
58
+
59
+ static VALUE rb_ary_comprehend_bang(VALUE ary)
60
+ {
61
+ VALUE *p, *t, *end;
62
+ long n;
63
+
64
+ rb_ary_modify(ary);
65
+ p = t = RARRAY_PTR(ary);
66
+ end = p + RARRAY_LEN(ary);
67
+
68
+ while (t < end) {
69
+ *t = rb_yield(*t);
70
+ if (NIL_P(*t)) t++;
71
+ else *p++ = *t++;
72
+ }
73
+ n = p - RARRAY_PTR(ary);
74
+ if (RARRAY_LEN(ary) == n) {
75
+ return Qnil;
76
+ }
77
+ ARY_SET_LEN(ary, n);
78
+ if (n * 2 < ARY_CAPA(ary) && ARY_DEFAULT_SIZE * 2 < ARY_CAPA(ary)) {
79
+ ary_resize_capa(ary, n * 2);
80
+ }
81
+
82
+ return ary;
83
+ }
84
+
85
+ static VALUE rb_ary_comprehend(ary)
86
+ VALUE ary;
87
+ {
88
+ ary = rb_ary_dup(ary);
89
+ rb_ary_comprehend_bang(ary);
90
+ return ary;
91
+ }
92
+
93
+ void Init_comprehend(void) {
94
+ rb_define_method(rb_cArray, "comprehend!", rb_ary_comprehend_bang, 0);
95
+ rb_define_method(rb_cArray, "comprehend", rb_ary_comprehend, 0);
96
+ }
@@ -0,0 +1,2 @@
1
+ require 'mkmf'
2
+ create_makefile('comprehend/comprehend')
data/lib/comprehend.rb ADDED
@@ -0,0 +1 @@
1
+ require "comprehend/comprehend"
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: comprehend
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aaron Weiner
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: ! '[1,2,3].comprehend{ |i| i.to_s if i<3 } == ["1","2"]'
31
+ email: aweiner@mdsol.com
32
+ executables: []
33
+ extensions:
34
+ - ext/comprehend/extconf.rb
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/comprehend.rb
38
+ - ext/comprehend/comprehend.c
39
+ - ext/comprehend/extconf.rb
40
+ homepage: http://github.com/mdsol-share/comprehend
41
+ licenses:
42
+ - MIT
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: 1.9.3
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.24
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Transform and compact an array in one iteration
65
+ test_files: []
66
+ has_rdoc: