/// <summary> /// This always returns a value, no bounds checks required /// Except when the list is empty, returns default(T) /// </summary> /// <param name="search"></param> /// <returns></returns> public T FindSafe(T search) { RWLock.EnterReadLock(); try { if (base.Count == 0) { return(default(T)); } EnsureComparer(); int a = 0; int b = base.Count; while (b - a > 1) { int mid = (a + b) / 2; if (Comparer.Compare(base[mid], search) > 0) { b = mid; } else { a = mid; } } return(base[a]); } finally { RWLock.ExitReadLock(); } }
public int AddOrUpdate(T elem) { RWLock.EnterWriteLock(); try { int index = InternalIndexOf(elem); if (index < 0) { index = ~index; } if (index >= base.Count) { base.Add(elem); } else { base[index] = elem; } OnInsert(elem); return(index); } finally { RWLock.ExitWriteLock(); } }
/// <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); }); }
/// <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); })); }
public bool Disconnect(int token) { lock (this.state) { RWLock.EnterWriteLock(); if (!this.state.Players.ContainsKey(token)) { return(false); } bool returnValue = this.state.Players.TryRemove(token, out var player); Callback.SendLog("Opuszczono pokój gry."); BroadcastMessage(player.Name + " opuścił grę."); BroadcastPlayerList(); if (!IsRequiredNumberOfPlayers() && state.State != State.Lobby) { BroadcastMessage("Brak wystarczającej liczby graczy do kontynuowania gry."); StopGame(); } if (state.State == State.Lobby) { this.state.Leaderboard.TryRemove(token, out _); } if (state.Players.Keys.Count == 1 && token == state.AdminToken) { state.AdminToken = this.state.Players.Values.First().Id; state.Players[state.AdminToken].Callback.SendPlayerData(new PlayerDto(state.AdminToken, player, true)); } RWLock.ExitWriteLock(); return(returnValue); } }
public T FindValue(Func <T, int> CompareResult) { RWLock.EnterReadLock(); try { int a = 0; int b = base.Count; while (true) { if (a == b) { return(default(T)); } int mid = a + ((b - a) / 2); switch (CompareResult(base[mid])) { case -1: a = mid + 1; break; case 1: b = mid; break; case 0: return(base[mid]); } } } finally { RWLock.ExitReadLock(); } }
private INode GetNode(TKey key) { return(RWLock.GetReadLock(_lock, _lockTimeout, delegate { WeakReference value; return (INode)(_index.TryGetValue(key, out value) ? value.Target : null); })); }
public void Init() { RWLockCount = 0; SpinLockCount = 0; rwLocker = new RWLock(); spinLocker = new SpinLock(); }
/// <summary>Remove all items from index</summary> public void ClearIndex() { RWLock.GetWriteLock(_lock, LockTimeout, delegate { index.Clear(); return(true); }); }
private INode <T> FindExistingNodeByKey(TKey key) { return(RWLock.GetReadLock(_lock, LockTimeout, delegate { WeakReference value; return (INode <T>)(index.TryGetValue(key, out value) ? value.Target : null); })); }
public new bool Remove(T item) { RWLock.EnterWriteLock(); try { return(base.Remove(item)); } finally { RWLock.ExitWriteLock(); } }
public new void Clear() { RWLock.EnterWriteLock(); try { base.Clear(); } finally { RWLock.ExitWriteLock(); } }
public new bool Add(T item) { RWLock.EnterWriteLock(); try { return(base.Add(item)); } finally { RWLock.ExitWriteLock(); } }
public int FirstIndexOf(T item) { RWLock.EnterReadLock(); try { return(InternalFirstIndexOf(item)); } finally { RWLock.ExitReadLock(); } }
/*** ***/ public new T[] ToArray() { RWLock.EnterReadLock(); try { return(base.ToArray()); } finally { RWLock.ExitReadLock(); } }
public int IndexOfElementOrSuccessor(T item) { RWLock.EnterReadLock(); try { return(InternalIndexOfElementOrSuccessor(item)); } finally { RWLock.ExitReadLock(); } }
/// <summary> /// You can't find or delete after this /// </summary> /// <param name="elem"></param> public void AddLast(T elem) { RWLock.EnterWriteLock(); try { base.Add(elem); } finally { OnInsert(elem); RWLock.ExitWriteLock(); } }
public virtual void NaturalMergeSort(IComparer <T> comparer) { RWLock.EnterWriteLock(); try { InternalNaturalMergeSort(comparer); } finally { RWLock.ExitWriteLock(); } }
/// <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); }); }
/// <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; })); }
/// <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; })); }
public new T this[int index] { get{ RWLock.EnterReadLock(); try { return(base[index]); } finally { RWLock.ExitReadLock(); } } }
public new ThreadsafeEnumerator <T> GetEnumerator() { ThreadsafeEnumerator <T> enu; RWLock.EnterReadLock(); try { enu = new ThreadsafeEnumerator <T>(this.ToArray(), Count); } finally { RWLock.ExitReadLock(); } return(enu); }
/// <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; })); }
private bool RemoveLru() { return(RWLock.GetWriteLock(_lock, _lockTimeout, delegate { if (_tail == null) { return false; } _data.Remove(_tail.Key); DeleteNode(_tail); return true; })); }
private void Dispose(bool disposing) { if (fCacheLock != null) { if (disposing) { Clear(); } fCacheLock.Dispose(); fCacheLock = null; } }
public new void RemoveAt(int index) { RWLock.EnterWriteLock(); try { T item = this[index]; base.RemoveAt(index); OnRemove(item); } finally { RWLock.ExitWriteLock(); } }
/// <summary> /// returns IndexOf or -1 /// </summary> /// <param name="item"></param> /// <returns></returns> public new int IndexOf(T item) { RWLock.EnterReadLock(); try { int idx = InternalIndexOf(item); if (idx < 0 || idx > base.Count) { return(-1); } return(idx); } finally { RWLock.ExitReadLock(); } }
/// <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; })); } }
public new void Insert(int index, T elem) { RWLock.EnterWriteLock(); try { if (elem == null) { return; } base.Insert(index, elem); OnInsert(elem); } finally { RWLock.ExitWriteLock(); } }