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
|
//
// 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>");
}
}
}
|