Beispiel #1
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)
 {
     // Only succeeds if there was nothing there before
     // meaning that if this succeeds it replaced nothing
     // If this fails whatever was there is still there
     var newNeedle = NeedleReservoir<T, TNeedle>.GetNeedle(item);
     if (_entries.Insert(index, newNeedle))
     {
         return true;
     }
     NeedleReservoir<T, TNeedle>.DonateNeedle(newNeedle);
     return false;
 }
Beispiel #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NeedleBucket{T, TNeedle}" /> class.
 /// </summary>
 /// <param name = "valueFactory">The delegate that is invoked to do the lazy initialization of the items.</param>
 /// <param name="capacity">The capacity.</param>
 public NeedleBucket(Func<T> valueFactory, int capacity)
 {
     if (valueFactory == null)
     {
         throw new ArgumentNullException("valueFactory");
     }
     if (!NeedleHelper.CanCreateNeedle<T, TNeedle>())
     {
         throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Unable to find a way to create {0}", typeof(TNeedle).Name));
     }
     _needleFactory = index => NeedleReservoir<T, TNeedle>.GetNeedle(valueFactory());
     _entries = new FixedSizeBucket<TNeedle>(capacity);
     _comparer = EqualityComparer<T>.Default;
 }
Beispiel #3
0
 /// <summary>
 /// Sets the item at the specified index.
 /// </summary>
 /// <param name="index">The index.</param>
 /// <param name="item">The item.</param>
 /// <param name="previous">The previous item in the specified index.</param>
 /// <returns>
 ///   <c>true</c> if the item was new; otherwise, <c>false</c>.
 /// </returns>
 /// <exception cref="System.ArgumentOutOfRangeException">index;index must be greater or equal to 0 and less than capacity</exception>
 public bool Exchange(int index, T item, out T previous)
 {
     TNeedle found;
     if (_entries.Exchange(index, NeedleReservoir<T, TNeedle>.GetNeedle(item), out found))
     {
         previous = default(T);
         return true;
     }
     // TryGetValue is null resistant
     found.TryGetValue(out previous);
     // This is a needle that is no longer referenced, we donate it
     NeedleReservoir<T, TNeedle>.DonateNeedle(found);
     return false;
 }
Beispiel #4
0
 public bool Update(int index, Func<T, T> itemUpdateFactory, Predicate<T> check, out bool isEmpty)
 {
     TNeedle newNeedle = null;
     Func<TNeedle, TNeedle> replacementFactory = needle => newNeedle = NeedleReservoir<T, TNeedle>.GetNeedle(itemUpdateFactory(needle.Value));
     Predicate<TNeedle> replacementCheck = needle => check(needle.Value);
     if (_entries.Update(index, replacementFactory, replacementCheck, out isEmpty))
     {
         return true;
     }
     if (newNeedle != null)
     {
     }
     NeedleReservoir<T, TNeedle>.DonateNeedle(newNeedle);
     return false;
 }
Beispiel #5
0
 /// <summary>
 /// Inserts the item at the specified index.
 /// </summary>
 /// <param name="index">The index.</param>
 /// <param name="item">The item.</param>
 /// <param name="previous">The previous item in the specified index.</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, out T previous)
 {
     // Only succeeds if there was nothing there before
     // meaning that if this succeeds it replaced nothing
     // If this fails whatever was there is still there
     TNeedle found;
     var newNeedle = NeedleReservoir<T, TNeedle>.GetNeedle(item);
     if (_entries.Insert(index, newNeedle, out found))
     {
         previous = default(T);
         return true;
     }
     NeedleReservoir<T, TNeedle>.DonateNeedle(newNeedle);
     // TryGetValue is null resistant
     found.TryGetValue(out previous);
     return false;
 }
Beispiel #6
0
 private WeakNeedle <TKey> PrivateGetNeedle(TKey key)
 {
     return(_reservoir.GetNeedle(key));
 }
Beispiel #7
0
 private static TNeedle PrivateGetNeedle(TKey key)
 {
     return(NeedleReservoir <TKey, TNeedle> .GetNeedle(key));
 }
Beispiel #8
0
 /// <summary>
 /// Sets the item at the specified index.
 /// </summary>
 /// <param name="index">The index.</param>
 /// <param name="item">The item.</param>
 /// <param name="isNew">if set to <c>true</c> the index was not previously used.</param>
 /// <exception cref="System.ArgumentOutOfRangeException">index;index must be greater or equal to 0 and less than capacity</exception>
 public void Set(int index, T item, out bool isNew)
 {
     // This may have replaced something
     _entries.Set(index, NeedleReservoir<T, TNeedle>.GetNeedle(item), out isNew);
 }