aboutsummaryrefslogtreecommitdiffstats
path: root/st.c
diff options
context:
space:
mode:
authorcharliesome <charliesome@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-04-25 05:03:30 +0000
committercharliesome <charliesome@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-04-25 05:03:30 +0000
commit5d7b5481ca60260ef05ee969c8b142143c4aa8c2 (patch)
tree484c920c961d1dc14b8745489589f97cd2ac0ee0 /st.c
parented33fcae5a1e6fca6fc7e10776b579c668d35451 (diff)
downloadruby-5d7b5481ca60260ef05ee969c8b142143c4aa8c2.tar.gz
* benchmark/bm_hash_shift.rb: add benchmark for Hash#shift
* hash.c (rb_hash_shift): use st_shift if hash is not being iterated to delete element without iterating the whole hash. * hash.c (shift_i): remove function * include/ruby/st.h (st_shift): add st_shift function * st.c (st_shift): ditto [Bug #8312] [ruby-core:54524] Patch by funny-falcon git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40457 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'st.c')
-rw-r--r--st.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/st.c b/st.c
index 9151267263..5b59538966 100644
--- a/st.c
+++ b/st.c
@@ -793,6 +793,35 @@ st_delete_safe(register st_table *table, register st_data_t *key, st_data_t *val
return 0;
}
+int
+st_shift(register st_table *table, register st_data_t *key, st_data_t *value)
+{
+ st_index_t hash_val;
+ st_table_entry **prev;
+ register st_table_entry *ptr;
+
+ if (table->num_entries == 0) {
+ if (value != 0) *value = 0;
+ return 0;
+ }
+
+ if (table->entries_packed) {
+ if (value != 0) *value = PVAL(table, 0);
+ *key = PKEY(table, 0);
+ remove_packed_entry(table, 0);
+ return 1;
+ }
+
+ prev = &table->bins[table->head->hash % table->num_bins];
+ while ((ptr = *prev) != table->head) prev = &ptr->next;
+ *prev = ptr->next;
+ if (value != 0) *value = ptr->record;
+ *key = ptr->key;
+ remove_entry(table, ptr);
+ st_free_entry(ptr);
+ return 1;
+}
+
void
st_cleanup_safe(st_table *table, st_data_t never)
{