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 implement ienumerable<t>, of type list<t>
  • collection must implement icollection<t>, of type linkedlist<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

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -