0

Problem Description

Hello, I'm re-writing an old program I made that solves nonograms. As part of solving it, I generate (for some of the lines) an array of all possible ways to fit the numbers in.

I want to create a piece of code, that will merge all the possible placements.

Meaning, that for example, in pseudo code:

[[0, 0, 1],
 [1, 0, 1],
 [0, 1, 1]]

OR - [1, 1, 1]
AND - [0, 0, 1]

The result of the two operations will let me know exactly where we have a fixed value. [?, ?, 1]

Assumptions

  • The solution is desired in C++.

  • The placements are stored in a vector<vector<bool>>.

  • The size of all the inner vectors is the same.

  • It doesn't matter which bitwise operation is done, just that it can be applied to all the vectors.

Possible Solution

#include <vector>
#include <iostream>

using namespace std;

int main() {
    vector<vector<bool>> options =
            {{false, false, true},
             {true,  false, true},
             {false, true,  true}};

    vector<bool> merge_and, merge_or;
    bool flag;
    int i, j;

    for (i = 0; i < options.size(); i++) {
        for (j = 0; j < options[0].size(); j++) cout << options[i][j];
        cout << endl;
    }
    cout << endl;

    for (i = 0; i < options[0].size(); i++) {
        flag = options[0][i];
        for (j = 1; j < options.size(); j++) {
            flag = flag & options[j][i];
        }
        merge_and.push_back(flag);
    }

    for (i = 0; i < options[0].size(); i++) {
        flag = options[0][i];
        for (j = 1; j < options.size(); j++) {
            flag = flag | options[j][i];
        }
        merge_or.push_back(flag);
    }

    cout << "AND ";
    for (const auto &i : merge_and) cout << i;
    cout << endl;

    cout << "OR ";
    for (const auto &i : merge_or) cout << i;
    cout << endl;
}

Output:

001
101
011

AND 001
OR 001

Further Metadata

If anyone is interested in the full implementation, once it's done and fully documented, I will upload it to my GitHub account.

Thanks in advance for the helpers and commentators.

Uriya Harpeness
  • 626
  • 1
  • 4
  • 19
  • Does this answer your question? [bitwise operations on vector](https://stackoverflow.com/questions/4048749/bitwise-operations-on-vectorbool) – Ken Y-N Sep 30 '20 at 08:28
  • 2
    Please post what you have tried so far, otherwise it is very likely to be closed due to a lack of actual code. – Ken Y-N Sep 30 '20 at 08:28
  • @KenY-N, this does not answer my question, since it's not on the vectors separately, but on all of them, per index. – Uriya Harpeness Sep 30 '20 at 08:30
  • @KenY-N, adding code example in a minute. – Uriya Harpeness Sep 30 '20 at 08:32
  • @KenY-N updated with example, please review if you can, – Uriya Harpeness Sep 30 '20 at 08:53
  • Would `std::bitset` better fit your needs? It has bitwise operators, the only requirement being that N is a compile-time constant. Or you can look at `boost::dynamic_bitset` which removes that restriction IIRC. – Tanveer Badar Sep 30 '20 at 08:58
  • @TanveerBadar I do not know the size at compile time, and do you have an example which shows how it may help me with this? – Uriya Harpeness Sep 30 '20 at 09:00
  • 1
    There is a typo in your code. You print out `merged_and` instead of `merged_or`. It gives the correct result if edited. – Vasilij Sep 30 '20 at 10:01
  • correct, fixed. – Uriya Harpeness Sep 30 '20 at 10:03
  • 1
    Since we have a working example, then I think this is now a problem for [codereview.se]. From what I understand, `std::vector` is quite slow, so posting over there people might suggest how to use `boost::dynamic_bitset` efficiently. – Ken Y-N Sep 30 '20 at 10:39

0 Answers0