예제 #1
0
        /// <summary>
        /// Creates a dictionary-like object used for caches.
        /// </summary>
        /// <param name="size">The maximum number of elements to store will be this number aligned to next ^2.</param>
        internal CacheDict(int size)
        {
            var alignedSize = NumericHelper.NextPowerOf2(size - 1);

            _mask    = alignedSize - 1;
            _entries = new Entry[alignedSize];
        }
예제 #2
0
        internal static void DonateArray(T[] donation)
        {
            // Assume anything could have been set to null, start no sync operation, this could be running during DomainUnload
            if (donation == null || Volatile.Read(ref _done) == 0)
            {
                return;
            }
            var pools = _pools;

            if (pools == null)
            {
                return;
            }
            var capacity = donation.Length;

            if (capacity == 0 || capacity < _minCapacity || capacity > _maxCapacity)
            {
                return;
            }
            capacity = NumericHelper.PopulationCount(capacity) == 1 ? capacity : NumericHelper.NextPowerOf2(capacity);
            var index = NumericHelper.Log2(capacity) - _minCapacityLog2;
            var pool  = pools[index];

            if (pool == null)
            {
                return;
            }
            pool.Donate(donation);
        }
예제 #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FixedSizeQueueBucket{T}" /> class.
 /// </summary>
 /// <param name="capacity">The capacity.</param>
 public FixedSizeQueueBucket(int capacity)
 {
     _capacity     = NumericHelper.NextPowerOf2(capacity);
     _preCount     = 0;
     _indexEnqueue = 0;
     _indexDequeue = 0;
     _bucket       = new Bucket <T>(_capacity);
 }
예제 #4
0
        public void NextPowerOf2Uint()
        {
            var input  = new uint[] { 835, 246444, 448905, 610268, 855701, 1017047, 1228188, 1717470, 1917025, 2130772 };
            var output = new uint[] { 1024, 262144, 524288, 1048576, 1048576, 1048576, 2097152, 2097152, 2097152, 4194304 };

            for (var index = 0; index < input.Length; index++)
            {
                Assert.AreEqual(output[index], NumericHelper.NextPowerOf2(input[index]));
            }
        }
예제 #5
0
 public LockContext(int capacity)
 {
     _capacity = NumericHelper.PopulationCount(capacity) == 1 ? capacity : NumericHelper.NextPowerOf2(capacity);
     _slots    = new NeedleBucket <LockSlot <T>, LazyNeedle <LockSlot <T> > >
                 (
         index => new LockSlot <T>
         (
             this,
             index,
             _version.AdvanceNewToken()
         ),
         _capacity
                 );
     _closedSlots = new FixedSizeQueue <LockSlot <T> >(_capacity);
 }
예제 #6
0
        internal static void DonateArray(T[] donation)
        {
            if (donation == null || Thread.VolatileRead(ref _done) == 0)
            {
                return;
            }
            var capacity = donation.Length;

            if (capacity == 0 || capacity < INT_MinCapacity || capacity > INT_MaxCapacity)
            {
                return;
            }
            capacity = NumericHelper.PopulationCount(capacity) == 1 ? capacity : NumericHelper.NextPowerOf2(capacity);
            var index = NumericHelper.Log2(capacity) - INT_MinCapacityLog2;

            _pools[index].Donate(donation);
        }
예제 #7
0
 internal static T[] GetArray(int capacity)
 {
     if (capacity == 0)
     {
         return(EmptyArray);
     }
     if (capacity < _minCapacity)
     {
         capacity = _minCapacity;
     }
     capacity = NumericHelper.PopulationCount(capacity) == 1 ? capacity : NumericHelper.NextPowerOf2(capacity);
     if (capacity <= _maxCapacity)
     {
         var index       = NumericHelper.Log2(capacity) - _minCapacityLog2;
         var currentPool = _pools[index];
         if (currentPool != null && currentPool.TryGet(out var result))
         {
             return(result);
         }
     }
     return(new T[capacity]);
 }
예제 #8
0
 internal static T[] GetArray(int capacity)
 {
     if (capacity == 0)
     {
         return(_emptyArray);
     }
     if (capacity < INT_MinCapacity)
     {
         capacity = INT_MinCapacity;
     }
     capacity = NumericHelper.PopulationCount(capacity) == 1 ? capacity : NumericHelper.NextPowerOf2(capacity);
     if (capacity <= INT_MaxCapacity && Thread.VolatileRead(ref _done) == 1)
     {
         var index = NumericHelper.Log2(capacity) - INT_MinCapacityLog2;
         T[] result;
         var currentPool = _pools[index];
         if (currentPool.TryGet(out result))
         {
             return(result);
         }
     }
     return(new T[capacity]);
 }
예제 #9
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="CircularBucket{T}" /> class.
 /// </summary>
 /// <param name="capacity">The capacity.</param>
 public CircularBucket(int capacity)
 {
     Capacity = NumericHelper.PopulationCount(capacity) == 1 ? capacity : NumericHelper.NextPowerOf2(capacity);
     _index   = -1;
     _entries = new FixedSizeBucket <T>(Capacity);
 }
예제 #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FixedSizeHashBucket" /> class.
 /// </summary>
 /// <param name="capacity">The capacity.</param>
 /// <param name="keyComparer">The key comparer.</param>
 public FixedSizeHashBucket(int capacity, IEqualityComparer <TKey> keyComparer)
 {
     _capacity    = NumericHelper.NextPowerOf2(capacity);
     _entries     = new Bucket <KeyValuePair <TKey, TValue> >(_capacity);
     _keyComparer = keyComparer ?? EqualityComparer <TKey> .Default;
 }
예제 #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FixedSizeQueue{T}" /> class.
 /// </summary>
 /// <param name="capacity">The capacity.</param>
 public FixedSizeQueue(int capacity)
 {
     Capacity      = NumericHelper.PopulationCount(capacity) == 1 ? capacity : NumericHelper.NextPowerOf2(capacity);
     _preCount     = 0;
     _indexEnqueue = 0;
     _indexDequeue = 0;
     _entries      = new FixedSizeBucket <T>(Capacity);
 }
예제 #12
0
 /// <summary>
 ///     Creates a dictionary-like object used for caches.
 /// </summary>
 /// <param name="capacity">The maximum number of elements to store will be this number aligned to next ^2.</param>
 public CacheDict(int capacity)
 {
     capacity = NumericHelper.PopulationCount(capacity) == 1 ? capacity : NumericHelper.NextPowerOf2(capacity);
     _entries = new Entry[capacity];
 }
예제 #13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CircularBucket{T}" /> class.
 /// </summary>
 /// <param name="capacity">The capacity.</param>
 public CircularBucket(int capacity)
 {
     _capacity = NumericHelper.NextPowerOf2(capacity);
     _index    = -1;
     _bucket   = new Bucket <T>(_capacity);
 }
예제 #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FixedSizeHashBucket" /> class.
 /// </summary>
 /// <param name="capacity">The capacity.</param>
 /// <param name="comparer">The comparer.</param>
 public FixedSizeSetBucket(int capacity, IEqualityComparer <T> comparer)
 {
     _capacity = NumericHelper.NextPowerOf2(capacity);
     _entries  = new Bucket <T>(_capacity);
     _comparer = comparer ?? EqualityComparer <T> .Default;
 }