ProfilingList: make a DEBUG-only CheckCapacity() function

This commit is contained in:
Colin McMillen 2020-03-20 13:22:32 -04:00
parent a6c297196a
commit 5ebc61015b

View File

@ -1,13 +1,14 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
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 readonly List<T> items;
private readonly string name;
private int previousCapacity;
private string name;
public ProfilingList(int capacity, string name) {
items = new List<T>(capacity);
@ -15,26 +16,28 @@ namespace SemiColinGames {
this.name = name;
}
public bool IsReadOnly {
get { return false; }
}
public void Add(T item) {
items.Add(item);
if (items.Capacity != previousCapacity) {
Debug.WriteLine($"{name} capacity: {previousCapacity} -> {items.Capacity}");
previousCapacity = items.Capacity;
}
CheckCapacity();
}
public void Insert(int index, T item) {
items.Insert(index, item);
CheckCapacity();
}
[Conditional("DEBUG")]
private void CheckCapacity() {
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() {