array_collapse 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/array_collapse/array_collapse.c +11 -1
- data/spec/array_collapse_spec.rb +24 -17
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc8d9055f4a22a1da3a2ccd1a719be77bb10538a
|
4
|
+
data.tar.gz: 32674b90af90ebe7576d3a35ea87753446503aef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 51a5ffe149f4156d336966cfb94d6a15d65d10cfffc0d8ef994a6dc4ee1d45ceef97c16ed692ab90fe512285deed8a775dfbe30384c9fe7f8e2be177825ce4f3
|
7
|
+
data.tar.gz: b068a250dfab3841830bf01d7d536515329b7391e5182efd66da2333d683179e03163ad467e85feea8f56b06adeeb50a2218f6f7cbcc5eb104ff9eb320236a8c
|
@@ -6,6 +6,10 @@ VALUE method_collapse(VALUE self, VALUE args) {
|
|
6
6
|
long len = RARRAY_LEN(self);
|
7
7
|
VALUE result = rb_ary_new2(len);
|
8
8
|
VALUE stack = rb_ary_new();
|
9
|
+
st_table *seen_nodes;
|
10
|
+
st_data_t id;
|
11
|
+
seen_nodes = st_init_numtable();
|
12
|
+
st_insert(seen_nodes, (st_data_t)self, (st_data_t)Qtrue);
|
9
13
|
|
10
14
|
#ifdef LOGGING
|
11
15
|
FILE *f = fopen("clog.txt", "w");
|
@@ -23,8 +27,14 @@ VALUE method_collapse(VALUE self, VALUE args) {
|
|
23
27
|
long i;
|
24
28
|
for(i = 0; i < RARRAY_LEN(array); i++) {
|
25
29
|
VALUE e = RARRAY_AREF(array,i);
|
30
|
+
id = (st_data_t)e;
|
31
|
+
if (st_lookup(seen_nodes, id, 0)) {
|
32
|
+
// drop recursive node
|
33
|
+
break;
|
34
|
+
}
|
26
35
|
switch (TYPE(e)) {
|
27
36
|
case T_ARRAY:
|
37
|
+
st_insert(seen_nodes, id, (st_data_t)Qtrue);
|
28
38
|
rb_ary_push(stack, e);
|
29
39
|
break;
|
30
40
|
default:
|
@@ -38,7 +48,7 @@ VALUE method_collapse(VALUE self, VALUE args) {
|
|
38
48
|
}
|
39
49
|
}
|
40
50
|
}
|
41
|
-
|
51
|
+
st_free_table(seen_nodes);
|
42
52
|
return result;
|
43
53
|
}
|
44
54
|
|
data/spec/array_collapse_spec.rb
CHANGED
@@ -1,21 +1,28 @@
|
|
1
1
|
require 'array_collapse'
|
2
2
|
|
3
|
-
RSpec.describe
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
3
|
+
RSpec.describe Array do
|
4
|
+
describe '#collapse' do
|
5
|
+
it 'lives on Array' do
|
6
|
+
expect([1].respond_to?(:collapse)).to eq(true)
|
7
|
+
end
|
8
|
+
it 'applies a block to every element' do
|
9
|
+
expect([1, 2].collapse { |e| e * 2 }).to eq([2, 4])
|
10
|
+
end
|
11
|
+
it 'flattens a nested array' do
|
12
|
+
expect([1, [2]].collapse).to eq([1, 2])
|
13
|
+
end
|
14
|
+
it 'flattens and maps' do
|
15
|
+
expect([1, [2, [3]]].collapse { |e| e * 2 }).to eq([2, 4, 6])
|
16
|
+
end
|
17
|
+
it 'drops nils' do
|
18
|
+
expect([nil, 1, [2]].collapse).to eq([1, 2])
|
19
|
+
expect([1, nil, [2, [3, nil]]].collapse { |e| e.nil? ? e : e * 2 }).
|
20
|
+
to eq([2, 4, 6])
|
21
|
+
end
|
22
|
+
it 'handles recursive arrays' do
|
23
|
+
rec_a = [1, [2, [3]]]
|
24
|
+
rec_a << rec_a
|
25
|
+
expect(rec_a.collapse).to eq([1, 2, 3])
|
26
|
+
end
|
20
27
|
end
|
21
28
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: array_collapse
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Moore-Niemi
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake-compiler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rspec
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -100,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
114
|
version: '0'
|
101
115
|
requirements: []
|
102
116
|
rubyforge_project:
|
103
|
-
rubygems_version: 2.
|
117
|
+
rubygems_version: 2.6.3
|
104
118
|
signing_key:
|
105
119
|
specification_version: 4
|
106
120
|
summary: Array#collapse
|