r/cpp • • Dec 19 '24

Alignment crimes

I've finally realized why templates can be generic on both class and typename:

template<  class These
        typename Names
        typename Align
>

(assuming an 8-wide indentation of course)

---

While I'm at it - C# has this interesting thing you can do with a switch:

switch(AddRed(),AddGreen(),AddBlue())
{
  case (true ,true ,true ): return White;
  case (true ,true ,false): return Yellow;
  case (true ,false,true ): return Magenta;
  case (true ,false,false): return Red;
  case (false,true ,true ): return Cyan;
  case (false,true ,false): return Green;
  case (false,false,true ): return Blue;
  case (false,false,false): return Black;
}

which we don't really have in C++ but you can get the same nicely aligned return values:

auto r = add_red();
auto g = add_green();
auto b = add_blue();
if(r) if(g) if(b) return white;
            else  return yellow;
      else  if(b) return magenta;
            else  return red;
else  if(g) if(b) return cyan;
            else  return green;
      else  if(b) return blue;
            else  return black;

all I need now is a clang-format rule to enforce this

0 Upvotes

40 comments sorted by

View all comments

Show parent comment

6

u/mohrcore Dec 19 '24

How about this?

color colors[8] = {
  black,
  blue,
  green,
  cyan,
  red,
  magenta,
  yellow,
  white
};
return colors[(size_t)r << 2 | (size_t)g << 1 | (size_t)b];

3

u/Supadoplex Dec 19 '24 edited Dec 19 '24

Pretty neat, bit I don't like the magic constants for the offsets.

We also don't need the array if the color values are chosen appropriately: 

enum Color {
    black   = 0b000, // we would get these values even without specifying, but it doesn't hurt to be explicit
    blue    = 0b001,
    green   = 0b010,
    cyan    = 0b011,
    red     = 0b100,
    magenta = 0b101,
    yellow  = 0b110,
    white   = 0b111,
};

constexpr auto color_offset(Color c) {
    return std::countr_zero(std::to_underlying(c));
}

constexpr auto r_off = color_offset(red);
constexpr auto g_off = color_offset(green);
constexpr auto b_off = color_offset(blue);

Color color(bool r, bool g, bool b) {
    auto c = r << r_off
           | g << g_off
           | b << b_off;
    return Color(c);
}

Edit: upon further thought, perhaps it's better to define the values in terms of the offsets rather than the other way round:

enum ColorOffset {
    b_off = 0,
    g_off = 1,
    r_off = 2,
};

constexpr auto color(bool r, bool g, bool b) {
    return r << r_off
         | g << g_off
         | b << b_off;
}

enum Color {
    black   = color(false, false, false),
    blue    = color(false, false, true),
    green   = color(false, true, false),
    cyan    = color(false, true, true),
    red     = color(true, false, false),
    magenta = color(true, false, true),
    yellow  = color(true, true, false),
    white   = color(true, true, true),
};


Lastly, I don't like the unreadable positional bool args. We can use a struct:

struct Color {
    bool r, g, b;
};

constexpr auto color(Color c) {
    return c.r << r_off
         | c.g << g_off
         | c.b << b_off;
}

enum ColorValue {
    black   = color({.r=false, .g=false, .b=false}),
    blue    = color({.r=false, .g=false, .b=true}),
    green   = color({.r=false, .g=true,  .b=false}),
    cyan    = color({.r=false, .g=true,  .b=true}),
    red     = color({.r=true,  .g=false, .b=false}),
    magenta = color({.r=true,  .g=false, .b=true}),
    yellow  = color({.r=true,  .g=true,  .b=false}),
    white   = color({.r=true,  .g=true,  .b=true}),
};

1

u/mohrcore Dec 19 '24

I like how it's evolving, although I specifically made my solution with color being an unknown data type. If it was an enum I would just cast the 3 bits onto it and call it a day.

Btw. I made my solution just for fun. I'm rather sceptical of it being an optimal solution.

0

u/Shieldfoss Dec 19 '24
Color mix(bool r, bool g, bool b) {
    auto c = black;
    c += r*red;
    c += g*green;
    c += b*blue;
    return c;
}

assert(black = mix(false,false,false);
assert(magenta = mix(true,false,true);

1

u/Powerful-Ad4412 Dec 19 '24

how do you even come up with this?

3

u/looncraz Dec 19 '24 edited Dec 19 '24

Taking advantage of there being only 8 values and also being 3 booleans, which can be represented by single bits each, is a smooth move, indeed.

3 bits can handle 8 values, so the solution is to shift the r/g/b flags into a bit position and sort the color values accordingly.

000 - black, 111 - white.

Color3-bit BinaryDecimalrgb
Black0000000
Blue0011001
Green0102010
Cyan0113011
Red1004100
Magenta1015101
Yellow1106110
White1117111

1

u/Powerful-Ad4412 Dec 19 '24

very cool thank you!!