I would like to add a range of numbers to a collection but only replacing null elements. True would be returned if elements have been replaced, otherwise false.
For example let’s say I have a collection with 6 elements that are all null
var collection = { null, null, null, null, null, null };
Then consider I have the following range of values
var a = { 2, 3, 4 };
var b = { 0, 1, 2, 3, 4, 5 };
var c = { 4, 5 };
I would like to add them to the collection in an efficient manner such that only the free slots are assigned the new values.
So if I were to add the range of numbers from a
var canAdd = collection.AddRange(a);
then canAdd
would be true and the collection would look like
{ null, null, 2, 3, 4, null }
Adding the range from b
would also return true and the collection would now look like
{ 0, 1, 2, 3, 4, 5 }
Adding the range from c
would be false as there are no free slots remaining.
Now I understand that this is the sort of thing sets are useful for. However, the range of numbers I want to add would be dynamic and determined by the XMin
and XMax
properties of a separate class. I would also like to avoid the following
- Iterating over the dynamic range of numbers from
XMin
toXMax
- Iterating over the collection for free slots
Is this possible with any of the data structures provided by C# (.NET libraries)?