2020-11-19 22:26:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
2020-03-20 17:12:12 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-03-20 17:22:32 +00:00
|
|
|
|
using System.Diagnostics;
|
2020-03-20 17:12:12 +00:00
|
|
|
|
|
|
|
|
|
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> {
|
2020-03-20 17:22:32 +00:00
|
|
|
|
private readonly List<T> items;
|
|
|
|
|
private readonly string name;
|
2020-03-20 17:12:12 +00:00
|
|
|
|
private int previousCapacity;
|
|
|
|
|
|
|
|
|
|
public ProfilingList(int capacity, string name) {
|
|
|
|
|
items = new List<T>(capacity);
|
|
|
|
|
previousCapacity = capacity;
|
|
|
|
|
this.name = name;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 17:22:32 +00:00
|
|
|
|
public bool IsReadOnly {
|
|
|
|
|
get { return false; }
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 17:12:12 +00:00
|
|
|
|
public void Add(T item) {
|
|
|
|
|
items.Add(item);
|
2020-03-20 17:22:32 +00:00
|
|
|
|
CheckCapacity();
|
2020-03-20 17:12:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Insert(int index, T item) {
|
|
|
|
|
items.Insert(index, item);
|
2020-03-20 17:22:32 +00:00
|
|
|
|
CheckCapacity();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Conditional("DEBUG")]
|
|
|
|
|
private void CheckCapacity() {
|
2020-03-20 17:12:12 +00:00
|
|
|
|
if (items.Capacity != previousCapacity) {
|
2020-03-20 17:23:47 +00:00
|
|
|
|
Debug.WriteLine($"ProfilingList {name} capacity: {previousCapacity} -> {items.Capacity}");
|
2020-03-20 17:12:12 +00:00
|
|
|
|
previousCapacity = items.Capacity;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-19 22:26:05 +00:00
|
|
|
|
public int RemoveAll(Predicate<T> match) {
|
|
|
|
|
return items.RemoveAll(match);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 17:12:12 +00:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|