1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* st.c (st_update): add existing parameter to the callback function.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35170 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-03-29 07:36:12 +00:00
parent 95b30b0d07
commit 30cea65767
7 changed files with 38 additions and 21 deletions

View file

@ -1,3 +1,7 @@
Thu Mar 29 16:36:10 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* st.c (st_update): add existing parameter to the callback function.
Thu Mar 29 16:35:32 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* lib/test/unit.rb (terminal_width, del_status_line, put_status):

View file

@ -2,9 +2,9 @@
#include <ruby/st.h>
static int
update_func(st_data_t key, st_data_t *value, st_data_t arg)
update_func(st_data_t key, st_data_t *value, st_data_t arg, int existing)
{
VALUE ret = rb_yield_values(2, (VALUE)key, (VALUE)*value);
VALUE ret = rb_yield_values(existing ? 2 : 1, (VALUE)key, (VALUE)*value);
switch (ret) {
case Qfalse:
return ST_STOP;

3
gc.c
View file

@ -3672,9 +3672,10 @@ wmap_allocate(VALUE klass)
}
static int
wmap_final_func(st_data_t key, st_data_t *value, st_data_t arg)
wmap_final_func(st_data_t key, st_data_t *value, st_data_t arg, int existing)
{
VALUE obj = (VALUE)key, ary = (VALUE)*value;
if (!existing) return ST_STOP;
rb_ary_delete(ary, obj);
if (!RARRAY_LEN(ary)) return ST_DELETE;
return ST_CONTINUE;

View file

@ -121,7 +121,8 @@ int st_insert(st_table *, st_data_t, st_data_t);
int st_insert2(st_table *, st_data_t, st_data_t, st_data_t (*)(st_data_t));
int st_lookup(st_table *, st_data_t, st_data_t *);
int st_get_key(st_table *, st_data_t, st_data_t *);
int st_update(st_table *table, st_data_t key, int (*func)(st_data_t key, st_data_t *value, st_data_t arg), st_data_t arg);
typedef int st_update_callback_func(st_data_t key, st_data_t *value, st_data_t arg, int existing);
int st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data_t arg);
int st_foreach(st_table *, int (*)(ANYARGS), st_data_t);
int st_foreach_check(st_table *, int (*)(ANYARGS), st_data_t, st_data_t);
int st_reverse_foreach(st_table *, int (*)(ANYARGS), st_data_t);

3
load.c
View file

@ -417,9 +417,10 @@ load_lock(const char *ftptr)
}
static int
release_barrier(st_data_t key, st_data_t *value, st_data_t done)
release_barrier(st_data_t key, st_data_t *value, st_data_t done, int existing)
{
VALUE barrier = (VALUE)*value;
if (!existing) return ST_STOP;
if (done ? rb_barrier_destroy(barrier) : rb_barrier_release(barrier)) {
/* still in-use */
return ST_CONTINUE;

37
st.c
View file

@ -825,12 +825,12 @@ st_cleanup_safe(st_table *table, st_data_t never)
}
int
st_update(st_table *table, st_data_t key, int (*func)(st_data_t key, st_data_t *value, st_data_t arg), st_data_t arg)
st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data_t arg)
{
st_index_t hash_val, bin_pos;
register st_table_entry *ptr, **last, *tmp;
st_data_t value;
int retval;
st_data_t value = 0;
int retval, existing = 0;
hash_val = do_hash(key, table);
@ -838,38 +838,49 @@ st_update(st_table *table, st_data_t key, int (*func)(st_data_t key, st_data_t *
st_index_t i = find_packed_index(table, hash_val, key);
if (i < table->real_entries) {
value = PVAL(table, i);
retval = (*func)(key, &value, arg);
existing = 1;
}
{
retval = (*func)(key, &value, arg, existing);
if (!table->entries_packed) {
FIND_ENTRY(table, ptr, hash_val, bin_pos);
if (ptr == 0) return 0;
goto unpacked;
}
switch (retval) {
case ST_CONTINUE:
if (!existing) {
add_packed_direct(table, key, value, hash_val);
break;
}
PVAL_SET(table, i, value);
break;
case ST_DELETE:
if (!existing) break;
remove_packed_entry(table, i);
}
return 1;
}
return 0;
return existing;
}
FIND_ENTRY(table, ptr, hash_val, bin_pos);
if (ptr == 0) {
return 0;
}
else {
if (ptr != 0) {
value = ptr->record;
retval = (*func)(ptr->key, &value, arg);
existing = 1;
}
{
retval = (*func)(ptr->key, &value, arg, existing);
unpacked:
switch (retval) {
case ST_CONTINUE:
if (!existing) {
add_direct(table, key, value, hash_val, hash_val % table->num_bins);
break;
}
ptr->record = value;
break;
case ST_DELETE:
if (!existing) break;
last = &table->bins[bin_pos];
for (; (tmp = *last) != 0; last = &tmp->next) {
if (ptr == tmp) {
@ -882,7 +893,7 @@ st_update(st_table *table, st_data_t key, int (*func)(st_data_t key, st_data_t *
}
break;
}
return 1;
return existing;
}
}

View file

@ -10,9 +10,8 @@ class Bug::StTable
end
def test_notfound
called = false
assert_equal(false, @tbl.st_update(:c) {called = true})
assert_equal(false, called)
assert_equal(false, @tbl.st_update(:c) {42})
assert_equal({a: 1, b: 2, c: 42}, @tbl)
end
def test_continue