CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2012
    Posts
    9

    How to program boolean union and intersection in C++?

    It involves some discrete mathematics.
    Any code snippet to get me started?
    Thanks
    Jack

  2. #2
    Join Date
    May 2012
    Posts
    9

    Re: How to program boolean union and intersection in C++?

    Or just union and intersection without boolean...

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How to program boolean union and intersection in C++?

    You might want to elaborate. I am not familiar with the terms "boolean union" and "boolean intersection", and a quick search brings up constructive solid geometry, which may or may not be what you are trying to ask about.

    If you are talking about sets in general: if you have two containers of elements that are sorted, then you can use std::set_union and std::set_intersection to obtain the union and intersection, treating these containers as sets.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Jul 2013
    Posts
    576

    Re: How to program boolean union and intersection in C++?

    Quote Originally Posted by luckie7456969 View Post
    It involves some discrete mathematics.
    Any code snippet to get me started?
    One very efficient set representation is the bitset. Here the individual bits of an integer are used to represent set elements. If the n'th bit of the integer is set then the n'th element of the set is present, otherwise not.

    Set operations can be performed very efficiently by basic bitwise operations. This is sometimes referred to as bit-fiddling. Basically the whole book 4A of Knuth's The Art of Computer Programming series is devoted to this. But to get going you only need the very basics, like here,

    http://www.catonmat.net/blog/low-lev...ely-must-know/

    Union and intersection becomes,

    int setA = .....;
    int setB = .....;
    int unionAB = setA | setB;
    int intersectionAB = setA & setB;

    To set the n'th bit you do,

    setA |= (1<<n);

    and to clear it,

    setA &= ~(1<<n);

    This is just the beginning and it goes on and on. It's quite amazing how many clever bitset algorithms there are available. My all time favorite is the so called Gosper's hack which allows you to efficiently generate all k-subsets of an n-set.

    The drawback of bitsets is that they're limited by the widest integer primitive type available which today typically is 64 bit (a long long). So in practice 64 elements are the biggest sets that can be handled by way of bitsets.
    Last edited by razzle; June 22nd, 2014 at 10:49 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured