- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我想在 ruby 中创建我自己的动态数组类(作为培训)。这个想法是有一个 DynamicArray 类,它有一个容量(在给定时刻它可以容纳的元素数量),一个大小(在给定时刻实际被插入数组的元素数量)和一个 static_array 是固定大小的静态整数数组。每当这个 static_array 已满时,我们将创建一个容量是原始 static_array 两倍的新静态数组,并将每个元素复制到新的 static_array 中。由于 ruby 中没有静态数组,我的想法是使用 FFI https://github.com/ffi/ffi .在 c 中创建一个函数,该函数创建一个大小为 n 的静态 int 数组,然后能够在我的 ruby 程序中使用它。我对 C 知之甚少,很难理解 FFI 的文档到目前为止,这是一个 create_array.c 文件,它定义了我的 c 函数来创建一个数组。
#include<stdio.h>
int * createArray ( int size )
{
int array[size];
return 0;
}
一个 create_array.h 文件(根据我对 FFI 的理解,你需要将你的 c 函数放在一个 c 库中。):
int * createArray ( int size )
这是我的 dynamic_array.rb 文件,它将按照以下方式执行某些操作:
require 'ffi'
class DynamicArray
extend FFI::Library
ffi_lib "./create_array.h"
attach_function :create_array, [:int], :int
def initialize
@size = 0
@capacity = 1
@current_index = 0
@static_array = create_array(@capacity)
end
def add(element)
@size += 1
resize_array if @size > @capacity
@static_array[@current_index] = element
@current_index += 1
end
private
def resize_array
@capacity = @capacity*2
new_arr = create_array(@capacity)
@static_array.each_with_index do |val, index|
new_arr[index] = val
end
@static_array = new_arr
end
end
以下是添加和调整大小的一些测试:
def test_add
dynamic_arr = DynamicArray.new
dynamic_arr.add(1)
dynamic_arr.add(2)
assert_equal(1, dynamic_arr.static_array[0])
assert_equal(2, dynamic_arr.static_array[1])
end
def test_resize_array
dynamic_arr = DynamicArray.new
dynamic_arr.add(1)
dynamic_arr.add(2)
assert_equal(2, dynamic_arr.capacity)
dynamic_arr.resize_array
assert_equal(4, dynamic_arr.capacity)
assert_equal
end
你能解释一下我应该怎么做才能完成这项工作吗?
最佳答案
看来您没有正确使用 C 代码。
在create_array
C函数:
malloc
分配它的内存。 (或 alloc
家族中的一些其他函数)把它们放在一起,这就是你的 create_array.c
文件看起来像:
#include <stdlib.h> /* in order to use malloc */
int * create_array (int size){
int *a = malloc(size * sizeof(int));
return a; /* returning the pointer to the array a*/
}
和你的头文件create_array.h
:
int * create_array(int);
为了包装所有内容,您仍然需要在 ruby 接触它之前对其进行编译:
gcc -shared -o create_array.so -fPIC create_array.c
此命令使用 gcc 将您的 C 代码编译到名为 create_array.so
的共享库中来自 create_array.c
源文件。需要安装 gcc 才能工作。
最后,您可以在 ruby 中使用 C 函数,并在您的 dynamic_array.rb
中进行一些修改。 :
require 'ffi'
class DynamicArray
extend FFI::Library
ffi_lib "./create_array.so" # using the shared lib
attach_function :create_array, [:int], :pointer # receiving a pointer to the array
# rest of your code
现在,这应该可以工作了!但是您的 ruby 代码仍然存在一些问题:
@static_array = create_array(@capacity)
您收到的是指向已分配数组的 C 指针,而不是数组本身,至少在 ruby 中不是。@static_array[@current_index] = element
不会工作 NoMethodError: undefined method '[]=' for #<FFI::Pointer address=0x000055d50e798600>
void add_to_array (int * array, int index, int number){
array[index] = number;
}
attach_function :add_to_array, [:pointer, :int, :int], :void
add_to_array(@static_array, @current_index, element)
@static_array.each_with_index
你需要用 C 编写代码。关于使用 FFI 和 C 函数在 ruby 中创建动态数组类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55440686/
我正在努力通过接受 void 的 FFI 传递结构并在另一端读回它。 有问题的库是 libtsm,一个终端状态机。它允许您提供输入,然后找出输入后终端将处于哪种状态。 它将其绘制函数声明为: pub
只是为了测试目的,我在 delphi 中创建了一个小的 DLL。代码为: library MyDll; uses SysUtils, Classes, Vcl.Dialogs;
这是我一直好奇的事情:我想知道 LuaJIT 的 FFI 模块如何设法使用正确的调用约定来调用外部 native 函数,而无需在用户原型(prototype)中进行任何声明。 我尝试阅读源代码以自己解
我有一个带有函数的 C 库,在一个不透明的结构上运行,定义如下: Foo* makeFoo(); // create a new Foo Foo* dupFoo(const Foo* orig); /
我正在尝试从 purescript 调用 navigator.geolocation.getCurrentPosition javascript 函数,但遇到了两个问题。 在 javascript 中
ruby 版本 2.2.4p230 RubyGem 版本 2.7.2 已安装 Ruby-devel 和 lib64ffi-devel。 使用 64 位 OpenMandriva 3.0、urpmi 和
我正在尝试使用 FFI 将以下 JavaScript 函数导入 PureScript: function getGreeting() { return "Hi, welcome to the sh
我对 Haskell 中的 FFI 有一些疑问 我知道我必须使用语言编译指示 {-# LANGUAGE ForeignFunctionInterface #-}但是当我使用 {-# LANGUAGE
当我尝试构建 Flutter项目至 IOS 它向我显示了这个错误,我尝试清理并尝试更改项目的目录。 Launching lib/main.dart on iPhone 12 Pro Max in de
完整堆栈跟踪: /Users/galharth/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencie
我试过运行 pod install在我的 Xcode 项目中,但出现以下错误 /System/Library/Frameworks/Ruby.framework/Versions/2.6/us
我需要在节点 Electron 项目中使用模块“ffi”。我可以使用“gyp”重建它并在节点中使用库,但我不能使用“electron-rebuild”重建它并在 Electron 中使用它。 我跑了:
以下警告是什么意思,我该如何解决它的原因? Warning: Unimplemented primitive used:removeEventListener 在 [@bs.val] external
谁能告诉我一个使用带有可变参数的 C 函数(例如 printf )和 Haskell 的外部函数接口(interface)的示例?我尝试搜索 HaskellWiki,但没有找到这样的示例。 谢谢! 最
我使用 FFI 是为了在 C 中使用一个函数,该函数接受一个结构并返回相同的结构。我看到的引用说我必须使用指向这些结构的指针才能将其导入 Haskell。所以,例如。 data Bar = Bar {
我想在带有 FFI 的 Rust 中包含一个动态 C 库。 该库实际上也是用 Rust 构建的,但公开了一个 C 接口(interface),因此它也可以从其他语言中使用。当我用 cargo 构建库(
我试图在winapi上写一个包装器。我想包装接受回调函数指针的函数。 例如,考虑以下情况: // The unsafe callback type the FFI function accepts t
我正在使用 rust-bindgen 从 Rust 访问 C 库。一些函数返回指向结构的可空指针,bindgen 表示为 extern "C" { pub fn get_some_data()
介绍 我正在用 inline-c 包装一个 C 数值库;一些函数可以将回调传递给步骤例程,考虑 ODE 的优化或时间积分。 特别是在原生 C 中,使用回调可以对连续数组进行操作,通过指针修改它们,并将
fn main() { let val = 0; unsafe { foo(&val) } } extern "C" { pub fn foo(val: *const u32)
我是一名优秀的程序员,十分优秀!