Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Message Automatically Removed

Message Automatically Removed

Scheduled Pinned Locked Moved The Weird and The Wonderful
13 Posts 10 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Brisingr Aerowing
    wrote on last edited by
    #1

    Message Automatically Removed

    A Richard DeemingR G T L 8 Replies Last reply
    0
    • B Brisingr Aerowing

      Message Automatically Removed

      A Offline
      A Offline
      Andrew Rissing
      wrote on last edited by
      #2

      I'm not sure if you were showcasing a weird or a wonderful here, because the second method won't perform as expected.

      Brisingr Aerowing wrote:

      public static void RemoveAllBut(this ICollection source, params object[] notRemove) { RemoveAllBut<object>(source.OfType<object>().ToArray(), notRemove); }

      1 Reply Last reply
      0
      • B Brisingr Aerowing

        Message Automatically Removed

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        With any decent implementation of ICollection<T> your method will produce an InvalidOperationException with the message: "Collection was modified; enumeration operation may not execute." You'll need to store the items to remove in a separate list, and remove them outside of any enumeration of the source list:

        public static void RemoveAllBut(this ICollection source, params T[] notRemove)
        {
        var itemsToRemove = source.Except(notRemove).ToList();
        itemsToRemove.ForEach(item => source.Remove(item));
        }

        As Andrew pointed out, your non-generic version won't work. It would need to be:

        public static void RemoveAllBut(this IList source, params object[] notRemove)
        {
        var itemsToRemove = source.Cast().Except(notRemove).ToList();
        itemsToRemove.ForEach(source.Remove);
        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined."
        ** - Homer**

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        • B Brisingr Aerowing

          Message Automatically Removed

          G Offline
          G Offline
          Gary Wheeler
          wrote on last edited by
          #4

          At least in .NET 3.5, your first function will throw an exception, since you are modifying the collection being enumerated.

          Software Zen: delete this;

          1 Reply Last reply
          0
          • B Brisingr Aerowing

            Message Automatically Removed

            T Offline
            T Offline
            TnTinMn
            wrote on last edited by
            #5

            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\]);
                }
            }
            

            }

            1 Reply Last reply
            0
            • B Brisingr Aerowing

              Message Automatically Removed

              L Offline
              L Offline
              Lutoslaw
              wrote on last edited by
              #6

              Posting in this particular forum triggers a find-what-is-bad thinking. Sorry. ;) 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

              Richard DeemingR 1 Reply Last reply
              0
              • B Brisingr Aerowing

                Message Automatically Removed

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                Yeah, what they said. Plus I'd avoid ToArray. And see whether or not a HashSet might be of use: HashSet<T>.IntersectWith Method[^] Or not, whatever. :sigh:

                1 Reply Last reply
                0
                • L Lutoslaw

                  Posting in this particular forum triggers a find-what-is-bad thinking. Sorry. ;) 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

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  Jacek Gajek wrote:

                  * -- "pastying" - is it a correct spelling? (derived from a verb "paste").

                  The word you're looking for is "pasting". However, I like the idea of using "pastying" when referring to anything connected to the Cornish foodstuff[^]. :)


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  L 1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    Jacek Gajek wrote:

                    * -- "pastying" - is it a correct spelling? (derived from a verb "paste").

                    The word you're looking for is "pasting". However, I like the idea of using "pastying" when referring to anything connected to the Cornish foodstuff[^]. :)


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    L Offline
                    L Offline
                    Lutoslaw
                    wrote on last edited by
                    #9

                    Thanks. Finally I know it. I didn't write "pasting" because it seemed to be related to "the past" and made no sense to me (or did it?).

                    Greetings - Jacek

                    1 Reply Last reply
                    0
                    • B Brisingr Aerowing

                      Message Automatically Removed

                      L Offline
                      L Offline
                      lewax00
                      wrote on last edited by
                      #10

                      Brisingr Aerowing wrote:

                      Message Automatically Manually Removed

                      FTFY ;P

                      1 Reply Last reply
                      0
                      • B Brisingr Aerowing

                        Message Automatically Removed

                        Z Offline
                        Z Offline
                        ZurdoDev
                        wrote on last edited by
                        #11

                        There was no need to remove your message. Your idea was a good one but I am guessing you didn't test it. And with all of the egos on this site (not saying anyone who responded necessarily has one) if you say something wrong or that can be perceived as wrong there are many who jump at the opportunity to attack. Lots of vultures. :)

                        There are only 10 types of people in the world, those who understand binary and those who don't.

                        L pkfoxP 2 Replies Last reply
                        0
                        • Z ZurdoDev

                          There was no need to remove your message. Your idea was a good one but I am guessing you didn't test it. And with all of the egos on this site (not saying anyone who responded necessarily has one) if you say something wrong or that can be perceived as wrong there are many who jump at the opportunity to attack. Lots of vultures. :)

                          There are only 10 types of people in the world, those who understand binary and those who don't.

                          L Offline
                          L Offline
                          lewax00
                          wrote on last edited by
                          #12

                          And in this case, most of the posts looked like they were trying to help (except mine ;P ).

                          1 Reply Last reply
                          0
                          • Z ZurdoDev

                            There was no need to remove your message. Your idea was a good one but I am guessing you didn't test it. And with all of the egos on this site (not saying anyone who responded necessarily has one) if you say something wrong or that can be perceived as wrong there are many who jump at the opportunity to attack. Lots of vultures. :)

                            There are only 10 types of people in the world, those who understand binary and those who don't.

                            pkfoxP Offline
                            pkfoxP Offline
                            pkfox
                            wrote on last edited by
                            #13

                            As CM says "Leave the egos at the door" When the going gets weird the weird turn pro - Hunter S Thompson RIP

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups