473,809 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to build a look up table?

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

Dec 9 '06 #1
16 32382
Podi wrote:
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"
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<stri ng, 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<Addr essKey, object>. You populate it like

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

and you read it like

string x = book[new AddressKey(9455 5)]; // 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.Equa ls 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.
Dec 9 '06 #2
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.Collecti ons.ObjectModel .Collection<Cit y>
{
public new City this[int zip]
{
get
{
foreach (City city in this)
if (city.Zip == zip)
return city;

throw new KeyNotFoundExce ption(
"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 KeyNotFoundExce ption(
"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.goog legroups.com...
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

Dec 9 '06 #3
Hi Jon,
B) book is a Dictionary<stri ng, object>. You populate it like
I think you mean, Dictionary<obje ct, string:)

--
Dave Sexton

"Jon Shemitz" <jo*@midnightbe ach.comwrote in message
news:45******** *******@midnigh tbeach.com...
Podi wrote:
>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"

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<stri ng, 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<Addr essKey, object>. You populate it like

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

and you read it like

string x = book[new AddressKey(9455 5)]; // 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.Equa ls 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.

Dec 9 '06 #4
Hi,

Actually, I like Jon's solutions better, although adding a business object
to encapsulate "City" might not be such a bad idea :)

--
Dave Sexton

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:uS******** ******@TK2MSFTN GP03.phx.gbl...
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.Collecti ons.ObjectModel .Collection<Cit y>
{
public new City this[int zip]
{
get
{
foreach (City city in this)
if (city.Zip == zip)
return city;

throw new KeyNotFoundExce ption(
"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 KeyNotFoundExce ption(
"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.goog legroups.com...
>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


Dec 9 '06 #5
Dave Sexton wrote:
B) book is a Dictionary<stri ng, object>. You populate it like

I think you mean, Dictionary<obje ct, string:)
Not again!

OK, time to volunteer for Soylent Green conversion.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
Dec 9 '06 #6
Prodi,

I like the often missed sortedlist for this.

http://msdn.microsoft.com/library/de...classtopic.asp

Cor

"Podi" <po*****@gmail. comschreef in bericht
news:11******** **************@ 80g2000cwy.goog legroups.com...
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

Dec 9 '06 #7
Dave Sexton wrote:
Hi,

Actually, I like Jon's solutions better, although adding a business object
to encapsulate "City" might not be such a bad idea :)
Thanks to all for the suggestions.

The simple HashTable is nice and clean.

I now have some new requirements such as the look up table takes other
objects as keys. Two classes are added:

class Person
{ // Implementation details not shown
};

class Company
{ // Implementation details not shown
};

Person me = new Person();
Person myFriend = new Person();

Company csco = new Company();
Company intc = new Company();

// Is this possible? Preferrablly without change any existing code.
book[me] = "Milpitas";
book[myFriend] = "Sunnyvale" ;
book[csco] = "San Jose";
book[intc] = "Santa Clara";

Dec 11 '06 #8
Podi wrote:
Thanks to all for the suggestions.

The simple HashTable is nice and clean.
Imho, the Dictionary<obje ct, stringis a lot cleaner - no need to
cast the result every time you read a value.
I now have some new requirements such as the look up table takes other
objects as keys. Two classes are added:

class Person
{ // Implementation details not shown
};

class Company
{ // Implementation details not shown
};

Person me = new Person();
Person myFriend = new Person();

Company csco = new Company();
Company intc = new Company();

// Is this possible? Preferrablly without change any existing code.
book[me] = "Milpitas";
book[myFriend] = "Sunnyvale" ;
book[csco] = "San Jose";
book[intc] = "Santa Clara";
Of course. Just be sure that the classes you use as keys override
GetHashCode and Equals.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
Dec 11 '06 #9
Cor,

You don't think a Hashtable or Dictionary<TKey , TValuewould be better
in this case?

Brian

Cor Ligthert [MVP] wrote:
Prodi,

I like the often missed sortedlist for this.

http://msdn.microsoft.com/library/de...classtopic.asp

Cor
Dec 11 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1439
by: Jeremy Ross | last post by:
Hello, I am looking for a function that will build a mysql table with the results of a query. If you could help that would be great, Thanks, Jeremy Ross
5
4896
by: john_williams_800 | last post by:
Hi; I am just starting to use the DOM to do some more advanced javascripting so please be patient with my question if it is an ignorant question. I would like to use the DOM to dynamically create an html table via javascript. While that table is being dynamically created in a javascript function I would like to dynamically insert text and a hyperlink with an image into each cell.
1
1450
by: CD | last post by:
I would like some guidance as to how to proceed to build a webpage in asp or aspx. I am going to hit a SQL db needing to get the following data: SELECT ServerName,PrimaryTech FROM tblservers GROUP BY PrimaryTech,ServerName The idea is to display the data in a table format where the Column header would the PrimaryTech and under each tech would the ServerName they are responsible for.
2
10265
by: Stanley Sinclair | last post by:
About to create a table which will "include" a BLOB. Am not sure how large to make the container and the tablespace. What I see says that BLOB is stored "separately." However, I don't know where. With a table kinda like: CREATE TABLE BLOB_TABLE ( BLOB_ID INT,
15
2147
by: kimi | last post by:
I have just started working on a project that is partially complete. It is an application that is using access to store test results. The test results are being stored in two Access 2000 databases. DB #1 = StudentDB DB #2 = TestResulstsDB Why are there 2 dbs? I do not know - but that is one of the tings that we will be changing. Combining all of the data into one database.
3
2411
by: John | last post by:
Hi, I need to build a BAUDOT lookup table for an encoder. I see to use a char as the index 'A' or 'B' and get back an array of booleans (five elements long). Baudot = {00011} Baudot = {11001} Can someone give me a pointer on how to build this as a static lookup
7
8397
by: rcamarda | last post by:
I wish to build a table based on values from another table. I need to populate a table between two dates from another table. Using the START_DT and END_DT, create records between those dates. I need a new column that is the days between the date and the MID_DT The data I wish to end with would look something like this: PERIOD DATE DAY_NO 200602 2005-07-06 -89 200602 2005-07-07 -88 200602 2005-07-08 -87
2
1213
by: Alien Clone | last post by:
I am using the following code within an Access database to retrieve data from Oracle and build a local table within the Access database. DoCmd.RunSQL "Select Field1, Field2, " _ & "'' as StartDate, '' as Spare, '' as StartSalary" _ & " into tbl_TEST from " _ & "IN 'ODBC Database;' 'ODBC;DSN=ITSP;UID=Userid;PWD=pswrd;'" This code builds the table called tbl_TEST with Oracle fields Field1 and Field2. The fields Startdate, Spare, and...
0
1119
by: Mark Rae [MVP] | last post by:
"egsdar" <egsdar@discussions.microsoft.comwrote in message news:6A834615-9B38-4129-931B-9C9114682E26@microsoft.com... OK, first things first: ASP.NET is *TOTALLY DIFFERENT* from ASP Classic. If you're just moving from ASP Classic to ASP.NET, the worst mistake you can make is to imagine that ASP.NET is the "next version" of ASP Classic. It isn't - not even close... As a general rule, there is no point whatsoever in even trying to...
2
1908
by: samvb | last post by:
Hi, From a database, I need to build a dynamic table that looks like this: ITEM1 ITEM2 ITEM3 ITEM4 ITEM5 ITEM6 It is dynamic. Sometimes only there could be 1 item or more than 4 items. I have tried 2 loops but it didn't work at all. My main problem is closing and ending the rows. And if the next row is less than 3 item, it is another problem. Sorry, I don't speak English well.
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9602
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.