/// <nodoc />
        protected ArrayLiteralWithNativeCustomMerge(EvaluationResult[] data, MergeFunction customNativeMergeFunction, LineInfo location, AbsolutePath path)
            : base(data, location, path)
        {
            Contract.Requires(customNativeMergeFunction != null);

            m_customNativeMergeFunction = customNativeMergeFunction;
        }
Exemplo n.º 2
0
    public LazySegTree(int N, MergeFunction <T> merge, ResolveFunction <T, U> resolve, T fill = default(T))
    {
        Count = N;

        this.merge   = merge;
        this.resolve = resolve;

        n_layer = 1;
        for (int k = 1; k < N; k <<= 1)
        {
            n_layer += 1;
        }

        segsize  = new int[n_layer];
        data     = new T[n_layer][];
        lazy     = new bool[n_layer][];
        lazyData = new U[n_layer][];
        for (int l = 0; l < n_layer; ++l)
        {
            segsize[l]  = 1 << (n_layer - 1 - l);
            data[l]     = new T[1 << l];
            lazy[l]     = new bool[1 << l];
            lazyData[l] = new U[1 << l];
            for (int j = 0; j < (1 << l); ++j)
            {
                data[l][j] = fill;
            }
        }
    }
Exemplo n.º 3
0
        void MergeTrees(XmlNode xmlNode, TreeViewItem tvNode, MergeFunction mergeFunction, Object userData)
        {
            if (mergeFunction(xmlNode, tvNode, userData))
            {
                return;
            }

            foreach (XmlNode xmlChild in xmlNode.ChildNodes)
            {
                TreeViewItem tvChild = null;

                if (tvNode.Items.Contains("Loading..."))
                {
                    tvNode.Items.Remove("Loading...");
                }

                foreach (TreeViewItem tvi in tvNode.Items) //try to find the new child node in the existing tree
                {
                    if ((String)tvi.Header == xmlChild.Name)
                    {
                        tvChild = tvi;
                        break;
                    }
                }

                if (tvChild == null) // the branch exist recursively merge
                {
                    tvChild        = new TreeViewItem();
                    tvChild.Header = xmlChild.Name;
                    tvNode.Items.Add(tvChild);
                }

                MergeTrees(xmlChild, tvChild, mergeFunction, userData);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Combines this and right guided by 'combine' function. This is common logic for both merge and override
        /// </summary>
        internal EvaluationResult Combine(Context context, MergeFunction combine, EvaluationResult right)
        {
            // If the right hand side is undefined, short-circuit and return left.
            if (right.IsUndefined)
            {
                return(EvaluationResult.Create(this));
            }

            // If the right hand side is not an object literal (e.g. a number), then the combine just returns it. Same thing if it is an array.
            var rightObject = right.Value as ObjectLiteral;

            if (rightObject == null || rightObject is ArrayLiteral)
            {
                return(right);
            }

            // So at this point we know both sides are object literals, and therefore right members should be combined with left members

            // The result of a combine will have a number of members bounded by the summation between right and left members
            // So only when both sides are ObjectLiteralSlim we *could* end up with something that fits into an ObjectLiteralSlim
            // The following strategy puts all left side values into a dictionary first, to avoid the naive n^2 lookup algorithm
            // TODO: Still something better could be worked out when the result fits in an ObjectLiteralSlim (like sorting the keys first) so we avoid going to an intermediate dictionary
            var objectLiteralN = this as ObjectLiteralN;
            var values         = objectLiteralN != null?objectLiteralN.Values.ToDictionary() : Members.ToDictionary(kvp => kvp.Key.Value, kvp => kvp.Value);

            foreach (var binding in rightObject.Members)
            {
                // If the right member exists on the left, we replace it with whatever 'combine' gives us. Otherwise, we just reflect the right side on the left side.
                if (values.TryGetValue(binding.Key.Value, out var rightMember))
                {
                    values[binding.Key.Value] = combine(rightMember, binding.Value);
                }
                else
                {
                    values[binding.Key.Value] = binding.Value;
                }
            }

            var entry    = context.TopStack;
            var location = entry.InvocationLocation;
            var path     = entry.Path;

            // We don't want to return an ObjectLiteralN unconditionally, to leverage ObjectLiteralSlim memory savings
            // If the result doesn't fit in an ObjectLiteralSlim, we already have a dictionary built for an ObjectLiteralN, so
            // we create it directly to avoid extra allocations
            if (values.Count > 5)
            {
                return(EvaluationResult.Create(new ObjectLiteralN(values, location, path)));
            }

            // Otherwise, we use the regular Create so it dispatches the creation to the right ObjectLiteralSlim
            return(EvaluationResult.Create(Create(values.SelectArray(kvp => new NamedValue(kvp.Key, kvp.Value)).ToList(), location, path)));
        }
Exemplo n.º 5
0
    public LazySegTree(int N, MergeFunction <T> merge, ResolveFunction <T, U> resolve, PostponeFunction <U> postpone, T fill = default(T))
    {
        Count = N;

        this.merge    = merge;
        this.resolve  = resolve;
        this.postpone = postpone;

        n_layer = 1;
        for (int k = 1; k < N; k <<= 1)
        {
            n_layer += 1;
        }

        segsize  = new int[n_layer];
        data     = new T[n_layer][];
        lazy     = new bool[n_layer][];
        lazyData = new U[n_layer][];
        for (int l = 0; l < n_layer; ++l)
        {
            segsize[l]  = 1 << (n_layer - 1 - l);
            data[l]     = new T[1 << l];
            lazy[l]     = new bool[1 << l];
            lazyData[l] = new U[1 << l];
        }

        for (int j = 0; j < data[n_layer - 1].Length; ++j)
        {
            data[n_layer - 1][j] = fill;
        }
        for (int l = n_layer - 2; l >= 0; --l)
        {
            for (int j = 0; j < (1 << l); ++j)
            {
                data[l][j] = merge(data[l + 1][j * 2], data[l + 1][j * 2 + 1], new STParams(layer: l, segsize: segsize[l], index: j));
            }
        }
    }
        /// <summary>
        /// Creates array literal from an existing array, adding a custom merge function
        /// </summary>
        public static ArrayLiteralWithNativeCustomMerge Create(ArrayLiteral arrayLiteral, MergeFunction customNativeMergeFunction, LineInfo location, AbsolutePath path)
        {
            Contract.Assert(arrayLiteral != null);
            Contract.Assert(customNativeMergeFunction != null);
            Contract.Assert(path.IsValid);

            var data = new EvaluationResult[arrayLiteral.Length];

            arrayLiteral.Copy(0, data, 0, arrayLiteral.Length);

            return(new ArrayLiteralWithNativeCustomMerge(data, customNativeMergeFunction, location, path));
        }