Search code examples
c++arraysboosteigenboost-multi-array

Defining my 2D array to support [] and () access operations


I am writing an image processing application in C++. To define my image type, I am considering using either a boost multi array, or boost ublas matrix or Eigen 2D matrix. I would like to carefully benchmark these for all various operations I intend to do and choose one accordingly.

However, I cannot afford to pause development. Hence, I would like to start writing code in such a way that it should be easy to swap my image type definition from either ublas, Eigen or multiarray to another. I don't think typedef will save me here, because the element accessor operators are different in these libraries.

For instance, you access elements of 2D array 'myArray' as follows in each of the three libraries : Boost multiarray : myArray[x][y] Boost ublas : myArray (x,y) Eigen 2DMatrix : myArray(x,y)

You can see the conflict is between [][] vs ( _ , _ ) way of accessing elements. Hence, I cannot write code for one type, and make it work with another type using a simple typedef.

Any ideas how to get around this?

I am thinking of wrapping the underlying type into a new universal type which standardizes access methodology, then I can simply swap one type for another using typedef, Are there any pitfalls I should be worried about? Will it cost me a lot of efficiency? What languages feature can exploit best here?

If you could please help me get started, I will write a code and paste it here for further review. P.S. I am not using any of the rich API of these three types. I am simply creating them, and accessing their elements.


Solution

  • I would use the Proxy Pattern for this case. You just easily wrap around the proxy and define a unique interface which will use your underlying objects. Hope this helps....

    Edit:

    I guess this link should be useful as well: Template Proxy