ritter
ritter

Reputation: 7699

Creating a composite type from two enum classes, ready for STL map

I would like to create a composite type out of two enum classes.

enum class Color {RED, GREEN, BLUE};
enum class Shape {SQUARE, CIRCLE, TRIANGLE};

class Object {
  Color color;
  Shape shape;
public:
};

In order to use Object in an STL container like std::map<> I would need to overload the less-than operator. However, in order to flatten both enum classes into one linear index I somehow need the number of elements (NoE) of the enum classes:

friend bool operator< (const Object &lhs, const Object &rhs) {
  return NoE(Shape)*lhs.color+lhs.shape < NoE(Shape)*rhs.color+rhs.shape;
}

How can this be done without entering the same information (number of elements) in two places in the program in a nice way? (Nice way means no FIRST_ELEMENT, LAST_ELEMENT, preprocessor magic, etc.)

Question (Number of elements in an enum) is similar but does not address enum classes.

I would like to know what is the best way to implement this kind of composite types in C++11. Is the enum class definition strong enough, or is it necessary to say:?

enum class Color {RED=0, GREEN=1, BLUE=2};
enum class Shape {SQUARE=0, CIRCLE=1, TRIANGLE=2};

Upvotes: 11

Views: 1170

Answers (6)

Dr_Sam
Dr_Sam

Reputation: 1878

What you are trying to express is that to determine the order of your Objects, you first need to compare the color, and then check the shape in case the color was the same. Instead of linearizing that, if would simply use boolean operators.

friend bool operator< (const Object &lhs, const Object &rhs) 
{
   return ( (lhs.color < rhs.color) 
           || ( (lhs.color == rhs.color ) && ( lhs.shape < rhs.color) ) )
}

EDIT: Actually, you can also use an upper bound for the number of objects, the behaviour will be the same:

friend bool operator< (const Object &lhs, const Object &rhs) {
  return 10000*lhs.color+lhs.shape < 10000*rhs.color+rhs.shape;
}

but that introduces a "magic number" (so not such a good idea).

Upvotes: 2

Timothy Shields
Timothy Shields

Reputation: 79441

Consider using simply std::tuple<Color, Shape> as the "composite enum." This will come with comparison operators already defined for you, using a dictionary ordering. For example, valid code:

bool b = std::make_tuple(Color::RED, Shape::CIRCLE)
       < std::make_tuple(Color::GREEN, Shape::SQUARE);

Upvotes: 7

hmjd
hmjd

Reputation: 121961

As commented and as already stated by others, give precedence to either Shape or Color in the operator< and only compare the other if the first is equal. An alternative implementation for operator< using std::tie:

#include <tuple>
friend bool operator<(const Object& lhs, const Object& rhs)
{
    return std::tie(lhs.color, lhs.shape) < std::tie(rhs.color, rhs.shape);
}

Upvotes: 15

huysentruitw
huysentruitw

Reputation: 28091

You only need to compare shape if color is the same for both.

Using a ternary you can make it look nice too:

friend bool operator< (const Object &lhs, const Object &rhs) {
     return lhs.color == rhs.color ? (lhs.shape < rhs.shape)
                                   : (lhs.color < rhs.color);
}

Upvotes: 2

You don't need a linear index, you can simply compare them lexicographically:

friend bool operator< (const Object &lhs, const Object &rhs) {
  if (lhs.color < rhs.color) return true;
  else if (lhs.color > rhs.color) return false;
  else return lhs.shape < rhs.shape;
}

Upvotes: 4

zakinster
zakinster

Reputation: 10688

That's a good question, but you don't actually need the number of Color to compare them :

friend bool operator< (const Object &lhs, const Object &rhs) {
    if(lhs.color > rhs.color) {
        return false;
    }
    if(lhs.color < rhs.color) { 
        return true;
    }
    return lhs.shape < rhs.shape;
}

Upvotes: 2

Related Questions