Example #1
0
 public void AddPower(EInteger input, EInteger output)
 {
     lock (this.outputs) {
         if (this.size < MaxSize)
         {
             // Shift newer entries down
             for (int i = this.size; i > 0; --i)
             {
                 this.inputs[i]     = this.inputs[i - 1];
                 this.inputsInts[i] = this.inputsInts[i - 1];
                 this.outputs[i]    = this.outputs[i - 1];
             }
             this.inputs[0]     = input;
             this.inputsInts[0] = input.CanFitInInt32() ? input.ToInt32Checked() : -1;
             this.outputs[0]    = output;
             ++this.size;
         }
         else
         {
             // Shift newer entries down
             for (int i = MaxSize - 1; i > 0; --i)
             {
                 this.inputs[i]     = this.inputs[i - 1];
                 this.inputsInts[i] = this.inputsInts[i - 1];
                 this.outputs[i]    = this.outputs[i - 1];
             }
             this.inputs[0]     = input;
             this.inputsInts[0] = input.CanFitInInt32() ? input.ToInt32Checked() : -1;
             this.outputs[0]    = output;
         }
     }
 }
Example #2
0
 internal static EInteger MultiplyByPowerOfFive(EInteger v, EInteger
                                                epower)
 {
     return(epower.CanFitInInt32() ? MultiplyByPowerOfFive(v,
                                                           epower.ToInt32Checked()) : v.Multiply(FindPowerOfFiveFromBig(
                                                                                                     epower)));
 }
Example #3
0
            public EInteger[] FindCachedPowerOrSmaller(EInteger bi)
            {
                EInteger[] ret      = null;
                EInteger   minValue = null;

                if (bi.CanFitInInt32())
                {
                    return(this.FindCachedPowerIntOrSmaller(bi.ToInt32Checked()));
                }
                lock (this.outputs) {
                    for (var i = 0; i < this.size; ++i)
                    {
                        if (this.inputs[i].CompareTo(bi) <= 0 && (minValue == null ||
                                                                  this.inputs[i].CompareTo(minValue) >= 0))
                        {
                            // DebugUtility.Log("Have cached power (" + inputs[i] + "," + bi + ") ");
                            ret      = new EInteger[2];
                            ret[0]   = this.inputs[i];
                            ret[1]   = this.outputs[i];
                            minValue = this.inputs[i];
                        }
                    }
                }
                return(ret);
            }
Example #4
0
        /// <include file='../../docs.xml'
        /// path='docs/doc[@name="M:PeterO.Numbers.FastInteger.AddBig(PeterO.Numbers.EInteger)"]/*'/>
        internal FastInteger AddBig(EInteger bigintVal)
        {
            this.CheckFrozen();
            switch (this.integerMode)
            {
            case 0: {
                return(bigintVal.CanFitInInt32() ? this.AddInt((int)bigintVal) :
                       this.Add(FastInteger.FromBig(bigintVal)));
            }

            case 1:
                this.integerMode = 2;
                this.largeValue  = this.mnum.ToEInteger();
                this.largeValue += bigintVal;
                break;

            case 2:
                this.largeValue += bigintVal;
                break;

            default:
                throw new InvalidOperationException();
            }
            return(this);
        }
Example #5
0
        internal static FastIntegerFixed FromBig(EInteger bigintVal)
        {
            if (bigintVal.CanFitInInt32())
            {
                return(new FastIntegerFixed(bigintVal.ToInt32Unchecked()));
            }
            var fi = new FastIntegerFixed(0);

            fi.integerMode = 2;
            fi.largeValue  = bigintVal;
            return(fi);
        }
Example #6
0
 public FastIntegerFixed Subtract(
     EInteger b)
 {
     if (this.integerMode == IntegerMode.SmallValue && b.CanFitInInt32())
     {
         return(this.Subtract(b.ToInt32Unchecked()));
     }
     else
     {
         return(FastIntegerFixed.FromBig(
                    this.ToEInteger().Subtract(b)));
     }
 }
Example #7
0
 public FastIntegerFixed Add(
     EInteger b)
 {
     if (this.integerMode == 0 && b.CanFitInInt32())
     {
         return(this.Add(b.ToInt32Unchecked()));
     }
     else
     {
         return(FastIntegerFixed.FromBig(
                    this.ToEInteger().Add(b)));
     }
 }
Example #8
0
 public CBORObject GetObject(EInteger bigIndex)
 {
     if (bigIndex.Sign < 0) {
     throw new CBORException("Unexpected index");
       }
       if (!bigIndex.CanFitInInt32()) {
     throw new CBORException("Index " + bigIndex +
             " is bigger than supported ");
       }
       var index = (int)bigIndex;
       if (index >= this.sharedObjects.Count) {
     throw new CBORException("Index " + index + " is not valid");
       }
       return this.sharedObjects[index];
 }
Example #9
0
        internal static EInteger FindPowerOfTenFromBig(EInteger
                                                       bigintExponent)
        {
            int sign = bigintExponent.Sign;

            if (sign < 0)
            {
                return(EInteger.Zero);
            }
            if (sign == 0)
            {
                return(EInteger.One);
            }
            return(bigintExponent.CanFitInInt32() ?
                   FindPowerOfTen(bigintExponent.ToInt32Checked()) :
                   FindPowerOfFiveFromBig(bigintExponent).ShiftLeft(bigintExponent));
        }
Example #10
0
 public EInteger GetCachedPower(EInteger bi)
 {
     if (bi.CanFitInInt32())
     {
         return(this.GetCachedPowerInt(bi.ToInt32Checked()));
     }
     lock (this.outputs) {
         for (var i = 0; i < this.size; ++i)
         {
             if (bi.Equals(this.inputs[i]))
             {
                 if (i != 0)
                 {
                     EInteger tmp;
                     // Move to head of cache if it isn't already
                     tmp            = this.inputs[i];
                     this.inputs[i] = this.inputs[0];
                     this.inputs[0] = tmp;
                     int tmpi = this.inputsInts[i];
                     this.inputsInts[i] = this.inputsInts[0];
                     this.inputsInts[0] = tmpi;
                     tmp             = this.outputs[i];
                     this.outputs[i] = this.outputs[0];
                     this.outputs[0] = tmp;
                     // Move formerly newest to next newest
                     if (i != 1)
                     {
                         tmp                = this.inputs[i];
                         this.inputs[i]     = this.inputs[1];
                         this.inputs[1]     = tmp;
                         tmpi               = this.inputsInts[i];
                         this.inputsInts[i] =
                             this.inputsInts[1];
                         this.inputsInts[1] = tmpi;
                         tmp             = this.outputs[i];
                         this.outputs[i] = this.outputs[1];
                         this.outputs[1] = tmp;
                     }
                 }
                 return(this.outputs[0]);
             }
         }
     }
     return(null);
 }
   public DigitShiftAccumulator(
 EInteger bigint,
 int lastDiscarded,
 int olderDiscarded) {
     if (bigint.CanFitInInt32()) {
       this.shiftedSmall = (int)bigint;
       if (this.shiftedSmall < 0) {
         throw new ArgumentException("shiftedSmall (" + this.shiftedSmall +
           ") is less than 0");
       }
       this.isSmall = true;
     } else {
       this.shiftedBigInt = bigint;
       this.isSmall = false;
     }
     this.bitsAfterLeftmost = (olderDiscarded != 0) ? 1 : 0;
     this.bitLeftmost = lastDiscarded;
   }
Example #12
0
 internal static FastInteger FromBig(EInteger bigintVal)
 {
     if (bigintVal.CanFitInInt32())
     {
         return(new FastInteger(bigintVal.ToInt32Unchecked()));
     }
     if (bigintVal.Sign > 0)
     {
         var fi = new FastInteger(0);
         fi.integerMode = 1;
         fi.mnum        = MutableNumber.FromEInteger(bigintVal);
         return(fi);
     }
     else
     {
         var fi = new FastInteger(0);
         fi.integerMode = 2;
         fi.largeValue  = bigintVal;
         return(fi);
     }
 }
Example #13
0
        internal static EInteger FindPowerOfFiveFromBig(EInteger diff)
        {
            int sign = diff.Sign;

            if (sign < 0)
            {
                return(EInteger.Zero);
            }
            if (sign == 0)
            {
                return(EInteger.One);
            }
            if (diff.CanFitInInt32())
            {
                return(FindPowerOfFive(diff.ToInt32Checked()));
            }
            EInteger epowprec = EInteger.One;
            EInteger ret      = EInteger.One;

            while (diff.Sign > 0)
            {
                if (!diff.IsEven)
                {
                    EInteger otherPower = ValuePowerOfFiveCache.GetCachedPower(epowprec);
                    if (otherPower == null)
                    {
                        // NOTE: Assumes powprec is 2 or greater and is a power of 2
                        EInteger prevPower = FindPowerOfFiveFromBig(epowprec.ShiftRight(
                                                                        1));
                        otherPower = prevPower.Multiply(prevPower);
                        ValuePowerOfFiveCache.AddPower(epowprec, otherPower);
                    }
                    ret = ret.Multiply(otherPower);
                }
                epowprec = epowprec.ShiftLeft(1);
                diff     = diff.ShiftRight(1);
            }
            return(ret);
        }
Example #14
0
 public DigitShiftAccumulator(
     EInteger bigint,
     int lastDiscarded,
     int olderDiscarded)
 {
     if (bigint.CanFitInInt32())
     {
         this.shiftedSmall = (int)bigint;
         if (this.shiftedSmall < 0)
         {
             throw new ArgumentException("shiftedSmall (" + this.shiftedSmall +
                                         ") is less than 0");
         }
         this.isSmall = true;
     }
     else
     {
         this.shiftedBigInt = bigint;
         this.isSmall       = false;
     }
     this.bitsAfterLeftmost = (olderDiscarded != 0) ? 1 : 0;
     this.bitLeftmost       = lastDiscarded;
 }
Example #15
0
 public BitShiftAccumulator(
     EInteger bigint,
     int lastDiscarded,
     int olderDiscarded)
 {
     if (bigint.Sign < 0)
     {
         throw new ArgumentException("bigint's sign(" + bigint.Sign +
                                     ") is less than 0");
     }
     if (bigint.CanFitInInt32())
     {
         this.isSmall      = true;
         this.shiftedSmall = (int)bigint;
     }
     else
     {
         this.shiftedBigInt = bigint;
     }
     this.discardedBitCount = new FastInteger(0);
     this.bitsAfterLeftmost = (olderDiscarded != 0) ? 1 : 0;
     this.bitLeftmost       = (lastDiscarded != 0) ? 1 : 0;
 }
Example #16
0
 public CBORObject GetString(EInteger bigIndex)
 {
     if (bigIndex.Sign < 0) {
     throw new CBORException("Unexpected index");
       }
       if (!bigIndex.CanFitInInt32()) {
     throw new CBORException("Index " + bigIndex +
       " is bigger than supported ");
       }
       var index = (int)bigIndex;
       List<CBORObject> lastList = this.stack[this.stack.Count - 1];
       if (index >= lastList.Count) {
     throw new CBORException("Index " + index + " is not valid");
       }
       CBORObject ret = lastList[index];
       // Byte strings are mutable, so make a copy
       return (ret.Type == CBORType.ByteString) ?
     CBORObject.FromObject(ret.GetByteString()) : ret;
 }
Example #17
0
   public BitShiftAccumulator(
 EInteger bigint,
 int lastDiscarded,
 int olderDiscarded) {
     if (bigint.Sign < 0) {
       throw new ArgumentException("bigint's sign (" + bigint.Sign +
         ") is less than 0");
     }
     if (bigint.CanFitInInt32()) {
       this.isSmall = true;
       this.shiftedSmall = (int)bigint;
     } else {
       this.shiftedBigInt = bigint;
     }
     this.discardedBitCount = new FastInteger(0);
     this.bitsAfterLeftmost = (olderDiscarded != 0) ? 1 : 0;
     this.bitLeftmost = (lastDiscarded != 0) ? 1 : 0;
   }
Example #18
0
 internal static FastIntegerFixed FromBig(EInteger bigintVal)
 {
     return(bigintVal.CanFitInInt32() ?
            FromInt32(bigintVal.ToInt32Unchecked()) : new
            FastIntegerFixed((byte)2, 0, bigintVal));
 }
Example #19
0
 internal static FastIntegerFixed FromBig(EInteger bigintVal)
 {
     return(bigintVal.CanFitInInt32() ?
            FromInt32(bigintVal.ToInt32Unchecked()) : new
            FastIntegerFixed(IntegerMode.LargeValue, 0, bigintVal));
 }
Example #20
0
 public void AddPower(EInteger input, EInteger output) {
   lock (this.outputs) {
     if (this.size < MaxSize) {
       // Shift newer entries down
       for (int i = this.size; i > 0; --i) {
         this.inputs[i] = this.inputs[i - 1];
         this.inputsInts[i] = this.inputsInts[i - 1];
         this.outputs[i] = this.outputs[i - 1];
       }
       this.inputs[0] = input;
  this.inputsInts[0] = input.CanFitInInt32() ? input.ToInt32Checked() : -1;
       this.outputs[0] = output;
       ++this.size;
     } else {
       // Shift newer entries down
       for (int i = MaxSize - 1; i > 0; --i) {
         this.inputs[i] = this.inputs[i - 1];
         this.inputsInts[i] = this.inputsInts[i - 1];
         this.outputs[i] = this.outputs[i - 1];
       }
       this.inputs[0] = input;
  this.inputsInts[0] = input.CanFitInInt32() ? input.ToInt32Checked() : -1;
       this.outputs[0] = output;
     }
   }
 }
Example #21
0
 public EInteger GetCachedPower(EInteger bi) {
   if (bi.CanFitInInt32()) {
     return this.GetCachedPowerInt(bi.ToInt32Checked());
   }
   lock (this.outputs) {
     for (var i = 0; i < this.size; ++i) {
       if (bi.Equals(this.inputs[i])) {
         if (i != 0) {
           EInteger tmp;
           // Move to head of cache if it isn't already
           tmp = this.inputs[i]; this.inputs[i] = this.inputs[0];
           this.inputs[0] = tmp;
           int tmpi = this.inputsInts[i]; this.inputsInts[i] =
           this.inputsInts[0]; this.inputsInts[0] = tmpi;
           tmp = this.outputs[i]; this.outputs[i] = this.outputs[0];
           this.outputs[0] = tmp;
           // Move formerly newest to next newest
           if (i != 1) {
             tmp = this.inputs[i]; this.inputs[i] = this.inputs[1];
             this.inputs[1] = tmp;
             tmpi = this.inputsInts[i]; this.inputsInts[i] =
             this.inputsInts[1]; this.inputsInts[1] = tmpi;
             tmp = this.outputs[i]; this.outputs[i] = this.outputs[1];
             this.outputs[1] = tmp;
           }
         }
         return this.outputs[0];
       }
     }
   }
   return null;
 }
Example #22
0
    public EInteger[] FindCachedPowerOrSmaller(EInteger bi) {
      EInteger[] ret = null;
      EInteger minValue = null;
      if (bi.CanFitInInt32()) {
        return this.FindCachedPowerIntOrSmaller(bi.ToInt32Checked());
      }
      lock (this.outputs) {
        for (var i = 0; i < this.size; ++i) {
          if (this.inputs[i].CompareTo(bi) <= 0 && (minValue == null ||
          this.inputs[i].CompareTo(minValue) >= 0)) {
 // DebugUtility.Log("Have cached power (" + inputs[i] + "," + bi + ") ");
            ret = new EInteger[2];
            ret[0] = this.inputs[i];
            ret[1] = this.outputs[i];
            minValue = this.inputs[i];
          }
        }
      }
      return ret;
    }