1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
//
// 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<K, V> : IImmutableMap<K, V>, IEquatable<IImmutableMap<K, V>> {
private readonly IImmutableIntMap<Pair<K, V>> immutable_int_map;
private readonly Func<K, int> keyConverter;
private ImmutableIntKeyMap (IImmutableIntMap<Pair<K, V>> map, Func<K, int> converter)
{
this.immutable_int_map = map;
this.keyConverter = converter;
}
#region Implementation of IImmutableMap<K,V>
public V this [K key]
{
get
{
V value;
TryGetValue (key, out value);
return value;
}
}
public K AnyKey
{
get { return Keys.First (); }
}
public IEnumerable<K> Keys
{
get
{
var res = new List<K> ();
this.immutable_int_map.Visit (data => res.Add (data.Key));
return res;
}
}
public int Count
{
get { return this.immutable_int_map.Count; }
}
public IImmutableMap<K, V> EmptyMap
{
get { return Empty (this.keyConverter); }
}
public IImmutableMap<K, V> Add (K key, V value)
{
return new ImmutableIntKeyMap<K, V> (this.immutable_int_map.Add (this.keyConverter (key), new Pair<K, V> (key, value)), this.keyConverter);
}
public IImmutableMap<K, V> Remove (K key)
{
return new ImmutableIntKeyMap<K, V> (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<K, V> 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<K, V, VisitStatus> func)
{
this.immutable_int_map.Visit (data => func (data.Key, data.Value));
}
#endregion
#region Implementation of IEquatable<IImmutableMap<K,V>>
public bool Equals (IImmutableMap<K, V> other)
{
return this == other;
}
#endregion
public static IImmutableMap<K, V> Empty (Func<K, int> keyConverter)
{
return new ImmutableIntKeyMap<K, V> (ImmutableIntMap<Pair<K, V>>.Empty, keyConverter);
}
public IImmutableMapFactory<K, V> Factory ()
{
return new MapFactory (this.keyConverter);
}
private class MapFactory : IImmutableMapFactory<K,V> {
private readonly Func<K, int> keyConverter;
public MapFactory (Func<K, int> keyConverter)
{
this.keyConverter = keyConverter;
}
public IImmutableMap<K, V> Empty { get { return ImmutableIntKeyMap<K, V>.Empty (keyConverter);}}
}
}
}
|