コード例 #1
0
 private void AddCode(CodeItem[] codes, ByteNode treeNode, int p, List <bool> list)
 {
     if (treeNode.Parent != null)
     {
         if (treeNode.IsLeftChild)
         {
             list.Add(false); // false stands for 0
         }
         else
         {
             list.Add(true); // true stands for 1
         }
     }
     if (treeNode.IsLeaf)
     {
         if (treeNode.Times > 0) // if the code never happen, we won't waste time on it
         {
             CodeItem codeItem = new CodeItem();
             codeItem.ByteValue        = treeNode.ByteValue;
             codeItem.CodeValue        = GetCodeValue(list);
             codeItem.CodeBits         = list.ToArray();
             codes[treeNode.ByteValue] = codeItem;
         }
     }
     else
     {
         AddCode(codes, treeNode.Left, p + 1, list); // traverse the tree recursively
         AddCode(codes, treeNode.Right, p + 1, list);
     }
     if (list.Count > 0)
     {
         list.RemoveAt(list.Count - 1);
     }
 }
コード例 #2
0
        public ByteTreeSearchBenchmarks()
        {
            var comparator       = new Comparator();
            var brancher         = new Brancher();
            var evaluator        = new Evaluator();
            var stateTransitions = new StateTransitions();

            _serial = new SerialAlfaBetaSearch <ByteNode, sbyte, sbyte>(
                evaluator,
                brancher,
                comparator,
                stateTransitions,
                sbyte.MaxValue,
                sbyte.MinValue);

            _serialTree = TreeGenerator.ReadTree();

            _dynamic = new DynamicTreeSplitting <AlfaBetaByteNode, sbyte, sbyte>(
                evaluator,
                brancher,
                comparator,
                stateTransitions
                );

            _dynamicTree = TreeGenerator.ReadAlfaBetaTree();

            _cts = new CancellationTokenSource();
        }
コード例 #3
0
        private Node ParseFactor()
        {
            Node  n;
            Token t = GetToken();

            switch (t)
            {
            case Token.Byte:
                n = new ByteNode(this.b, positions.Count);
                positions.Add(n);
                AddCharacterClass(b);
                break;

            case Token.Dot:
                n = new AnyNode(positions.Count);
                positions.Add(n);
                break;

            case Token.LParen:
                n = Parse();
                Expect(Token.RParen);
                break;

            default:
                return(SyntaxError());
            }
            return(n);
        }
コード例 #4
0
        public override void Compress(BinaryReader reader, BinaryWriter writer)
        {
            ByteNode[] items = new ByteNode[256];
            for (int i = 0; i < 256; i++)
            {
                items[i] = new ByteNode((byte)i);
            }

            while (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                items[reader.ReadByte()].Times++;
            }

            // save frequency table
            ByteNode.Serializer.Serialize(writer.BaseStream, items);

            CodeItem[]      codes    = GetCodes(items);
            List <CodeItem> notEmpty = new List <CodeItem>();
            Dictionary <byte, IList <bool> > byteDic = new Dictionary <byte, IList <bool> >();

            foreach (CodeItem ci in codes)
            {
                if (ci != null)
                {
                    notEmpty.Add(ci);
                    byteDic[ci.ByteValue] = ci.CodeBits;
                }
            }

            List <bool> allBits = new List <bool>();

            // convert original byte to our bit code
            reader.BaseStream.Seek(0, SeekOrigin.Begin);
            int lastPercent = 0;

            while (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                lastPercent = RaiseEvent(reader, lastPercent);
                allBits.AddRange(byteDic[reader.ReadByte()]);
            }

            // complementary bits
            byte length = 0;

            if (allBits.Count % 8 != 0)
            {
                length = (byte)(8 - allBits.Count % 8);
                allBits.AddRange(new bool[length]);
            }

            writer.Write(length);
            // write all bits
            for (int i = 0; i < allBits.Count - 7; i += 8)
            {
                writer.Write(ToByte(allBits, i));
            }

            RaiseFinishEvent();
        }
コード例 #5
0
 public ByteNode(ByteNode lhs, ByteNode rhs)
 {
     Left       = lhs;
     Right      = rhs;
     lhs.Parent = this;
     rhs.Parent = this;
     Times      = lhs.Times + rhs.Times;
 }
コード例 #6
0
            public int CompareTo(object obj)
            {
                if (obj == null || obj.GetType() != GetType())
                {
                    throw new ArgumentException("obj can't be null or the other type");
                }
                ByteNode bi = (ByteNode)obj;

                return(Times.CompareTo(bi.Times));
            }
コード例 #7
0
        public static ByteNode ParseByteTree(string tree, byte branchingFactor)
        {
            var bytes = tree.Split(' ');

            var depth = -1;
            var sum   = 0;

            while (sum < bytes.Length)
            {
                depth++;
                sum += (int)Math.Pow(branchingFactor, depth);
            }

            if (sum != bytes.Length)
            {
                throw new ArgumentException("Tree of unequal depth");
            }

            var queue        = new Queue <ByteNode>();
            var startDequeue = false;
            var bottomLevel  = (int)Math.Pow(branchingFactor, depth);

            for (var i = bytes.Length - 1; i >= 0; i--)
            {
                ByteNode node;

                if (queue.Count == bottomLevel)
                {
                    startDequeue = true;
                }

                if (startDequeue)
                {
                    var children = new ByteNode[branchingFactor];
                    for (var j = 0; j < branchingFactor; j++)
                    {
                        children[j] = queue.Dequeue();
                    }

                    node = new ByteNode(sbyte.Parse(bytes[i]), !children[0].IsMaxPlayer, children);
                }
                else
                {
                    var children = new ByteNode[0];
                    node = new ByteNode(sbyte.Parse(bytes[i]), depth % 2 == 0, children);
                }

                queue.Enqueue(node);
            }

            return(queue.Dequeue());
        }
コード例 #8
0
 public static ByteNode[] Deserialize(Stream stream)
 {
     ByteNode[] bns = new ByteNode[256];
     for (int i = 0; i < 256; i++)
     {
         bns[i]           = new ByteNode();
         bns[i].ByteValue = (byte)stream.ReadByte();
         byte[] buff = new byte[2];
         stream.Read(buff, 0, 2);
         bns[i].Times = BitConverter.ToUInt16(buff, 0);
     }
     return(bns);
 }
コード例 #9
0
        private CodeItem[] GetCodes(ByteNode[] items)
        {
            Heap <ByteNode> heap = new Heap <ByteNode>(items);

            while (heap.Count > 1)
            {
                ByteNode lhs    = heap.DeleteMin();       // left node is smaller / 0
                ByteNode rhs    = heap.DeleteMin();       // right node is bigger / 1
                ByteNode parent = new ByteNode(lhs, rhs); // give them a parent
                heap.Add(parent);
            }
            CodeItem[] codes = new CodeItem[256];
            AddCode(codes, heap.DeleteMin(), 0, new List <bool>());
            return(codes);
        }
コード例 #10
0
            public override void ComputeFollowPos(RegexpBuilder bld)
            {
                node.ComputeFollowPos(bld);
                FollowPos = node.FollowPos;
                for (int i = 0; i != LastPos.Length; ++i)
                {
                    if (!LastPos[i])
                    {
                        continue;
                    }

                    ByteNode ii = (ByteNode)bld.positions[i];
                    ii.FollowPos.Or(FirstPos);
                }
            }
コード例 #11
0
            public override void ComputeFollowPos(RegexpBuilder bld)
            {
                Left.ComputeFollowPos(bld);
                Right.ComputeFollowPos(bld);
                FollowPos = new BitArray(0);
                for (int i = 0; i != Left.LastPos.Length; ++i)
                {
                    if (!Left.LastPos.Get(i))
                    {
                        continue;
                    }

                    ByteNode ii = (ByteNode)bld.positions[i];
                    ii.FollowPos.Or(Right.FirstPos);
                }
            }
コード例 #12
0
        void ReplaceNode(ITree parent, int index, object val, SimpleTypeEnum type)
        {
            CommonTree node  = null;
            var        token = new CommonToken(-1, val.ToString());

            switch (type)
            {
            case SimpleTypeEnum.Bool:
                node = new BoolNode(token);
                break;

            case SimpleTypeEnum.Byte:
                node = new ByteNode(token);
                break;

            case SimpleTypeEnum.Char:
                node = new CharNode(token);
                break;

            case SimpleTypeEnum.Int:
                node = new IntegerNode(token);
                break;

            case SimpleTypeEnum.Float:
                node = new FloatNode(token);
                break;

            case SimpleTypeEnum.Double:
                node = new RealNode(token);
                break;

            default:
                throw new ArgumentOutOfRangeException("type", type, null);
            }

            parent.ReplaceChildren(index, index, node);
        }
コード例 #13
0
        private State [] BuildDfaTable(Node n)
        {
            List <State> dStates = new List <State>();

            // Create the default, error state.

            State err = new State(new BitArray(n.FirstPos.Length), charClasses);

            AddState(dStates, err);

            // Create the initial state.

            State s0 = new State(n.FirstPos, charClasses);

            AddState(dStates, s0);

            // Start the worklist.

            WorkList <State> worklist = new WorkList <State>();

            worklist.Add(s0);
            State t;

            while (worklist.TryGetWorkItem(out t))
            {
                Debug.WriteLine(t.ToString());
                for (int a = 0; a != charClasses; ++a)
                {
                    // Create U, a state consisting of the positions in
                    // FollowPos(p) where p is any position in t that has
                    // an 'a'.

                    State u = new State(new BitArray(positions.Count), charClasses);
                    for (int p = 0; p != t.Positions.Length; ++p)
                    {
                        if (!t.Positions[p])
                        {
                            continue;
                        }
                        ByteNode pp = (ByteNode)positions[p];
                        if (pp.Any || alphabet[pp.startByte] == a)
                        {
                            u.Positions.Or(pp.FollowPos);
                        }
                        t.Accepts |= pp.Accepts;
                    }

                    if (IsEmptySet(u.Positions))
                    {
                        u = null;
                    }
                    else
                    {
                        State uu = FindState(dStates, u.Positions);
                        if (uu == null)
                        {
                            AddState(dStates, u);
                            worklist.Add(u);
                        }
                        else
                        {
                            u = uu;
                        }
                    }
                    t.NextState[a] = u;
                }
                Debug.WriteLine("t complete: " + t);
            }
            return(dStates.ToArray());
        }
コード例 #14
0
ファイル: RegexpBuilder.cs プロジェクト: gitter-badger/reko
		private Node ParseFactor()
		{
			Node n;
			Token t = GetToken();
			switch (t)
			{
			case Token.Byte: 
				n = new ByteNode(this.b, positions.Count);
				positions.Add(n);
				AddCharacterClass(b);
				break;
			case Token.Dot:
				n = new AnyNode(positions.Count);
				positions.Add(n);
				break;
			case Token.LParen: 
				n = Parse();
				Expect(Token.RParen);
				break;
			default:
				return SyntaxError();
			}
			return n;
		}