diff options
author | glass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-10-10 12:06:01 +0000 |
---|---|---|
committer | glass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-10-10 12:06:01 +0000 |
commit | 026955e37517d07679ea8044d939400d6e9beaaf (patch) | |
tree | 0892965fb6152241df67902c60b6918a1f7cac13 /st.c | |
parent | 4599d3efd7bfda591fd29e5647af2d5aa8be486c (diff) |
* st.c (st_keys): define st_keys() for performance improvement of
Hash#keys and Array#uniq.
* st.h: ditto.
* hash.c (rb_hash_keys): use st_keys().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43238 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'st.c')
-rw-r--r-- | st.c | 31 |
1 files changed, 31 insertions, 0 deletions
@@ -1091,6 +1091,37 @@ st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg) return 0; } +VALUE +st_keys(st_table *table) +{ + st_table_entry *ptr = NULL; + st_data_t key, never = (st_data_t)Qundef; + VALUE keys = rb_ary_new_capa(table->num_entries); + + if (table->entries_packed) { + st_index_t i; + + for (i = 0; i < table->real_entries; i++) { + key = PKEY(table, i); + if (key == never) continue; + rb_ary_push(keys, (VALUE)key); + } + } + else { + ptr = table->head; + } + + if (ptr != 0) { + do { + key = ptr->key; + if (key != never) rb_ary_push(keys, (VALUE)key); + ptr = ptr->fore; + } while (ptr && table->head); + } + + return keys; +} + #if 0 /* unused right now */ int st_reverse_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg) |