如何构建查找表? [英] How to build a look up table?

查看:45
本文介绍了如何构建查找表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#中执行此操作?


我想通过邮政编码(整数)或

手机查找城市查询表(哈希)数字(字符串),它看起来像


x = book [94555]; // x ==" Fremont"

x = book [" 510-818-8888"]; // x ==" Fremont"

x = book [10002]; // x ==" Manhattan"

x = book [" 212-966-1234"]; // x ==曼哈顿


谢谢,

P

How to do this in C#?

I want to have a lookup table (hash) of city by zip code (integer) or
phone number (string), and it would look something like

x = book[94555]; // x == "Fremont"
x = book["510-818-8888"]; // x == "Fremont"
x = book[10002]; // x == "Manhattan"
x = book["212-966-1234"]; // x == "Manhattan"

Thanks,
P

推荐答案

Podi写道:
Podi wrote:

如何在C#中执行此操作?


我想要一个按邮政编码查询表(哈希)(整数)或

电话号码(字符串),它看起来像


x = book [94555 ]。 // x ==" Fremont"

x = book [" 510-818-8888"]; // x ==" Fremont"

x = book [10002]; // x ==" Manhattan"

x = book [" 212-966-1234"]; // x ==" Manhattan"
How to do this in C#?

I want to have a lookup table (hash) of city by zip code (integer) or
phone number (string), and it would look something like

x = book[94555]; // x == "Fremont"
x = book["510-818-8888"]; // x == "Fremont"
x = book[10002]; // x == "Manhattan"
x = book["212-966-1234"]; // x == "Manhattan"



有多种方法可以做到这一点。


A)book是HashTable。你填写它就像


book [94555] =" Fremont";

book [" 510-818-8888"] =" Fremont" ;


你读它就像


string x =(string)book [94555]; // x ==" Fremont"

x =(字符串)book [" 510-818-8888"]; // x ==" Fremont"


B)book是Dictionary< string,object>。你填写它就像


book [94555] =" Fremont";

book [" 510-818-8888"] =" Fremont" ;


和你一样阅读


//无需演员

string x = book [94555] ; // x ==" Fremont"

x = book [" 510-818-8888"]; // x ==" Fremont"


C)book是Dictionary< AddressKey,object>。你填写它就像


book [new AddressKey(94555)] =" Fremont";

book [new AddressKey(" 510-818-8888" ;)] =弗里蒙特;


你读它就像


string x = book [new AddressKey(94555)]; // x ==" Fremont"

x = book [new AddressKey(" 510-818-8888")]; // x ==" Fremont"

(C)涉及更多代码,你基本上会复制

的逻辑相同(A)和(B)免费获得 - 盒装int永远不会像字符串一样和b $ b $相同。但是,有可能盒装

整数不会以有效的方式覆盖Equals,它们会按字节顺序进行比较,而AddressKey.Equals可能会更快。我想

我应该做一些实验,但我怀疑(C)会明显地增加b / b



我'和(B)一起去,除非我不得不坚持1.1。


-


..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net

您需要了解的内容。

There''s a range of ways to do it.

A) book is a HashTable. You populate it like

book[94555] = "Fremont";
book["510-818-8888"] = "Fremont";

and you read it like

string x = (string) book[94555]; // x == "Fremont"
x = (string) book["510-818-8888"]; // x == "Fremont"

B) book is a Dictionary<string, object>. You populate it like

book[94555] = "Fremont";
book["510-818-8888"] = "Fremont";

and you read it like

// no cast needed
string x = book[94555]; // x == "Fremont"
x = book["510-818-8888"]; // x == "Fremont"

C) book is a Dictionary<AddressKey, object>. You populate it like

book[new AddressKey(94555)] = "Fremont";
book[new AddressKey("510-818-8888")] = "Fremont";

and you read it like

string x = book[new AddressKey(94555)]; // x == "Fremont"
x = book[new AddressKey("510-818-8888")]; // x == "Fremont"

(C) involves a lot more code, and you''ll be essentially duplicating
the same logic that (A) and (B) get for free - a boxed int is never
the same as a string and all that. It is possible, however, boxed
integers don''t override Equals in an efficient way, that they do a
bytewise comparison, and AddressKey.Equals might be faster. I suppose
I should do some experiments, but I doubt that (C) will be noticeably
faster.

I''d go with (B), unless I had to stick to 1.1 still.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.


您好,


创建一个业务对象来封装数据:


级城市

{

//使用公共财产

public int Zip;

公共字符串电话;

}


这取决于你是想要提高性能还是更少的内存

脚印。你必须做出权衡取舍。如果你在2.0框架上使用

并且想要占用一小部分内存而牺牲了b / b
的性能(尽管它对于相对较大的<无论如何都不重要br />
数据集,具体取决于搜索的执行频率):


class CityList

:System.Collections.ObjectModel。收藏< City>

{

public new City this [int zip]

{

get

{

foreach(此城市)

if(city.Zip == zip)

返回城市;


抛出新的KeyNotFoundException(

"未找到指定zip的城市:+ + zip);

}

}


公共城市[字符串电话]

{

get

{

foreach(此城市)

if(city.Phone == phone)

返回城市;


抛出新的KeyNotFoundException(

"未找到指定phon的城市e: +电话);

}

}

}


如果你想提高性能(以内存为代价)你可以在CityList类本身内维护两个已排序的索引(List< T>)和

显式实现IList,而不是从Collection< l继承它。 T> ;.一个

列表< Tinstance可以按邮编排序,另一个可以通过电话排序(调用

排序方法),在每个相应的索引器中你可以调用BinarySearch

而不是迭代集合中的每个项目。在一个列表上执行的任何操作

也必须在另一个列表上执行。例如,清除,

添加,插入,删除。


-

Dave Sexton


Podi < po ***** @ gmail.comwrote in message

news:11 ********************** @ 80g2000cwy。 googlegro ups.com ...
Hi,

Create a business object to encapsulate the data:

class City
{
// use public properties instead
public int Zip;
public string Phone;
}

It depends on whether you want increased performance or less of a memory
footprint. There''s a trade-off you''ll have to make there. If you''re using
the 2.0 framework and want a small memory footprint at the expense of
performance (although it shouldn''t really matter anyway for relatively large
sets of data, depending on how frequently searches will be performed):

class CityList
: System.Collections.ObjectModel.Collection<City>
{
public new City this[int zip]
{
get
{
foreach (City city in this)
if (city.Zip == zip)
return city;

throw new KeyNotFoundException(
"City not found with specified zip: " + zip);
}
}

public City this[string phone]
{
get
{
foreach (City city in this)
if (city.Phone == phone)
return city;

throw new KeyNotFoundException(
"City not found with specified phone: " + phone);
}
}
}

If you want to increase performance (at the expense of memory) you can
maintain two sorted indexes (List<T>) within the CityList class itself and
implement IList explicitly instead of inheriting it from Collection<T>. One
List<Tinstance could be sorted by zip and the other by phone (call the
Sort method), and in each respective indexer you could call BinarySearch
instead of iterating over every item in the collection. Any operation
performed on one list must be performed on the other as well. e.g., Clear,
Add, Insert, Remove.

--
Dave Sexton

"Podi" <po*****@gmail.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...

如何在C#中执行此操作?


我想要一个查找表(哈希)城市的邮政编码(整数)或

电话号码(字符串),它看起来像


x = book [94555]; // x ==" Fremont"

x = book [" 510-818-8888"]; // x ==" Fremont"

x = book [10002]; // x ==" Manhattan"

x = book [" 212-966-1234"]; // x ==" Manhattan"


谢谢,

P
How to do this in C#?

I want to have a lookup table (hash) of city by zip code (integer) or
phone number (string), and it would look something like

x = book[94555]; // x == "Fremont"
x = book["510-818-8888"]; // x == "Fremont"
x = book[10002]; // x == "Manhattan"
x = book["212-966-1234"]; // x == "Manhattan"

Thanks,
P



嗨Jon,
Hi Jon,

B)book是Dictionary< string,object>。你把它填充为
B) book is a Dictionary<string, object>. You populate it like



我想你的意思是,Dictionary< object,string :)


-

Dave Sexton


" Jon Shemitz" < jo*@midnightbeach.com写信息

news:45 *************** @ midnightbeach.com ...

I think you mean, Dictionary<object, string:)

--
Dave Sexton

"Jon Shemitz" <jo*@midnightbeach.comwrote in message
news:45***************@midnightbeach.com...


Podi写道:
Podi wrote:

>如何在C#中执行此操作?

我想要一个通过邮政编码(整数)或
电话号码(字符串)查找城市的查询表(哈希),它看起来像是

x = book [94555]; // x ==" Fremont"
x = book [" 510-818-8888"]; // x ==" Fremont"
x = book [10002]; // x ==" Manhattan"
x = book [" 212-966-1234"]; // x ==" Manhattan"
>How to do this in C#?

I want to have a lookup table (hash) of city by zip code (integer) or
phone number (string), and it would look something like

x = book[94555]; // x == "Fremont"
x = book["510-818-8888"]; // x == "Fremont"
x = book[10002]; // x == "Manhattan"
x = book["212-966-1234"]; // x == "Manhattan"



有很多方法可以做到这一点。


A)book是HashTable。你填写它就像


book [94555] =" Fremont";

book [" 510-818-8888"] =" Fremont" ;


你读它就像


string x =(string)book [94555]; // x ==" Fremont"

x =(字符串)book [" 510-818-8888"]; // x ==" Fremont"


B)book是Dictionary< string,object>。你填写它就像


book [94555] =" Fremont";

book [" 510-818-8888"] =" Fremont" ;


和你一样阅读


//无需演员

string x = book [94555] ; // x ==" Fremont"

x = book [" 510-818-8888"]; // x ==" Fremont"


C)book是Dictionary< AddressKey,object>。你填写它就像


book [new AddressKey(94555)] =" Fremont";

book [new AddressKey(" 510-818-8888" ;)] =弗里蒙特;


你读它就像


string x = book [new AddressKey(94555)]; // x ==" Fremont"

x = book [new AddressKey(" 510-818-8888")]; // x ==" Fremont"

(C)涉及更多代码,你基本上会复制

的逻辑相同(A)和(B)免费获得 - 盒装int永远不会像字符串一样和b $ b $相同。但是,有可能盒装

整数不会以有效的方式覆盖Equals,它们会按字节顺序进行比较,而AddressKey.Equals可能会更快。我想

我应该做一些实验,但我怀疑(C)会明显地增加b / b



我'和(B)一起去,除非我不得不坚持1.1。


-


适用于Delphi程序员的.NET 2.0
www.midnightbeach.com/.net

你需要知道什么。


There''s a range of ways to do it.

A) book is a HashTable. You populate it like

book[94555] = "Fremont";
book["510-818-8888"] = "Fremont";

and you read it like

string x = (string) book[94555]; // x == "Fremont"
x = (string) book["510-818-8888"]; // x == "Fremont"

B) book is a Dictionary<string, object>. You populate it like

book[94555] = "Fremont";
book["510-818-8888"] = "Fremont";

and you read it like

// no cast needed
string x = book[94555]; // x == "Fremont"
x = book["510-818-8888"]; // x == "Fremont"

C) book is a Dictionary<AddressKey, object>. You populate it like

book[new AddressKey(94555)] = "Fremont";
book[new AddressKey("510-818-8888")] = "Fremont";

and you read it like

string x = book[new AddressKey(94555)]; // x == "Fremont"
x = book[new AddressKey("510-818-8888")]; // x == "Fremont"

(C) involves a lot more code, and you''ll be essentially duplicating
the same logic that (A) and (B) get for free - a boxed int is never
the same as a string and all that. It is possible, however, boxed
integers don''t override Equals in an efficient way, that they do a
bytewise comparison, and AddressKey.Equals might be faster. I suppose
I should do some experiments, but I doubt that (C) will be noticeably
faster.

I''d go with (B), unless I had to stick to 1.1 still.

--

.NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.



这篇关于如何构建查找表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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