473,774 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

find closest item in keyed collection

Does the .NET framework provide a class which will find the item in
the collection with a key which is closest ( greater than or equal,
less than or equal ) to the keys of the collection?

ex: collection keys are 20, 30, 40, 50, 60, 70, 80

find key which is >= 35.

would return the 30 key.

thanks,
Dec 17 '07 #1
22 4727
Oops; "Single" should have been "First"...

Marc
Dec 17 '07 #2
Still dont understand why >=35 isnt 40... some new math I did miss?

//CY
Dec 17 '07 #3
If the poster is looking for the nearest property to a particular
address,
then his numbers may indeed be keys.
Yes; I was thrown by the OP *only* mentioning a single series of
numeric data, rather than two.
I followed up with a post acknowledging this. Although lets be fair -
a lot of things could have been a little clearer in the original
question.

Marc
Dec 17 '07 #4


"Marc Gravell" wrote:
If the poster is looking for the nearest property to a particular
address,
then his numbers may indeed be keys.

Yes; I was thrown by the OP *only* mentioning a single series of
numeric data, rather than two.
I followed up with a post acknowledging this. Although lets be fair -
a lot of things could have been a little clearer in the original
question.

Marc

You are right on both counts. Sorry, I responded before seeing the other
posts. You gave a slick solution.

>
Dec 17 '07 #5

"Steve Richter" <St************ @gmail.comwrote in message
news:3a******** *************** ***********@q3g 2000hsg.googleg roups.com...
Does the .NET framework provide a class which will find the item in
the collection with a key which is closest ( greater than or equal,
less than or equal ) to the keys of the collection?

ex: collection keys are 20, 30, 40, 50, 60, 70, 80

find key which is >= 35.

would return the 30 key.
Maintain a SortedList containing these keys, SortedList can find the closest
(or following, or preceding) entry in O(log N) time.
>
thanks,

Dec 17 '07 #6
Liz

"Ben Voigt [C++ MVP]" <rb*@nospam.nos pamwrote in message
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
>
"Steve Richter" <St************ @gmail.comwrote in message
news:3a******** *************** ***********@q3g 2000hsg.googleg roups.com...
>Does the .NET framework provide a class which will find the item in
the collection with a key which is closest ( greater than or equal,
less than or equal ) to the keys of the collection?

ex: collection keys are 20, 30, 40, 50, 60, 70, 80

find key which is >= 35.

would return the 30 key.

Maintain a SortedList containing these keys, SortedList can find the
closest (or following, or preceding) entry in O(log N) time.

with what method? do you mean by iterating the list ?

I think the OP was looking for a function of the sort you used to see in
xBASE dialects ... think it was called SOFTSEEK(), which would return either
the key sought or the next highest-valued key, if any ... I'm not aware of
such a function in the Framework
Dec 17 '07 #7
On Dec 17, 5:46 pm, "Liz" <l...@tiredofsp am.comwrote:
"Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote in messagenews:%2* *************** @TK2MSFTNGP06.p hx.gbl...
"Steve Richter" <StephenRich... @gmail.comwrote in message
news:3a******** *************** ***********@q3g 2000hsg.googleg roups.com...
Does the .NET framework provide a class which will find the item in
the collection with a key which is closest ( greater than or equal,
less than or equal ) to the keys of the collection?
ex: collection keys are 20, 30, 40, 50, 60, 70, 80
find key which is >= 35.
would return the 30 key.
Maintain a SortedList containing these keys, SortedList can find the
closest (or following, or preceding) entry in O(log N) time.

with what method? do you mean by iterating the list ?

I think the OP was looking for a function of the sort you used to see in
xBASE dialects ... think it was called SOFTSEEK(), which would return either
the key sought or the next highest-valued key, if any ... I'm not aware of
such a function in the Framework
thanks for the responses ( and sorry for the confusion on what I
intended as <= 35 ).

what I have is a text file concatenated as a single string. As I scan
and parse the string I want to be able to determine the line number
and position in the line of any offset in the string. As I concat the
text lines, the line number and start/end offset in the string of the
line would be stored in a collection. When I find something at offset
2022 in the string, I want to find in the collection of "line numbers/
start offset in the string" the offset which is <= 2022. That would
tell me the text file line number.

-Steve

Dec 18 '07 #8
One-way searches are fortunately easier than 2-way proximity... you
get to stop sooner ;-p
If this is volume usage, then the LINQ approach I posted earlier would
be a poor choice (it would sort each time); Ben mentioned a SortedList
which may be helpful... I can't only see equality methods myself, but
you could perhaps simply walk the list until you find one that is >
2022 and return the previous (if you see what I mean...).

Of course, if you are adding to the list in sequence, you could just
use a regular list, and do the same walk.
If you are adding to the list while iterating, it is probably just the
last element.

Just for completeness (to contrast to my prior post); in LINQ terms
this is:

IDictionary<int , stringdata = new SortedList<int,
string{
{20, "a"}, {30, "b"}, {40, "c"}, {50, "d"},
{60, "e"}, {70, "f"}, {80, "g"}
};
KeyValuePair<in t, stringresult =
data.TakeWhile( x =x.Key <= 35).Last();

(I'm using TakeWhile here since we know the data is sorted; hence it
will stop reading when it finds something that doesn't meet the <= 35
condition).

Of course, if you are generally accessing recent data you may be
better keeping the list inverted (perhaps even just a List<Tand
Insert at 0; or use a custom comparer to automatically invert the list
in-place [very easy]) and check for the first match:

KeyValuePair<in t, stringresult =
data.SkipWhile( x =x.Key 35).First();

Marc
Dec 18 '07 #9

"Marc Gravell" <ma**********@g mail.comwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
One-way searches are fortunately easier than 2-way proximity... you get to
stop sooner ;-p
If this is volume usage, then the LINQ approach I posted earlier would be
a poor choice (it would sort each time); Ben mentioned a SortedList which
may be helpful... I can't only see equality methods myself, but you could
perhaps simply walk the list until you find one that is 2022 and return
the previous (if you see what I mean...).
Ack! SortedList.Inde xOfKey doesn't return the index for not found items,
while List.BinarySear ch does. But you can't run BinarySearch on a sorted
list! An extension method would be good...

So either reimplement BinarySearch to work on a SortedList or use List and
List.BinarySear ch and keep the list sorted yourself.
>
Of course, if you are adding to the list in sequence, you could just use a
regular list, and do the same walk.
If you are adding to the list while iterating, it is probably just the
last element.

Just for completeness (to contrast to my prior post); in LINQ terms this
is:

IDictionary<int , stringdata = new SortedList<int, string{
{20, "a"}, {30, "b"}, {40, "c"}, {50, "d"},
{60, "e"}, {70, "f"}, {80, "g"}
};
KeyValuePair<in t, stringresult =
data.TakeWhile( x =x.Key <= 35).Last();

(I'm using TakeWhile here since we know the data is sorted; hence it will
stop reading when it finds something that doesn't meet the <= 35
condition).

Of course, if you are generally accessing recent data you may be better
keeping the list inverted (perhaps even just a List<Tand Insert at 0; or
use a custom comparer to automatically invert the list in-place [very
easy]) and check for the first match:

KeyValuePair<in t, stringresult =
data.SkipWhile( x =x.Key 35).First();

Marc

Dec 18 '07 #10

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

Similar topics

0
4074
by: panik | last post by:
Hi, I have a custom collection that implements CollectionBase. The collection is called Sensors and contains a list of Sensor objects. There is the usual index using an integer (Sensors). Is there a way of using a custom indexer (for example Sensors) that does *not* need to loop through each item in the collection and compare them?
13
12322
by: mike | last post by:
I have ListArray with number in Eg: 1, 1.456, 2.43, 4, 6.78 next i have a decimal variable containing one number EG: 1.786 Could someone please tell me how i find the "closest match" number below the decimal variable from the arraylist. Thanks ever so much in advance
2
5574
by: Terry Olsen | last post by:
I'm using a listview control with 2 columns (in detail view). I need to find an item by text so that I can add a subitem to it. I haven't been successful so far. Any help is appreciated.
1
3037
by: SP | last post by:
I have created an abstract class inheriting from KeyedCollection<long, TItemto use as a base class for my collections. In some derived classes I am providing a new indexer property for the key (long). I need to access my collection by index and by key however the new long indexer is used for BOTH int and long, i.e. myCollection will use the long indexer NOT the base classes int indexer. Why is that? The workaround is to also define a new...
6
1735
by: Hyun-jik Bae | last post by:
Is there any way how to get the item which has the most similar key in key-value collection object such as Dictionary<or SortedDictionary<> although there is no matching key? If there is none, is there any substitution class for enabling this? Please reply. Thanks in advance. Hyun-jik Bae
2
4918
by: Jon Slaughter | last post by:
topic says it all? Is there any simple way? msdn says that the keyed collection is build from both a sortedlist and sorted dictionary. http://msdn2.microsoft.com/en-us/library/5z658b67.aspx (well, it doesn't say that specifically but it says it when talking about the sorted versions so I'm not sure) I've found this
0
9454
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
10267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9914
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...
0
8939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
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
6717
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
5355
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...
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.