[try Beta version]
Not logged in

 
assert a pair of integers in a std::array

Nov 21, 2017 at 3:41pm

How can I assert if two integers are valid given the following declarations:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

enum entry {empty, nought, cross};

class board {
public:
  board();
  
  // Returns a copy of the entry in the given row and column
  entry operator()(int row, int col) const;

  // Returns a reference to the entry in the given row and column
  entry& operator()(int row, int col);

  // being & end for iteration over cells in COLUMN-MAJOR order
  auto begin() const { return array_.begin(); }
  auto end() const { return array_.end(); }

private:
    std::array<entry, 9> array_;
};


The entry to the array is with a row and column integers.
What would be the most efficient method be for asserting if two integers (row,col) are indeed valid?

For example;
1,1 - valid
-1,1 - invalid
3,4 - invalid
3,3 - valid
for the following layout
1
2
3
4
5
6
  cout << "Row and Column positions : \n";
   << " 1,1 | 1,2 | 1,3 \n";
   << "-----+-----+---- \n";
   << " 2,1 | 2,2 | 2,3 \n";
   << "-----+-----+---- \n";
   << " 3,1 | 3,2 | 3,3 \n\n";
Last edited on Nov 21, 2017 at 3:43pm
Nov 21, 2017 at 4:09pm

There is various bittwiddling to play with, but https://godbolt.org/ indicates that

if ( i< 1 || i> 3)

is very few instructions. Bittwiddling looks to take more instructions
Nov 21, 2017 at 4:32pm
What would be the most efficient method be for asserting if two integers (row,col) are indeed valid?

this topic has been covered in the past vis-a-vis stdin input which should give some ideas:
http://www.cplusplus.com/forum/beginner/206234/
http://www.cplusplus.com/forum/beginner/211726/
Topic archived. No new replies allowed.