• 0

C# Compare Multiple Ranges to Multiple Ranges


Question

I've spent a lot of time trying to think through this issue on my own, but I'm not getting a better answer than what I have and my answer is HORRIBLE.  So...  feel free to pop me upside my head and say; "dumbass...."

 

I have a situation where I have multiple ranges.  i.e. 100 to 200 and 100 to 200 (yes that is the same value ranges, but each range is a separate list of property values.  (just go with it)   I have a second set of values.  Each object in this second set can have a range within those two ranges.  (I'll give examples below)  What I am attempting to find out is the subset of values from the first sets that are not within the second set.

 

Property Sets

"Set1"  range from 0 to 3

"Set2"  range from 0 to 3

 

Object Sets

"Object1"  range for "Set1" from 0 to 2, range for "Set2" 0 to 3

"Object2"  range for "Set1" from 0 to 3, range for "Set2" 0 to 2

 

In this example this example there would be a subset for for the values not found in the objects that would equal

Set1 3, Set2, 3

3 answers to this question

Recommended Posts

  • 0

So,

Object Sets define a sub-range of the property sets.

And you are trying to find the values NOT covered in the sub-range?

Rephrasing,

Set1 and Set2 are ranges of values, while Object1 and Object2 define sub-range of Set1 and Set2.

Which values are not in the sub-ranges of Object1 and Object2?

Do you want a list of these values back?

EDIT: Or you want the list of items which different between range of Set1 and range of Set2 in Object1/Object2?

EDIT 3: Nvm...

EDIT 4: Here is a hint on my current understanding,

            var ss1 = set1.Take(object1.range1.To)
                .Skip(object1.range1.From);

            var ss2 = set2.Take(object1.range2.To)
                .Skip(object1.range2.From);


            var answerForObject1 = ss1
                .Except(ss2)
                .Union(ss2.Except(ss1))
                .ToList();
Edited by _Alexander
  • 0

To say this more simply, are you looking for relative compliment? http://en.wikipedia.org/wiki/Complement_(set_theory) (check the picture).

 

If so, the answer is basically setB.Except(setA) (using Alexander's code above).

  • 0

If these are contiguous sequences of natural numbers, then you can simply compare the bounds which would be more efficient than a more general solution based on Except as already suggested.
 
For example, if the complete range is { 100..200 }, and the object range is { 150..175 }, then the set of missing values is { 100..149 } U { 176.. 200 }. More generally speaking if the complete range is { C1..C2 } and the object range is { O1..O2 }, then the set of missing values is { C1..O1 - 1 } U { O2 + 1..C2 }.

In C#:

Enumerable.Range(c1, o1-c1).Concat(Enumerable.Range(o2+1, c2-o2))

Of course this assumes that o1>=c1 and o2<=c2.

This topic is now closed to further replies.
  • Posts

    • How can all of these Neowin deals be "an all time low price". I find that pretty hard to believe.
    • Oh wow, a patch for an Unreal game to improve performance? We'll see...
    • Get this massive 4TB WD_BLACK SN7100 SSD at a new all-time low price by Taras Buria Xbox owners recently received a new storage upgrade option, which allows them to equip the Xbox Series X|S with 4TB of extra space. That card, however, has an eye-watering price tag—nearly as much as the 1TB Xbox Series S. On the PC side, though, things are much better. Right now, if you need a 4TB SSD without selling a kidney, you can get the WD_BLACK SN7100 PCIe Gen4 SSD. It is now available on Amazon at a new all-time low price after a 16% discount (nearly half the cost of the new 4TB Xbox Storage Expansion Card). The SN7100 is a fast, reliable, and, more importantly, affordable Gen4 solid-state drive. Its speeds are rated up to 7,000MB/s sequential read and 6,700MB/s sequential write, while random input-output speeds are rated at 900K IOPS read and 1,350K IOPS write. These specs are not record-breaking, but they are fast enough for modern gaming (DirectStorage is supported), fast loading times in games and apps, and quick file transfer. The WD_BLACK SN7100 has a limited five-year warranty and endurance rating up to 2,400 TBW. You can monitor the health of your drive in the WD_Black Dashboard app on Windows. As for compatibility, you can use the SN7100 in any PC that accommodates M.2 2280 PCIe Gen4 drives, including PlayStation 5. It is compatible with PCIe Gen3 systems, but the maximum speeds will be limited. 4TB WD_BLACK SN7100 PCIe Gen4 Solid-State Drive - $249.99 | 16% off on Amazon US This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • Snagit 2025.2.0 by Razvan Serea Snagit is the most complete screen capture utility available. Showing someone exactly what you see on your screen is sometimes the quickest and clearest way to communicate. With Snagit, you can select anything on your screen – an area, image, article, Web page, or error message – and capture it. Then, save the screen capture to a file, send it to Snagit​'s editor to add professional effects, share it by e-mail, or drop it into PowerPoint®, Word®, or another favorite application. Capture and share images, text or video from your PC. Create beautiful presentations, flawless documentation and quickly save online content. The latest version of Snagit offers a totally new interface and workflow - making SnagIt easier for beginners to use, while still providing maximum convenience and flexibility for the screen capture experts. Snagit 2025.2.0 changelog: Edit Images from Camtasia Snagit can now be used to seamlessly edit images from Camtasia. Requires Camtasia version 2025.2.0 or later. In the Camtasia Media Bin, right-click an image and select the Edit in Snagit option. In Snagit, make your edits. When finished, click Send in the "Send changes to Camtasia" notification to replace the image on your Camtasia timeline. Step Capture Improved the Step Capture template to accommodate longer auto-populated step text. Annotations such as Callout or Arrow tool objects now anchor to and move with sections as sections are added to, reordered, or deleted from templates. Subscription Software Updated the Account dropdown to open automatically when new subscription related messages are present. Performance Improvements Improved the startup time for Snagit Capture application. Updates for IT Administrators Updated BouncyCastle.Cryptography package to address CVE-2024-29857 and CVE-2024-30172. Removed dependency on Xceed Zip. Updated LeadTools DLLs. Fixed an issue where Snagit might not recognize offline subscription activation. Fixed an issue where the video recording toolbar could be hidden from users when using Snagit in virtual environments or with remote desktop applications. Bug Fixes Fixed an issue where using Step Capture with the Share destination set to File in the Capture Window could result in data loss. Fixed a crash that could occur when capturing on some HDR monitors. Fixed an issue where the cursor object in a capture might include some of the background image in certain situations. Fixed an issue with the Box share destination to use the default browser for authentication. Fixed an issue where the Blur tool Color property could show when the blur or pixelate Type was selected. Fixed an issue where the privacy policy link in Snagit's installer might not open in the expected language. (PONRPD) Download: Snagit 64-bit | 419.0 MB (Shareware) Links: SnagIt Home Page | Release Notes Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      jfam earned a badge
      Week One Done
    • First Post
      survivor303 earned a badge
      First Post
    • Week One Done
      CHUNWEI earned a badge
      Week One Done
    • One Year In
      survivor303 earned a badge
      One Year In
    • Week One Done
      jbatch earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      420
    2. 2
      +FloatingFatMan
      185
    3. 3
      snowy owl
      183
    4. 4
      ATLien_0
      179
    5. 5
      Xenon
      140
  • Tell a friend

    Love Neowin? Tell a friend!