// // ImmutableIntKeyMap.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.Linq; namespace Mono.CodeContracts.Static.DataStructures { class ImmutableIntKeyMap : IImmutableMap, IEquatable> { private readonly IImmutableIntMap> immutable_int_map; private readonly Func keyConverter; private ImmutableIntKeyMap (IImmutableIntMap> map, Func converter) { this.immutable_int_map = map; this.keyConverter = converter; } #region Implementation of IImmutableMap public V this [K key] { get { V value; TryGetValue (key, out value); return value; } } public K AnyKey { get { return Keys.First (); } } public IEnumerable Keys { get { var res = new List (); this.immutable_int_map.Visit (data => res.Add (data.Key)); return res; } } public int Count { get { return this.immutable_int_map.Count; } } public IImmutableMap EmptyMap { get { return Empty (this.keyConverter); } } public IImmutableMap Add (K key, V value) { return new ImmutableIntKeyMap (this.immutable_int_map.Add (this.keyConverter (key), new Pair (key, value)), this.keyConverter); } public IImmutableMap Remove (K key) { return new ImmutableIntKeyMap (this.immutable_int_map.Remove (this.keyConverter (key)), this.keyConverter); } public bool ContainsKey (K key) { return this.immutable_int_map.Contains (this.keyConverter (key)); } public bool TryGetValue(K key, out V value) { Pair pair = this.immutable_int_map[this.keyConverter(key)]; if (pair != null) return true.With(pair.Value, out value); return false.Without(out value); } public void Visit (Func func) { this.immutable_int_map.Visit (data => func (data.Key, data.Value)); } #endregion #region Implementation of IEquatable> public bool Equals (IImmutableMap other) { return this == other; } #endregion public static IImmutableMap Empty (Func keyConverter) { return new ImmutableIntKeyMap (ImmutableIntMap>.Empty, keyConverter); } public IImmutableMapFactory Factory () { return new MapFactory (this.keyConverter); } private class MapFactory : IImmutableMapFactory { private readonly Func keyConverter; public MapFactory (Func keyConverter) { this.keyConverter = keyConverter; } public IImmutableMap Empty { get { return ImmutableIntKeyMap.Empty (keyConverter);}} } } }