Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / Mono.CodeContracts / Mono.CodeContracts.Static.DataStructures / ImmutableIntMap.cs
index 39bb01ce9c2d30d93c458607e1549745466322a3..0727f852bf92c849be03b67e387f72d2ea4c7278 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 // 
 
-using System;
-using System.Collections.Generic;
-using System.Linq;
+using Mono.CodeContracts.Static.DataStructures.Patricia;
 
 namespace Mono.CodeContracts.Static.DataStructures {
-       class ImmutableIntMap<T> : IImmutableIntMap<T> {
-               private readonly Dictionary<int, T> map = new Dictionary<int, T> ();
-
-               private ImmutableIntMap (Dictionary<int, T> map)
-               {
-                       this.map = map;
-               }
-
-               #region Implementation of IImm  utableIntMap<T>
-               public T this [int key]
-               {
-                       get
-                       {
-                               if (this.map == null || !this.map.ContainsKey (key))
-                                       return default(T);
-
-                               return this.map [key];
-                       }
-               }
-
-               public T Any
-               {
-                       get
-                       {
-                               if (this.map == null)
-                                       return default(T);
-                               return this.map.Values.First ();
-                       }
-               }
-
-               public IEnumerable<T> Values
-               {
-                       get
-                       {
-                               if (this.map == null)
-                                       return Enumerable.Empty<T> ();
-                               return this.map.Values;
-                       }
-               }
-
-               public IEnumerable<int> Keys
-               {
-                       get
-                       {
-                               if (this.map == null)
-                                       return Enumerable.Empty<int> ();
-                               return this.map.Keys;
-                       }
-               }
-
-               public int Count
-               {
-                       get
-                       {
-                               if (this.map == null)
-                                       return 0;
-                               return this.map.Count;
-                       }
-               }
-
-               public T Lookup (int key)
-               {
-                       if (this.map == null)
-                               return default(T);
-                       return this.map [key];
-               }
-
-               public bool Contains (int key)
-               {
-                       if (this.map == null)
-                               return false;
-                       return this.map.ContainsKey (key);
-               }
-
-               public IImmutableIntMap<T> Add (int key, T value)
-               {
-                       if (this.map == null)
-                               return new ImmutableIntMap<T> (new Dictionary<int, T> {{key, value}});
-
-                       var newDict = new Dictionary<int, T> (this.map);
-                       newDict [key] = value;
-                       return new ImmutableIntMap<T> (newDict);
-               }
-
-               public IImmutableIntMap<T> Remove (int key)
-               {
-                       if (this.map == null || !this.map.ContainsKey (key))
-                               return this;
-
-                       var newDict = new Dictionary<int, T> (this.map);
-                       newDict.Remove (key);
-                       return new ImmutableIntMap<T> (newDict);
-               }
-
-               public void Visit (Action<T> action)
-               {
-                       foreach (T value in Values)
-                               action (value);
-               }
-
-               public void Visit (Action<int, T> action)
-               {
-                       foreach (int key in Keys)
-                               action (key, this [key]);
-               }
-               #endregion
-
-               public static IImmutableIntMap<T> Empty ()
-               {
-                       return new ImmutableIntMap<T> (null);
-               }
+        public static class ImmutableIntMap<T>
+       {
+        public static readonly IImmutableIntMap<T> Empty = EmptyNode<T>.Instance;    
        }
 }