Пример #1
0
 private Node MutateOrCreate(TValue value, Node left, Node right, Lineage lin)
 {
     if (_lineage.AllowMutation(lin))
     {
         Value  = value;
         Left   = left;
         Right  = right;
         Height = left.Height > right.Height ? left.Height + 1 : right.Height + 1;
         Count  = 1 + left.Count + right.Count;
         return(this);
     }
     else
     {
         return(new Node(Key, value, left, right, Comparer, lin));
     }
 }
Пример #2
0
            /// <summary>
            ///     Removes the last element.
            /// </summary>
            /// <param name="lineage"></param>
            /// <returns></returns>
            public override Node RemoveLast(Lineage lineage)
            {
#if ASSERTS
                var oldLength  = Length;
                var oldArrSize = ArrSize;
                var beforeLast = Length > 1 ? Optional.Some(this[Length - 2]) : Optional.None;
#endif
                Node ret;
                if (Lineage.AllowMutation(lineage))
                {
                    ret = Remove_MUTATES();
                }
                else
                {
                    var newArr = Arr.Resize(ArrSize - 1);
                    ret = new Leaf(newArr, lineage, ArrSize - 1);
                }
#if ASSERTS
                ret.Length.AssertEqual(oldLength - 1);
                ret.ArrSize.AssertEqual(oldArrSize - 1);
                if (beforeLast.IsSome)
                {
                    ret[ret.Length - 1].AssertEqual(beforeLast.Value);
                }
#endif
                return(ret);
            }
Пример #3
0
 public Node WithBucket(Bucket newBucket, Lineage lineage)
 {
     if (Lineage.AllowMutation(lineage))
     {
         Bucket = newBucket;
         Count  = Left.Count + Right.Count + newBucket.Count;
         return(this);
     }
     return(new Node(Hash, newBucket, Left, Right, newBucket.Eq, lineage));
 }
Пример #4
0
            Node MutateOrCreate(int count, int arrSize, Node[] arr, Lineage lineage)
            {
                var ret = Lineage.AllowMutation(lineage)
                                        ? _mutate(count, arrSize, arr) : new Parent(Height, count, arr, lineage, arrSize);

#if ASSERTS
                ret.Length.AssertEqual(arr.Resize(ret.ArrSize).Aggregate(0, (tot, cur) => cur != null ? tot + cur.Length : tot));
                var leafIndex   = Array.FindIndex(arr, node => node != null && !node.IsParent);
                var parentIndex = Array.FindLastIndex(arr, node => node != null && node.IsParent);
                leafIndex.AssertEqual(i => i == -1 || i > parentIndex);
#endif
                return(ret);
            }
Пример #5
0
 Node[] UpdateStore(int index, Node v, Lineage lineage)
 {
     if (Lineage.AllowMutation(lineage))
     {
         if (Arr.Length < 32)
         {
             Arr = Arr.Resize(32);
         }
         Arr[index] = v;
         return(Arr);
     }
     else
     {
         return(Arr.Update(index, v, ArrSize));
     }
 }
Пример #6
0
 Node[] AddToStore(Node v, Lineage lineage)
 {
     if (Lineage.AllowMutation(lineage))
     {
         if (Arr.Length < 32)
         {
             Arr = Arr.Resize(32);
         }
         Arr[ArrSize] = v;
         return(Arr);
     }
     else
     {
         var arr = Arr.Resize(ArrSize + 1);
         arr[ArrSize] = v;
         return(arr);
     }
 }
Пример #7
0
                public override Digit Update(int index, Leaf <TValue> leaf, Lineage lineage)
                {
                    if (Lineage.AllowMutation(lineage))
                    {
                        return(Update_MUTATES(index, leaf));
                    }
#if ASSERTS
                    leaf.AssertNotNull();
#endif
                    var    whereIsThisIndex = WhereIsThisIndex(index);
                    TChild res;
                    switch (whereIsThisIndex)
                    {
                    case IN_START:
                    case IN_MIDDLE_OF_1:
                        res = First.Update(index, leaf, lineage);
                        return(CreateCheckNull(lineage, res, Second, Third, Fourth));

                    case IN_START_OF_2:
                    case IN_MIDDLE_OF_2:
                        res = Second.Update(index - First.Measure, leaf, lineage);
                        return(CreateCheckNull(lineage, First, res, Third, Fourth));

                    case IN_START_OF_3:
                    case IN_MIDDLE_OF_3:
                        res = Third.Update(index - First.Measure - Second.Measure, leaf, lineage);
                        return(CreateCheckNull(lineage, First, Second, res, Fourth));

                    case IN_START_OF_4:
                    case IN_MIDDLE_OF_4:
                        res = Fourth.Update(index - First.Measure - Second.Measure - Third.Measure, leaf, lineage);
                        return(CreateCheckNull(lineage, First, Second, Third, res));

                    case IN_END:
                    case OUTSIDE:
                        throw ImplErrors.Arg_out_of_range("index", index);

                    default:
                        throw ImplErrors.Invalid_execution_path("Checked all index locations.");
                    }
                }
Пример #8
0
 Node MutateOrCreate(int arrSize, TValue[] arr, Lineage lineage)
 {
     return(Lineage.AllowMutation(lineage) ? _mutate(arr, arrSize) : new Leaf(arr, lineage, arrSize));
 }
Пример #9
0
 private Digit MutateOrCreate(TChild a, TChild b, TChild c, TChild d, Lineage lineage)
 {
     return(Lineage.AllowMutation(lineage)
                                 ? _mutate(a.Measure + b.Measure + c.Measure + d.Measure, 4, a, b, c, d) : new Digit(a, b, c, d, lineage));
 }
Пример #10
0
 private Digit MutateOrCreate(TChild a, TChild b, Lineage lineage)
 {
     return(Lineage.AllowMutation(lineage) ? _mutate(a.Measure + b.Measure, 2, a, b) : new Digit(a, b, lineage));
 }
Пример #11
0
 private Digit MutateOrCreate(TChild a, Lineage lineage)
 {
     return(Lineage.AllowMutation(lineage) ? _mutate(a.Measure, 1, a) : new Digit(a, lineage));
 }