How to optimize setting such data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#define setCount(x) .count = x
#define setWord(x) .word = new (const char *[3])
#define setFunc(x) .func = new (void (*[3])())

void Reader::initSprites() {
    threadCount = 2;

    ready = new bool[threadCount];
    operationCount = new int[threadCount];
    operation = new (Operation (*[threadCount]));

    ///Sprite thread
    operationCount[0] = 6;
    operation[0] = new Operation[operationCount[0]];

    operation[0][0] setCount(3);
    operation[0][0] setWord (3) {"texture", "=", ""};
    operation[0][0] setFunc (3) {NULL, NULL, &Sprites::Load::setTextureFile};

    operation[0][1] setCount(3);
    operation[0][1] setWord (3) {"sizeX", "=", ""};
    operation[0][1] setFunc (3) {NULL, NULL, &Sprites::Load::setSizeX};

    operation[0][2] setCount(3);
    operation[0][2] setWord (3) {"sizeY", "=", ""};
    operation[0][2] setFunc (3) {NULL, NULL, &Sprites::Load::setSizeY};

    operation[0][3] setCount(3);
    operation[0][3] setWord (3) {"offsetX", "=", ""};
    operation[0][3] setFunc (3) {NULL, NULL, &Sprites::Load::setOffsetX};

    operation[0][4] setCount(3);
    operation[0][4] setWord (3) {"offsetY", "=", ""};
    operation[0][4] setFunc (3) {NULL, NULL, &Sprites::Load::setOffsetY};

    operation[0][5] setCount(3);
    operation[0][5] setWord (3) {"sheet", "{", "}"};
    operation[0][5] setFunc (3) {NULL, &Sprites::Sheet::Load::beginThread, &Sprites::Sheet::Load::endThread};

    ///Sheet thread
    operationCount[1] = 6;
    operation[1] = new Operation[operationCount[1]];

    operation[1][0] setCount(1);
    operation[1][0] setWord (1) {"vertical"};
    operation[1][0] setFunc (1) {&Sprites::Sheet::Load::vertical};

    operation[1][1] setCount(1);
    operation[1][1] setWord (1) {"horizontal"};
    operation[1][1] setFunc (1) {&Sprites::Sheet::Load::horizontal};

    operation[1][2] setCount(3);
    operation[1][2] setWord (3) {"firstX", "=", ""};
    operation[1][2] setFunc (3) {NULL, NULL, &Sprites::Sheet::Load::setFirstX};

    operation[1][3] setCount(3);
    operation[1][3] setWord (3) {"firstY", "=", ""};
    operation[1][3] setFunc (3) {NULL, NULL, &Sprites::Sheet::Load::setFirstY};

    operation[1][4] setCount(3);
    operation[1][4] setWord (3) {"frames", "=", ""};
    operation[1][4] setFunc (3) {NULL, NULL, &Sprites::Sheet::Load::setFrames};

    operation[1][5] setCount(3);
    operation[1][5] setWord (3) {"variations", "=", ""};
    operation[1][5] setFunc (3) {NULL, NULL, &Sprites::Sheet::Load::setVariations};
}


This procedure inits operations for reading files of Sprites type. How should I optimize it or at least make more attractive?

Second, I am going now through loop to determine operation by word read:
1
2
3
4
5
6
7
8
9
10
11
            file >> word;
            for (int i = 0; i != operationCount[thread]; i++)
            if (word == operation[thread][i].word[0]) {
                if (operation[thread][i].count != 1) {
                    code[thread] = i;
                    stage[thread] = 1;
                }
                if (operation[thread][i].func[0] != NULL)
                    operation[thread][i].func[0]();
                break;
            }
Is there better way to do this?
Yep. You should also allocate each pointer from the const char* [3] pointer.
Your use of defines is really ugly.

You haven't shown us your Operation class, but I suspect setCount, setWord and setFunc should be members of your Operation class instead of being defines.
Operation is just data struct:
1
2
3
4
5
    struct Operation {
        int count;
        const char ** word;
        void (** func)();
    };
I create each of them and just then set all values.

How could I use brackets to init then if I use setData as class member? That goes against optimization.

From OOP point, initiating operation like this is okay, because Operation is a private sub-class of Reader:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
struct Reader {
    friend struct Sprites;
    private:
        struct Operation {
            int count;
            const char ** word;
            void (** func)();
        };

        static int threadCount;
        static int * operationCount;
        static Operation ** operation;

        static std::ifstream file;
        static int thread;
        static bool * ready;

        static void checkFail();
        static void nextThread();
        static void prevThread();

    public:
        static void initSprites();
        static void deinit();

        static void read(const char * filename);
};
Or it isn't okay?

Added: I mean, if struct-owner does your init instead of constructor.
Last edited on
Compiler optimizations will probably do the same job as your defines.
Just stick to OOP, it'll be easier later on, when you need to edit your program.

Also, Reader shouldn't really be static... unless you really want a single instance of Reader, which could be bad, since you could load stuff from multiple threads at the same time if Reader wasn't static, improving loading speed.
Last edited on
So I need to make constructor with varied number of parameters?
Last edited on
No.
For something like that, making a constructor instead of set/get functions won't change much.

You should make all functions and members non-static (only the ones who shouldn't be globals).
Thank you!

qac
Topic archived. No new replies allowed.