110 likes | 126 Views
Homework 2 review. 09/20/02. Homework 1 review Solution. using System; using System.Collections; using System.Diagnostics; namespace BC.Core { using Exceptions; namespace Collections { [Serializable] public class Set: ICollection, ICloneable { protected Hashtable S;
E N D
Homework 2 review 09/20/02
Homework 1 reviewSolution using System; using System.Collections; using System.Diagnostics; namespace BC.Core { using Exceptions; namespace Collections { [Serializable] publicclass Set: ICollection, ICloneable { protected Hashtable S; public Set () { S = new Hashtable(); } public Set (object o) : this() { Add(o); } . . .
Homework 1 reviewSolution (cont.) . . . publicint Count { get { return S.Count; } } publicbool Empty { get { return Count == 0; } } publicbool Contains (object o) { return S.Contains(o); } publicvoid CopyTo (System.Array a, int i) { S.Keys.CopyTo(a, i); } . . .
Homework 1 reviewSolution (cont.) . . . publicbool IsSynchronized { get { return S.Keys.IsSynchronized; } } publicobject SyncRoot { get { return S.Keys.SyncRoot; } } publicvoid Add (object o) { S[o] = null; } public IEnumerator GetEnumerator () { return S.Keys.GetEnumerator(); } . . .
Homework 1 reviewSolution (cont.) . . . publicobject Clone () { Set r = new Set(); foreach (object o inthis) r.Add(o); return r; } publicstaticbool isNull (Set s) { return (object)s == null; } publicobject Singleton { get { Debug.Assert(Count <= 1); foreach (object o inthis) return o; returnnull; } } . . .
Homework 1 reviewSolution (cont.) . . . publicstaticbooloperator <= (Set s1, Set s2) { foreach (object o in s1) if (!s2.Contains(o)) returnfalse; returntrue; } publicstaticbooloperator >= (Set s1, Set s2) { return s2 <= s1; } publicstaticbooloperator == (Set s1, Set s2) { if (isNull(s1) || isNull(s2)) return isNull(s1) && isNull(s2); else return s1 <= s2 && s2 <= s1; } publicstaticbooloperator != (Set s1, Set s2) { return !(s1 == s2); } . . .
Homework 1 reviewSolution (cont.) . . . publicstatic Set operator * (Set s1, Set s2) { Set r = new Set(); foreach (object o in s1) if (s2.Contains(o)) r.Add(o); return r; } publicstatic Set operator + (Set s1, Set s2) { Set r = new Set(); foreach (object o in s1) r.Add(o); foreach (object o in s2) r.Add(o); return r; } publicstatic Set operator - (Set s1, Set s2) { Set r = new Set(); foreach (object o in s1) if (!s2.Contains(o)) r.Add(o); return r; } . . .
Homework 1 reviewSolution (cont.) . . . publicoverridebool Equals (object o) { Set s = o as Set; return s != null && this == s; } publicoverrideint GetHashCode () { int r = 0; foreach (object o inthis) r ^= o.GetHashCode(); return r; } publicoverridestring ToString () { string r = "{\n"; foreach (object o inthis) r += string.Format("\t{0}\n", o); return r + "}\n"; } } } }
Homework 1 reviewWhat was useful • “foreach” • Properties • Operator overloading
Homework 1 reviewWhat not to do • Write Java code in C# • It works, but that is not the point • Use enumerators explicitly • Use for when you can use foreach • Propagate “Equals” and “GetHashCode” to the base class • Make array operations take linear (or more) time • Use a.dict.Keys in foreach instead of just “a” • Write complex code when simple one is OK • Build strings the old way… +=, +, +, +
Homework 1 reviewA General Guideline • Use conversions instead of overloading multiple versions of the same operator… • The long way: • Set operator +(Set s, IEnumerable e); • Set operator +(IEnumerable e, Set s); • Instead: • Set operator +(Set s1, Set s2); • implicit operator Set (IEnumerable e);