如何投射(和 Marshall)CityHash std::pair从 C# 到 C++反之亦然

发布于 2024-11-25 20:23:38 字数 1953 浏览 1 评论 0 原文

CityHash 允许我们生成 128 位哈希值,但整数的 128 位表示被定义为一对 uint64 (如 CityHash.h 的标头):

typedef boost::uint64_t uint64;
typedef std::pair<uint64, uint64> uint128;

我有一个 .NET 包装器,允许我调用city hash 的 64 位版本:

public ref class CityHashDotNet
{
public:
    inline static void CityHash64Batch(const char* const value, uint64* const hashes, const int numValues)
    {
        // I have a wrapper for CityHash that allows me to make batch calls (saves on interops)
        CityHashWrapper::CityHash64Batch(value, hashes, numValues);
    }
    //...
}

这使我可以通过将值内存指针转换为 sbyte* 并将哈希内存指针转换为 ulong* 轻松地从 C# 生成哈希值调用 CityHashDotNet 包装器函数:

// The MemoryPointer is an IntPtr
CityHashDotNet.CityHash64Batch((sbyte*)values.MemoryPointer, (ulong*)hashes.MemoryPointer, size);

我想围绕 128 位版本的城市哈希创建一个包装器,但我不知道如何编组 std::pair这是哈希所必需的。我定义了一个与 std::pair 匹配的类,并且我计划使用它来复制 std::pair 结构:

public class Pair<T1, T2>
{
    public T1 First { get; set; }
    public T2 Second { get; set; }

    public Pair(T1 first, T2 second)
    {
        First = first;
        Second = second;
    }
}

问题是我没有办法将 IntPtr 转换为 std::pair*ulonguint64 相同>)。我尝试将其转换为 Pair*,但出现构建错误:

// Does not work!
CityHashDotNet.CityHash128Batch((sbyte*)values.MemoryPointer, (Pair<ulong,ulong>*)hashes.MemoryPointer, size);

我收到的错误是:

error CS1503: ... cannot convert from `Pair<ulong,ulong>*` to `std.pair<unsigned __int64,unsigned __int64>*`

如何解决此问题?

CityHash allows us to generate 128-bit hashes, but the 128-bit representation of an integer is defined as a pair of uint64s (as seen in the header of CityHash.h):

typedef boost::uint64_t uint64;
typedef std::pair<uint64, uint64> uint128;

I have a .NET wrapper that allows me to call the 64-bit version of city hash:

public ref class CityHashDotNet
{
public:
    inline static void CityHash64Batch(const char* const value, uint64* const hashes, const int numValues)
    {
        // I have a wrapper for CityHash that allows me to make batch calls (saves on interops)
        CityHashWrapper::CityHash64Batch(value, hashes, numValues);
    }
    //...
}

This allows me to easily generate hashes from C# by casting the value memory pointer to sbyte* and the hash memory pointer to ulong* and calling the CityHashDotNet wrapper function:

// The MemoryPointer is an IntPtr
CityHashDotNet.CityHash64Batch((sbyte*)values.MemoryPointer, (ulong*)hashes.MemoryPointer, size);

I would like to make a wrapper around the 128-bit version of city hash, but I don't know how to Marshall the std::pair that's necessary for the hash. I defined a class that matches the std::pair and I'm planning on using it to duplicate the std::pair structure:

public class Pair<T1, T2>
{
    public T1 First { get; set; }
    public T2 Second { get; set; }

    public Pair(T1 first, T2 second)
    {
        First = first;
        Second = second;
    }
}

The problem is that I have no way to cast an IntPtr to an std::pair<ulong,ulong>* (ulong is the same as uint64). I tried casting it to Pair<ulong,ulong>*, but I get a build error:

// Does not work!
CityHashDotNet.CityHash128Batch((sbyte*)values.MemoryPointer, (Pair<ulong,ulong>*)hashes.MemoryPointer, size);

The error I get is:

error CS1503: ... cannot convert from `Pair<ulong,ulong>*` to `std.pair<unsigned __int64,unsigned __int64>*`

How do I get around this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謌踐踏愛綪 2024-12-02 20:23:38

您无法在指向这些类型的指针之间进行转换,它们没有兼容的布局。您必须在每种类型的数组之间复制数据,一次复制一个字段。在 C++/CLI 代码中执行此操作,让 C# 代码看到漂亮的 .NET 数组。

You can't cast between pointers to these types, they don't have compatible layout. You'll have to copy the data between arrays of each type, one field at a time. Do this inside the C++/CLI code, and let the C# code see nice .NET arrays.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文