Skip to content
Snippets Groups Projects
gmlmip_stub.cc 2.02 KiB
Newer Older
  • Learn to ignore specific revisions
  • Thorsten Wißmann's avatar
    Thorsten Wißmann committed
    #define __STDC_LIMIT_MACROS
    #define __STDC_FORMAT_MACROS
    
    #include <utility> // for pair
    #include <vector>
    
    #include <set>
    
    Thorsten Wißmann's avatar
    Thorsten Wißmann committed
    
    extern "C" {
      #include <caml/mlvalues.h>
      #include <caml/alloc.h>
      #include <caml/memory.h>
      #include <caml/custom.h>
    }
    
    #include "GMLMIP-0.1/onestep.h"
    
    //
    using namespace std;
    
    extern "C" {
      CAMLprim value gmlRules_stub(value modalities);
    }
    
    template<class T>
    
    value set2CamlList(const set<T>& vec, value (*f)(const T&)) {
    
    Thorsten Wißmann's avatar
    Thorsten Wißmann committed
        CAMLlocal2( cli, cons );
        cli = Val_emptylist;
    
    
        for (typename set<T>::const_reverse_iterator it = vec.rbegin();
             it != vec.rend();
             ++it)
        {
    
    Thorsten Wißmann's avatar
    Thorsten Wißmann committed
            cons = caml_alloc(2, 0);
    
    
            Store_field( cons, 0, f(*it) );  // head
            Store_field( cons, 1, cli );     // tail
    
    Thorsten Wißmann's avatar
    Thorsten Wißmann committed
    
            cli = cons;
        }
    
        return cli ;
    }
    
    
    
    
    static CAMLprim value pair2caml(const int& t) {
    
    Thorsten Wißmann's avatar
    Thorsten Wißmann committed
        CAMLlocal1( abc );
        abc = caml_alloc(2, 0);
    
        bool neg = t < 0;
        Store_field( abc, 0, Val_int(neg ?(-t) : t));
        Store_field( abc, 1, Val_bool(!neg));
    
    Thorsten Wißmann's avatar
    Thorsten Wißmann committed
        return abc;
    }
    
    
    static CAMLprim value set2caml(const set<int>& vt) {
        return set2CamlList(vt, pair2caml);
    
    static CAMLprim value setset2caml(const set<set<int> >& vt) {
        return set2CamlList(vt, set2caml);
    }
    
    
    
    Thorsten Wißmann's avatar
    Thorsten Wißmann committed
    CAMLprim value gmlRules_stub(value modalities) {
        // parse caml-modalities to a C++ vector
        CAMLparam1( modalities );
        value block = modalities;
        vector<pair<pair<bool,int>,int> > modvec;
        while (Is_block(block)) {
            CAMLlocal3( vpos, vcnt, vformula );
            vpos = Field(Field(block, 0), 0);
            vcnt = Field(Field(block, 0), 1);
            vformula = Field(Field(block, 0), 2);
            modvec.push_back(make_pair(make_pair(
                Bool_val(vpos),
                Int_val(vcnt)),
                Int_val(vformula)));
    
            block = Field(block, 1);
    
    Thorsten Wißmann's avatar
    Thorsten Wißmann committed
        }
        // Do one rule step and save result in rulevec
        GMLConclusion rulevec = gmlmip_onestep_gml(modvec);
        // convert rulevec into ocaml list of pairs
        CAMLlocal1( res );
    
        res = set2CamlList(rulevec, setset2caml);
    
    Thorsten Wißmann's avatar
    Thorsten Wißmann committed
        CAMLreturn( res );
    }