/// <summary> /// Removes an item from the list. /// </summary> /// <param name="item">The item to be removed</param> /// <returns>True if the item was removed, or false if it was not found</returns> public bool Remove(T item) { MultiRange range = item.Range; int removed = 0; for (int i = 0; i < range.Count; i++) { var subrange = range.GetSubRange(i); if (IsInvalid(ref subrange)) { continue; } removed += _items.Remove(subrange.Address, item); } if (removed > 0) { // All deleted intervals are for the same item - the one we removed. Count--; } return(removed > 0); }
/// <summary> /// Adds a new item to the list. /// </summary> /// <param name="item">The item to be added</param> public void Add(T item) { MultiRange range = item.Range; for (int i = 0; i < range.Count; i++) { var subrange = range.GetSubRange(i); _items.Add(subrange.Address, subrange.EndAddress, item); } Count++; }
/// <summary> /// Gets all items on the list overlapping the specified memory ranges. /// </summary> /// <param name="range">Ranges of memory being searched</param> /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param> /// <returns>The number of overlapping items found</returns> public int FindOverlaps(MultiRange range, ref T[] output) { int overlapCount = 0; for (int i = 0; i < range.Count; i++) { var subrange = range.GetSubRange(i); if (IsInvalid(ref subrange)) { continue; } overlapCount = _items.Get(subrange.Address, subrange.EndAddress, ref output, overlapCount); } // Remove any duplicates, caused by items having multiple sub range nodes in the tree. if (overlapCount > 1) { int insertPtr = 0; for (int i = 0; i < overlapCount; i++) { T item = output[i]; bool duplicate = false; for (int j = insertPtr - 1; j >= 0; j--) { if (item.Equals(output[j])) { duplicate = true; break; } } if (!duplicate) { if (insertPtr != i) { output[insertPtr] = item; } insertPtr++; } } overlapCount = insertPtr; } return(overlapCount); }
/// <summary> /// Adds a new item to the list. /// </summary> /// <param name="item">The item to be added</param> public void Add(T item) { MultiRange range = item.Range; for (int i = 0; i < range.Count; i++) { var subrange = range.GetSubRange(i); if (IsInvalid(ref subrange)) { continue; } _items.Add(subrange.Address, subrange.EndAddress, item); } Count++; }
/// <summary> /// Gets all items on the list overlapping the specified memory ranges. /// </summary> /// <param name="range">Ranges of memory being searched</param> /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param> /// <returns>The number of overlapping items found</returns> public int FindOverlaps(MultiRange range, ref T[] output) { int outputIndex = 0; foreach (T item in _items) { if (item.Range.OverlapsWith(range)) { if (outputIndex == output.Length) { Array.Resize(ref output, outputIndex + ArrayGrowthSize); } output[outputIndex++] = item; } } return(outputIndex); }