Project

General

Profile

Statistics
| Branch: | Revision:

cool / src / lib / gmlmip_stub.c @ 7c4d2eb4

History | View | Annotate | Download (2.52 KB)

1
#define __STDC_LIMIT_MACROS
2
#define __STDC_FORMAT_MACROS
3

    
4
#include <utility> // for pair
5
#include <vector>
6
#include <set>
7

    
8
extern "C" {
9
  #include <caml/mlvalues.h>
10
  #include <caml/alloc.h>
11
  #include <caml/memory.h>
12
  #include <caml/custom.h>
13
}
14

    
15
#include "GMLMIP-0.1/onestep.h"
16

    
17
//
18
using namespace std;
19

    
20
extern "C" {
21
  CAMLprim value gmlRules_stub(value modalities);
22
}
23

    
24
template<class T>
25
value set2CamlList(const set<T>& vec, value (*f)(const T&)) {
26
    CAMLlocal2( cli, cons );
27
    cli = Val_emptylist;
28

    
29
    for (typename set<T>::const_reverse_iterator it = vec.rbegin();
30
         it != vec.rend();
31
         ++it)
32
    {
33
        cons = caml_alloc(2, 0);
34

    
35
        Store_field( cons, 0, f(*it) );  // head
36
        Store_field( cons, 1, cli );     // tail
37

    
38
        cli = cons;
39
    }
40

    
41
    return cli ;
42
}
43

    
44

    
45

    
46
static CAMLprim value pair2caml(const int& t) {
47
    CAMLlocal1( abc );
48
    abc = caml_alloc(2, 0);
49
    bool neg = t < 0;
50
    Store_field( abc, 0, Val_int((neg ?(-t) : t) - 1));
51
    Store_field( abc, 1, Val_bool(!neg));
52
    return abc;
53
}
54

    
55
static CAMLprim value set2caml(const set<int>& vt) {
56
    return set2CamlList(vt, pair2caml);
57
}
58

    
59
static CAMLprim value setset2caml(const set<set<int> >& vt) {
60
    return set2CamlList(vt, set2caml);
61
}
62

    
63
static void printModVec(FILE* f, const vector<pair<pair<bool,int>,int> >& modvec) {
64
    fprintf(f, "GML-Input: /\\ (");
65
    for (int i = 0; i < modvec.size(); i++) {
66
        bool isdia = modvec[i].first.first;
67
        int cnt = modvec[i].first.second;
68
        int underl = modvec[i].second;
69
        if (i > 0) fprintf(f, ",");
70
        if (isdia) {
71
            fprintf(f, " <%d> p%d", cnt, underl);
72
        } else {
73
            fprintf(f, " [%d] p%d", cnt, underl);
74
        }
75
    }
76
    fprintf(f, " )\n");
77
}
78

    
79
CAMLprim value gmlRules_stub(value modalities) {
80
    // parse caml-modalities to a C++ vector
81
    CAMLparam1( modalities );
82
    value block = modalities;
83
    vector<pair<pair<bool,int>,int> > modvec;
84
    while (Is_block(block)) {
85
        CAMLlocal3( vpos, vcnt, vformula );
86
        vpos = Field(Field(block, 0), 0);
87
        vcnt = Field(Field(block, 0), 1);
88
        vformula = Field(Field(block, 0), 2);
89
        modvec.push_back(make_pair(make_pair(
90
            Bool_val(vpos),
91
            Int_val(vcnt)),
92
            1+Int_val(vformula)));
93
        block = Field(block, 1);
94
    }
95
    // Do one rule step and save result in rulevec
96
    GMLConclusion rulevec = gmlmip_onestep_gml(modvec);
97
    // convert rulevec into ocaml list of pairs
98
    CAMLlocal1( res );
99
    res = set2CamlList(rulevec, setset2caml);
100
    CAMLreturn( res );
101
}
102