Beispiel #1
0
            private void Reduce(MutableInteger z)
            {
                // var qhat = (z >> (bLength * (k - 1))) * mu >> (bLength * (k + 1));
                var reg1 = store.Allocate().Set(z);

                reg1.RightShift(bToTheKMinusOneLength);
#if false
                var reg2.store.Allocate().SetProductShifted(reg1, muRep, bToTheKPlusOneLength);
#else
                var reg2 = store.Allocate().SetProduct(reg1, muRep);
                reg2.RightShift(bToTheKPlusOneLength);
#endif
                // var r = z % bToTheKPlusOne - qhat * p % bToTheKPlusOne;
                z.Mask(bToTheKPlusOneLength);
#if true
                reg1.SetProductMasked(reg2, pRep, bToTheKPlusOneLength);
#else
                reg1.SetProduct(reg2, pRep);
                reg1.Mask(bToTheKPlusOneLength);
#endif
                // if (r.Sign == -1) r += bToTheKPlusOne;
                if (z < reg1)
                {
                    z.SetBit(bToTheKPlusOneLength, true);
                }
                z.Subtract(reg1);
                // while (r >= p) r -= p;
                while (z >= pRep)
                {
                    z.Subtract(pRep);
                }
                store.Release(reg1);
                store.Release(reg2);
            }
        internal override void ParseHeader()
        {
            // Parse "@@@ -55,12 -163,13 +163,15 @@@ protected boolean"
            //
            byte[]         buf = file.buf;
            MutableInteger ptr = new MutableInteger();

            ptr.value = RawParseUtils.NextLF(buf, startOffset, ' ');
            for (int n = 0; n < old.Length; n++)
            {
                old[n].startLine = -RawParseUtils.ParseBase10(buf, ptr.value, ptr);
                if (buf[ptr.value] == ',')
                {
                    old[n].lineCount = RawParseUtils.ParseBase10(buf, ptr.value + 1, ptr);
                }
                else
                {
                    old[n].lineCount = 1;
                }
            }
            newStartLine = RawParseUtils.ParseBase10(buf, ptr.value + 1, ptr);
            if (buf[ptr.value] == ',')
            {
                newLineCount = RawParseUtils.ParseBase10(buf, ptr.value + 1, ptr);
            }
            else
            {
                newLineCount = 1;
            }
        }
        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;
        }
 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;
 }
        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);
        }
        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);
        }
        /**
         * Removes a specified number of copies of an object from the bag.
         *
         * @param object  the object to remove
         * @param nCopies  the number of copies to remove
         * @return true if the bag changed
         */
        public virtual bool remove(Object obj, int nCopies)
        {
            MutableInteger mut = (MutableInteger)map.get(obj);

            if (mut == null)
            {
                return(false);
            }
            if (nCopies <= 0)
            {
                return(false);
            }
            modCount++;
            if (nCopies < mut.value)
            {
                mut.value -= nCopies;
                sizeJ     -= nCopies;
            }
            else
            {
                map.remove(obj);
                sizeJ -= mut.value;
            }
            return(true);
        }
        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);
        }
Beispiel #9
0
 public Residue(Reducer reducer, BigInteger x)
     : this(reducer)
 {
     r = reducer.CreateRep();
     r.Set(x);
     reducer.Reduce(r);
 }
        public virtual string ToString(NumberFormat nf)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("{");
            IList <E> list = new List <E>(map.Keys);

            try
            {
                (IList)list.Sort();
            }
            catch (Exception)
            {
            }
            // see if it can be sorted
            for (IEnumerator <E> iter = list.GetEnumerator(); iter.MoveNext();)
            {
                object         key = iter.Current;
                MutableInteger d   = map[key];
                sb.Append(key + "=");
                sb.Append(nf.Format(d));
                if (iter.MoveNext())
                {
                    sb.Append(", ");
                }
            }
            sb.Append("}");
            return(sb.ToString());
        }
Beispiel #11
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);
         }
     }
 }
Beispiel #12
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);
         }
     }
 }
Beispiel #13
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);
         }
     }
 }
Beispiel #14
0
        internal virtual void ParseHeader()
        {
            // Parse "@@ -236,9 +236,9 @@ protected boolean"
            //
            byte[]         buf = file.buf;
            MutableInteger ptr = new MutableInteger();

            ptr.value     = RawParseUtils.NextLF(buf, startOffset, ' ');
            old.startLine = -RawParseUtils.ParseBase10(buf, ptr.value, ptr);
            if (buf[ptr.value] == ',')
            {
                old.lineCount = RawParseUtils.ParseBase10(buf, ptr.value + 1, ptr);
            }
            else
            {
                old.lineCount = 1;
            }
            newStartLine = RawParseUtils.ParseBase10(buf, ptr.value + 1, ptr);
            if (buf[ptr.value] == ',')
            {
                newLineCount = RawParseUtils.ParseBase10(buf, ptr.value + 1, ptr);
            }
            else
            {
                newLineCount = 1;
            }
        }
Beispiel #15
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);
 }
Beispiel #16
0
        private static MutableInteger ActivationCounter(IObjectContainer container)
        {
            var activationCounter = new MutableInteger();

            EventRegistryFactory.ForObjectContainer(container)
            .Activating += (sender, args) => activationCounter.Increment();
            return(activationCounter);
        }
            /// <exception cref="NGit.Errors.CorruptObjectException"></exception>
            internal virtual void SetType(string typeStr)
            {
                byte[]         typeRaw = Constants.Encode(typeStr + " ");
                MutableInteger ptr     = new MutableInteger();

                this.type = Constants.DecodeTypeString(this.id, typeRaw, unchecked ((byte)' '), ptr
                                                       );
            }
        internal DirCacheTree(byte[] @in, MutableInteger off, NGit.Dircache.DirCacheTree
                              myParent)
        {
            parent = myParent;
            int ptr     = RawParseUtils.Next(@in, off.value, '\0');
            int nameLen = ptr - off.value - 1;

            if (nameLen > 0)
            {
                encodedName = new byte[nameLen];
                System.Array.Copy(@in, off.value, encodedName, 0, nameLen);
            }
            else
            {
                encodedName = NO_NAME;
            }
            entrySpan = RawParseUtils.ParseBase10(@in, ptr, off);
            int subcnt = RawParseUtils.ParseBase10(@in, off.value, off);

            off.value = RawParseUtils.Next(@in, off.value, '\n');
            if (entrySpan >= 0)
            {
                // Valid trees have a positive entry count and an id of a
                // tree object that should exist in the object database.
                //
                id         = ObjectId.FromRaw(@in, off.value);
                off.value += Constants.OBJECT_ID_LENGTH;
            }
            if (subcnt > 0)
            {
                bool alreadySorted = true;
                children = new NGit.Dircache.DirCacheTree[subcnt];
                for (int i = 0; i < subcnt; i++)
                {
                    children[i] = new NGit.Dircache.DirCacheTree(@in, off, this);
                    // C Git's ordering differs from our own; it prefers to
                    // sort by length first. This sometimes produces a sort
                    // we do not desire. On the other hand it may have been
                    // created by us, and be sorted the way we want.
                    //
                    if (alreadySorted && i > 0 && TREE_CMP.Compare(children[i - 1], children[i]) > 0)
                    {
                        alreadySorted = false;
                    }
                }
                if (!alreadySorted)
                {
                    Arrays.Sort(children, 0, subcnt, TREE_CMP);
                }
            }
            else
            {
                // Leaf level trees have no children, only (file) entries.
                //
                children = NO_CHILDREN;
            }
            childCnt = subcnt;
        }
Beispiel #19
0
 private void Reduce(MutableInteger t, MutableInteger u, MutableInteger v)
 {
     t.MontgomeryCIOS(u, v, nRep, k0);
     if (t >= nRep)
     {
         t.Subtract(nRep);
     }
     Debug.Assert(t < nRep);
 }
Beispiel #20
0
 private void Reduce(MutableInteger t)
 {
     t.MontgomerySOS(nRep, k0);
     if (t >= nRep)
     {
         t.Subtract(nRep);
     }
     Debug.Assert(t < nRep);
 }
        /**
         * Returns the number of occurrence of the given element in this bag
         * by looking up its count in the underlying map.
         *
         * @param object  the object to search for
         * @return the number of occurrences of the object, zero if not found
         */
        public virtual int getCount(Object obj)
        {
            MutableInteger count = (MutableInteger)map.get(obj);

            if (count != null)
            {
                return(count.value);
            }
            return(0);
        }
    public Parameters(String alias, String connective, MutableInteger queryParamCounter) {
        this.alias = alias;
        this.connective = connective;
        this.queryParamCounter = queryParamCounter;

        subParameters = new List<Parameters>();   
        negatedParameters = new List<Parameters>();
        expressions = new List<String>();
        localQueryParamValues = new Dictionary<String, Object>(); 
    }
Beispiel #23
0
        public Parameters(String alias, String connective, MutableInteger queryParamCounter)
        {
            this.alias             = alias;
            this.connective        = connective;
            this.queryParamCounter = queryParamCounter;

            subParameters         = new List <Parameters>();
            negatedParameters     = new List <Parameters>();
            expressions           = new List <String>();
            localQueryParamValues = new Dictionary <String, Object>();
        }
        /// <summary>
        /// Returns the current count for the given key, which is 0 if it hasn't
        /// been
        /// seen before.
        /// </summary>
        /// <remarks>
        /// Returns the current count for the given key, which is 0 if it hasn't
        /// been
        /// seen before. This is a convenient version of
        /// <c>get</c>
        /// that casts
        /// and extracts the primitive value.
        /// </remarks>
        public virtual int GetIntCount(object key)
        {
            MutableInteger count = map[key];

            if (count == null)
            {
                return(defaultValue);
            }
            // haven't seen this object before -> 0 count
            return(count);
        }
        /**
         * Gets a hash code for the Bag compatible with the definition of equals.
         * The hash code is defined as the sum total of a hash code for each element.
         * The per element hash code is defined as
         * <code>(e==null ? 0 : e.hashCode()) ^ noOccurances)</code>.
         * This hash code is compatible with the Set interface.
         *
         * @return the hash code of the Bag
         */
        public override int GetHashCode()
        {
            int total = 0;

            for (java.util.Iterator <java.util.MapNS.Entry <Object, Object> > it = map.entrySet().iterator(); it.hasNext();)
            {
                java.util.MapNS.Entry <Object, Object> entry = (java.util.MapNS.Entry <Object, Object>)it.next();
                Object         element = entry.getKey();
                MutableInteger count   = (MutableInteger)entry.getValue();
                total += (element == null ? 0 : element.GetHashCode()) ^ count.value;
            }
            return(total);
        }
        /**
         * Removes all copies of the specified object from the bag.
         *
         * @param object  the object to remove
         * @return true if the bag changed
         */
        public virtual bool remove(Object obj)
        {
            MutableInteger mut = (MutableInteger)map.get(obj);

            if (mut == null)
            {
                return(false);
            }
            modCount++;
            map.remove(obj);
            sizeJ -= mut.value;
            return(true);
        }
Beispiel #27
0
        /// <summary>
        /// Returns the positional index of the left edge of a tree <i>t</i>
        /// within a given root, as defined by the size of the yield of all
        /// material preceding <i>t</i>.
        /// </summary>
        public static int LeftEdge(Tree t, Tree root)
        {
            MutableInteger i = new MutableInteger(0);

            if (LeftEdge(t, root, i))
            {
                return(i);
            }
            else
            {
                throw new Exception("Tree is not a descendant of root.");
            }
        }
Beispiel #28
0
        //      return -1;
        /// <summary>
        /// Returns the positional index of the left edge of a tree <i>t</i>
        /// within a given root, as defined by the size of the yield of all
        /// material preceding <i>t</i>.
        /// </summary>
        /// <remarks>
        /// Returns the positional index of the left edge of a tree <i>t</i>
        /// within a given root, as defined by the size of the yield of all
        /// material preceding <i>t</i>.
        /// This method returns -1 if no path is found, rather than exceptioning.
        /// </remarks>
        /// <seealso cref="LeftEdge(Tree, Tree)"/>
        public static int LeftEdgeUnsafe(Tree t, Tree root)
        {
            MutableInteger i = new MutableInteger(0);

            if (LeftEdge(t, root, i))
            {
                return(i);
            }
            else
            {
                return(-1);
            }
        }
Beispiel #29
0
        //      return root.yield().size() + 1;
        /// <summary>
        /// Returns the positional index of the right edge of a tree
        /// <i>t</i> within a given root, as defined by the size of the yield
        /// of all material preceding <i>t</i> plus all the material
        /// contained in <i>t</i>.
        /// </summary>
        /// <remarks>
        /// Returns the positional index of the right edge of a tree
        /// <i>t</i> within a given root, as defined by the size of the yield
        /// of all material preceding <i>t</i> plus all the material
        /// contained in <i>t</i>.
        /// This method returns root.yield().size() + 1 if no path is found, rather than exceptioning.
        /// </remarks>
        /// <seealso cref="RightEdge(Tree, Tree)"/>
        public static int RightEdgeUnsafe(Tree t, Tree root)
        {
            MutableInteger i = new MutableInteger(root.Yield().Count);

            if (RightEdge(t, root, i))
            {
                return(i);
            }
            else
            {
                return(root.Yield().Count + 1);
            }
        }
Beispiel #30
0
        /// <summary>
        /// Returns the positional index of the right edge of a tree
        /// <i>t</i> within a given root, as defined by the size of the yield
        /// of all material preceding <i>t</i> plus all the material
        /// contained in <i>t</i>.
        /// </summary>
        public static int RightEdge(Tree t, Tree root)
        {
            MutableInteger i = new MutableInteger(root.Yield().Count);

            if (RightEdge(t, root, i))
            {
                return(i);
            }
            else
            {
                throw new Exception("Tree is not a descendant of root.");
            }
        }
 /// <summary>Sets the current count for the given key.</summary>
 /// <remarks>
 /// Sets the current count for the given key. This will wipe out any existing
 /// count for that key.
 /// <p>
 /// To add to a count instead of replacing it, use
 /// <see cref="IntCounter{E}.IncrementCount(object, int)"/>
 /// .
 /// </remarks>
 public virtual void SetCount(E key, int count)
 {
     if (tempMInteger == null)
     {
         tempMInteger = new MutableInteger();
     }
     tempMInteger.Set(count);
     tempMInteger = map[key] = tempMInteger;
     totalCount  += count;
     if (tempMInteger != null)
     {
         totalCount -= tempMInteger;
     }
 }
        /// <summary>Removes the given key from this Counter.</summary>
        /// <remarks>
        /// Removes the given key from this Counter. Its count will now be 0 and it
        /// will no longer be considered previously seen.
        /// </remarks>
        public override double Remove(E key)
        {
            totalCount -= GetCount(key);
            // subtract removed count from total (may be 0)
            MutableInteger val = Sharpen.Collections.Remove(map, key);

            if (val == null)
            {
                return(double.NaN);
            }
            else
            {
                return(val);
            }
        }
Beispiel #33
0
        private QueryBuilder(String entityName, String alias, MutableInteger aliasCounter, MutableInteger paramCounter)
        {
            this.entityName   = entityName;
            this.alias        = alias;
            this.aliasCounter = aliasCounter;
            this.paramCounter = paramCounter;

            rootParameters = new Parameters(alias, "and", paramCounter);

            froms       = new List <Pair <String, String> >();
            orders      = new List <Pair <String, Boolean> >();
            projections = new List <String>();

            AddFrom(entityName, alias);
        }
 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;
 }