public void RemoveAt(int index) { DoBaseWrite(() => { WriteCollection.RemoveAt(index); }); }
/// <summary> /// /// </summary> /// <param name="i"></param> public void RemoveAt(int i) { var Item = this[i]; DoBaseWrite(() => WriteCollection.RemoveAt(i)); OnItemRemoved(Item); OnItemsChanged(); }
// ************************************************************************ // Protected Methods // ************************************************************************ #region Protected Methods /// <summary> /// Removes all items from the ICollection<T>. /// </summary> protected void DoBaseClear(Action action) { // Don't use BaseCollection.Clear(), it causes problems because it // sends a reset event, and then the collection needs to be read out through // an enumerator. Use RemoveAt instead until the collection is empty. // Using remove from end after testing with this speed test: // // // speed test for using RemoveAt(int index); // using System; // using System.Collections.Generic; // using System.Collections.ObjectModel; // using System.Diagnostics; // // namespace ConsoleApplication1 { // class Program { // static void Main(string[] args) { // var coll = new Collection<int>(); // for (int ix = 0; ix < 100000; ++ix) coll.Add(ix); // var sw = Stopwatch.StartNew(); // while (coll.Count > 0) coll.RemoveAt(0); // sw.Stop(); // Console.WriteLine("Removed from start {0}ms",sw.ElapsedMilliseconds); // for (int ix = 0; ix < 100000; ++ix) coll.Add(ix); // sw = Stopwatch.StartNew(); // while (coll.Count > 0) coll.RemoveAt(coll.Count - 1); // Console.WriteLine("Removed from end {0}ms",sw.ElapsedMilliseconds); // Console.ReadLine(); // } // } // } // // Output: // Removed from start 4494ms // Removed from end 3ms // Need a special case of DoBaseWrite for a set changes to make sure that nothing else does a change // while we are in the middle of doing a collection of changes. _readWriteLock.TryEnterUpgradeableReadLock(Timeout.Infinite); try { _readWriteLock.TryEnterWriteLock(Timeout.Infinite); action(); while (WriteCollection.Count > 0) { _newSnapshotRequired = true; WriteCollection.RemoveAt(WriteCollection.Count - 1); } } finally { if (_readWriteLock.IsWriteLockHeld) { _readWriteLock.ExitWriteLock(); } _readWriteLock.ExitUpgradeableReadLock(); } }
/// <summary> /// Removes the element with the specified key from the IDictionary<TKey, TValue>. /// </summary> /// <param name="key"> /// The key of the element to remove. /// </param> /// <returns> /// True if the element is successfully removed; otherwise, false. This method also returns false if key was not found in the original IDictionary<TKey, TValue>. /// </returns> public bool Remove(TKey key) { return(DoBaseWrite(() => { DoubleLinkListIndexNode node; if (_keyToIndex.TryGetValue(key, out node)) { WriteCollection.RemoveAt(node.Index); if (node == _lastNode) { _lastNode = node.Previous; } node.Remove(); } return _keyToIndex.Remove(key); })); }
protected void DoBaseClear(Action action) { _readWriteLock.TryEnterUpgradeableReadLock(Timeout.Infinite); try { _readWriteLock.TryEnterWriteLock(Timeout.Infinite); action(); while (WriteCollection.Count > 0) { _newSnapshotRequired = true; WriteCollection.RemoveAt(WriteCollection.Count - 1); } } finally { if (_readWriteLock.IsWriteLockHeld) { _readWriteLock.ExitWriteLock(); } _readWriteLock.ExitUpgradeableReadLock(); } }