Ejemplo n.º 1
0
        public SppfNode CreateBranch(
            Production prod,
            ArraySlice <SppfNode> parts,
            IStackLookback <SppfNode> stackLookback)
        {
            // Produce more dense tree
            if (parts.Count == 0)
            {
                return(GetDefault(prod.OutcomeToken, stackLookback));
            }

            SppfNode[] children = new SppfNode[prod.Pattern.Length];
            parts.CopyTo(children, 0);

            Loc location  = Loc.Unknown;
            int partCount = parts.Count;

            for (int i = 0; i != partCount; ++i)
            {
                location += children[i].Location;
            }

            if (prod.PatternTokens.Length > parts.Count)
            {
                FillEpsilonSuffix(prod.Index, parts.Count, children, parts.Count, stackLookback);
            }

            return(new SppfNode(prod.Index, location, children));
        }
Ejemplo n.º 2
0
 internal static void VerifyCopyTo <T>(ArraySlice <T> slice, T[] copyToTargetArray) where T : unmanaged, IEquatable <T>
 {
     slice.CopyTo(copyToTargetArray, 1);
     for (int i = 0; i < slice.Count; ++i)
     {
         Assert.Equal(slice[i], copyToTargetArray[i + 1]);
     }
 }
Ejemplo n.º 3
0
        public string ShouldCopyTo(int start, int len, bool asc, int len2)
        {
            var array = Enumerable.Range(0, 100).ToArray();

            var slice  = new ArraySlice <int>(array, start, len, asc);
            var array2 = new int[len2];

            slice.CopyTo(array2, 0);

            return(string.Join(",", array2));
        }
Ejemplo n.º 4
0
        public void Trim()
        {
            if (_requiresTrim == false)
            {
                return;
            }

            // Compute new size needed for SmallValueArray
            int totalSmallValueLength = _smallValueArray?.Length ?? 0;
            int newSmallValueLength   = totalSmallValueLength;

            foreach (var pair in _largeValueDictionary)
            {
                int length = pair.Value.Count;
                if (length <= MaximumSmallValueLength)
                {
                    int index     = pair.Key;
                    int oldLength = ((index < _valueEndInPage?.Length) ? EndPosition(index) - StartPosition(index) : 0);
                    newSmallValueLength += (length - oldLength);
                }
            }

            // Make new arrays
            T[]      newSmallValueArray    = new T[newSmallValueLength];
            int[]    newPageStartInChapter = new int[(Count / PageRowCount) + 1];
            ushort[] newValueEndInPage     = new ushort[Count];

            // Copy every small-enough value to the new array and remove from LargeValueDictionary
            int lastNonEmptyIndex = -1;
            int currentPageStart  = 0;
            int nextIndex         = 0;

            for (int i = 0; i < Count; ++i)
            {
                // Set new page start for each page
                if ((i % PageRowCount) == 0)
                {
                    currentPageStart = nextIndex;
                    newPageStartInChapter[i / PageRowCount] = currentPageStart;
                }

                // Get current value
                ArraySlice <T> value = this[i];

                // Copy the value to the new _smallValueArray, if it fits
                if (value.Count <= MaximumSmallValueLength)
                {
                    value.CopyTo(newSmallValueArray, nextIndex);
                    nextIndex += value.Count;
                    _largeValueDictionary.Remove(i);

                    if (value.Count > 0)
                    {
                        lastNonEmptyIndex = i;
                    }
                }

                // Set new valueEnd
                newValueEndInPage[i] = (ushort)(nextIndex - currentPageStart);
            }

            if (_largeValueDictionary.Count == 0)
            {
                _largeValueDictionary = null;
            }
            _smallValueArray    = newSmallValueArray;
            _pageStartInChapter = newPageStartInChapter;
            _valueEndInPage     = newValueEndInPage;

            _requiresTrim      = false;
            _lastNonEmptyIndex = lastNonEmptyIndex;
        }