mirror of
https://github.com/ruby/ruby.git
synced 2026-10-28 16:36:20 +08:00
Use rb_len_t for lengths in hash.c, st.c and set.c
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cfabfcd520
commit
77f04f52ee
3 files changed
+46
-46
Notes:
git
2026-10-09 05:02:07 +00:00
No files matched your search
@@ -1943,9 +1943,9 @@ rb_hash_init(rb_execution_context_t *ec, VALUE hash, VALUE capa_value, VALUE ifn
|
||||
rb_hash_modify(hash);
|
||||
|
||||
if (capa_value != INT2FIX(0)) {
|
||||
long capa = NUM2LONG(capa_value);
|
||||
rb_len_t capa = NUM2LEN(capa_value);
|
||||
if (capa > 0 && RHASH_AR_TABLE_P(hash) && RHASH_SIZE(hash) == 0 &&
|
||||
(unsigned long)capa > RHASH_AR_TABLE_MAX_BOUND(hash)) {
|
||||
(rb_ulen_t)capa > RHASH_AR_TABLE_MAX_BOUND(hash)) {
|
||||
hash_st_table_init(hash, capa);
|
||||
}
|
||||
}
|
||||
@@ -2039,14 +2039,14 @@ rb_hash_s_create(int argc, VALUE *argv, VALUE klass)
|
||||
}
|
||||
|
||||
hash = 0;
|
||||
long i;
|
||||
rb_len_t i;
|
||||
for (i = 0; i < RARRAY_LEN(tmp); ++i) {
|
||||
VALUE e = RARRAY_AREF(tmp, i);
|
||||
VALUE v = rb_check_array_type(e);
|
||||
VALUE key, val = Qnil;
|
||||
|
||||
if (NIL_P(v)) {
|
||||
rb_raise(rb_eArgError, "wrong element type %s at %ld (expected array)",
|
||||
rb_raise(rb_eArgError, "wrong element type %s at %"PRIdLEN" (expected array)",
|
||||
rb_builtin_class_name(e), i);
|
||||
}
|
||||
|
||||
@@ -2063,7 +2063,7 @@ rb_hash_s_create(int argc, VALUE *argv, VALUE klass)
|
||||
|
||||
switch (RARRAY_LEN(v)) {
|
||||
default:
|
||||
rb_raise(rb_eArgError, "invalid number of elements (%ld for 1..2)",
|
||||
rb_raise(rb_eArgError, "invalid number of elements (%"PRIdLEN" for 1..2)",
|
||||
RARRAY_LEN(v));
|
||||
case 2:
|
||||
val = RARRAY_AREF(v, 1);
|
||||
@@ -3661,7 +3661,7 @@ rb_hash_transform_keys_bang(int argc, VALUE *argv, VALUE hash)
|
||||
}
|
||||
rb_hash_modify_check(hash);
|
||||
if (!RHASH_TABLE_EMPTY_P(hash)) {
|
||||
long i;
|
||||
rb_len_t i;
|
||||
VALUE new_keys = hash_hidden_new(RHASH_SIZE(hash));
|
||||
VALUE pairs = rb_ary_hidden_new(RHASH_SIZE(hash) * 2);
|
||||
rb_hash_foreach(hash, flatten_i, pairs);
|
||||
@@ -3826,7 +3826,7 @@ rb_hash_to_a(VALUE hash)
|
||||
static bool
|
||||
symbol_key_needs_quote(VALUE str)
|
||||
{
|
||||
long len = RSTRING_LEN(str);
|
||||
rb_len_t len = RSTRING_LEN(str);
|
||||
if (len == 0 || !rb_str_symname_p(str)) return true;
|
||||
const char *s = RSTRING_PTR(str);
|
||||
char first = s[0];
|
||||
@@ -3946,7 +3946,7 @@ rb_hash_set_pair(VALUE hash, VALUE arg)
|
||||
rb_builtin_class_name(arg));
|
||||
}
|
||||
if (RARRAY_LEN(pair) != 2) {
|
||||
rb_raise(rb_eArgError, "element has wrong array length (expected 2, was %ld)",
|
||||
rb_raise(rb_eArgError, "element has wrong array length (expected 2, was %"PRIdLEN")",
|
||||
RARRAY_LEN(pair));
|
||||
}
|
||||
rb_hash_aset(hash, RARRAY_AREF(pair, 0), RARRAY_AREF(pair, 1));
|
||||
@@ -5377,9 +5377,9 @@ key_stringify(VALUE hash, VALUE key)
|
||||
}
|
||||
|
||||
static void
|
||||
ar_bulk_insert(VALUE hash, long argc, const VALUE *argv)
|
||||
ar_bulk_insert(VALUE hash, rb_len_t argc, const VALUE *argv)
|
||||
{
|
||||
long i;
|
||||
rb_len_t i;
|
||||
for (i = 0; i < argc; ) {
|
||||
st_data_t k = key_stringify(hash, argv[i++]);
|
||||
st_data_t v = argv[i++];
|
||||
@@ -6044,7 +6044,7 @@ static VALUE
|
||||
env_each_key(VALUE ehash)
|
||||
{
|
||||
VALUE keys;
|
||||
long i;
|
||||
rb_len_t i;
|
||||
|
||||
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
||||
keys = env_keys(FALSE);
|
||||
@@ -6115,7 +6115,7 @@ static VALUE
|
||||
env_each_value(VALUE ehash)
|
||||
{
|
||||
VALUE values;
|
||||
long i;
|
||||
rb_len_t i;
|
||||
|
||||
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
||||
values = env_values();
|
||||
@@ -6146,7 +6146,7 @@ env_each_value(VALUE ehash)
|
||||
static VALUE
|
||||
env_each_pair(VALUE ehash)
|
||||
{
|
||||
long i;
|
||||
rb_len_t i;
|
||||
|
||||
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
||||
|
||||
@@ -6207,7 +6207,7 @@ static VALUE
|
||||
env_reject_bang(VALUE ehash)
|
||||
{
|
||||
VALUE keys;
|
||||
long i;
|
||||
rb_len_t i;
|
||||
int del = 0;
|
||||
|
||||
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
||||
@@ -6300,7 +6300,7 @@ env_select(VALUE ehash)
|
||||
{
|
||||
VALUE result;
|
||||
VALUE keys;
|
||||
long i;
|
||||
rb_len_t i;
|
||||
|
||||
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
||||
result = rb_hash_new();
|
||||
@@ -6346,7 +6346,7 @@ static VALUE
|
||||
env_select_bang(VALUE ehash)
|
||||
{
|
||||
VALUE keys;
|
||||
long i;
|
||||
rb_len_t i;
|
||||
int del = 0;
|
||||
|
||||
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
||||
@@ -6422,7 +6422,7 @@ VALUE
|
||||
rb_env_clear(void)
|
||||
{
|
||||
VALUE keys;
|
||||
long i;
|
||||
rb_len_t i;
|
||||
|
||||
keys = env_keys(TRUE);
|
||||
for (i=0; i<RARRAY_LEN(keys); i++) {
|
||||
@@ -7005,13 +7005,13 @@ env_invert(VALUE _)
|
||||
static void
|
||||
keylist_delete(VALUE keys, VALUE key)
|
||||
{
|
||||
long keylen, elen;
|
||||
rb_len_t keylen, elen;
|
||||
const char *keyptr, *eptr;
|
||||
RSTRING_GETMEM(key, keyptr, keylen);
|
||||
/* Don't stop at first key, as it is possible to have
|
||||
multiple environment values with the same key.
|
||||
*/
|
||||
for (long i=0; i<RARRAY_LEN(keys); i++) {
|
||||
for (rb_len_t i=0; i<RARRAY_LEN(keys); i++) {
|
||||
VALUE e = RARRAY_AREF(keys, i);
|
||||
RSTRING_GETMEM(e, eptr, elen);
|
||||
if (elen != keylen) continue;
|
||||
@@ -7053,7 +7053,7 @@ static VALUE
|
||||
env_replace(VALUE env, VALUE hash)
|
||||
{
|
||||
VALUE keys;
|
||||
long i;
|
||||
rb_len_t i;
|
||||
|
||||
keys = env_keys(TRUE);
|
||||
if (env == hash) return env;
|
||||
|
||||
@@ -559,7 +559,7 @@ set_i_initialize(int argc, VALUE *argv, VALUE set)
|
||||
|
||||
if (argc > 0 && (other = argv[0]) != Qnil) {
|
||||
if (RB_TYPE_P(other, T_ARRAY)) {
|
||||
long i;
|
||||
rb_len_t i;
|
||||
int block_given = rb_block_given_p();
|
||||
set_table *into = RSET_TABLE(set);
|
||||
for (i=0; i<RARRAY_LEN(other); i++) {
|
||||
@@ -979,28 +979,28 @@ set_i_classify(VALUE set)
|
||||
}
|
||||
|
||||
// Union-find with path compression
|
||||
static long
|
||||
set_divide_union_find_root(long *uf_parents, long index, long *tmp_array)
|
||||
static rb_len_t
|
||||
set_divide_union_find_root(rb_len_t *uf_parents, rb_len_t index, rb_len_t *tmp_array)
|
||||
{
|
||||
long root = uf_parents[index];
|
||||
long update_size = 0;
|
||||
rb_len_t root = uf_parents[index];
|
||||
rb_len_t update_size = 0;
|
||||
while (root != index) {
|
||||
tmp_array[update_size++] = index;
|
||||
index = root;
|
||||
root = uf_parents[index];
|
||||
}
|
||||
for (long j = 0; j < update_size; j++) {
|
||||
long idx = tmp_array[j];
|
||||
for (rb_len_t j = 0; j < update_size; j++) {
|
||||
rb_len_t idx = tmp_array[j];
|
||||
uf_parents[idx] = root;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
static void
|
||||
set_divide_union_find_merge(long *uf_parents, long i, long j, long *tmp_array)
|
||||
set_divide_union_find_merge(rb_len_t *uf_parents, rb_len_t i, rb_len_t j, rb_len_t *tmp_array)
|
||||
{
|
||||
long root_i = set_divide_union_find_root(uf_parents, i, tmp_array);
|
||||
long root_j = set_divide_union_find_root(uf_parents, j, tmp_array);
|
||||
rb_len_t root_i = set_divide_union_find_root(uf_parents, i, tmp_array);
|
||||
rb_len_t root_j = set_divide_union_find_root(uf_parents, j, tmp_array);
|
||||
if (root_i != root_j) uf_parents[root_j] = root_i;
|
||||
}
|
||||
|
||||
@@ -1008,19 +1008,19 @@ static VALUE
|
||||
set_divide_arity2(VALUE set)
|
||||
{
|
||||
VALUE tmp, uf;
|
||||
long size, *uf_parents, *tmp_array;
|
||||
rb_len_t size, *uf_parents, *tmp_array;
|
||||
VALUE set_class = rb_obj_class(set);
|
||||
VALUE items = set_i_to_a(set);
|
||||
rb_ary_freeze(items);
|
||||
size = RARRAY_LEN(items);
|
||||
tmp_array = ALLOCV_N(long, tmp, size);
|
||||
uf_parents = ALLOCV_N(long, uf, size);
|
||||
for (long i = 0; i < size; i++) {
|
||||
tmp_array = ALLOCV_N(rb_len_t, tmp, size);
|
||||
uf_parents = ALLOCV_N(rb_len_t, uf, size);
|
||||
for (rb_len_t i = 0; i < size; i++) {
|
||||
uf_parents[i] = i;
|
||||
}
|
||||
for (long i = 0; i < size - 1; i++) {
|
||||
for (rb_len_t i = 0; i < size - 1; i++) {
|
||||
VALUE item1 = RARRAY_AREF(items, i);
|
||||
for (long j = i + 1; j < size; j++) {
|
||||
for (rb_len_t j = i + 1; j < size; j++) {
|
||||
VALUE item2 = RARRAY_AREF(items, j);
|
||||
if (RTEST(rb_yield_values(2, item1, item2)) &&
|
||||
RTEST(rb_yield_values(2, item2, item1))) {
|
||||
@@ -1030,16 +1030,16 @@ set_divide_arity2(VALUE set)
|
||||
}
|
||||
VALUE final_set = set_s_create(0, 0, rb_cSet);
|
||||
VALUE hash = rb_hash_new();
|
||||
for (long i = 0; i < size; i++) {
|
||||
for (rb_len_t i = 0; i < size; i++) {
|
||||
VALUE v = RARRAY_AREF(items, i);
|
||||
long root = set_divide_union_find_root(uf_parents, i, tmp_array);
|
||||
VALUE subset = rb_hash_aref(hash, LONG2FIX(root));
|
||||
rb_len_t root = set_divide_union_find_root(uf_parents, i, tmp_array);
|
||||
VALUE subset = rb_hash_aref(hash, LEN2NUM(root));
|
||||
if (subset == Qnil) {
|
||||
subset = set_s_alloc(set_class);
|
||||
if (RSET_COMPARE_BY_IDENTITY(set)) {
|
||||
set_i_compare_by_identity(subset);
|
||||
}
|
||||
rb_hash_aset(hash, LONG2FIX(root), subset);
|
||||
rb_hash_aset(hash, LEN2NUM(root), subset);
|
||||
set_i_add(final_set, subset);
|
||||
}
|
||||
set_i_add(subset, v);
|
||||
@@ -1290,7 +1290,7 @@ set_merge_enum_into(VALUE set, VALUE arg)
|
||||
set_iter(arg, set_merge_i, (st_data_t)&args);
|
||||
}
|
||||
else if (RB_TYPE_P(arg, T_ARRAY)) {
|
||||
long i;
|
||||
rb_len_t i;
|
||||
set_table *into = RSET_TABLE(set);
|
||||
for (i=0; i<RARRAY_LEN(arg); i++) {
|
||||
set_table_insert_wb(into, set, RARRAY_AREF(arg, i));
|
||||
@@ -2260,7 +2260,7 @@ set_to_hash_i(st_data_t key, st_data_t arg)
|
||||
static VALUE
|
||||
set_i_to_h(VALUE set)
|
||||
{
|
||||
long size = RSET_SIZE(set);
|
||||
rb_len_t size = RSET_SIZE(set);
|
||||
VALUE hash;
|
||||
if (RSET_COMPARE_BY_IDENTITY(set)) {
|
||||
hash = rb_ident_hash_new_capa(size);
|
||||
|
||||
@@ -2419,9 +2419,9 @@ st_insert_single(st_table *tab, VALUE hash, VALUE key, VALUE val)
|
||||
}
|
||||
|
||||
static void
|
||||
st_insert_linear(st_table *tab, long argc, const VALUE *argv, VALUE hash)
|
||||
st_insert_linear(st_table *tab, rb_len_t argc, const VALUE *argv, VALUE hash)
|
||||
{
|
||||
long i;
|
||||
rb_len_t i;
|
||||
|
||||
for (i = 0; i < argc; /* */) {
|
||||
st_data_t k = st_stringify(argv[i++]);
|
||||
@@ -2433,9 +2433,9 @@ st_insert_linear(st_table *tab, long argc, const VALUE *argv, VALUE hash)
|
||||
}
|
||||
|
||||
static void
|
||||
st_insert_generic(st_table *tab, long argc, const VALUE *argv, VALUE hash)
|
||||
st_insert_generic(st_table *tab, rb_len_t argc, const VALUE *argv, VALUE hash)
|
||||
{
|
||||
long i;
|
||||
rb_len_t i;
|
||||
|
||||
/* push elems */
|
||||
for (i = 0; i < argc; /* */) {
|
||||
|
||||
Reference in new issue
Block a user