From 2250171071320a480b7d3250183356ede80d02bd Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 25 Jul 2025 08:08:28 +0900 Subject: [PATCH] mruby-array-ext: refactor ary_uniq to use ary_uniq_bang This removes code duplication by making ary_uniq call ary_uniq_bang on a duplicated array, centralizing the uniqueness logic. Co-authored-by: Gemini --- mrbgems/mruby-array-ext/src/array.c | 58 +++++++---------------------- 1 file changed, 13 insertions(+), 45 deletions(-) diff --git a/mrbgems/mruby-array-ext/src/array.c b/mrbgems/mruby-array-ext/src/array.c index 060134995..0f28b7498 100644 --- a/mrbgems/mruby-array-ext/src/array.c +++ b/mrbgems/mruby-array-ext/src/array.c @@ -880,51 +880,6 @@ ary_fill_exec(mrb_state *mrb, mrb_value self) return self; } -/* - * Internal helper for Array#uniq without blocks. - * Uses hash-based deduplication for large arrays, - * linear search for small arrays. - */ -static mrb_value -ary_uniq(mrb_state *mrb, mrb_value self) -{ - mrb_int len = RARRAY_LEN(self); - mrb_value result = mrb_ary_new_capa(mrb, len); - - if (len == 0) { - return result; - } - - if (len > SET_OP_HASH_THRESHOLD) { - mrb_value hash = mrb_hash_new_capa(mrb, len); - for (mrb_int i = 0; i < len; i++) { - mrb_value elem = RARRAY_PTR(self)[i]; - if (mrb_nil_p(mrb_hash_get(mrb, hash, elem))) { - mrb_hash_set(mrb, hash, elem, mrb_true_value()); - mrb_ary_push(mrb, result, elem); - } - } - } - else { - for (mrb_int i = 0; i < len; i++) { - mrb_value elem = RARRAY_PTR(self)[i]; - mrb_bool found = FALSE; - mrb_int result_len = RARRAY_LEN(result); - for (mrb_int j = 0; j < result_len; j++) { - if (mrb_equal(mrb, elem, RARRAY_PTR(result)[j])) { - found = TRUE; - break; - } - } - if (!found) { - mrb_ary_push(mrb, result, elem); - } - } - } - - return result; -} - /* * Internal helper for Array#uniq! without blocks. * Modifies array in-place, returns nil if no changes. @@ -981,6 +936,19 @@ ary_uniq_bang(mrb_state *mrb, mrb_value self) return self; } +/* + * Internal helper for Array#uniq without blocks. + * Uses hash-based deduplication for large arrays, + * linear search for small arrays. + */ +static mrb_value +ary_uniq(mrb_state *mrb, mrb_value self) +{ + mrb_value ary = mrb_ary_dup(mrb, self); + ary_uniq_bang(mrb, ary); + return ary; +} + /* Internal helper for flatten operations using iterative stack-based approach */ static mrb_value flatten_internal(mrb_state *mrb, mrb_value self, mrb_int level, mrb_bool *modified)