【问题标题】:How to return memory address of directory graph to use later checking the item?如何返回目录图的内存地址以供以后检查项目使用?
【发布时间】:2019-01-28 16:12:30
【问题描述】:

我想在我的 javascript 项目中使用 dawg。我使用 Emscripten 将 dawg 代码 here 转换为 webassembly。

该项目提供了一个库 dawgdic,用于构建和访问使用有向无环词图 (DAWG) 实现的字典。

dawg 是通过将 trie 最小化为确定性有限自动机 (DFA) 来构造的,因此 dawg 在内存使用方面具有优势

我参考了给定的链接以转换为 Webassemly。 here

The below code store items in dawg graph format.
    #include <cassert>
    #include <iostream>
    #include <string>
    #include <dawgdic/dawg-builder.h>
    #include <dawgdic/dictionary-builder.h>
    #include <emscripten/emscripten.h>
    using namespace std;

    #ifdef __cplusplus
    extern "C" {
    #endif
    int EMSCRIPTEN_KEEPALIVE addToDawg(void(*f)(dawgdic::Dawg *c)) {
         dawgdic::DawgBuilder dawg_builder;
  assert(dawg_builder.Insert("apple"));
  assert(dawg_builder.Insert("cherry"));
  assert(!dawg_builder.Insert("banana"));
  assert(dawg_builder.Insert("durian"));
  dawgdic::Dawg dawg;
  dawg_builder.Finish(&dawg);
        return 0;
      }

    #ifdef __cplusplus
    }
    #endif

    Here is link to see output: [here](https://demophp.digi-corp.com/nitin/dawg-builder-test.html)

    How can I return address of memory segment where dawg is stored to Javascript such that later I can call another method to check if item exist in dawg graph? like dawg_dic.Contains("apple").

【问题讨论】:

    标签: javascript emscripten webassembly


    【解决方案1】:

    听起来你想在堆上创建一个dawgdic::Dawg(使用new)然后返回它的地址。在 JS 方面,这个返回值将只是数字,然后你可以将它传递给一个接受 dawgdic::Dawq* 的函数。

    所以您的 API 可能如下所示:

    dawgdic::Dawg* createDawg() EMSCRIPTEN_KEEPALIVE;
    void addToDawg(dawgdic::Dawg *c) EMSCRIPTEN_KEEPALIVE;
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多