From 1b2562ed6503f3bb7c19514b5e9571c1fcb79360 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 7 Jul 2010 23:59:18 +0200 Subject: Added equality checks --- src/Term.vala | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) (limited to 'src') diff --git a/src/Term.vala b/src/Term.vala index 1e063c1..7037e48 100644 --- a/src/Term.vala +++ b/src/Term.vala @@ -1,6 +1,7 @@ namespace Eva { public interface Term : Object { public abstract string to_string(); + public abstract bool equals(Term o); public abstract void encode(Erl.Buffer buffer); @@ -108,6 +109,18 @@ namespace Eva { return value.to_string(); } + public bool equals(Term o) { + if(o is Int) { + return value == (o as Int).value; + } + else if(o is UInt) { + return value == (o as UInt).value; + } + else { + return false; + } + } + public void encode(Erl.Buffer buffer) { buffer.encode_long(value); } @@ -124,6 +137,19 @@ namespace Eva { return value.to_string(); } + public bool equals(Term o) { + if(o is Int) { + return value == (o as Int).value; + } + else if(o is UInt) { + return value == (o as UInt).value; + } + else { + return false; + } + } + + public void encode(Erl.Buffer buffer) { buffer.encode_ulong(value); } @@ -140,6 +166,15 @@ namespace Eva { return value.to_string(); } + public bool equals(Term o) { + if(o is Double) { + return value == (o as Double).value; + } + else { + return false; + } + } + public void encode(Erl.Buffer buffer) { buffer.encode_double(value); } @@ -156,6 +191,15 @@ namespace Eva { return value.to_string(); } + public bool equals(Term o) { + if(o is Atom) { + return value == (o as Atom).value; + } + else { + return false; + } + } + public void encode(Erl.Buffer buffer) { buffer.encode_atom(value.to_utf8()); } @@ -186,6 +230,23 @@ namespace Eva { return ret + "}"; } + public bool equals(Term o) { + if(!(o is Tuple)) + return false; + + Tuple t = o as Tuple; + + if(t.value.size != value.size) + return false; + + for(int i = 0; i < value.size; ++i) { + if(!value[i].equals(t.value[i])) + return false; + } + + return true; + } + public void encode(Erl.Buffer buffer) { buffer.encode_tuple_header(value.size); @@ -222,6 +283,61 @@ namespace Eva { tail = null; } + public bool equals(Term o) { + if(o is List) { + List l = o as List; + + if(list.size != l.list.size) + return false; + if((tail == null) != (l.tail == null)) + return false; + if(tail != null && !tail.equals(l.tail)) + return false; + + Gee.Iterator lt = l.list.iterator(); + lt.first(); + foreach(Term t in list) { + if(!t.equals(lt.get())) + return false; + + lt.next(); + } + + return true; + } + else if(o is String) { + if(tail != null) + return false; + + string s = ""; + + foreach(Term t in list) { + if(t is Int) { + Int i = t as Int; + if(i.value < 0 || i.value > 255) + return false; + + s += i.value.to_string("%c"); + } + else if(t is UInt) { + UInt i = t as UInt; + if(i.value > 255) + return false; + + s += i.value.to_string("%c"); + } + else { + return false; + } + } + + return (s == (o as String).value); + } + else { + return false; + } + } + public string to_string() { string ret = "["; bool first = true; @@ -269,6 +385,18 @@ namespace Eva { return value.to_string(); } + public bool equals(Term o) { + if(o is String) { + return (value == (o as String).value); + } + else if(o is List) { + return o.equals(this); + } + else { + return false; + } + } + public void encode(Erl.Buffer buffer) { buffer.encode_string(value.to_utf8()); } @@ -287,6 +415,21 @@ namespace Eva { return "#Bin"; } + public bool equals(Term o) { + if(o is Binary) { + Binary b = o as Binary; + + if(b.len != len) + return false; + + return (Memory.cmp(value, b.value, len) == 0); + } + else { + return false; + } + } + + public void encode(Erl.Buffer buffer) { buffer.encode_binary((char*)value, len); } @@ -310,6 +453,15 @@ namespace Eva { return "<" + node + "." + num.to_string() + "." + serial.to_string() + ">"; } + public bool equals(Term o) { + if(!(o is Pid)) + return false; + + Pid p = o as Pid; + + return (node == p.node && num == p.num && serial == p.serial && creation == p.creation); + } + public void encode(Erl.Buffer buffer) { Erl.Pid pid = Erl.Pid(); char[] nodedata = node.to_utf8(); @@ -342,6 +494,15 @@ namespace Eva { return "#Port"; } + public bool equals(Term o) { + if(!(o is Port)) + return false; + + Port p = o as Port; + + return (node == p.node && id == p.id && creation == p.creation); + } + public void encode(Erl.Buffer buffer) { Erl.Port port = Erl.Port(); char[] nodedata = node.to_utf8(); @@ -379,6 +540,24 @@ namespace Eva { return "#Ref"; } + public bool equals(Term o) { + if(!(o is Ref)) + return false; + + Ref r = o as Ref; + + if(node != r.node || len != r.len || creation != r.creation) { + return false; + } + + for(int i = 0; i < len; ++i) { + if(n[i] != r.n[i]) + return false; + } + + return true; + } + public void encode(Erl.Buffer buffer) { Erl.Ref reference = Erl.Ref(); char[] nodedata = node.to_utf8(); -- cgit v1.2.3