快速搜索颜色? [英] Searching fast through colors ?

查看:94
本文介绍了快速搜索颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想找到一种更快捷的搜索大量颜色并获取相关ID值的方法。通常,代码列在下面,它工作正常,但不幸的是很慢。我想我需要先以某种方式对列表进行排序然后搜索它(.NET二进制搜索?)



有人有什么建议吗?



非常感谢您的帮助!

 公共  MyColor 
公共 属性 ID As 整数
公开 属性 RGB As 颜色
结束

' 添加了大量颜色到列表:
Dim allMyColors 作为列表( of MyColor)= 列表( MyColor)

' 然后使用已知的R,G和B值我搜索它是否存在并获得相关的ID值

Dim mc As MyColor = allMyColors.FirstOrDefault( Function ( colorage.colorage.RGB.R = R colorage.RGB.G = G colorage.RGB。 B = B)

如果 mc IsNot 没什么 然后
返回 mc.ID
< span class =code-keyword> Else
返回 0
结束 如果

解决方案

尝试使用键入 Color 词典 ,其值为 MyColor 类或仅 ID 值。

< pre lang =vb.net> Dim allMyColors As Dictionary( Of 颜色,整数)= 字典( Of 颜色,整数)()
...
Dim id As 整数
Dim key As Color = Color.FromArgb(R,G,B)
If allMyColors.TryGetValue(key,id)然后
返回 id
Else

返回 0
结束 如果


Hi,

I am trying to find a quicker way to search through a large number of colors and get their associated ID values. Generally the code is listed below and it works fine but is unfortunately quite slow. I guess I need to sort the list somehow first and then search it (.NET binarysearch?)

Does anyone have any suggestions please?

Many thanks for any assistance!

Public Class MyColor
    Public Property ID As Integer
    Public Property RGB As Color
End Class

'  Large amount of colors added to a list:
Dim allMyColors As List(Of MyColor) = New List(Of MyColor)

' Then with a known R,G and B value I search to see if it exists and get the associated ID value

Dim mc As MyColor = allMyColors.FirstOrDefault(Function(colorage) colorage.RGB.R = R And colorage.RGB.G = G And colorage.RGB.B = B)

If mc IsNot Nothing Then
Return mc.ID
Else
Return 0
End If

解决方案

Try using a Dictionary keyed on the Color, where the value is either the MyColor class or just the ID value.

Dim allMyColors As Dictionary(Of Color, Integer) = New Dictionary(Of Color, Integer)()
...
Dim id As Integer
Dim key As Color = Color.FromArgb(R, G, B)
If allMyColors.TryGetValue(key, id) Then
    Return id
Else
    Return 0
End If


这篇关于快速搜索颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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