From 2fc49125e73554d68b4ee8874b9f84725b6f2157 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 9 Jul 2010 14:02:35 +0200 Subject: Immutable tuples --- src/Term.vala | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/Term.vala b/src/Term.vala index e0033d6..f2799e9 100644 --- a/src/Term.vala +++ b/src/Term.vala @@ -65,13 +65,13 @@ namespace Eva { case Erl.TermType.SMALL_TUPLE: case Erl.TermType.LARGE_TUPLE: assert(Erl.decode_tuple_header(buffer, ref index, out size) == 0); - Tuple term = new Tuple(); + Term[] terms = new Term[size]; for(int i = 0; i < size; ++i) { - term.value.add(do_decode(buffer, ref index)); + terms[i] = do_decode(buffer, ref index); } - return term; + return new Tuple(terms); case Erl.TermType.NIL: case Erl.TermType.LIST: @@ -269,18 +269,29 @@ namespace Eva { } public class Tuple : Object, Term { - public Gee.List value {get; construct;} + private Term[] elements {get; set;} + + public Tuple(Term[] terms) { + elements = terms; + } + + public int size { + get { + return elements.length; + } + } - public Tuple() { - Gee.List list = new Gee.ArrayList(); - Object(value: list); + public new unowned Term get(int i) { + assert(i >= 0 && i < size); + + return elements[i]; } public string to_string() { string ret = "{"; bool first = true; - foreach(Term term in value) { + foreach(Term term in elements) { if(first) { first = false; ret += term.to_string(); @@ -303,11 +314,11 @@ namespace Eva { Tuple t = o as Tuple; - if(t.value.size != value.size) + if(t.size != size) return false; - for(int i = 0; i < value.size; ++i) { - if(!value[i].do_match(t.value[i], vars, aliases)) + for(int i = 0; i < size; ++i) { + if(!this[i].do_match(t[i], vars, aliases)) return false; } @@ -315,9 +326,9 @@ namespace Eva { } public void encode(Erl.Buffer buffer) { - buffer.encode_tuple_header(value.size); + buffer.encode_tuple_header(size); - foreach(Term term in value) { + foreach(Term term in elements) { term.encode(buffer); } } -- cgit v1.2.3