Skip to content

Instantly share code, notes, and snippets.

@korchoon
Created June 23, 2020 21:06
Show Gist options
  • Save korchoon/34953075fe37c9b19d3724ffb3b04bc6 to your computer and use it in GitHub Desktop.
Save korchoon/34953075fe37c9b19d3724ffb3b04bc6 to your computer and use it in GitHub Desktop.
public class GrowList<T> {
public T[] Items;
public int Count;
public GrowList (int capacity) {
Items = new T[capacity];
Count = 0;
}
public void Add (T item) {
if (Items.Length == Count) {
Array.Resize (ref Items, Items.Length << 1);
}
Items[Count++] = item;
}
public void EnsureCapacity (int count) {
if (Items.Length < count) {
var len = Items.Length << 1;
while (len <= count) {
len <<= 1;
}
Array.Resize (ref Items, len);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment