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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
//
// EnvironmentDomain.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;
using System.Linq;
using Mono.CodeContracts.Static.DataStructures;
namespace Mono.CodeContracts.Static.Lattices {
class EnvironmentDomain<K, V> : IAbstractDomain<EnvironmentDomain<K, V>>
where V : IAbstractDomain<V>
where K : IEquatable<K> {
readonly IImmutableMapFactory<K, V> factory;
readonly IImmutableMap<K, V> map;
EnvironmentDomain (IImmutableMapFactory<K, V> factory)
: this (factory, factory.Empty)
{
}
EnvironmentDomain (IImmutableMap<K, V> map)
: this (map.Factory (), map)
{
}
EnvironmentDomain (IImmutableMapFactory<K, V> factory, IImmutableMap<K, V> map)
{
this.factory = factory;
this.map = map;
}
public V this [K key] { get { return map == null ? default(V) : map[key]; } }
public IEnumerable<K> Keys { get { return map.Keys; } }
#region IAbstractDomain<EnvironmentDomain<K,V>> Members
public EnvironmentDomain<K, V> Top { get { return new EnvironmentDomain<K, V> (factory.Empty); } }
public EnvironmentDomain<K, V> Bottom { get { return new EnvironmentDomain<K, V> (factory, null); } }
public bool IsTop { get { return map != null && map.Count == 0; } }
public bool IsBottom { get { return map == null; } }
public EnvironmentDomain<K, V> Join (EnvironmentDomain<K, V> that)
{
return JoinOrWiden (that, (a, b) => a.Join (b));
}
public EnvironmentDomain<K, V> Widen (EnvironmentDomain<K, V> that)
{
return JoinOrWiden (that, (a, b) => a.Widen (b));
}
EnvironmentDomain<K, V> JoinOrWiden (EnvironmentDomain<K, V> that, Func<V, V, V> op)
{
if (ReferenceEquals (map, that.map) || IsBottom)
return that;
if (that.IsBottom)
return this;
IImmutableMap<K, V> min;
IImmutableMap<K, V> max;
GetMinAndMaxByCount (map, that.map, out min, out max);
var result = min; // intersection of keys
foreach (var key in min.Keys) {
V thatValue;
if (max.TryGetValue (key, out thatValue)) {
var join = op (min[key], thatValue);
if (join.IsBottom)
return Bottom;
result = join.IsTop ? result.Remove (key) : result.Add (key, join);
}
else
result = result.Remove (key);
}
return new EnvironmentDomain<K, V> (result);
}
public EnvironmentDomain<K, V> Join (EnvironmentDomain<K, V> that, bool widening, out bool weaker)
{
//todo: remove it
weaker = false;
if (map == that.map || IsTop)
return this;
if (that.IsTop) {
weaker = !IsTop;
return that;
}
if (IsBottom) {
weaker = !that.IsBottom;
return that;
}
if (that.IsBottom)
return this;
IImmutableMap<K, V> min;
IImmutableMap<K, V> max;
GetMinAndMaxByCount (map, that.map, out min, out max);
var intersect = min;
foreach (var key in min.Keys) {
if (!max.ContainsKey (key))
intersect = intersect.Remove (key);
else {
bool keyWeaker;
var join = min[key].Join (max[key], widening, out keyWeaker);
if (keyWeaker) {
weaker = true;
intersect = join.IsTop ? intersect.Remove (key) : intersect.Add (key, join);
}
}
}
weaker |= intersect.Count < map.Count;
return new EnvironmentDomain<K, V> (intersect);
}
public EnvironmentDomain<K, V> Meet (EnvironmentDomain<K, V> that)
{
if (ReferenceEquals (map, that.map))
return this;
if (IsTop)
return that;
if (that.IsTop || IsBottom)
return this;
if (that.IsBottom)
return that;
IImmutableMap<K, V> min;
IImmutableMap<K, V> max;
GetMinAndMaxByCount (map, that.map, out min, out max);
var union = max;
foreach (var key in min.Keys) {
if (!max.ContainsKey (key))
union = union.Add (key, min[key]);
else {
var meet = min[key].Meet (max[key]);
union = union.Add (key, meet);
}
}
return new EnvironmentDomain<K, V> (union);
}
public bool LessEqual (EnvironmentDomain<K, V> that)
{
bool result;
if (this.TryTrivialLessEqual (that, out result))
return result;
if (map.Count < that.map.Count)
return false;
return that.map.Keys.All (key => map.ContainsKey (key) && map[key].LessEqual (that.map[key]));
}
public EnvironmentDomain<K, V> ImmutableVersion ()
{
return this;
}
public EnvironmentDomain<K, V> Clone ()
{
return this;
}
public void Dump (TextWriter tw)
{
if (IsTop)
tw.WriteLine ("Top");
else if (IsBottom)
tw.WriteLine (this.BottomSymbolIfAny ());
else {
map.Visit ((k, v) => {
tw.WriteLine ("{0} -> {1}", k, v);
return VisitStatus.ContinueVisit;
});
}
}
#endregion
public static EnvironmentDomain<K, V> TopValue (Func<K, int> keyConverter)
{
if (keyConverter == null)
throw new ArgumentNullException ("keyConverter");
return new EnvironmentDomain<K, V> (ImmutableIntKeyMap<K, V>.Empty (keyConverter));
}
public static EnvironmentDomain<K, V> TopValue ()
{
return new EnvironmentDomain<K, V> (ImmutableMap<K, V>.Empty);
}
public static EnvironmentDomain<K, V> BottomValue (Func<K, int> keyConverter)
{
if (keyConverter == null)
throw new ArgumentNullException ("keyConverter");
return new EnvironmentDomain<K, V> (ImmutableIntKeyMap<K, V>.Empty (keyConverter).Factory (), null);
}
public static EnvironmentDomain<K, V> BottomValue ()
{
return new EnvironmentDomain<K, V> (ImmutableMap<K, V>.Empty.Factory (), null);
}
public EnvironmentDomain<K, V> With (K key, V value)
{
if (value.IsTop)
return Without (key);
return new EnvironmentDomain<K, V> (map.Add (key, value));
}
public EnvironmentDomain<K, V> RefineWith (K key, V value)
{
V old;
if (map.TryGetValue (key, out old))
value = value.Meet (old);
return With (key, value);
}
public EnvironmentDomain<K, V> Without (K key)
{
return new EnvironmentDomain<K, V> (map.Remove (key));
}
public bool Contains (K key)
{
return map != null && map.ContainsKey (key);
}
public bool TryGetValue (K key, out V value)
{
if (map == null)
return false.Without (out value);
return map.TryGetValue (key, out value);
}
public EnvironmentDomain<K, V> Empty ()
{
return new EnvironmentDomain<K, V> (factory.Empty);
}
static bool GetMinAndMaxByCount
(IImmutableMap<K, V> a, IImmutableMap<K, V> b,
out IImmutableMap<K, V> min, out IImmutableMap<K, V> max)
{
if (a.Count < b.Count) {
min = a;
max = b;
return true;
}
max = a;
min = b;
return false;
}
}
}
|