示例#1
0
        /// <summary>
        /// Attempts to add the specified key and associated value.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="offset">The offset from the default index.</param>
        /// <param name="isCollision">if set to <c>true</c> the attempt resulted in a collision.</param>
        /// <returns>The index where the key and associated value were added.</returns>
        public int Add(TKey key, TValue value, int offset, out bool isCollision)
        {
            int index = Index(key, offset);
            var entry = new KeyValuePair <TKey, TValue>(key, value);
            KeyValuePair <TKey, TValue> previous;

            if (_entries.Insert(index, entry, out previous))
            {
                isCollision = false;
                return(index);
            }
            else
            {
                isCollision = !_keyComparer.Equals(previous.Key, key);
                return(-1);
            }
        }
示例#2
0
        /// <summary>
        /// Attempts to add the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="offset">The offset from the default index.</param>
        /// <param name="isCollision">if set to <c>true</c> the attempt resulted in a collision.</param>
        /// <returns>The index where the item were added.</returns>
        public int Add(T item, int offset, out bool isCollision)
        {
            int index = Index(item, offset);
            T   previous;

            if (_entries.Insert(index, item, out previous))
            {
                isCollision = false;
                return(index);
            }
            else
            {
                isCollision = !_comparer.Equals(previous, item);
                return(-1);
            }
        }
示例#3
0
 /// <summary>
 /// Attempts to Adds the specified item at the front.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <returns>
 ///   <c>true</c> if the item was added; otherwise, <c>false</c>.
 /// </returns>
 public bool Enqueue(T item)
 {
     if (_bucket.Count < _capacity)
     {
         var preCount = Interlocked.Increment(ref _preCount);
         if (preCount <= _capacity)
         {
             var index = (Interlocked.Increment(ref _indexEnqueue) - 1) & (_capacity - 1);
             if (_bucket.Insert(index, item))
             {
                 return(true);
             }
         }
         Interlocked.Decrement(ref _preCount);
     }
     return(false);
 }
示例#4
0
 /// <summary>
 /// Inserts the item at the specified index.
 /// </summary>
 /// <param name="index">The index.</param>
 /// <param name="item">The item.</param>
 /// <returns>
 ///   <c>true</c> if the item was inserted; otherwise, <c>false</c>.
 /// </returns>
 /// <exception cref="System.ArgumentOutOfRangeException">index;index must be greater or equal to 0 and less than capacity.</exception>
 /// <remarks>
 /// The insertion can fail if the index is already used or is being written by another thread.
 /// If the index is being written it can be understood that the insert operation happened before but the item was overwritten or removed.
 /// </remarks>
 public bool Insert(int index, T item)
 {
     return(_wrapped.Insert(index, item));
 }