Override [][] operator(s)

Discussion in 'C' started by joakimk, May 23, 2008.

  1. joakimk

    joakimk New Member

    Joined:
    May 23, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    /*
    "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
    this:
    */

    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] == 0) H[j] = 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);
    ADDELEMENT(gj, i);
    }
     
  2. joakimk

    joakimk New Member

    Joined:
    May 23, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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[j] = H[j];

    to

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

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice