Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,152 @@
//
// BranchNode.cs
//
// Authors:
// Alexander Chebaturkin (chebaturkin@gmail.com)
//
// Copyright (C) 2012 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.Text;
namespace Mono.CodeContracts.Static.DataStructures.Patricia
{
public class BranchNode<T> : PatriciaTrieNode<T>
{
private readonly int count;
public readonly int Prefix;
public readonly int BranchingBit;
public readonly PatriciaTrieNode<T> Left;
public readonly PatriciaTrieNode<T> Right;
public override int Key { get { return Prefix; } }
public override int Count { get { return count; } }
public BranchNode(int prefix, int branchingBit, PatriciaTrieNode<T> left, PatriciaTrieNode<T> right)
{
Prefix = prefix;
BranchingBit = branchingBit;
Left = left;
Right = right;
count = left.Count + right.Count;
}
public override bool Contains(int key)
{
if (!MatchPrefix(key, Prefix, BranchingBit))
return false;
var child = IsZeroBitAt(key, BranchingBit) ? Left : Right;
return child.Contains(key);
}
public override IImmutableIntMap<T> Add(int key, T value)
{
if (!MatchPrefix(key, Prefix, BranchingBit))
return Join(new LeafNode<T>(key, value), this);
if (IsZeroBitAt(key, BranchingBit))
return new BranchNode<T>(Prefix, BranchingBit, (PatriciaTrieNode<T>)Left.Add(key, value), Right);
return new BranchNode<T>(Prefix, BranchingBit, Left, (PatriciaTrieNode<T>)Right.Add(key, value));
}
public override IImmutableIntMap<T> Remove(int key)
{
var left = Left;
var right = Right;
if (IsZeroBitAt(key, BranchingBit))
{
left = (PatriciaTrieNode<T>)left.Remove(key);
if (left.Count == 0)
return right;
}
else
{
right = (PatriciaTrieNode<T>)right.Remove(key);
if (right.Count == 0)
return left;
}
return Join(left, right);
}
public override void Visit(Action<T> action)
{
Left.Visit(action);
Right.Visit(action);
}
public override void Visit(Action<int, T> action)
{
Left.Visit(action);
Right.Visit(action);
}
public override T Lookup(int key)
{
BranchNode<T> current = this;
PatriciaTrieNode<T> child;
do
{
child = IsZeroBitAt(key, current.BranchingBit) ? current.Left : current.Right;
current = child as BranchNode<T>;
}
while (current != null);
return child.Lookup(key);
}
protected internal override void FillKeysTo(List<int> list)
{
Left.FillKeysTo(list);
Right.FillKeysTo(list);
}
protected internal override void FillValuesTo(List<T> list)
{
Left.FillValuesTo(list);
Right.FillValuesTo(list);
}
protected internal override void AppendToBuilder(StringBuilder sb)
{
Left.AppendToBuilder(sb);
Right.AppendToBuilder(sb);
}
protected internal override void Dump(TextWriter tw, string prefix)
{
tw.WriteLine(prefix + "<Branch Prefix={0} BranchingBit={1}>", Prefix, BranchingBit);
Left.Dump(tw, prefix + " ");
Right.Dump(tw, prefix + " ");
tw.WriteLine(prefix + "</Branch>");
}
}
}

View File

@@ -0,0 +1,93 @@
//
// EmptyNode.cs
//
// Authors:
// Alexander Chebaturkin (chebaturkin@gmail.com)
//
// Copyright (C) 2012 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.Text;
namespace Mono.CodeContracts.Static.DataStructures.Patricia
{
public class EmptyNode<T> : PatriciaTrieNode<T>
{
public static readonly EmptyNode<T> Instance = new EmptyNode<T>();
public override bool Contains(int key)
{
return false;
}
public override int Key { get { throw new NotSupportedException("No key for empty node");} }
public override int Count
{
get { return 0; }
}
public override IImmutableIntMap<T> Add(int key, T value)
{
return new LeafNode<T>(key, value);
}
public override IImmutableIntMap<T> Remove(int key)
{
return this;
}
public override void Visit(Action<T> action)
{
}
public override void Visit(Action<int, T> action)
{
}
protected internal override void FillKeysTo(List<int> list)
{
}
protected internal override void FillValuesTo(List<T> list)
{
}
protected internal override void AppendToBuilder(StringBuilder sb)
{
sb.Append("*");
}
protected internal override void Dump(TextWriter tw, string prefix)
{
tw.WriteLine(prefix + "<empty/>");
}
public override T Lookup(int key)
{
return default(T);
}
}
}

View File

@@ -0,0 +1,106 @@
//
// LeafNode.cs
//
// Authors:
// Alexander Chebaturkin (chebaturkin@gmail.com)
//
// Copyright (C) 2012 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.Text;
namespace Mono.CodeContracts.Static.DataStructures.Patricia
{
public class LeafNode<T> : PatriciaTrieNode<T>
{
private readonly int _key;
public override bool Contains(int key)
{
return key == _key;
}
public override int Key { get { return _key; } }
public override int Count { get { return 1; } }
public T Value { get; private set; }
public LeafNode(int key, T value)
{
_key = key;
Value = value;
}
public override IImmutableIntMap<T> Add(int key, T value)
{
if (key == Key)
return new LeafNode<T>(key, value);
return Join(new LeafNode<T>(key, value), this);
}
public override IImmutableIntMap<T> Remove(int key)
{
if (key == this.Key)
return EmptyNode<T>.Instance;
return this;
}
public override void Visit(Action<T> action)
{
action(Value);
}
public override void Visit(Action<int, T> action)
{
action(Key, Value);
}
protected internal override void FillKeysTo(List<int> list)
{
list.Add(_key);
}
protected internal override void FillValuesTo(List<T> list)
{
list.Add(Value);
}
protected internal override void AppendToBuilder(StringBuilder sb)
{
sb.AppendFormat("{0}->'{1}' ", _key, Value);
}
protected internal override void Dump(TextWriter tw, string prefix)
{
tw.WriteLine(prefix + "<Leaf Key={0} Value='{1}' />", _key, Value);
}
public override T Lookup(int key)
{
return key == _key ? Value : default(T);
}
}
}

View File

@@ -0,0 +1,133 @@
//
// PatriciaTrieNode.cs
//
// Authors:
// Alexander Chebaturkin (chebaturkin@gmail.com)
//
// Copyright (C) 2012 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.Text;
namespace Mono.CodeContracts.Static.DataStructures.Patricia
{
public abstract class PatriciaTrieNode<T> : IImmutableIntMap<T>
{
public T this[int key]
{
get { return this.Lookup(key); }
}
public T Any { get; private set; }
public abstract bool Contains(int key);
public abstract int Key { get; }
public abstract int Count { get; }
public abstract IImmutableIntMap<T> Add(int key, T value);
public abstract IImmutableIntMap<T> Remove(int key);
public abstract void Visit(Action<T> action);
public abstract void Visit(Action<int, T> action);
public abstract T Lookup(int key);
public IEnumerable<int> Keys
{
get
{
var list = new List<int>(this.Count);
FillKeysTo(list);
return list;
}
}
public IEnumerable<T> Values
{
get
{
var list = new List<T>(this.Count);
FillValuesTo(list);
return list;
}
}
public void Dump(TextWriter tw)
{
Dump(tw, string.Empty);
}
protected internal abstract void FillKeysTo(List<int> list);
protected internal abstract void FillValuesTo(List<T> list);
protected internal abstract void AppendToBuilder(StringBuilder sb);
protected internal abstract void Dump(TextWriter tw, string prefix);
public override string ToString()
{
var sb = new StringBuilder();
AppendToBuilder(sb);
return sb.ToString();
}
protected static IImmutableIntMap<T> Join(PatriciaTrieNode<T> left, PatriciaTrieNode<T> right)
{
int keyLeft = left.Key;
int keyRight = right.Key;
int branchingBit = BranchingBit(keyLeft, keyRight);
var prefix = MaskWithBit(keyLeft, branchingBit);
if (IsZeroBitAt(keyLeft, branchingBit))
return new BranchNode<T>(prefix, branchingBit, left, right);
return new BranchNode<T>(prefix, branchingBit, right, left);
}
protected static bool IsZeroBitAt(int key, int branchingBit)
{
return (key & branchingBit) == 0;
}
private static int BranchingBit(int left, int right)
{
return LowestBit(left ^ right);
}
private static int LowestBit(int x)
{
return x & -x;
}
private static int MaskWithBit(int key, int mask)
{
return key & (mask - 1);
}
protected static bool MatchPrefix(int key, int prefix, int maskBit)
{
return MaskWithBit(key, maskBit) == prefix;
}
}
}