internal static void Add(TList list, TElement item) { if (list == null) { throw new ArgumentNullException("list"); } if (item == null) { throw new ArgumentNullException("item"); } list.InvokeInMonitorLock(() => RaiseItemAdded(list, item, item.GetInMonitorLock(() => { ValidateItemOrphaned(list, item); return(list.GetInChange(() => { long i = list.Count; RaiseAddingItem(list, item, i); if (list._firstNode == null) { list._firstNode = list._lastNode = item; list.Count = 1; } else { list._lastNode._nextNode = item; list._lastNode = item; list.Count++; } item._container = list; return i; })); }))); }
internal static long IndexOf(TList list, TElement item) { if (list == null) { throw new ArgumentNullException("list"); } return(list.GetInMonitorLock(() => { if (item != null && item.GetInMonitorLock(() => item._container != null && ReferenceEquals(item._container, list))) { if (item._nextNode == null) { return list.Count - 1; } long index = -1L; for (TElement node = list._firstNode; node != null; node = node._nextNode) { index++; if (ReferenceEquals(node, item)) { return index; } } } return -1L; })); }
internal static bool Contains(TList list, TElement item) { if (list == null) { throw new ArgumentNullException("list"); } return(item != null && item.GetInMonitorLock(() => item._container != null && ReferenceEquals(item._container, list))); }
internal static bool Remove(TList list, TElement item) { if (list == null) { throw new ArgumentNullException("list"); } if (item == null) { return(false); } return(list.GetInMonitorLock(() => { if (list.Count == 0) { return false; } if (item.GetInMonitorLock(() => item._container == null || !ReferenceEquals(item._container, list))) { return false; } if (ReferenceEquals(list._firstNode, item)) { list.InvokeInChange(() => { RaiseRemovingItem(list, item, 0L); list._firstNode = item._nextNode; if (list._firstNode == null) { list.Count = 0L; list._lastNode = null; } else if (list._firstNode._nextNode == null) { list.Count = 1L; list._lastNode = list._firstNode; } else { list.Count--; } item._container = null; item._nextNode = null; }); RaiseItemRemoved(list, item, 0L); return true; } long index = 0L; TElement previousItem = list._firstNode; for (TElement node = previousItem._nextNode; node != null; node = node._nextNode) { index++; if (ReferenceEquals(node, item)) { list.InvokeInChange(() => { RaiseRemovingItem(list, item, index); previousItem._nextNode = item._nextNode; if (previousItem._nextNode == null) { list._lastNode = previousItem; } }); RaiseItemRemoved(list, item, index); return true; } previousItem = node; } return false; })); }