Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
9422fdbc
Unverified
Commit
9422fdbc
authored
May 14, 2021
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mruby-array-ext/array.c: implement `Array#compact` in C.
parent
bdb5d85a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
36 deletions
+59
-36
mrbgems/mruby-array-ext/mrblib/array.rb
mrbgems/mruby-array-ext/mrblib/array.rb
+0
-35
mrbgems/mruby-array-ext/src/array.c
mrbgems/mruby-array-ext/src/array.c
+59
-1
No files found.
mrbgems/mruby-array-ext/mrblib/array.rb
View file @
9422fdbc
...
...
@@ -294,41 +294,6 @@ class Array
end
end
##
# call-seq:
# ary.compact -> new_ary
#
# Returns a copy of +self+ with all +nil+ elements removed.
#
# [ "a", nil, "b", nil, "c", nil ].compact
# #=> [ "a", "b", "c" ]
#
def
compact
result
=
self
.
dup
result
.
compact!
result
end
##
# call-seq:
# ary.compact! -> ary or nil
#
# Removes +nil+ elements from the array.
# Returns +nil+ if no changes were made, otherwise returns
# <i>ary</i>.
#
# [ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ]
# [ "a", "b", "c" ].compact! #=> nil
#
def
compact!
result
=
self
.
select
{
|
e
|
!
e
.
nil?
}
if
result
.
size
==
self
.
size
nil
else
self
.
replace
(
result
)
end
end
# for efficiency
def
reverse_each
(
&
block
)
return
to_enum
:reverse_each
unless
block
...
...
mrbgems/mruby-array-ext/src/array.c
View file @
9422fdbc
...
...
@@ -183,6 +183,62 @@ mrb_ary_slice_bang(mrb_state *mrb, mrb_value self)
return
ary
;
}
/*
* call-seq:
* ary.compact -> new_ary
*
* Returns a copy of +self+ with all +nil+ elements removed.
*
* [ "a", nil, "b", nil, "c", nil ].compact
* #=> [ "a", "b", "c" ]
*/
static
mrb_value
mrb_ary_compact
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_value
ary
=
mrb_ary_new
(
mrb
);
mrb_int
len
=
RARRAY_LEN
(
self
);
mrb_value
*
p
=
RARRAY_PTR
(
self
);
for
(
mrb_int
i
=
0
;
i
<
len
;
++
i
)
{
if
(
!
mrb_nil_p
(
p
[
i
]))
{
mrb_ary_push
(
mrb
,
ary
,
p
[
i
]);
}
}
return
ary
;
}
/*
* call-seq:
* ary.compact! -> ary or nil
*
* Removes +nil+ elements from the array.
* Returns +nil+ if no changes were made, otherwise returns
* <i>ary</i>.
*
* [ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ]
* [ "a", "b", "c" ].compact! #=> nil
*/
static
mrb_value
mrb_ary_compact_bang
(
mrb_state
*
mrb
,
mrb_value
self
)
{
struct
RArray
*
a
=
mrb_ary_ptr
(
self
);
mrb_int
i
,
j
=
0
;
mrb_int
len
=
ARY_LEN
(
a
);
mrb_value
*
p
=
ARY_PTR
(
a
);
mrb_ary_modify
(
mrb
,
a
);
for
(
i
=
0
;
i
<
len
;
++
i
)
{
if
(
!
mrb_nil_p
(
p
[
i
]))
{
if
(
i
!=
j
)
p
[
j
]
=
p
[
i
];
j
++
;
}
}
if
(
i
==
j
)
return
mrb_nil_value
();
if
(
j
<
len
)
ARY_SET_LEN
(
RARRAY
(
self
),
j
);
return
self
;
}
void
mrb_mruby_array_ext_gem_init
(
mrb_state
*
mrb
)
{
...
...
@@ -192,7 +248,9 @@ mrb_mruby_array_ext_gem_init(mrb_state* mrb)
mrb_define_method
(
mrb
,
a
,
"at"
,
mrb_ary_at
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
a
,
"rassoc"
,
mrb_ary_rassoc
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
a
,
"values_at"
,
mrb_ary_values_at
,
MRB_ARGS_ANY
());
mrb_define_method
(
mrb
,
a
,
"slice!"
,
mrb_ary_slice_bang
,
MRB_ARGS_ARG
(
1
,
1
));
mrb_define_method
(
mrb
,
a
,
"slice!"
,
mrb_ary_slice_bang
,
MRB_ARGS_ARG
(
1
,
1
));
mrb_define_method
(
mrb
,
a
,
"compact"
,
mrb_ary_compact
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
a
,
"compact!"
,
mrb_ary_compact_bang
,
MRB_ARGS_NONE
());
}
void
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment