Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Override [][] operator(s) (http://www.go4expert.com/forums/override-operator-s-t10757/)

joakimk 23May2008 22:41

Override [][] operator(s)
"matrix" H used to be declared in myClass.cpp, as a "global" array;
int H[m][n];

I'm trying to phase this out (for obvious reasons), and have something known
as a "Nauty graph" instead. This is basically an array of unsigned ints, where
the rows of a matrix are laid out in memory, using bits. The library/package
is written in C, but my code "on top" is C++.

I also want to move the declaration of H into myClass.h, which makes more sense.
The challenge now is to handle all the references to H[][] that exist in my code.

The neatest fix, I think, and I don't know whether this is possible, would be
to override the [][] operator(s) of a Nauty graph, for something like

int m = 5, n = 10; // actual size of matrix (50 bits)
int w = (n+WORDSIZE-1)/WORDSIZE; // w = "width" in memory (32 bits WORDSIZE on my arch)
H = new unsigned int[m * w]; // init H to desired size

for (int j = 0; j < m; j++)
for (int i = 0; i < n; i++)
if (H[j][i] == 0) H[j][i] = 1; // just an example

Here, both test and assignment are handled with the usual array [][] operators.
Right now, I've moved H into myClass.h, and changed it from "int H[N][N];" to "graph *H;"

Lines such as in the example give me lots of compiler errors (of course):
error: invalid types "unsigned int[int]" for array subscript

I would need to override [][] with something like this, using Nauty methods;

// a) test
int test(graph *g, int n, int j, int i) {
int w = (n + WORDSIZE - 1) / WORDSIZE;
set *gj;
gj = GRAPHROW(g, j, w); // row j of g
if (ISELEMENT(gj,i)) return 1;
return 0;

// b) assignment
void assn(graph *g, int n, int j, int i) {
int w = (n + WORDSIZE -1) / WORDSIZE;
set *gj;
gj = GRAPHROW(g, j, w);

joakimk 23May2008 22:45

Re: Override [][] operator(s)
Is this even possible? It would save me a whole bunch of search-replace in my code.
My alternative is to replace like this:

H[i][j] = H[j][i];


setValue(H, i, j, getValue(H, j, i));

All times are GMT +5.5. The time now is 17:38.