Example #1
0
 public void Insert(int index, T item)
 {
     DoBaseWrite(() =>
     {
         WriteCollection.Insert(index, item);
     });
 }
Example #2
0
        public void Collections_Add()
        {
            ReadCollection.AssertEmpty();

            var item = CreateItem1();

            WriteCollection.Add(item);

            ReadCollection.AssertContainsOnly(item);

            WriteCollection.Add(item);

            if (AllowDuplicateEntries)
            {
                ReadCollection.AssertSequence(item, item);
            }
            else
            {
                ReadCollection.AssertContainsOnly(item);
            }

            WriteCollection.Remove(item);

            if (AllowDuplicateEntries)
            {
                ReadCollection.AssertContainsOnly(item);
            }
            else
            {
                ReadCollection.AssertEmpty();
            }
        }
Example #3
0
 public void Add(T item)
 {
     DoBaseWrite(() =>
     {
         WriteCollection.Add(item);
     });
 }
Example #4
0
 public void RemoveAt(int index)
 {
     DoBaseWrite(() =>
     {
         WriteCollection.RemoveAt(index);
     });
 }
Example #5
0
 public bool Remove(T item)
 {
     return(DoBaseWrite(() =>
     {
         return WriteCollection.Remove(item);
     }));
 }
Example #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="Item"></param>
        /// <returns></returns>
        public bool Remove(T Item)
        {
            var Result = DoBaseWrite(() => WriteCollection.Remove(Item));

            OnItemRemoved(Item);
            OnItemsChanged();
            return(Result);
        }
Example #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="i"></param>
        public void RemoveAt(int i)
        {
            var Item = this[i];

            DoBaseWrite(() => WriteCollection.RemoveAt(i));
            OnItemRemoved(Item);
            OnItemsChanged();
        }
        protected virtual void BaseAdd(TKey key, TValue value)
        {
            var node = new DoubleLinkListIndexNode(_lastNode, _keyToIndex.Count);

            _keyToIndex.Add(key, node);
            _lastNode = node;
            WriteCollection.Add(new KeyValuePair <TKey, TValue>(key, value));
        }
        // ************************************************************************
        // 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();
            }
        }
Example #10
0
        public void Collections_Clear()
        {
            var item1 = CreateItem1();
            var item2 = CreateItem2();

            WriteCollection.Add(item1);
            WriteCollection.Add(item2);

            WriteCollection.Clear();

            ReadCollection.AssertEmpty();
        }
Example #11
0
 /// <summary>
 ///   Initializes a new instance of the FileByteProvider class.
 /// </summary>
 /// <param name = "fileName"></param>
 public FileByteProvider(string fileName) {
     _writes = new WriteCollection();
     FileName = fileName;
     try {
         // try to open in write mode
         _fileStream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
     } catch {
         // write mode failed, try to open in read-only and fileshare friendly mode.
         _fileStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
         _readOnly = true;
     }
 }
 /// <summary>
 ///   Initializes a new instance of the FileByteProvider class.
 /// </summary>
 /// <param name = "fileName"></param>
 public FileByteProvider(string fileName)
 {
     _writes  = new WriteCollection();
     FileName = fileName;
     try {
         // try to open in write mode
         _fileStream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
     } catch {
         // write mode failed, try to open in read-only and fileshare friendly mode.
         _fileStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
         _readOnly   = true;
     }
 }
 /// <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);
     }));
 }
Example #14
0
        public void Collections_Order()
        {
            var item1 = CreateItem1();
            var item2 = CreateItem2();

            WriteCollection.Add(item1);
            WriteCollection.Add(item2);

            if (PreservesOrder)
            {
                ReadCollection.AssertSequence(item1, item2);
            }
            else
            {
                ReadCollection.AssertContainsOnly(item1, item2);
            }
        }
Example #15
0
        public void Collections_CopyTo()
        {
            WriteCollection.Add(CreateItem1());
            WriteCollection.Add(CreateItem2());
            WriteCollection.Add(CreateItem3());
            WriteCollection.Add(CreateItem2());

            T[] array = new T[ReadCollection.Count];
            ReadCollection.CopyTo(array, 0);

            if (PreservesOrder)
            {
                ReadCollection.AssertSequence(array);
            }
            else
            {
                ReadCollection.AssertContainsOnly(array);
            }
        }
        /// <summary>
        /// Adds an element with the provided key and value to the IDictionary<TKey, TValue>.
        /// </summary>
        /// <param name="key">
        /// The object to use as the key of the element to add.
        /// </param>
        /// <param name="value">
        /// The object to use as the value of the element to add.
        /// </param>
        protected override void BaseAdd(TKey key, TValue value)
        {
            int index = _sorter.GetInsertIndex(WriteCollection.Count, key, delegate(int testIndex) {
                return(WriteCollection[testIndex].Key);
            });

            if (index >= WriteCollection.Count)
            {
                base.BaseAdd(key, value);
            }
            else
            {
                TKey listKey = WriteCollection[index].Key;
                DoubleLinkListIndexNode next    = _keyToIndex[listKey];
                DoubleLinkListIndexNode newNode = new DoubleLinkListIndexNode(next.Previous, next);
                _keyToIndex[key] = newNode;
                WriteCollection.Insert(index, new KeyValuePair <TKey, TValue>(key, value));
            }
        }
Example #17
0
        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();
            }
        }
Example #18
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="Item"></param>
 public void Add(T Item)
 {
     DoBaseWrite(() => WriteCollection.Add(Item));
     OnItemAdded(Item);
     OnItemsChanged();
 }
Example #19
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="i"></param>
 /// <param name="Item"></param>
 public void Insert(int i, T Item)
 {
     DoBaseWrite(() => WriteCollection.Insert(i, Item));
     OnItemInserted(Item, i);
     OnItemsChanged();
 }
Example #20
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="Items"></param>
 public void Add(IEnumerable <T> Items)
 {
     Items.ForEach(i => DoBaseWrite(() => WriteCollection.Add(i)));
     OnItemsAdded(Items);
     OnItemsChanged();
 }