summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJürg Billeter <j@bitron.ch>2010-08-11 23:46:41 +0200
committerJürg Billeter <j@bitron.ch>2010-09-18 01:00:13 +0200
commitce112eee8ca8b8996cb9a159d1d8ea0ea660ae2b (patch)
tree7a075e29ed68f9c5f4d31a2eaefc4d1791e08834
parent28cee66a6207dd2c0956265f2a49fdd34979ccee (diff)
downloadvala-ce112eee8ca8b8996cb9a159d1d8ea0ea660ae2b.tar.gz
Support iteration via index to improve performance
-rw-r--r--vala/valaforeachstatement.vala33
1 files changed, 33 insertions, 0 deletions
diff --git a/vala/valaforeachstatement.vala b/vala/valaforeachstatement.vala
index bc0cf3d07..469c51e18 100644
--- a/vala/valaforeachstatement.vala
+++ b/vala/valaforeachstatement.vala
@@ -189,9 +189,42 @@ public class Vala.ForeachStatement : Block {
}
}
+ bool check_with_index (SemanticAnalyzer analyzer, DataType collection_type) {
+ var get_method = collection_type.get_member ("get") as Method;
+ if (get_method == null) {
+ return false;
+ }
+ if (get_method.get_parameters ().size != 1) {
+ return false;
+ }
+ var size_property = collection_type.get_member ("size") as Property;
+ if (size_property == null) {
+ return false;
+ }
+
+ add_statement (new DeclarationStatement (new LocalVariable (null, "_%s_list".printf (variable_name), collection, source_reference), source_reference));
+ add_statement (new DeclarationStatement (new LocalVariable (null, "_%s_size".printf (variable_name), new MemberAccess (new MemberAccess.simple ("_%s_list".printf (variable_name), source_reference), "size", source_reference), source_reference), source_reference));
+ add_statement (new DeclarationStatement (new LocalVariable (null, "_%s_index".printf (variable_name), new UnaryExpression (UnaryOperator.MINUS, new IntegerLiteral ("1", source_reference), source_reference), source_reference), source_reference));
+ var next = new UnaryExpression (UnaryOperator.INCREMENT, new MemberAccess.simple ("_%s_index".printf (variable_name), source_reference), source_reference);
+ var conditional = new BinaryExpression (BinaryOperator.LESS_THAN, next, new MemberAccess.simple ("_%s_size".printf (variable_name), source_reference), source_reference);
+ var loop = new WhileStatement (conditional, body, source_reference);
+ add_statement (loop);
+
+ var get_call = new MethodCall (new MemberAccess (new MemberAccess.simple ("_%s_list".printf (variable_name), source_reference), "get", source_reference), source_reference);
+ get_call.add_argument (new MemberAccess.simple ("_%s_index".printf (variable_name), source_reference));
+ body.insert_statement (0, new DeclarationStatement (new LocalVariable (type_reference, variable_name, get_call, source_reference), source_reference));
+
+ checked = false;
+ return base.check (analyzer);
+ }
+
bool check_with_iterator (SemanticAnalyzer analyzer, DataType collection_type) {
use_iterator = true;
+ if (check_with_index (analyzer, collection_type)) {
+ return true;
+ }
+
var iterator_method = collection_type.get_member ("iterator") as Method;
if (iterator_method == null) {
Report.error (collection.source_reference, "`%s' does not have an `iterator' method".printf (collection_type.to_string ()));