Click here to Skip to main content

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrasing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Extension Methods PinmemberAndrew Rissing19-Jul-13 8:06 
GeneralRe: Extension Methods PinprofessionalRichard Deeming19-Jul-13 8:20 
GeneralRe: Extension Methods PinmemberGary Wheeler19-Jul-13 8:21 
GeneralRe: Extension Methods PinmemberTnTinMn19-Jul-13 9:00 
In addition to the comments above, each of those Remove statements will incur an array copy cost. Count the occurrence of each item in notRemove, clear the collection, then add the items in notRemove by their respective counts.
 
Possibly something like this:
 
public static void RemoveAllBut<T>(this ICollection<T> source, params T[] notRemove)
{
    Int32[] counts = new Int32[notRemove.Length];
    EqualityComparer<T> comparer = EqualityComparer<T>.Default;
 
    foreach (T itm in source)
    {
        for (Int32 i = 0; i <= counts.Length - 1; i++)
        {
            if (comparer.Equals(notRemove[i], itm))
            {
                counts[i] += 1;
                break;
            }
        }
    }
    source.Clear();
    for (Int32 i = 0; i <= counts.Length - 1; i++)
    {
        for (Int32 j = 1; j <= counts[i]; j++)
        {
            source.Add(notRemove[i]);
        }
    }
 
}

GeneralRe: Extension Methods PinmemberJacek Gajek19-Jul-13 9:08 
GeneralRe: Extension Methods PinprofessionalRichard Deeming19-Jul-13 9:55 
GeneralRe: Extension Methods PinmemberJacek Gajek19-Jul-13 10:06 
GeneralRe: Extension Methods PinprofessionalPIEBALDconsult19-Jul-13 9:09 
GeneralRe: Message Automatically Removed Pinmemberlewax0019-Jul-13 10:35 
GeneralRe: Message Automatically Removed Pinmemberryanb3119-Jul-13 11:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 29 Mar 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid