客观记忆比较

发布于 2024-11-07 07:21:32 字数 880 浏览 0 评论 0原文

我想编写一种算法,将一个内存块与另一个内存块进行比较并提供客观值,从而确定匹配的质量。我研究了 memcmp,它真正有用的只是确定两个内存块是否相同。我已经编写了一个递归函数来完成此任务,但它工作得不太正常。

DWORD CMemory::Compare( LPBYTE pDst, LPBYTE pSrc, DWORD len )
{
    DWORD dwDiff;

    if ( len == 0 )
    {
        dwDiff = 0;
    }
    else
    {
        dwDiff = (*pSrc - *pDst) * len; // * len is attempt to weight difference by MSB
        dwDiff += this->Compare( pSrc + 1, pDst + 1, len - 1 );
    }

    return dwDiff;
}

这个想法是,两个内存空间匹配得越紧密,返回值就越低。例如,假设有三个内存块,分别包含 Hello World 0 !Hello World 1 !Hello World 2 !,我想找出哪个内存块与候选 hello world 1 ! 是“最佳匹配”。我的想法是,我将运行 Compare 函数三次,依次将候选值与每个内存块进行比较,并且 Compare 应该返回包含 的内存块的最低值>你好世界1!。然而,它实际上所做的是返回包含 Hellow World 2 ! 的最后一个内存块的最低值。

有人对如何改进此功能有任何想法吗?谢谢。

I would like to write an algorithm that compares one memory block with another and provides an objective value, so as to determine the quality of the match. I've investigated memcmp, and all it is really useful for is to determine whether two memory blocks are identical or not. I've written a recursive function to accomplish this, but it's not working quite right.

DWORD CMemory::Compare( LPBYTE pDst, LPBYTE pSrc, DWORD len )
{
    DWORD dwDiff;

    if ( len == 0 )
    {
        dwDiff = 0;
    }
    else
    {
        dwDiff = (*pSrc - *pDst) * len; // * len is attempt to weight difference by MSB
        dwDiff += this->Compare( pSrc + 1, pDst + 1, len - 1 );
    }

    return dwDiff;
}

The idea is that the more closely the two memory spaces match, the lower the return value will be. For example, let's say there are three memory blocks containing Hello World 0 !, Hello World 1 !, and Hello World 2 !, respectively, and I would like to find out which memory block is a "best match" with candidate hello world 1 !. The idea is that I would run the Compare function three times comparing the candidate with each memory block in turn, and Compare should return the lowest value for the memory block containing Hello World 1 !. However, what it's doing in reality, is returning the lowest value for the last memory block containing Hellow World 2 !.

Does anyone have any ideas on how I can improve this function? Thanks.

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

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

发布评论

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

评论(4

·深蓝 2024-11-14 07:21:32

我认为你需要取 (*pSrc - *pDst) 的绝对值。在“Hello World 1 !”中,数字位置为 0,而在“Hello World 2 !”中你会得到 -1,并且 -1 小于 0。

另外,如果你在很长一段内存上使用它,你可能会遇到堆栈问题,所以你可能想让它迭代。

您的算法不会考虑插入或删除的字符,因为它会逐个位置进行比较。如果你担心这一点,问题就会变得更加困难。

I think you need to take the absolute value of (*pSrc - *pDst). In "Hello World 1 !", you're getting a 0 for the number position, while in "Hello World 2 !" you're getting a -1, and -1 is less than 0.

Also, if you use this on a long section of memory you could run into stack problems, so you might want to make it iterative.

Your algorithm won't account for a character inserted or deleted, since it does a position by position compare. If you're worried about that, the problem gets much harder.

獨角戲 2024-11-14 07:21:32

考虑写abs(*pSrc-*pDst)?否则,您会得到负值,该值始终低于完美匹配 (0)。

Considered writing abs(*pSrc-*pDst)? As otherwise you get negative values, which are allways lower than the perfect match (0).

︶ ̄淡然 2024-11-14 07:21:32

为了改进这一点...

提供源和目标的长度。
提供值“n”,用于比较源和目标的 n 个字节。
当源和目的地大小不同时,您需要处理这种情况,否则您将在走出终点时遇到问题。

不要使用递归,除非您正在处理非常小的内存块。
您只需使用循环即可完成相同的工作。
调用这个方法确实非常昂贵。

To improve this...

Supply a length for both the source and the destination.
Supply a value 'n', for comparing n bytes of the source and destination.
You need to handle the case when the source and destination aren't the same size, or you're going to have problems with walking off the end.

Don't use recursion, unless you're dealing with really small blocks of memory.
You can do the same work by just using a loop.
This method is really really expensive to call.

独留℉清风醉 2024-11-14 07:21:32

如果您要比较字符串,您可能需要查看 soundex

If you're comparing strings, you may want to look into soundex.

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