RemoveAll for a SortedList

M

Michael Nesslinger

Hello,

i am looking for an easy way to do a "RemoveAll(Predicate<T> match)" for
a SortedList like it is possible for a List.

My first question is:
Why is the Method not available for the SortedList.
And my second one:
How can it be done the best (fastest) way for a SortedList

Thanks for any ideas

Michael Neßlinger
 
N

Nicholas Paldino [.NET/C# MVP]

Michael,

The only way you are going to be able to do this is by looping through
each item and then determining if it should be removed:

public static int RemoveAllFromSortedList<TKey, TValue>(SortedList<TKey,
TValue> sortedList,
Predicate<KeyValuePair<TKey, TValue>> match)
{
// The number of items removed.
int itemsRemoved = 0;

// Cycle through each of the values in the sorted list.
foreach (KeyValuePair<TKey, TValue> pair in sortedList)
{
// If the predicate returns true, then remove the item.
if (match(pair))
{
// Remove the item.
sortedList.Remove(pair.Key);

// Increment the number of items removed.
itemsRemoved++;
}
}

// Return the items removed.
return itemsRemoved;
}

Hope this helps.
 
M

Michael Nesslinger

Nicholas said:
Michael,

The only way you are going to be able to do this is by looping through
each item and then determining if it should be removed:

public static int RemoveAllFromSortedList<TKey, TValue>(SortedList<TKey,
TValue> sortedList,
Predicate<KeyValuePair<TKey, TValue>> match)
{
// The number of items removed.
int itemsRemoved = 0;

// Cycle through each of the values in the sorted list.
foreach (KeyValuePair<TKey, TValue> pair in sortedList)
{
// If the predicate returns true, then remove the item.
if (match(pair))
{
// Remove the item.
sortedList.Remove(pair.Key);

// Increment the number of items removed.
itemsRemoved++;
}
}

// Return the items removed.
return itemsRemoved;
}

Hope this helps.
Thanks,
that is what i was suspecting. I just hoped for some nice trick to avoid
this.

Michael Neßlinger
 
B

Ben Voigt [C++ MVP]

Nicholas Paldino said:
Michael,

The only way you are going to be able to do this is by looping through
each item and then determining if it should be removed:

But loop from the end, to avoid repeated defragmentation.
public static int RemoveAllFromSortedList<TKey, TValue>(SortedList<TKey,
TValue> sortedList,
Predicate<KeyValuePair<TKey, TValue>> match)
{
// The number of items removed.
int itemsRemoved = 0;

// Cycle through each of the values in the sorted list.
foreach (KeyValuePair<TKey, TValue> pair in sortedList)
{
// If the predicate returns true, then remove the item.
if (match(pair))
{
// Remove the item.
sortedList.Remove(pair.Key);

// Increment the number of items removed.
itemsRemoved++;
}
}

// Return the items removed.
return itemsRemoved;
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael Nesslinger said:
Hello,

i am looking for an easy way to do a "RemoveAll(Predicate<T> match)" for
a SortedList like it is possible for a List.

My first question is:
Why is the Method not available for the SortedList.
And my second one:
How can it be done the best (fastest) way for a SortedList

Thanks for any ideas

Michael Neßlinger
 
B

Ben Voigt [C++ MVP]

Nicholas Paldino said:
Michael,

The only way you are going to be able to do this is by looping through
each item and then determining if it should be removed:

Stupid me... any sort of repeated removal involves suicidal defragmentation.
Copy items selectively into a new list instead.
public static int RemoveAllFromSortedList<TKey, TValue>(SortedList<TKey,
TValue> sortedList,
Predicate<KeyValuePair<TKey, TValue>> match)
{
// The number of items removed.
int itemsRemoved = 0;

// Cycle through each of the values in the sorted list.
foreach (KeyValuePair<TKey, TValue> pair in sortedList)
{
// If the predicate returns true, then remove the item.
if (match(pair))
{
// Remove the item.
sortedList.Remove(pair.Key);

// Increment the number of items removed.
itemsRemoved++;
}
}

// Return the items removed.
return itemsRemoved;
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael Nesslinger said:
Hello,

i am looking for an easy way to do a "RemoveAll(Predicate<T> match)" for
a SortedList like it is possible for a List.

My first question is:
Why is the Method not available for the SortedList.
And my second one:
How can it be done the best (fastest) way for a SortedList

Thanks for any ideas

Michael Neßlinger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top