What is wrong with using List for this? This is nothing but an implementation of IList, and you can do the splitting yourself. But if you want to do it transparently:
Implement IList (it's just an interface, nothing special about that. Maybe I donβt understand the question?) And reinforce it with arrays of the desired size. Then your Get() will take the index / sizeOfArrays index as the index of the array containing the element you index % sizeOfArrays , and return the index % sizeOfArrays th element in that array.
For pleasure, because it's lazy Friday, I wrote something. Note:
- I have not tested it
- I canβt comment on the correctness of your statements that this can help avoid memory fragmentation, I just blindly looked at your request.
- I don't know if List or any other collection is already smart enough to do just that.
- I made some decisions that may be wrong for you (that is, you cannot blindly refuse this in your code if you are now using arrays. Look at the implementation of
Item , especially the installer, for example
However, here is the starting point, which reduced my motivational deficit before leaving. I left some interesting methods as an exercise for a respected reader (or OP) ..; -)
public class PartitionList<T> : IList<T> { private readonly int _maxCountPerList; private readonly IList<IList<T>> _lists; public PartitionList(int maxCountPerList) { _maxCountPerList = maxCountPerList; _lists = new List<IList<T>> { new List<T>() }; } public IEnumerator<T> GetEnumerator() { return _lists.SelectMany(list => list).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public void Add(T item) { var lastList = _lists[_lists.Count - 1]; if (lastList.Count == _maxCountPerList) { lastList = new List<T>(); _lists.Add(lastList); } lastList.Add(item); } public void Clear() { while (_lists.Count > 1) _lists.RemoveAt(1); _lists[0].Clear(); } public bool Contains(T item) { return _lists.Any(sublist => sublist.Contains(item)); } public void CopyTo(T[] array, int arrayIndex) {
source share