1 / 11

Homework 2 review

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;

Télécharger la présentation

Homework 2 review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Homework 2 review 09/20/02

  2. 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); } . . .

  3. 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); } . . .

  4. 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(); } . . .

  5. 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; } } . . .

  6. 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); } . . .

  7. 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; } . . .

  8. 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"; } } } }

  9. Homework 1 reviewWhat was useful • “foreach” • Properties • Operator overloading

  10. 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… +=, +, +, +

  11. 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);

More Related