Ejemplo n.º 1
0
        public static TableData ExtractData(Config config, ConfigTable table)
        {
            var charSets     = FATools.ExtractAlphabet(table.Graph);
            var charClassMap = GetCharClassMap(table.Graph, charSets);
            var stateMap     = GetStateMap(table.Graph);

            var stateCount = table.Graph.States.Count;

            var statistics = new Statistics()
            {
                CharClassifications = charSets.Length,
                States = stateCount,
            };

            var data = new TableData()
            {
                TableID    = table.Index,
                Statistics = statistics,
            };

            {
                var combined = ExtractTransitionTable(table, charSets, charClassMap, stateMap, statistics);

                var offsetsSectionLen = stateCount;
                var transitionTable   = new int[offsetsSectionLen + combined.Count];
                combined.CopyTo(transitionTable, offsetsSectionLen);

                for (var i = 0; i < stateCount; i++)
                {
                    var offset = combined.GetOffset(i);

                    if (offset.HasValue)
                    {
                        transitionTable[i] = offset.Value + offsetsSectionLen;
                    }
                }

                var transitionsBlob = CompressedBlob.Compress(config.Manager.TableCompression, ElementSizeStrategy.Get(config.Manager.ElementSize), transitionTable);

                statistics.TransitionsRunTime       = transitionTable.Length;
                statistics.TransitionsAssemblyBytes = transitionsBlob.Bytes;
                data.TransitionTable = transitionsBlob;
            }

            {
                var ranges = GetRanges(charSets, charClassMap);
                ExtractClasificationTable(ranges, out var boundries, out var classifications);

                statistics.CharRanges            = ranges.Length;
                data.CharClassificationBoundries = boundries;
                data.CharClassification          = classifications;
                data.StateMap = stateMap;
            }

            return(data);
        }
Ejemplo n.º 2
0
        public void None([ValueSource(nameof(_sizeValues))] int size)
        {
            var sizex = ElementSizeStrategy.Get((TableElementSize)size);

            var cb = CompressedBlob.Compress(Compression.None, sizex, _sampleBlob1);

            Assert.That(cb.Method, Is.EqualTo(Compression.None));
            Assert.That(cb.ElementSize, Is.EqualTo(sizex));
            Assert.That(cb.BlobSize, Is.EqualTo(sizex));
            Assert.That(cb, Is.EquivalentTo(_sampleBlob1));
        }
Ejemplo n.º 3
0
        public void Auto([ValueSource(nameof(_sizeValues))] int size)
        {
            var sizex = ElementSizeStrategy.Get((TableElementSize)size);

            var cb1 = CompressedBlob.Compress(Compression.Auto, sizex, _sampleBlob1);

            Assert.That(cb1.Method, Is.EqualTo(Compression.Simple));
            Assert.That(cb1.ElementSize, Is.EqualTo(sizex));
            Assert.That(cb1.BlobSize, Is.EqualTo(sizex));
            Assert.That(cb1, Is.EquivalentTo(Replace(_sampleBlob1Simple, -1, sizex.MaxValue)));

            var cb2 = CompressedBlob.Compress(Compression.Auto, sizex, _sampleBlob2);

            Assert.That(cb2.Method, Is.EqualTo(Compression.None));
            Assert.That(cb2.ElementSize, Is.EqualTo(sizex));
            Assert.That(cb2.BlobSize, Is.EqualTo(sizex));
            Assert.That(cb2, Is.EquivalentTo(_sampleBlob2));
        }
Ejemplo n.º 4
0
        public void CTB([ValueSource(nameof(_sizeValues))] int size)
        {
            var sizex = ElementSizeStrategy.Get((TableElementSize)size);

            var cb1 = CompressedBlob.Compress(Compression.CTB, sizex, _sampleBlob1);

            Assert.That(cb1.Method, Is.EqualTo(Compression.CTB));
            Assert.That(cb1.ElementSize, Is.EqualTo(sizex));
            Assert.That(cb1.BlobSize, Is.EqualTo(U8SizeStrategy.Instance));
            Assert.That(cb1, Is.EquivalentTo(_sampleBlob1CTB));

            var cb2 = CompressedBlob.Compress(Compression.CTB, sizex, _sampleBlob2);

            Assert.That(cb2.Method, Is.EqualTo(Compression.CTB));
            Assert.That(cb2.ElementSize, Is.EqualTo(sizex));
            Assert.That(cb2.BlobSize, Is.EqualTo(U8SizeStrategy.Instance));
            Assert.That(cb2, Is.EquivalentTo(_sampleBlob2CTB));
        }
Ejemplo n.º 5
0
        public void CopyBytesTo()
        {
            var src = new int[]
            {
                0x01, 0x02, 0x03, 0x04,
                0x11, 0x12, 0x13, 0x14,
                0x21, 0x22, 0x23, 0x24,
                0x31, 0x32, 0x33, 0x34,
            };

            var expected = new byte[]
            {
                0x12, 0x13, 0x14, 0x21,
                0x22, 0x00, 0x00, 0x00,
                0x00, 0x00,
            };

            var cb1    = CompressedBlob.Compress(Compression.None, U8SizeStrategy.Instance, src);
            var buffer = new byte[10];

            cb1.CopyBytesTo(buffer, 5, 5);

            Assert.That(buffer, Is.EquivalentTo(expected));
        }
Ejemplo n.º 6
0
        static CompressedBlob ExtractTransitionTableBlob(Config config, Statistics statistics, TableData data)
        {
            var graph              = config.Graph.Graph;
            var actionsList        = new int[data.StateMap.Count];
            var lastGotoStateIndex = data.StateMap.Count;

            var fragments = new List <TableFragment>();

            foreach (var state in graph.States)
            {
                var index         = data.StateMap[state];
                var transitionRow = ExtractTransitionRow(data, state);
                var gotoRow       = ExtractGotoRow(data, state);

                if (transitionRow.Fragment == null)
                {
                    actionsList[index] = transitionRow.ShortCircuitReduction;
                    statistics.StatesShortCircuited++;

                    if (gotoRow != null)
                    {
                        statistics.StatesWithGotos++;
                    }
                }
                else
                {
                    fragments.Add(transitionRow.Fragment);

                    if (transitionRow.HasReduction && transitionRow.HasShift)
                    {
                        statistics.StatesWithSRConflicts++;
                    }

                    if (gotoRow != null)
                    {
                        statistics.StatesWithGotos++;
                    }
                    else if (!transitionRow.HasReduction || !transitionRow.HasShift)
                    {
                        statistics.StatesOther++;
                    }
                }

                if (gotoRow != null)
                {
                    fragments.Add(gotoRow);
                    statistics.NonTerminalColumns = Math.Max(statistics.NonTerminalColumns, gotoRow.Count);
                    lastGotoStateIndex            = data.StateMap[state] + 1;
                }
            }

            var combined = TableFragment.Combine(fragments);

            var offsetSectionLen = graph.States.Count + lastGotoStateIndex;

            Array.Resize(ref actionsList, offsetSectionLen + combined.Count);

            for (var i = 0; i < graph.States.Count << 1; i++)
            {
                var offset = combined.GetOffset(i);

                if (offset.HasValue)
                {
                    actionsList[i] = offset.Value + offsetSectionLen;
                }
            }

            combined.CopyTo(actionsList, offsetSectionLen);

            var actionsBlob = CompressedBlob.Compress(config.Manager.TableCompression, ElementSizeStrategy.Get(config.Manager.ElementSize), actionsList);

            statistics.ActionsRunTime       = actionsList.Length;
            statistics.ActionsAssemblyBytes = actionsBlob.Bytes;
            statistics.GotoOffsetsLen       = lastGotoStateIndex;
            return(actionsBlob);
        }
Ejemplo n.º 7
0
        static void WriteLargeIntArray(TextWriter writer, int indent, string name, string suffix, int wrap, CompressedBlob data)
        {
            CodeGenHelper.WriteIndent(writer, indent);
            writer.Write(name);

            if (!string.IsNullOrEmpty(suffix))
            {
                writer.Write('_');
                writer.Write(suffix);
            }

            writer.Write(" = ");
            CodeGenHelper.WriteLargeIntArray(writer, indent, wrap, data);
            writer.WriteLine(";");
        }