/// <summary> /// Sets the capacity of the collection preserving all of the /// existing instances by copying them over to the new structure. /// This is an expensive operation that should be avoided whenever /// possible. /// </summary> protected void SetCapacity(Int64 capacity) { if (capacity == 0) { ReusableArray = null; } else { var newArray = _arrayFactory.CreateAtLeast <T>(capacity); if (ReusableArray != null) { if (newArray.Size > _array.Size) { ReusableArray.GetArray().CopyTo(newArray.GetArray(), 0); } else { var newArrayData = newArray.GetArray(); var oldArray = _array.GetArray(); for (var index = 0; index < newArray.Size; index++) { newArrayData[index] = oldArray[index]; } } } ReusableArray = newArray; } }
public override string ToString() { if (ReusableArray == null || Length < 1) { return(String.Empty); } return(new String(ReusableArray.GetArray(), 0, (int)Length)); }
public IStringBuilder Append(string text) { if (string.IsNullOrEmpty(text)) { return(this); } var startIndex = Length; Length = Length + text.Length; text.CopyTo(0, ReusableArray.GetArray(), (int)startIndex, text.Length); return(this); }
private IArray <T> GetArray <T>(Pool <T> pool) { var array = (ReusableArray <T>)pool.DequeueOrDefault(); if (array == null) { array = new ReusableArray <T>(pool.ArraySize); } return(array.Initialize( (IReusable reusable) => { var a = (IArray <T>)reusable; if (a.IsReusable) { pool.Enqueue(a); } })); }
/// <summary> /// Sorts the collection using the provided comparer /// </summary> public void Sort(IComparer <T> comparer) { Array.Sort(ReusableArray.GetArray(), 0, (int)Length, comparer); }