Click here to Skip to main content
12,950,272 members (68,401 online)

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
professionalMatthew Faithfull24-Jul-13 5:24 
GeneralRe: Misaligned elephants Pin
jschell24-Jul-13 9:26
memberjschell24-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
memberAndrew Rissing19-Jul-13 7:06 
GeneralRe: Extension Methods Pin
Richard Deeming19-Jul-13 7:20
professionalRichard Deeming19-Jul-13 7:20 
GeneralRe: Extension Methods Pin
Gary Wheeler19-Jul-13 7:21
memberGary Wheeler19-Jul-13 7:21 
GeneralRe: Extension Methods Pin
TnTinMn19-Jul-13 8:00
memberTnTinMn19-Jul-13 8:00 
GeneralRe: Extension Methods Pin
Jacek Gajek19-Jul-13 8:08
memberJacek Gajek19-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
professionalRichard Deeming19-Jul-13 8:55 
GeneralRe: Extension Methods Pin
Jacek Gajek19-Jul-13 9:06
memberJacek Gajek19-Jul-13 9:06 
GeneralRe: Extension Methods Pin
PIEBALDconsult19-Jul-13 8:09
professionalPIEBALDconsult19-Jul-13 8:09 
GeneralRe: Message Automatically Removed Pin
lewax0019-Jul-13 9:35
memberlewax0019-Jul-13 9:35 
GeneralRe: Message Automatically Removed Pin
ryanb3119-Jul-13 10:22
memberryanb3119-Jul-13 10:22 
GeneralRe: Message Automatically Removed Pin
lewax0021-Jul-13 11:03
memberlewax0021-Jul-13 11:03 
GeneralRe: Message Automatically Removed Pin
pkfox29-Jul-13 22:26
memberpkfox29-Jul-13 22:26 
GeneralThis one is for the Java experts Pin
Grimlock18-Jul-13 0:56
professionalGrimlock18-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
memberFredrik Bornander18-Jul-13 2:10 
GeneralRe: This one is for the Java experts Pin
Grimlock18-Jul-13 2:27
professionalGrimlock18-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
membersvella23-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
Amitosh S.M.18-Jul-13 15:53
memberAmitosh S.M.18-Jul-13 15:53 
GeneralRe: This one is for the Java experts Pin
ExcellentOrg22-Jul-13 21:30
groupExcellentOrg22-Jul-13 21:30 
GeneralRe: This one is for the Java experts Pin
englebart23-Jul-13 2:57
memberenglebart23-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.


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 26 May 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid