[macruby-changes] [3570] MacRuby/trunk/array.c

source_changes at macosforge.org source_changes at macosforge.org
Tue Feb 16 22:50:22 PST 2010


Revision: 3570
          http://trac.macosforge.org/projects/ruby/changeset/3570
Author:   lsansonetti at apple.com
Date:     2010-02-16 22:50:22 -0800 (Tue, 16 Feb 2010)
Log Message:
-----------
rary_delete_element(): better algorithm

Modified Paths:
--------------
    MacRuby/trunk/array.c

Modified: MacRuby/trunk/array.c
===================================================================
--- MacRuby/trunk/array.c	2010-02-17 05:36:28 UTC (rev 3569)
+++ MacRuby/trunk/array.c	2010-02-17 06:50:22 UTC (rev 3570)
@@ -1627,24 +1627,52 @@
  */
 
 static bool
-rary_delete_element(VALUE ary, VALUE item)
+rary_delete_element(VALUE ary, VALUE item, bool use_equal)
 {
     rary_modify(ary);
-    bool changed = false;
-    size_t pos = 0;
-    while (pos < RARY(ary)->len
-	    && (pos = rary_index_of_item(ary, pos, item))
-	    != NOT_FOUND) {
-	rary_erase(ary, pos, 1);
-	changed = true;
+
+    VALUE *p = rary_ptr(ary);
+    VALUE *t = p;
+    VALUE *end = p + RARY(ary)->len;
+
+    if (use_equal) {
+	while (t < end) {
+	    if (RTEST(rb_equal_fast(*t, item))) {
+		t++;
+	    }
+	    else {
+		GC_WB(p, *t);
+		p++;
+		t++;
+	    }
+	}
     }
-    return changed;
+    else {
+	while (t < end) {
+	    if (*t == item) {
+		t++;
+	    }
+	    else {
+		GC_WB(p, *t);
+		p++;
+		t++;
+	    }
+	}
+    }
+
+    const size_t n = p - rary_ptr(ary);
+    if (RARY(ary)->len == n) {
+	// Nothing changed.
+	return false;
+    }
+    RARY(ary)->len = n;
+    return true;
 }
 
 VALUE
 rary_delete(VALUE ary, SEL sel, VALUE item)
 {
-    const bool changed = rary_delete_element(ary, item);
+    const bool changed = rary_delete_element(ary, item, true);
     if (!changed) {
 	if (rb_block_given_p()) {
 	    return rb_yield(item);
@@ -2572,7 +2600,7 @@
 static VALUE
 rary_compact_bang(VALUE ary, SEL sel)
 {
-    return rary_delete_element(ary, Qnil) ? ary : Qnil;
+    return rary_delete_element(ary, Qnil, false) ? ary : Qnil;
 }
 
 /*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100216/0dc4320a/attachment.html>


More information about the macruby-changes mailing list