Example #1
0
        /// <summary>
        /// Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
        /// </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>
        /// <exception cref="T:System.ArgumentNullException">
        ///     <paramref name="key"/> is null.
        /// </exception>
        /// <exception cref="T:System.ArgumentException">
        /// An element with the same key already exists in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
        /// </exception>
        /// <exception cref="T:System.NotSupportedException">
        /// The <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.
        /// </exception>
        public void Add(TKey key, TValue value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            RWLock.GetWriteLock(_lock, _lockTimeout, delegate
            {
                // Throw out old one if reusing a key.
                if (_data.ContainsKey(key))
                {
                    Remove(key);
                }
                // Keep throwing out old items to make space. Stop if empty.
                while (_data.Count >= _capacity)
                {
                    if (!RemoveLru())
                    {
                        break;
                    }
                }
                if (_data.Count >= _capacity)
                {
                    // Just not enough room.
                    return(false);
                }
                Node node = new Node(key, value);
                _data.Add(key, node);
                InsertNode(node);
                return(true);
            });
        }
Example #2
0
 /// <summary>
 /// Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
 /// </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 <paramref name="key"/> was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2"/>.
 /// </returns>
 /// <exception cref="T:System.ArgumentNullException">
 ///     <paramref name="key"/> is null.
 /// </exception>
 /// <exception cref="T:System.NotSupportedException">
 /// The <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.
 /// </exception>
 public bool Remove(TKey key)
 {
     return(RWLock.GetWriteLock(_lock, _lockTimeout, delegate
     {
         return InternalRemove(key);
     }));
 }
Example #3
0
 /// <summary>Remove all items from index</summary>
 public void ClearIndex()
 {
     RWLock.GetWriteLock(_lock, LockTimeout, delegate
     {
         index.Clear();
         return(true);
     });
 }
Example #4
0
 /// <summary>
 /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
 /// </summary>
 /// <exception cref="T:System.NotSupportedException">
 /// The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
 /// </exception>
 public void Clear()
 {
     RWLock.GetWriteLock(_lock, _lockTimeout, delegate
     {
         _data.Clear();
         _head = null;
         _tail = null;
         return(true);
     });
 }
Example #5
0
        /// <summary>AddAsNode new item to index</summary>
        /// <param name="item">item to add</param>
        /// <returns>was item key previously contained in index</returns>
        public bool AddItem(INode <T> item)
        {
            TKey key = _getKey(item.Value);

            return(RWLock.GetWriteLock(_lock, LockTimeout, delegate
            {
                bool alreadyExisted = index.ContainsKey(key);
                index[key] = new WeakReference(item, false);
                return alreadyExisted;
            }));
        }
Example #6
0
            /// <summary>Add new item to index</summary>
            /// <param name="item">item to add</param>
            /// <returns>was item key previously contained in index</returns>
            public bool AddItem(INode item)
            {
                TKey key = _getKey(item.Value);

                return(RWLock.GetWriteLock(_lock, _lockTimeout, delegate
                {
                    bool isDup = _index.ContainsKey(key);
                    _index[key] = new WeakReference(item, false);
                    return isDup;
                }));
            }
Example #7
0
 private bool RemoveLru()
 {
     return(RWLock.GetWriteLock(_lock, _lockTimeout, delegate
     {
         if (_tail == null)
         {
             return false;
         }
         _data.Remove(_tail.Key);
         DeleteNode(_tail);
         return true;
     }));
 }
Example #8
0
 /// <summary>removes all items from index and reloads each item (this gets rid of dead nodes)</summary>
 public int RebuildIndex()
 {
     lock (_owner._lifeSpan)
         return(RWLock.GetWriteLock(_lock, _lockTimeout, delegate
         {
             _index.Clear();
             foreach (INode item in _owner._lifeSpan)
             {
                 AddItem(item);
             }
             return _index.Count;
         }));
 }
Example #9
0
        /// <summary>removes all items from index and reloads each item (this gets rid of dead nodes)</summary>
        public int RebuildIndex()
        {
            lock (lifespanManager)
            {
                return(RWLock.GetWriteLock(_lock, LockTimeout, delegate
                {
                    index.Clear();
                    foreach (INode <T> item in lifespanManager)
                    {
                        AddItem(item);
                    }

                    return index.Count;
                }));
            }
        }
Example #10
0
        /// <summary>
        /// Adds an item.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public virtual bool AddItem(TKey key, TValue value)
        {
            if (!_dict.ContainsKey(key))
            {
                return(RWLock.GetWriteLock(
                           _dictLock,
                           RWLock.DEFAULT_RWLOCK_TIMEOUT,
                           () =>
                {
                    if (!_dict.ContainsKey(key))
                    {
                        _dict.Add(key, value);
                        return true;
                    }
                    return false;
                }
                           ));
            }

            return(false);
        }
Example #11
0
        /// <summary>
        /// Notifies the dependency changed.
        /// </summary>
        /// <param name="dependency">The dependency.</param>
        /// <param name="ignoreCase">if set to <c>true</c> [ignore case].</param>
        /// <param name="partiallyMatch">if set to <c>true</c> [partially match].</param>
        public void NotifyDependencyChanged(string dependency, bool ignoreCase, bool partiallyMatch)
        {
            List <DependingKey <TKey> >        list = new List <DependingKey <TKey> >();
            IEnumerator <DependingKey <TKey> > en   = this.Keys.GetEnumerator();

            RWLock.GetWriteLock(Lock, LockTimeout, delegate
            {
                while (en.MoveNext())
                {
                    if (en.Current.Depends(dependency, ignoreCase, partiallyMatch))
                    {
                        list.Add(en.Current);
                    }
                }
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; ++i)
                    {
                        this.InternalRemove(list[i]);
                    }
                }
                return(true);
            });
        }