Colin McMillen
4 years ago
4 changed files with 86 additions and 4 deletions
@ -0,0 +1,81 @@ |
|||||
|
using System.Collections; |
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace SemiColinGames { |
||||
|
// An IList<T>, backed by a List<T>, that prints out a debug message any time that the Capacity
|
||||
|
// of the underlying List changes.
|
||||
|
public class ProfilingList<T> : IList<T> { |
||||
|
private List<T> items; |
||||
|
private int previousCapacity; |
||||
|
private string name; |
||||
|
|
||||
|
public ProfilingList(int capacity, string name) { |
||||
|
items = new List<T>(capacity); |
||||
|
previousCapacity = capacity; |
||||
|
this.name = name; |
||||
|
} |
||||
|
|
||||
|
public void Add(T item) { |
||||
|
items.Add(item); |
||||
|
if (items.Capacity != previousCapacity) { |
||||
|
Debug.WriteLine($"{name} capacity: {previousCapacity} -> {items.Capacity}"); |
||||
|
previousCapacity = items.Capacity; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void Insert(int index, T item) { |
||||
|
items.Insert(index, item); |
||||
|
if (items.Capacity != previousCapacity) { |
||||
|
Debug.WriteLine($"{name} capacity: {previousCapacity} -> {items.Capacity}"); |
||||
|
previousCapacity = items.Capacity; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public bool IsReadOnly { |
||||
|
get { return false; } |
||||
|
} |
||||
|
|
||||
|
// Everything below this point is boilerplate delegation to the underlying list.
|
||||
|
|
||||
|
public void Clear() { |
||||
|
items.Clear(); |
||||
|
} |
||||
|
|
||||
|
public bool Contains(T item) { |
||||
|
return items.Contains(item); |
||||
|
} |
||||
|
|
||||
|
public int Count { |
||||
|
get => items.Count; |
||||
|
} |
||||
|
|
||||
|
public int IndexOf(T item) { |
||||
|
return items.IndexOf(item); |
||||
|
} |
||||
|
|
||||
|
public bool Remove(T item) { |
||||
|
return items.Remove(item); |
||||
|
} |
||||
|
|
||||
|
public void CopyTo(T[] array, int arrayIndex) { |
||||
|
items.CopyTo(array, arrayIndex); |
||||
|
} |
||||
|
|
||||
|
public void RemoveAt(int index) { |
||||
|
items.RemoveAt(index); |
||||
|
} |
||||
|
|
||||
|
public T this[int index] { |
||||
|
get { return items[index]; } |
||||
|
set { items[index] = value; } |
||||
|
} |
||||
|
|
||||
|
public IEnumerator<T> GetEnumerator() { |
||||
|
return items.GetEnumerator(); |
||||
|
} |
||||
|
|
||||
|
IEnumerator IEnumerable.GetEnumerator() { |
||||
|
return items.GetEnumerator(); |
||||
|
} |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue