Home > Enterprise >  Suggestion for a pattern involving bits using bit manipulation
Suggestion for a pattern involving bits using bit manipulation

Time:02-02

I currently have a structure like this

struct foo
{
  UINT64 optionA = 0;
  UINT64 optionB = 0;
  UINT64 optionC = 0;
}

I am attempting to write a function for comparing two objects of foo based on optionA , optionB and optionC. Essentially what I would like the function to do is check if optionA is the same in both if no then return the lowest object back. If the optionA in both objects are the same then it will check optionB in both objects and would return the lowest one. If optionB are the same in both then it will look at optionC and return the lowest.

I am thinking that this could be accomplished by assigning a UINT64 no to each object. Then assigning bits based on priority to that no and then comparing each and returning back the lesser one. I am not sure how to go about that approach any suggestions to do this would be appreciated.

CodePudding user response:

The code is currently missing an operator that checks for equallity.

Example:

constexpr bool operator==(const foo& a, const foo& b) {
    return
        a.optionA == b.optionA &&
        a.optionB == b.optionB &&
        a.optionC == b.optionC;
}

With that, the below would compile (meaningfully):

using UINT64 = /* unsigned long long */; // usually

struct foo {
    UINT64 optionA = 0;
    UINT64 optionB = 0;
    UINT64 optionC = 0;
};

constexpr bool operator==(const foo& a, const foo& b) {
    return
        a.optionA == b.optionA &&
        a.optionB == b.optionB &&
        a.optionC == b.optionC;
}

int main() {
    constexpr foo a, b;
    static_assert(a == b);
}

CodePudding user response:

With C 20, default operator <=> and operator == would do the job.

struct foo
{
  UINT64 optionA = 0;
  UINT64 optionB = 0;
  UINT64 optionC = 0;

  auto operator <=>(const foo&) const = default;
  bool operator ==(const foo&) const = default;
};

else, std::tuple might help:

bool operator ==(const foo& lhs, const foo& rhs)
{
   return std::tie(lhs.optionA, lhs.optionB, lhs.optionC)
       == std::tie(rhs.optionA, rhs.optionB, rhs.optionC);
}

bool operator <(const foo& lhs, const foo& rhs)
{
   return std::tie(lhs.optionA, lhs.optionB, lhs.optionC)
        < std::tie(rhs.optionA, rhs.optionB, rhs.optionC);
}
  •  Tags:  
  • Related