boris-kz / CogAlg

This project is a Computer Vision implementation of general hierarchical pattern discovery principles introduced in README
http://www.cognitivealgorithm.info
MIT License
90 stars 42 forks source link

C++ #35

Closed Twenkid closed 3 years ago

Twenkid commented 4 years ago

https://github.com/boris-kz/CogAlg/blob/d0abf04dce77298badc906fea971ad322f894c20/frame_2D_alg/alternative%20versions/frame_blobs_cpp_Mayukh/form_P_cpp#L31

double at(const double *arr, int r, int c) {
    int index = r * 4 + c;
    return arr[index];
}

==>

double at(const double *arr, unsigned int r, unsigned int c) {
    auto index = r << 2 + c;
    return arr[index];
}

==>

double at(const double *arr, unsigned int r, unsigned int c) {
    return arr[r << 2 + c];
}

... ==> Direct usage without a function call or suggesting inline.

Shift for MUL/DIV by powers of two is a well known trick, but I don't know which compilers apply it and when, also SHIFT for signed int could change the sign.

Also, in the code there's type mixing of float-double-int.

Shared ptr is double (overkill), then read to float, then it's assigned as int/long, all would involve type conversion instructions - is it unavoidable?

boris-kz commented 4 years ago

Khanh tried to set up packages to run it, it took too much time, we are not currently pursuing C++.

On Thu, Feb 6, 2020, 4:39 AM Todor Arnaudov notifications@github.com wrote:

https://github.com/boris-kz/CogAlg/blob/d0abf04dce77298badc906fea971ad322f894c20/frame_2D_alg/alternative%20versions/frame_blobs_cpp_Mayukh/form_P_cpp#L31

double at(const double arr, int r, int c) { int index = r 4 + c; return arr[index]; }

==>

double at(const double *arr, unsigned int r, unsigned int c) { auto index = r << 2 + c; return arr[index]; }

==>

double at(const double *arr, unsigned int r, unsigned int c) { return arr[r << 2 + c]; }

... ==> Direct usage without a function call or suggesting inline.

Shift for MUL/DIV by powers of two is a well known trick, but I don't know which compilers apply it and when, also SHIFT for signed int could change the sign.

Also, in the code there's type mixing of float-double-int.

Shared ptr is double (overkill), then read to float, then it's assigned as int/long, all would involve type conversion instructions - is it unavoidable?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/boris-kz/CogAlg/issues/35?email_source=notifications&email_token=AFABOGJ4PMIN53FO2L3PGBLRBPLDHA5CNFSM4KQZRFKKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4ILOPTLA, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFABOGJGFCD2AOKSKH4XTX3RBPLDHANCNFSM4KQZRFKA .