Beispiel #1
0
        /**
         * Compute the alpha decay factor such that the weight of an entry with age 'targetAgeInSeconds' is targetWeight'
         */
        public static double ComputeAlpha(double targetWeight, long targetAgeInSeconds)
        {
            ParameterCheck.Check(targetAgeInSeconds > 0, "targetAgeInSeconds must be > 0");
            ParameterCheck.Check(targetWeight > 0 && targetWeight < 1, "targetWeight must be in range (0, 1)");

            return(-Math.Log(targetWeight) / targetAgeInSeconds);
        }
Beispiel #2
0
        public void Merge(DecayCounter decayCounter)
        {
            if (decayCounter == null)
            {
                throw new ArgumentNullException("decayCounter");
            }

            ParameterCheck.Check(decayCounter.Alpha == this.Alpha, $"Expected decayCounter to have alpha {this.Alpha}, but was {decayCounter.Alpha}.");

            lock (SyncRoot)
            {
                // if the landmark this counter is behind the other counter
                if (this.LandmarkInSeconds < decayCounter.LandmarkInSeconds)
                {
                    // rescale this counter to the other counter, and add
                    this.RescaleToNewLandmark(decayCounter.LandmarkInSeconds);
                    this.Count += decayCounter.Count;
                }
                else
                {
                    // rescale the other counter and add
                    double OtherRescaledCount = decayCounter.Count / this.Weight(this.LandmarkInSeconds, decayCounter.LandmarkInSeconds);
                    this.Count += OtherRescaledCount;
                }
            }
        }
Beispiel #3
0
        public long ToBytes()
        {
            double Bytes = this.GetValue(DataSizeUnit.BYTE);

            ParameterCheck.Check(Bytes <= Int64.MaxValue, "Size in bytes is too large to be represented in bytes as a long.");

            return((long)Bytes);
        }
Beispiel #4
0
        /// <summary>
        /// Creates a slice over the specified array range.
        /// </summary>
        /// <param name="base"></param>
        /// <param name="offset">The array position at which the slice begins</param>
        /// <param name="length">The number of array positions to include in the slice</param>
        public Slice(byte[] @base, int offset, int length)
        {
            ParameterCheck.OutOfRange(offset < @base.Length && offset + length < @base.Length, "offset");

            this.Base         = @base ?? throw new ArgumentNullException("base");
            this.Address      = ARRAY_BYTE_BASE_OFFSET + offset;
            this.Size         = length;
            this.RetainedSize = INSTANCE_SIZE + Marshal.SizeOf(@base);
            this.Reference    = (offset == 0 && length == @base.Length) ? COMPACT : NOT_COMPACT;
        }
Beispiel #5
0
        public static Slice Allocate(int capacity)
        {
            if (capacity == 0)
            {
                return(EMPTY_SLICE);
            }

            ParameterCheck.Check(capacity <= MAX_ARRAY_SIZE, $"Cannot allocate slice largert than {MAX_ARRAY_SIZE} bytes.");

            return(new Slice(new byte[capacity]));
        }