Click here to Skip to main content
15,884,176 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
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 
GeneralRe: Extension Methods Pin
Lutosław19-Jul-13 8:08
Lutosław19-Jul-13 8:08 
Posting in this particular forum triggers a find-what-is-bad thinking. Sorry. Wink | ;)
1. It's O(n^3) which is not good. Did you consider working on sorted collections? Then it would be just O(n). (more precisely, O(max{n,m})).
Why O(n^3):
1. foreach loop
2. Contains method which searches notRemove
3. Remove(itm) method which has to find index of itm (in worst case, n calls to Equals method) and, if it's an array list, it has to shift all elements by one which makes it even worse.
Besides, I though that you cannot edit a collection inside foreach.

2. I method 2: Why copy all elements to a new array? Without it, it would be memory complexity of Ω(1), in situ operation. Now it's Ω(n) because it allocates a new array in the process.

3. Formatting horror, but maybe it was screwed up during pastying*.
if (notRemove.Contains(itm)) continue;
    source.Remove(itm);

Shouldn't it be:
if (notRemove.Contains(itm))
     continue;
source.Remove(itm);


IF it is known that all items in notRemoves appear in the source exactly once, then what about:
public static void RemoveAllBut<T>(this ICollection<T> source, params T[] notRemove)
{
  source.Clear();
  foreach(var x in notRemove) 
    source.Add(x);
  // because we don't have source.AddAll, unfortunately
}

which is O(m) and Ω(1).
* -- "pastying" - is it a correct spelling? (derived from a verb "paste").
Greetings - Jacek

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 
GeneralRe: This one is for the Java experts Pin
englebart23-Jul-13 2:57
professionalenglebart23-Jul-13 2:57 

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.