From b426e1b1fafdab688c646cef8245fa72403d37bb Mon Sep 17 00:00:00 2001 From: glass Date: Wed, 27 Nov 2013 16:07:10 +0000 Subject: * st.c (st_keys): define st_keys(). it writes each key to buffer. * hash.c (rb_hash_keys): use st_keys() for performance improvement if st_data_t and VALUE are compatible. * st.h: define macro ST_DATA_COMPATIBLE_P() to predicate whether st_data_t and passed type are compatible. * configure.in: check existence of builtin function to use in ST_DATA_COMPATIBLE_P(). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43885 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- st.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'st.c') diff --git a/st.c b/st.c index c8f72c68c8..1fbf1f5e7c 100644 --- a/st.c +++ b/st.c @@ -1091,6 +1091,35 @@ st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg) return 0; } +st_index_t +st_keys(st_table *table, st_data_t *keys, st_index_t size) +{ + st_data_t key, never = (st_data_t)Qundef; + st_data_t *keys_start = keys; + + if (table->entries_packed) { + st_index_t i; + + if (size > table->real_entries) size = table->real_entries; + for (i = 0; i < size; i++) { + key = PKEY(table, i); + if (key == never) continue; + *keys++ = key; + } + } + else { + st_table_entry *ptr = table->head; + st_data_t *keys_end = keys + size; + while (ptr && keys < keys_end) { + key = ptr->key; + if (key != never) *keys++ = key; + ptr = ptr->fore; + } + } + + return keys - keys_start; +} + #if 0 /* unused right now */ int st_reverse_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg) -- cgit v1.2.3