generator: faster Equals implementation

This commit is contained in:
Stephan Sundermann 2013-10-08 16:27:47 +02:00 committed by Andrés G. Aragoneses
parent 115659f46b
commit 94da3fb2ab

View file

@ -1,8 +1,11 @@
// GtkSharp.Generation.StructBase.cs - The Structure/Boxed Base Class. // GtkSharp.Generation.StructBase.cs - The Structure/Boxed Base Class.
// //
// Author: Mike Kestner <mkestner@speakeasy.net> // Authors:
// Mike Kestner <mkestner@speakeasy.net>
// Stephan Sundermann <stephansundermann@gmail.com>
// //
// Copyright (c) 2001-2003 Mike Kestner // Copyright (c) 2001-2003 Mike Kestner
// Copyright (c) 2013 Stephan Sundermann
// //
// This program is free software; you can redistribute it and/or // This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the GNU General Public // modify it under the terms of version 2 of the GNU General Public
@ -111,36 +114,43 @@ namespace GtkSharp.Generation {
{ {
int bitfields = 0; int bitfields = 0;
bool need_field = true; bool need_field = true;
StringBuilder sb = new StringBuilder (); StringBuilder hashcode = new StringBuilder ();
StringBuilder equals = new StringBuilder ();
sw.WriteLine ("\t\tpublic bool Equals ({0} other)", Name); sw.WriteLine ("\t\tpublic bool Equals ({0} other)", Name);
sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t{");
sw.WriteLine ("\t\t\tbool isEqual = true;"); hashcode.Append ("this.GetType ().FullName.GetHashCode ()");
sb.Append ("this.GetType ().FullName.GetHashCode ()"); equals.Append ("true");
foreach (StructField field in fields) { foreach (StructField field in fields) {
if (field.IsPadding) if (field.IsPadding)
continue; continue;
if (field.IsBitfield) { if (field.IsBitfield) {
if (need_field) { if (need_field) {
sw.WriteLine ("\t\t\tisEqual &= bitfield{0}.Equals (other._bitfield{0});", bitfields); equals.Append (" && _bitfield");
//if (sb.Length > 0) equals.Append (bitfields);
sb.Append (" ^ "); equals.Append (".Equals (other._bitfield");
sb.Append ("_bitfield"); equals.Append (bitfields);
sb.Append (bitfields++); equals.Append (")");
sb.Append (".GetHashCode ()"); hashcode.Append (" ^ ");
hashcode.Append ("_bitfield");
hashcode.Append (bitfields++);
hashcode.Append (".GetHashCode ()");
need_field = false; need_field = false;
} }
} else { } else {
need_field = true; need_field = true;
sw.WriteLine ("\t\t\tisEqual &= {0}.Equals (other.{0});", field.EqualityName); equals.Append (" && ");
//if (sb.Length > 0) equals.Append (field.EqualityName);
sb.Append (" ^ "); equals.Append (".Equals (other.");
sb.Append (field.EqualityName); equals.Append (field.EqualityName);
sb.Append (".GetHashCode ()"); equals.Append (")");
hashcode.Append (" ^ ");
hashcode.Append (field.EqualityName);
hashcode.Append (".GetHashCode ()");
} }
} }
sw.WriteLine ("\t\t\treturn isEqual;"); sw.WriteLine ("\t\t\treturn {0};", equals.ToString ());
sw.WriteLine ("\t\t}"); sw.WriteLine ("\t\t}");
sw.WriteLine (); sw.WriteLine ();
sw.WriteLine ("\t\tpublic override bool Equals (object other)"); sw.WriteLine ("\t\tpublic override bool Equals (object other)");
@ -152,7 +162,7 @@ namespace GtkSharp.Generation {
return; return;
sw.WriteLine ("\t\tpublic override int GetHashCode ()"); sw.WriteLine ("\t\tpublic override int GetHashCode ()");
sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t{");
sw.WriteLine ("\t\t\treturn {0};", sb.ToString ()); sw.WriteLine ("\t\t\treturn {0};", hashcode.ToString ());
sw.WriteLine ("\t\t}"); sw.WriteLine ("\t\t}");
sw.WriteLine (); sw.WriteLine ();