public static bool TryGet(LinkII <TKey, TValue> link, TKey key, out TValue value) { while (link != null) { if ((object)key == (object)link.Key) { value = link.Value; return(true); } link = link.Tail; } value = default(TValue); return(false); }
public static bool TryAdd(ref LinkII <TKey, TValue> head, TKey key, ref TValue value) { bool tryAgain; do { var snapshot = Interlocked.CompareExchange(ref head, null, null); TValue found; if (TryGet(snapshot, key, out found)) { // existing match; report the existing value instead value = found; return(false); } var newNode = new LinkII <TKey, TValue>(key, value, snapshot); // did somebody move our cheese? tryAgain = Interlocked.CompareExchange(ref head, newNode, snapshot) != snapshot; } while (tryAgain); return(true); }
private LinkII(TKey key, TValue value, LinkII <TKey, TValue> tail) { Key = key; Value = value; Tail = tail; }