Click here to Skip to main content
15,887,135 members

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, embarrassing 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: Misaligned elephants Pin
Stuart Dootson24-Jul-13 5:18
professionalStuart Dootson24-Jul-13 5:18 
GeneralRe: Misaligned elephants Pin
Matthew Faithfull24-Jul-13 5:24
Matthew Faithfull24-Jul-13 5:24 
GeneralRe: Misaligned elephants Pin
jschell24-Jul-13 9:26
jschell24-Jul-13 9:26 
GeneralMessage Automatically Removed Pin
Brisingr Aerowing19-Jul-13 6:46
professionalBrisingr Aerowing19-Jul-13 6:46 
GeneralRe: Extension Methods Pin
Andrew Rissing19-Jul-13 7:06
Andrew Rissing19-Jul-13 7:06 
GeneralRe: Extension Methods Pin
Richard Deeming19-Jul-13 7:20
mveRichard Deeming19-Jul-13 7:20 
GeneralRe: Extension Methods Pin
Gary Wheeler19-Jul-13 7:21
Gary Wheeler19-Jul-13 7:21 
GeneralRe: Extension Methods Pin
TnTinMn19-Jul-13 8:00
TnTinMn19-Jul-13 8: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:

C#
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 Pin
Lutosław19-Jul-13 8:08
Lutosław19-Jul-13 8:08 
GeneralRe: Extension Methods Pin
Richard Deeming19-Jul-13 8:55
mveRichard Deeming19-Jul-13 8:55 
GeneralRe: Extension Methods Pin
Lutosław19-Jul-13 9:06
Lutosław19-Jul-13 9:06 
GeneralRe: Extension Methods Pin
PIEBALDconsult19-Jul-13 8:09
mvePIEBALDconsult19-Jul-13 8:09 
GeneralRe: Message Automatically Removed Pin
lewax0019-Jul-13 9:35
lewax0019-Jul-13 9:35 
GeneralRe: Message Automatically Removed Pin
ZurdoDev19-Jul-13 10:22
professionalZurdoDev19-Jul-13 10:22 
GeneralRe: Message Automatically Removed Pin
lewax0021-Jul-13 11:03
lewax0021-Jul-13 11:03 
GeneralRe: Message Automatically Removed Pin
pkfox29-Jul-13 22:26
professionalpkfox29-Jul-13 22:26 
GeneralThis one is for the Java experts Pin
Dennis_E18-Jul-13 0:56
professionalDennis_E18-Jul-13 0:56 
GeneralRe: This one is for the Java experts Pin
Nagy Vilmos18-Jul-13 1:52
professionalNagy Vilmos18-Jul-13 1:52 
GeneralRe: This one is for the Java experts Pin
Fredrik Bornander18-Jul-13 2:10
professionalFredrik Bornander18-Jul-13 2:10 
GeneralRe: This one is for the Java experts Pin
Dennis_E18-Jul-13 2:27
professionalDennis_E18-Jul-13 2:27 
GeneralRe: This one is for the Java experts Pin
Nagy Vilmos18-Jul-13 3:54
professionalNagy Vilmos18-Jul-13 3:54 
GeneralRe: This one is for the Java experts Pin
svella23-Jul-13 3:57
svella23-Jul-13 3:57 
GeneralRe: This one is for the Java experts Pin
Brisingr Aerowing18-Jul-13 6:40
professionalBrisingr Aerowing18-Jul-13 6:40 
GeneralRe: This one is for the Java experts Pin
AlphaDeltaTheta18-Jul-13 15:53
AlphaDeltaTheta18-Jul-13 15:53 
GeneralRe: This one is for the Java experts Pin
ExcellentOrg22-Jul-13 21:30
ExcellentOrg22-Jul-13 21:30 

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

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