- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将复数数据填充到 Vec<f64>
中通过外部 C 库(最好不要更改),形式为 [i_0_real, i_0_imag, i_1_real, i_1_imag, ...]
看起来这个 Vec<f64>
与 Vec<num_complex::Complex<f64>>
具有相同的内存布局鉴于 num_complex::Complex<f64>
,一半的长度将是的数据结构与 [f64; 2]
的内存布局兼容。如记录here .我想这样使用它而不需要重新分配可能很大的缓冲区。
我假设使用 from_raw_parts()
是有效的在std::vec::Vec
伪造一个新的 Vec
拥有旧 Vec
的所有权的内存(通过忘记旧的 Vec
)并使用 size / 2
和 capacity / 2
,但这需要不安全的代码。是否有一种“安全”的方式来进行这种数据重新解释?
Vec
在 Rust 中分配为 Vec<f64>
并由使用 .as_mut_ptr()
的 C 函数填充填写 Vec<f64>
.
我当前的编译不安全实现:
extern crate num_complex;
pub fn convert_to_complex_unsafe(mut buffer: Vec<f64>) -> Vec<num_complex::Complex<f64>> {
let new_vec = unsafe {
Vec::from_raw_parts(
buffer.as_mut_ptr() as *mut num_complex::Complex<f64>,
buffer.len() / 2,
buffer.capacity() / 2,
)
};
std::mem::forget(buffer);
return new_vec;
}
fn main() {
println!(
"Converted vector: {:?}",
convert_to_complex_unsafe(vec![3.0, 4.0, 5.0, 6.0])
);
}
最佳答案
Is there a "safe" way to do this kind of data re-interpretation?
没有。至少,这是因为您需要了解的信息并未在 Rust 类型系统中表达,而是通过散文(又名文档)表达:
Complex<T>
is memory layout compatible with an array[T; 2]
.
If a
Vec
has allocated memory, then [...] its pointer points tolen
initialized, contiguous elements in order (what you would see if you coerced it to a slice),—
Vec
docs
Arrays coerce to slices (
[T]
)
自 Complex
与数组内存兼容,数组的数据与切片内存兼容,Vec
的数据与切片内存兼容,此转换应该是安全的,即使编译器无法告知这一点。
此信息应附加(通过注释)到您的不安全 block 。
我会对您的函数做一些小的调整:
有两个Vec
同时指向相同的数据让我非常紧张。通过引入一些变量并在创建另一个变量之前忘记一个变量,可以轻松避免这种情况。
删除 return
关键字更地道
添加一些断言数据的起始长度是二的倍数。
作为rodrigo points out ,容量很容易是奇数。为了避免这种情况,我们调用 shrink_to_fit
.这有一个缺点,即 Vec
可能需要重新分配和复制内存,具体取决于实现情况。
展开 unsafe
block 以覆盖确保维护安全不变量所需的所有相关代码。
pub fn convert_to_complex(mut buffer: Vec<f64>) -> Vec<num_complex::Complex<f64>> {
// This is where I'd put the rationale for why this `unsafe` block
// upholds the guarantees that I must ensure. Too bad I
// copy-and-pasted from Stack Overflow without reading this comment!
unsafe {
buffer.shrink_to_fit();
let ptr = buffer.as_mut_ptr() as *mut num_complex::Complex<f64>;
let len = buffer.len();
let cap = buffer.capacity();
assert!(len % 2 == 0);
assert!(cap % 2 == 0);
std::mem::forget(buffer);
Vec::from_raw_parts(ptr, len / 2, cap / 2)
}
}
为了避免所有关于容量的担忧,您可以将切片转换为 Vec
.这也没有任何额外的内存分配。它更简单,因为我们可以“丢失”任何奇数尾随值,因为 Vec
仍然维护它们。
pub fn convert_to_complex(buffer: &[f64]) -> &[num_complex::Complex<f64>] {
// This is where I'd put the rationale for why this `unsafe` block
// upholds the guarantees that I must ensure. Too bad I
// copy-and-pasted from Stack Overflow without reading this comment!
unsafe {
let ptr = buffer.as_ptr() as *const num_complex::Complex<f64>;
let len = buffer.len();
assert!(len % 2 == 0);
std::slice::from_raw_parts(ptr, len / 2)
}
}
关于rust - 如何安全地将 Vec<f64> 重新解释为大小减半的 Vec<num_complex::Complex<f64>>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54185667/
我有一个使用 c++ 的大型代码库标题和许多std::complex对象。但现在我还想使用其他几个使用 fftw 的库( spinsfast 和 ) .不幸的是,混合这两种类型的复合体似乎与 gc
我是 Maxima 的新手,在文档中找不到如何对复数进行正式计算。当我使用未指定的变量时,Maxima 似乎假设它们是真实的: 例如,共轭(x)返回 x。 有没有办法解决这个问题? 提前致谢。 最佳答
我是学习大O表示法的新手,并想到了这个问题。复杂度 O(a * b) 的名称是什么?是线性复杂度吗?多项式?或者是其他东西。实现代码如下。 function twoInputsMult(a, b) {
我是学习大O表示法的新手,并想到了这个问题。复杂度 O(a * b) 的名称是什么?是线性复杂度吗?多项式?或者是其他东西。实现代码如下。 function twoInputsMult(a, b) {
这是我的 Complex 类,我重载了“+” class Complex(object): def __init__(self, real, imag): self.__ima
我正在使用 R5RS 标准的 Scheme 实现。 现在假设您必须找出一个元素 '(2 3 4) 是否在列表 '(1 2 3 4) 中。 至于示例,更严格地说,您希望: 1. (is-in? '(2
注意事项: 我正在使用 Apple LLVM 版本 6.0 (clang-600.0.56)(基于 LLVM 3.5svn)在 OSX 上编译 具体来说,我正在尝试从 LibIIR 编译一个整体源,这
这段fortran代码最初是用Fortran 77格式编写的(我稍后会展示它)。拿到后,我通过转换工具将其转换为f90免费格式。使用intel fortran编译器 ifort,编译和运行和以前一样好
我有一个实现用户定义的算术类型的MyType类。此类提供以下转换运算符 struct MyType { ... operator double() { return to_double
我目前正在使用 Cxx 来允许 Julia 代码与 C++ 库交互。我想做的一部分是在两个方向上有效地传递复杂数据的集合(通常是 vector )。也就是说,我想要以下内容: cv = [1 + 2i
假设我有一个名为“汽车”的实体。 我使用复杂类型来定义“引擎”部分。 [TableName("T_CAR")] public sealed class Car:IEngine { ... pu
我想使用 static_cast 将 complex 转换为 complex 。 Convert complex to complex 我正在尝试做与这篇文章相同的事情,但我需要使用 static_c
` ?
对于多项式方程求解器,最好将其模板化为任何可用类型: template class PolynomialEquation { public: private: array myEquatio
为了在 cython 中将实部与复部分开,我通常使用 complex.real 和 complex.imag 来完成这项工作。然而,这确实会在 html 输出中生成颜色为“python red”的代码
我的问题很简单: Are both "complex float" and "float complex" valid C? 两者似乎都被 gcc 接受而没有警告。 最佳答案 complex 是 co
以下声明有什么区别? 结构体 *ptr1=(结构体*)malloc(4*sizeof(结构体)); 结构体 (*ptr1)[4]=(结构体*)malloc(sizeof(结构体)); 哪个更好用? 最
我想创建一个 C++ 类的复数。这里是Complex.h(最基本的形式) #ifndef _COMPLEX #define _COMPLEX #include "TVector2.h" class C
我已经使用 Visual Studio 2012 和 NDepend 对我的解决方案进行了代码分析 对于方法 MethodA,Visual Studio 显示复杂度为 105,Ndepend 显示为
我的代码: #include using std::cin; using std::cout; using std::istream; using std::ostream; template cl
我在 swift3 中有以下代码,我正在使用 swift lint 对代码进行 linting。给出代码如下: func selectedMenuInLoggedOutState(sender
我是一名优秀的程序员,十分优秀!