// // ImmutableSet.cs // // Authors: // Alexander Chebaturkin (chebaturkin@gmail.com) // // Copyright (C) 2011 Alexander Chebaturkin // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // using System; using System.Collections.Generic; using System.IO; namespace Mono.CodeContracts.Static.DataStructures { class ImmutableSet : IImmutableSet where T : IEquatable { private readonly IImmutableMap underlying; public ImmutableSet (IImmutableMap immutableMap) { this.underlying = immutableMap; } #region Implementation of IImmutableSet public T Any { get { return this.underlying.AnyKey; } } public int Count { get { return this.underlying.Count; } } public IEnumerable Elements { get { return this.underlying.Keys; } } public IImmutableSet Add (T item) { return new ImmutableSet (this.underlying.Add (item, Dummy.Value)); } public IImmutableSet Remove (T item) { return new ImmutableSet (this.underlying.Remove (item)); } public bool Contains (T item) { return this.underlying.ContainsKey (item); } public bool IsContainedIn (IImmutableSet that) { if (Count > that.Count) return false; bool result = true; this.underlying.Visit ((e, dummy) => { if (that.Contains (e)) return VisitStatus.ContinueVisit; result = false; return VisitStatus.StopVisit; }); return result; } public IImmutableSet Intersect (IImmutableSet that) { if (this == that) return this; if (Count == 0) return this; IImmutableSet set; IImmutableSet larger; if (Count < that.Count) { set = this; larger = that; } else { if (that.Count == 0) return that; set = that; larger = this; } IImmutableSet result = set; set.Visit ((e) => { if (!larger.Contains (e)) result = result.Remove (e); }); return result; } public IImmutableSet Union (IImmutableSet that) { if (this == that) return this; if (Count == 0) return that; IImmutableSet smaller; IImmutableSet larger; if (Count < that.Count) { smaller = this; larger = that; } else { if (that.Count == 0) return this; smaller = that; larger = this; } IImmutableSet result = larger; smaller.Visit (e => { result = result.Add (e); }); return result; } public void Visit (Action visitor) { this.underlying.Visit ((elem, dummy) => { visitor (elem); return VisitStatus.ContinueVisit; }); } public void Dump (TextWriter tw) { tw.Write ("{"); bool first = true; foreach (T key in this.underlying.Keys) { if (!first) tw.Write (", "); else first = false; tw.Write ("{0}", key); } tw.WriteLine ("}"); } public static IImmutableSet Empty () { return new ImmutableSet (ImmutableMap.Empty); } public static IImmutableSet Empty (Func keyConverter) { return new ImmutableSet (ImmutableIntKeyMap.Empty (keyConverter)); } #endregion } }