• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

How would I implement this C++ class in my Lua scripting (1 Viewer)

AI_Messiah

Newcomer
Joined
Mar 15, 2021
Messages
9
Reaction score
0
I would like this function to be available to Lua scripts. I am assuming that it would be more efficient to code this in C++.

This is my header:

C++:
#pragma once
class Exchanger
{
public:
    Exchanger(bool mod);
    ~Exchanger();
    void exch_con(int* ref, int siz, int* oref, int osiz);
    void exchadd_con(int* ref1, int siz1, int* ref2, int siz2, int* oref, int osiz);
    double* exch(double* inv);
    double* exchadd(double* inv1, double* inv2);
private:
    bool mode;
    bool conf;
    int* inrefs1;
    int* inrefs2;
    int* outrefs;
    int innum1;
    int innum2;
    int outnum;
    int outsiz;
};

This is my source:

C++:
#include "exchanger.h"
#include <vector>
using namespace std;

Exchanger::Exchanger(bool mod) {
    mode = mod;
    conf = false;
}
Exchanger::~Exchanger() {
    delete[] inrefs1;
    delete[] inrefs2;
    delete[] outrefs;
}

void Exchanger::exch_con(int* ref, int siz, int* oref, int osiz) {
    vector<int> tempi;
    vector<int> tempo;
    if (mode) {
        int max = 0;
        bool nfound1;
        bool nfound2;
        int plac = 0;
        for (int i = 0; i < siz; i++) {
            if (ref > max) max = ref;
        }
        for (int i = 1; i < max + 1; i++) {
            nfound1 = true;
            for (int j = 0; j < siz && nfound1; j++) if (ref[j] == i) {
                nfound1 = false;
                tempi.push_back(j);
                nfound2 = true;
                for (int k = 0; k < osiz && nfound2; k++) if (oref[k] == i) {
                    nfound2 = false;
                    tempo.push_back(k);
                    plac++;

                }
            }
            if (nfound2 && !nfound1) throw("Mismatched symbals in Exchanger");
        }
        innum1 = plac;
        outnum = plac;
        inrefs1 = new int[plac];
        outrefs = new int[plac];
        for (int i = 0; i < plac; i++) {
            inrefs1 = tempi.at(i);
            outrefs = tempo.at(i);
        }
        conf = true;
    }
}
void Exchanger::exchadd_con(int* ref1, int siz1, int* ref2, int siz2, int* oref, int osiz) {
    if (!mode) {
        vector<int> tempi1;
        vector<int> tempi2;
        vector<int> tempo1;
        vector<int> tempo2;
        int max = 0;
        bool nfound1;
        bool nfound2;
        int plac1 = 0;
        int plac2 = 0;
        for (int i = 0; i < siz1; i++) if (ref1 > max) max = ref1;
        for (int i = 0; i < siz2; i++) if (ref2 > max) max = ref2;
        for (int i = 1; i < max + 1; i++) {
            nfound1 = true;
            for (int j = 0; j < siz1 && nfound1; j++) if (ref1[j] == i) {
                nfound1 = false;
                tempi1.push_back(j);
                nfound2 = true;
                for (int k = 0; k < osiz && nfound2; k++) if (oref[k] == i) {
                    nfound2 = false;
                    tempo1.push_back(k);
                    plac1++;

                }
            }
            if (nfound2 && !nfound1) throw("Mismatched symbals in Exchanger");
            if (nfound1) {
                for (int j = 0; j < siz2 && nfound1; j++) if (ref2[j] == i) {
                    nfound1 = false;
                    tempi2.push_back(j);
                    nfound2 = true;
                    for (int k = 0; k < osiz && nfound2; k++) if (oref[k] == i) {
                        nfound2 = false;
                        tempo2.push_back(k);
                        plac2++;

                    }
                }
                if (nfound2 && !nfound1) throw("Mismatched symbals in Exchanger");
            }
        }
        innum1 = plac1;
        outnum = plac1 + plac2;
        inrefs1 = new int[plac1];
        outrefs = new int[plac1 + plac2];
        for (int i = 0; i < plac1; i++) {
            inrefs1 = tempi1.at(i);
            outrefs = tempo1.at(i);
        }
        for (int i = 0; i < plac2; i++) {
            inrefs2 = tempi2.at(i);
            outrefs[i + plac1] = tempo2.at(i);
        }
        conf = true;
    }
}

double* Exchanger::exch(double* inv) {
    double* retv = new double[0];
    if (conf && mode) {
        retv = new double[outsiz];
        double exch;
        for (int i = 0; i < outsiz; i++) retv = 0;
        for (int i = 0; i < outsiz; i++) {
            if (conf && mode) {
                for (int i = 0; i < innum1; i++) {
                    exch = inv[inrefs1];
                    retv[outrefs] = exch;
                }
            }
        }
    }
    return retv;
}
double* Exchanger::exchadd(double* inv1, double* inv2) {
    double* retv = new double[0];
    if (conf && !mode) {
        retv = new double[outsiz];
        double exch;
        for (int i = 0; i < outsiz; i++) retv = 0;
            for (int i = 0; i < innum1; i++) {
                exch = inv1[inrefs1];
                retv[outrefs] = exch;
            }
            for (int i = 0; i < innum2; i++) {
                exch = inv2[inrefs2];
                retv[outrefs[i + innum1]] = exch;
            }
    }
    return retv;
}
 
Top