HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

boost::unordered_set intersection using templates

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
templatesusingunordered_setintersectionboost

Problem

I wrote this function to do unordered_set intersection using templates. I have seen this answer but I thought that it was overkill. I would like the method to take in 2 sets and return a set.

class SetUtilites{
public:
    template
    static boost::unordered_set intersection(const boost::unordered_set &set1, const boost::unordered_set &set2){
        if(set1.size()  iSet;

            boost::unordered_set::iterator it;
            for(it = set1.begin(); it != set1.end();++it){
                if(set2.find(*it) != set2.end()){
                    iSet.insert(*it);
                }
            }
            return iSet;
        }else{
            return intersection(set2,set1);
        }
    }
};

Solution

-
SetUtilites is misspelled; it should be SetUtilities.

-
For better readability, consider doing something about this long line:

static boost::unordered_set intersection(const boost::unordered_set &set1, const boost::unordered_set &set2)


You could shorten the types as such via typedef, but it may not help very much, or it may even make things uglier. You could also wrap the line in some way. Choose whichever works best.

-
Do you even need a class for this? It just contains one function and no data members, so it's not really doing anything relevant as one. Instead, just make this a free function (non-member function) and put it into a namespace with a similar name as the class.

-
The name iSet sort of sounds like Hungarian notation, although it appears to be a shortened form of intersectionSet. If you still don't want to call it something that lengthy, you could rename it to something like finalSet, indicating that it will be returned from the function.

Code Snippets

static boost::unordered_set<Type> intersection(const boost::unordered_set<Type> &set1, const boost::unordered_set<Type> &set2)

Context

StackExchange Code Review Q#48714, answer score: 10

Revisions (0)

No revisions yet.