1

As shown in the following figure, I have two libraries in my C++ code which use different indexing syntax, one starting with 0 another starting with 1. The C++ code is pretty sophisticated and this difference is very troublesome for me. I was wondering if anybody ran into this trouble before, and if there is a trick for this. I don't know, for example is there a trick to make them have the same indexing syntax!!?

enter image description here

enter image description here

I sloved the problem with something like this:

#define KFULL(row,col) kFull(row+1,col+1)
    AG_Matrix kFull(4,4,5.0);
    std::cout<<kFull(1,1)<<'\n';//prints 5
    std::cout<<kFull(0,0)<<'\n';//prints garbage value
    std::cout<<KFULL(0,0)<<'\n';//prints 5
    int i=int(0);int j=int(0);
    std::cout<<KFULL(i,j)<<'\n';//prints 5

Therefore every time I declare an object of AG_Matrix class, I also define a macro for that object to take care of the issue of 0-based and 1-based indexing. This solution was suggested by one of the guys below which simply worked.

Megidd
  • 7,089
  • 6
  • 65
  • 142
  • 2
    This has nothing to do with syntax. Its a choice the library developers made, and there is really not much you can do about it. Aside from wrapping them yourself to give them consisten behavior wrt 0 or 1 based indexing. – Borgleader Sep 25 '14 at 18:06
  • The real solution would be to remove the use of the 1-based matrix class from the code as much as possible. – D Drmmr Sep 25 '14 at 20:38
  • @DDrmmr Currently, I have to use both 0-based and 1-based libraries. But gradually I'm going to get rid of one of them. – Megidd Sep 25 '14 at 20:52

2 Answers2

1

It'd be best to modify the code of AG_Matrix to behave the way you want it. This has the lowest overhead in the generated code.

A workaround would be to derive from AG_Matrix and reimplement operator()(int,int). You'd then use the derived class instead of AG_Matrix, and it could be used in place of AG_Matrix when passed to other code that expects an AG_Matrix.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • Thanks. The `AG_Matrix` class is implemented inside a DLL, I don't have the source code. I'm wondering if I can still implement a derived class for the `AG_Matrix` class. – Megidd Sep 25 '14 at 18:33
  • 1
    @user3853917 Sure. It's not relevant where it's implemented. In any case, if that matrix class is any good, most of its implementation is in the header file anyway - certainly the indexing operations are (just look at the header). – Kuba hasn't forgotten Monica Sep 26 '14 at 03:45
1

Bad hack

#define KFULL(row, column) KFull(row + 1, column + 1)

KFULL(0, 0) = 4.0
Kastaneda
  • 739
  • 1
  • 8
  • 15
  • 1
    Can you add some explanation please? Answers with only code are not so helpful. – Keith Pinson Sep 25 '14 at 18:48
  • 1
    We defined a macro, that increase your indexes. Line KFULL(0, 0) after preprocessing will look like KFull(0 + 1, 0 + 1). P.S. sorry for my english, i'm learning. – Kastaneda Sep 25 '14 at 19:04
  • @Kastaneda I tested your suggestion, it worked! `#define KFULL(row,col) kFull(row+1,col+1) AG_Matrix kFull(4,4,5.0); std::cout< – Megidd Sep 25 '14 at 19:25