c# - Take & remove elements from collection -
what's performant way remove n elements collection , add removed n elements existing, different, collection?
currently i've got this:
var entries = collection.take(5).tolist(); foreach(var entry in entries) collection.remove(entry); othercollection.addrange(entries);
however, doesn't performant @ me (multiple linear algorithms instead of one).
a possible solution may of course change collection implementation - long following requirements met:
othercollection
must implementienumerable<t>
, of typelist<t>
collection
must implementicollection<t>
, of typelinkedlist<t>
hint: entries not implement equals()
or gethashcode()
.
what's performant way reach goal?
as has been hard understand performance considerations, here once more code example:
var entries = collection.take(1000).tolist(); // 1000 steps foreach(var entry in entries) // 1000 * 1 steps (as remove finds element @ beginning) collection.remove(entry); othercollection.addrange(entries); // 1000 steps
= 3000 steps in total => want reduce single 1000 steps.
with use case best data structure seems queue. when using queue method can this:
public static ienumerable<t> takeandremove<t>(queue<t> queue, int count) { count = math.min(queue.count, count); (int = 0; < count; i++) yield return queue.dequeue(); }
Comments
Post a Comment