在.NET Hashtable(或其他结构)中获取最接近/下一个匹配项 [英] Get closest/next match in .NET Hashtable (or other structure)

查看:58
本文介绍了在.NET Hashtable(或其他结构)中获取最接近/下一个匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作场景,其中我们有几个不同的数据表,其格式类似于以下格式:

I have a scenario at work where we have several different tables of data in a format similar to the following:

Table Name: HingeArms
Hght   Part #1       Part #2
33     S-HG-088-00   S-HG-089-00
41     S-HG-084-00   S-HG-085-00
49     S-HG-033-00   S-HG-036-00
57     S-HG-034-00   S-HG-037-00

第一列(可能还有更多列)包含按升序排序的数字数据,并表示确定要获取的数据的正确记录的范围(例如,高度<= 33,则第1部分= S-HG-088-00,高度< = 41,然后第1部分= S-HG-084-00,等等)

Where the first column (and possibly more) contains numeric data sorted ascending and represents a range to determine the proper record of data to get (e.g. height <= 33 then Part 1 = S-HG-088-00, height <= 41 then Part 1 = S-HG-084-00, etc.)

我需要查找并选择给定指定值的最接近的匹配项.例如,给定高度= 34.25,我需要在上面的集合中获得第二条记录:

I need to lookup and select the nearest match given a specified value. For example, given a height = 34.25, I need to get second record in the set above:

41     S-HG-084-00   S-HG-085-00

这些表当前存储在从CSV文件加载的数据的VB.NET哈希表缓存"中,其中哈希表的键是表名和表中一个或多个代表记录".例如,对于上表,第一条记录的哈希表添加为:

These tables are currently stored in a VB.NET Hashtable "cache" of data loaded from a CSV file, where the key for the Hashtable is a composite of the table name and one or more columns from the table that represent the "key" for the record. For example, for the above table, the Hashtable Add for the first record would be:

ht.Add("HingeArms,33","S-HG-088-00,S-HG-089-00")

这似乎不是最佳选择,我可以根据需要灵活地更改结构(高速缓存包含可直接查找的其他表中的数据...这些范围"表由于简单"而被丢弃了)).我一直在寻找Hashtable/Dictionary上的"Next"方法,以便为我提供该范围内最接近的匹配记录,但这显然在VB.NET的股票类中不可用.

This seems less than optimal and I have some flexibility to change the structure if necessary (the cache contains data from other tables where direct lookup is possible... these "range" tables just got dumped in because it was "easy"). I was looking for a "Next" method on a Hashtable/Dictionary to give me the closest matching record in the range, but that's obviously not available on the stock classes in VB.NET.

关于使用Hashtable或以其他结构完成我正在寻找的方式的任何想法吗?它必须具有高性能,因为查找通常会在代码的不同部分中被调用.任何想法将不胜感激.谢谢.

Any ideas on a way to do what I'm looking for with a Hashtable or in a different structure? It needs to be performant as the lookup will get called often in different sections of code. Any thoughts would be greatly appreciated. Thanks.

推荐答案

哈希表不是一个好的数据结构,因为项目是根据其哈希码而不是其值散布在内部数组周围的.

A hashtable is not a good data structure for this, because items are scattered around the internal array according to their hash code, not their values.

使用排序的数组

Use a sorted array or List<T> and perform a binary search, e.g.

设置:

var values = new List<HingeArm>
{
    new HingeArm(33, "S-HG-088-00", "S-HG-089-00"),
    new HingeArm(41, "S-HG-084-00", "S-HG-085-00"),
    new HingeArm(49, "S-HG-033-00", "S-HG-036-00"),
    new HingeArm(57, "S-HG-034-00", "S-HG-037-00"),
};

values.Sort((x, y) => x.Height.CompareTo(y.Height));

var keys = values.Select(x => x.Height).ToList();

查找:

var index = keys.BinarySearch(34.25);
if (index < 0)
{
    index = ~index;
}

var result = values[index];
// result == { Height = 41, Part1 = "S-HG-084-00", Part2 = "S-HG-085-00" }

这篇关于在.NET Hashtable(或其他结构)中获取最接近/下一个匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆