public static MutableInteger SetGreatestCommonDivisor(this MutableInteger c, MutableInteger a, MutableInteger b, MutableIntegerStore store)
        {
            var reg1 = store.Allocate();

            if (a.IsZero)
            {
                c.Set(b);
            }
            else if (b.IsZero)
            {
                c.Set(a);
            }
            else
            {
                reg1.Set(a);
                c.Set(b);
                while (true)
                {
                    reg1.Modulo(c);
                    if (reg1.IsZero)
                    {
                        break;
                    }
                    c.Modulo(reg1);
                    if (c.IsZero)
                    {
                        c.Set(reg1);
                        break;
                    }
                }
            }
            store.Release(reg1);
            return(c);
        }
Exemplo n.º 2
0
 internal static bool LeftEdge(Tree t, Tree t1, MutableInteger i)
 {
     if (t == t1)
     {
         return(true);
     }
     else
     {
         if (t1.IsLeaf())
         {
             int j = t1.Yield().Count;
             // so that empties don't add size
             i.Set(i + j);
             return(false);
         }
         else
         {
             foreach (Tree kid in t1.Children())
             {
                 if (LeftEdge(t, kid, i))
                 {
                     return(true);
                 }
             }
             return(false);
         }
     }
 }
Exemplo n.º 3
0
 internal static Tree GetPreTerminal(Tree tree, MutableInteger i, int n)
 {
     if (i == n)
     {
         if (tree.IsPreTerminal())
         {
             return(tree);
         }
         else
         {
             return(GetPreTerminal(tree.Children()[0], i, n));
         }
     }
     else
     {
         if (tree.IsPreTerminal())
         {
             i.Set(i + tree.Yield().Count);
             return(null);
         }
         else
         {
             foreach (Tree kid in tree.Children())
             {
                 Tree result = GetPreTerminal(kid, i, n);
                 if (result != null)
                 {
                     return(result);
                 }
             }
             return(null);
         }
     }
 }
Exemplo n.º 4
0
 internal static bool RightEdge(Tree t, Tree t1, MutableInteger i)
 {
     if (t == t1)
     {
         return(true);
     }
     else
     {
         if (t1.IsLeaf())
         {
             int j = t1.Yield().Count;
             // so that empties don't add size
             i.Set(i - j);
             return(false);
         }
         else
         {
             Tree[] kids = t1.Children();
             for (int j = kids.Length - 1; j >= 0; j--)
             {
                 if (RightEdge(t, kids[j], i))
                 {
                     return(true);
                 }
             }
             return(false);
         }
     }
 }
Exemplo n.º 5
0
 public Residue(Reducer reducer, BigInteger x)
     : this(reducer)
 {
     r = reducer.CreateRep();
     r.Set(x);
     reducer.Reduce(r);
 }
        public static MutableInteger SetModularInversePowerOfTwoModulus(this MutableInteger c, MutableInteger d, int n, MutableIntegerStore store)
        {
            // See 9.2 in: http://gmplib.org/~tege/divcnst-pldi94.pdf
            c.Set(d);
            var two  = store.Allocate().Set(2);
            var reg1 = store.Allocate();
            var reg2 = store.Allocate();

            for (int m = 3; m < n; m *= 2)
            {
                reg1.Set(c);
                reg2.SetProduct(reg1, d);
                reg2.Mask(n);
                reg2.SetDifference(two, reg2);
                c.SetProduct(reg1, reg2);
                c.Mask(n);
            }
            if (c.Sign == -1)
            {
                c.Add(reg1.Set(1).LeftShift(n));
            }
            store.Release(two);
            store.Release(reg1);
            store.Release(reg2);
            return(c);
        }
        public static MutableInteger SetModularInverse(this MutableInteger c, MutableInteger a, MutableInteger b, MutableIntegerStore store)
        {
            var p         = store.Allocate().Set(a);
            var q         = store.Allocate().Set(b);
            var x0        = store.Allocate().Set(1);
            var x1        = store.Allocate().Set(0);
            var quotient  = store.Allocate();
            var remainder = store.Allocate();
            var product   = store.Allocate();

            while (!q.IsZero)
            {
                remainder.Set(p).ModuloWithQuotient(q, quotient);
                var tmpp = p;
                p = q;
                q = tmpp.Set(remainder);
                var tmpx = x1;
                x1 = x0.Subtract(product.SetProduct(quotient, x1));
                x0 = tmpx;
            }
            c.Set(x0);
            if (c.Sign == -1)
            {
                c.Add(b);
            }

            store.Release(p);
            store.Release(q);
            store.Release(x0);
            store.Release(x1);
            store.Release(quotient);
            store.Release(remainder);
            return(c);
        }
Exemplo n.º 8
0
 public Reducer(MutableIntegerReduction reduction, BigInteger n)
     : base(reduction, n)
 {
     length = (n.GetBitLength() + 31) / 32 * 2 + 1;
     store  = new MutableIntegerStore(length);
     nRep   = store.Allocate();
     nRep.Set(n);
 }
        /// <summary>Adds the given count to the current count for the given key.</summary>
        /// <remarks>
        /// Adds the given count to the current count for the given key. If the key
        /// hasn't been seen before, it is assumed to have count 0, and thus this
        /// method will set its count to the given amount. Negative increments are
        /// equivalent to calling
        /// <c>decrementCount</c>
        /// .
        /// <p>
        /// To more conveniently increment the count by 1, use
        /// <see cref="IntCounter{E}.IncrementCount(object)"/>
        /// .
        /// <p>
        /// To set a count to a specific value instead of incrementing it, use
        /// <see cref="IntCounter{E}.SetCount(object, int)"/>
        /// .
        /// </remarks>
        public virtual int IncrementCount(E key, int count)
        {
            if (tempMInteger == null)
            {
                tempMInteger = new MutableInteger();
            }
            MutableInteger oldMInteger = map[key] = tempMInteger;

            totalCount += count;
            if (oldMInteger != null)
            {
                count += oldMInteger;
            }
            tempMInteger.Set(count);
            tempMInteger = oldMInteger;
            return(count);
        }
Exemplo n.º 10
0
            public Reducer(IReductionAlgorithm <BigInteger> reduction, BigInteger p)
            {
                this.reduction = reduction;
                this.p         = p;
                bLength        = 32;
                b = BigInteger.One << bLength;
                var pLength = p.GetBitLength();

                k  = (pLength - 1) / bLength + 1;
                mu = BigInteger.Pow(b, 2 * k) / p;

                var muLength = mu.GetBitLength();

                length = (pLength + 31) / 32 * 2 + (muLength + 31) / 32;
                store  = new MutableIntegerStore(length);
                muRep  = store.Allocate();
                pRep   = store.Allocate();
                muRep.Set(mu);
                pRep.Set(p);
                bToTheKMinusOneLength = bLength * (k - 1);
                bToTheKPlusOneLength  = bLength * (k + 1);
            }
Exemplo n.º 11
0
 public IResidue <BigInteger> Set(BigInteger x)
 {
     r.Set(x);
     return(this);
 }