summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog4
-rw-r--r--ext/-test-/st/update/update.c4
-rw-r--r--gc.c3
-rw-r--r--include/ruby/st.h3
-rw-r--r--load.c3
-rw-r--r--st.c37
-rw-r--r--test/-ext-/st/test_update.rb5
7 files changed, 38 insertions, 21 deletions
diff --git a/ChangeLog b/ChangeLog
index f490767..bd37f02 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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):
diff --git a/ext/-test-/st/update/update.c b/ext/-test-/st/update/update.c
index 33db042..0c82a7a 100644
--- a/ext/-test-/st/update/update.c
+++ b/ext/-test-/st/update/update.c
@@ -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;
diff --git a/gc.c b/gc.c
index d260168..46091f6 100644
--- a/gc.c
+++ b/gc.c
@@ -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;
diff --git a/include/ruby/st.h b/include/ruby/st.h
index c0d5e01..c461de9 100644
--- a/include/ruby/st.h
+++ b/include/ruby/st.h
@@ -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);
diff --git a/load.c b/load.c
index a320c51..b85d903 100644
--- a/load.c
+++ b/load.c
@@ -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;
diff --git a/st.c b/st.c
index ad943e9..bb802cf 100644
--- a/st.c
+++ b/st.c
@@ -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;
}
}
diff --git a/test/-ext-/st/test_update.rb b/test/-ext-/st/test_update.rb
index 68fee0b..68625af 100644
--- a/test/-ext-/st/test_update.rb
+++ b/test/-ext-/st/test_update.rb
@@ -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