e$B1sF#$G$9!#e(B
2009/02/06 1:01 Yusuke ENDOH [email protected]:
e$BF1$8MWAG$rJ#?t2se(B yield e$B$9$k2DG=@-$,$"$j$^$9!#e(B
$ ./ruby -e ‘h = {1=>1,2=>2}; h.each {|x| p x; h.delete(2) }’
[1, 1]
[1, 1]
e$B$9$0$K$O2r7h:v$,;W$$$D$+$J$$$N$G!"$A$g$C$H9M$($^$9!#e(B
e$B8=>u$Ne(B st_foreach e$B$@$H!“e(BHash
e$B$N%$%F%l!<%HCf$K%O%C%7%e$NMWAG$r:o=|$9$k$He(B
e$B:o=|$5$l$F$$$J$$MWAG$G$b<h$j$3$$9$3$H$,$”$k$h$&$G$9!#e(B
a = (1…3).map do |i|
x = Object.new
def x.hash; 1; end
x
end
h = {}
a.each {|x| h[x] = true }
p h #=> {#Object:0x81fcdb0=>true, #Object:0x81fcce8=>true,
#Object:0x81fcc5c=>true}
h.each do |x|
p x
h.delete(a[0])
h.delete(a[1])
end
#=> [#Object:0x81fcdb0, true]
e$B$@$1$7$+=P$J$$!"e(B[#Object:0x81fcc5c=>true, true] e$B$O=P$k$O$:e(B
p h #=> {#Object:0x81fcc5c=>true}
e$B860x$O%$%F%l!<%H$N=*N;>r7o$,@5$7$/$J$$$?$a$G$9$,!"e(BHash
e$B$N=g=x$re(B
e$B%k!<%W9=B$$G4IM}$7$F$$$k8B$j2r7h$G$-$J$$$h$&$J5$$,$7$^$7$?!#e(B
e$B=g=x$rAPJ}8~%j%9%H$G4IM}$9$l$PD>$j$^$7$?!#e(B
Index: include/ruby/st.h
— include/ruby/st.h (revision 22101)
+++ include/ruby/st.h (working copy)
@@ -75,7 +75,7 @@
#endif
st_index_t num_entries : ST_INDEX_BITS - 1;
struct st_table_entry **bins;
- struct st_table_entry *head;
- struct st_table_entry *head, *back;
};
#define st_is_member(table,key) st_lookup(table,key,(st_data_t *)0)
Index: st.c
— st.c (revision 22101)
+++ st.c (working copy)
@@ -176,6 +176,7 @@
tbl->num_bins = size;
tbl->bins = (st_table_entry *)Calloc(size,
sizeof(st_table_entry));
tbl->head = 0;
void
@@ -335,7 +337,7 @@
#define ADD_DIRECT(table, key, value, hash_val, bin_pos)
do {\
- st_table_entry *entry, *head;\
- st_table_entry *entry;
if (table->num_entries/(table->num_bins) > ST_DEFAULT_MAX_DENSITY)
{
rehash(table);
bin_pos = hash_val % table->num_bins;
@@ -347,13 +349,14 @@
entry->key = key;
entry->record = value;
entry->next = table->bins[bin_pos];\
- if ((head = table->head) != 0) {\
- entry->fore = head;\
- (entry->back = head->back)->fore = entry;\
- head->back = entry;\
- if (table->head != 0) {\
- entry->fore = 0;\
- (entry->back = table->back)->fore = entry;\
- table->back = entry;
}
else {\
- table->head = entry->fore = entry->back = entry;\
- table->head = table->back = entry;\
- entry->fore = entry->back = 0;
}
table->bins[bin_pos] = entry;
table->num_entries++;
@@ -455,7 +458,7 @@
hash_val = ptr->hash % new_num_bins;
ptr->next = new_bins[hash_val];
new_bins[hash_val] = ptr;
- } while ((ptr = ptr->fore) != table->head);
- } while ((ptr = ptr->fore) != 0);
}
}
@@ -502,10 +505,8 @@
entry->back = prev;
*tail = prev = entry;
tail = &entry->fore;
- } while ((ptr = ptr->fore) != old_table->head);
- entry = new_table->head;
- entry->back = prev;
- *tail = entry;
-
} while ((ptr = ptr->fore) != 0);
-
new_table->back = prev;
}
return new_table;
@@ -513,14 +514,16 @@
#define REMOVE_ENTRY(table, ptr) do
{ \
- if (ptr == ptr->fore) { \
- if (ptr->fore == 0 && ptr->back == 0) {
table->head = 0; \
-
table->back = 0; \
}
else {
st_table_entry *fore = ptr->fore, *back = ptr->back; \
-
fore->back = back; \
-
back->fore = fore; \
-
if (fore) fore->back = back; \
-
if (back) back->fore = fore; \
if (ptr == table->head) table->head = fore; \
-
if (ptr == table->back) table->back = back; \
}
table->num_entries–;
} while (0)
@@ -613,7 +616,7 @@
{
st_table_entry *ptr, **last, *tmp;
enum st_retval retval;
-
int i;
if (table->entries_packed) {
for (i = 0; i < table->num_entries; i++) {
@@ -651,7 +654,6 @@
if ((ptr = table->head) != 0) {
do {
*/
@@ -683,7 +685,7 @@
}
}
}
- } while (!end && table->head);
- } while (ptr && table->head);
}
return 0;
}
@@ -694,7 +696,7 @@
{
st_table_entry *ptr, **last, *tmp;
enum st_retval retval;
-
end = ptr == table->head;
retval = (*func)(ptr->key, ptr->record, arg, 0);
switch (retval) {
case ST_CHECK: /* check if hash is modified during iteration
*/
@@ -766,7 +767,7 @@
free(tmp);
table->num_entries–;
}
- } while (!end && table->head);
- } while (ptr && table->head);
}
return 0;
}