/// <summary> /// Resizes the Subpopulation to a new size. If the size is smaller, then /// the Subpopulation is truncated such that the higher indexed individuals /// may be deleted.If the size is larger, then the resulting Subpopulation will have /// null individuals (this almost certainly is not what you will want). /// </summary> /// <remarks> /// BRS: This should NEVER result in any null individuals (we're using List{T}). /// By changing the capacity of a list lower we free memory up for garbage collection. /// If what is wanted is explicit increase in capacity then we should probably have /// a "forceNewCapacity" flag as a second argument. /// </remarks> /// <param name="toThis">A new "Capacity" value.</param> public void Resize(int toThis) { var n = Individuals.Count - toThis; if (n <= 0) { return; // list will grow on its own } // Hopefully this is faster than reallocating // and copying to a new, smaller list. Note // that the current Capacity of the list stays the same. Individuals.RemoveFromTopDesctructively(n); }
public void Truncate(int toThis) { // if we're dealing with an actual List<T> then reducing the capacity // is the easiest and fastest way to truncate. If it's an array // or some other form of IList<T>, then we need to use the interface. if (Individuals is List <Individual> list) { list.Capacity = toThis; // Reclaims memory! ;-) } else { // NOTE: The individuals removed are returned (in their original relative order), // but we have no use for them here. When the return value goes out of scope // the garbage collector will decide when to reclaim the memory. Individuals.RemoveFromTopDesctructively(Individuals.Count - toThis); // TODO: Decide whether or not we should call TrimExcess() to reduce capacity. } }