示例#1
0
        /// <summary>
        /// Adds the specified item. Will not overwrite existing items.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns>Returns the position where the item was added, -1 otherwise.</returns>
        public int TryAdd(T item)
        {
            var index = Interlocked.Increment(ref _index) & (_capacity - 1);

            if (_entries.InsertInternal(index, item))
            {
                return(index);
            }
            return(-1);
        }
 /// <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 Add(T item)
 {
     if (_entries.Count < _capacity)
     {
         var preCount = Interlocked.Increment(ref _preCount);
         if (preCount <= _capacity)
         {
             var index = (Interlocked.Increment(ref _indexEnqueue) - 1) & (_capacity - 1);
             if (_entries.InsertInternal(index, item))
             {
                 return(true);
             }
         }
         Interlocked.Decrement(ref _preCount);
     }
     return(false);
 }