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
3554fc54
Commit
3554fc54
authored
Aug 25, 2018
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a new function `mrb_hash_merge()`.
parent
b06dad43
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
0 deletions
+43
-0
include/mruby/hash.h
include/mruby/hash.h
+10
-0
src/hash.c
src/hash.c
+33
-0
No files found.
include/mruby/hash.h
View file @
3554fc54
...
...
@@ -174,6 +174,16 @@ MRB_API mrb_value mrb_hash_clear(mrb_state *mrb, mrb_value hash);
*/
MRB_API
mrb_value
mrb_hash_dup
(
mrb_state
*
mrb
,
mrb_value
hash
);
/*
* Merges two hashes. The first hash will be modified by the
* second hash.
*
* @param mrb The mruby state reference.
* @param hash1 The target hash.
* @param hash2 Updating hash
*/
MRB_API
void
mrb_hash_merge
(
mrb_state
*
mrb
,
mrb_value
hash1
,
mrb_value
hash2
);
/* declaration of struct kh_ht */
/* be careful when you touch the internal */
typedef
struct
{
...
...
src/hash.c
View file @
3554fc54
...
...
@@ -951,6 +951,39 @@ mrb_hash_has_value(mrb_state *mrb, mrb_value hash)
return
mrb_false_value
();
}
MRB_API
void
mrb_hash_merge
(
mrb_state
*
mrb
,
mrb_value
hash1
,
mrb_value
hash2
)
{
khash_t
(
ht
)
*
h1
;
khash_t
(
ht
)
*
h2
;
khiter_t
k
;
mrb_hash_modify
(
mrb
,
hash1
);
hash2
=
mrb_check_hash_type
(
mrb
,
hash2
);
h1
=
RHASH_TBL
(
hash1
);
h2
=
RHASH_TBL
(
hash2
);
if
(
!
h1
)
{
RHASH_TBL
(
hash1
)
=
kh_copy
(
ht
,
mrb
,
h2
);
return
;
}
for
(
k
=
kh_begin
(
h2
);
k
!=
kh_end
(
h2
);
k
++
)
{
khiter_t
k1
;
int
r
;
if
(
!
kh_exist
(
h2
,
k
))
continue
;
k1
=
kh_put2
(
ht
,
mrb
,
h1
,
kh_key
(
h2
,
k
),
&
r
);
kh_value
(
h1
,
k1
).
v
=
kh_value
(
h2
,
k
).
v
;
if
(
r
!=
0
)
{
/* expand */
kh_key
(
h1
,
k1
)
=
kh_key
(
h2
,
k
);
kh_value
(
h1
,
k1
).
n
=
kh_size
(
h1
)
-
1
;
}
}
mrb_write_barrier
(
mrb
,
(
struct
RBasic
*
)
RHASH
(
hash1
));
return
;
}
void
mrb_init_hash
(
mrb_state
*
mrb
)
{
...
...
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