IDictionary. Find value

S

shapper

Hello,

I have a IDictionary<Int, String> Contraints.

How can I find if there is a Value (not a key) in this Dictionary
which String contains the word "text" in it?

Thanks,
Miguel
 
P

Peter Duniho

shapper said:
Hello,

I have a IDictionary<Int, String> Contraints.

How can I find if there is a Value (not a key) in this Dictionary
which String contains the word "text" in it?

You have to search all the values.

In general, you should avoid designing code with a requirement like
that. It has obvious inefficiency problems, and would generally negate
at least some of the point of using a hash-table data structure in the
first place.

Pete
 
S

shapper

You have to search all the values.

In general, you should avoid designing code with a requirement like
that.  It has obvious inefficiency problems, and would generally negate
at least some of the point of using a hash-table data structure in the
first place.

Pete

In this case it makes some sense. Let me explain. I can use:

if (source.Constraints.Select(c => c.Value.Contains("text")).Count() >
0)

Or

if (source.Constraints.ContainsKey(1))
if (source.Constraints[1].Contains("text"))

What I am trying to check if there is an item with key 1.
If there is then i need to check if its value contains text.

So you say that this second approach is more correct.
I could also use one because I know only one value will contain "text"
or none.

Basically I am adding constraints to assets.
In this case I am checking id a constraint of type MymeType (1) is
associated with Asset (source).
If there is such constraint then I am checking if the mime type
contains the word text.

So I suppose this second approach would be better, correct?

Thanks,
Miguel
 
P

Peter Duniho

shapper said:
In this case it makes some sense.

Really? Because, so far it doesn't seem so.
Let me explain. I can use:

if (source.Constraints.Select(c => c.Value.Contains("text")).Count() >
0)

Or

if (source.Constraints.ContainsKey(1))
if (source.Constraints[1].Contains("text"))

What I am trying to check if there is an item with key 1.
If there is then i need to check if its value contains text.

First, if you know that the text of interest is associated with only a
specific key, why in the world would you search all the values?

Second, even your second example isn't very efficient. This would be
much better:

string str;

if (source.Constraints.TryGetValue(1, out str) &&
str.Contains("text"))
{
// do something
}
So you say that this second approach is more correct.

Yes, of course I would. If it satisfies your design goal, its vastly
better.
I could also use one because I know only one value will contain "text"
or none.

Non sequitur. The number of values in the dictionary that may contain
the text you're looking for has no effect on the efficiency of searching
_every_ value.

Or rather, it certainly doesn't argue in favor of the less-efficient
approach; to the contrary, it ensures that the less-efficient approach
is going to have nearly the worst-case performance, because almost every
single string will have to be searched all the way to the end (the
string that does contain the text you're looking for can be searched
only partially if the text is not at the end).
Basically I am adding constraints to assets.
In this case I am checking id a constraint of type MymeType (1) is
associated with Asset (source).
If there is such constraint then I am checking if the mime type
contains the word text.

So I suppose this second approach would be better, correct?

The word "assets" has no well-defined technical meaning, so I have to
make too many assumptions to understand the above statements for me to
make any definitive statements.

That said, if you're dealing with MIME types, they are a restricted
enough kind of string that, as compared to examining every value in the
dictionary, it would be much more efficient for you to generate an index
of the MIME type components used that allows you to quickly work back to
assets given a string that could be part of a MIME type.

But, if you know in advance that only one of your assets will contain
the text you're looking for _and_ you know which asset that might be,
then yes…it's definitely much more efficient to just get the value for
that asset and check it. Even an indexing approach would be overkill in
that case.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top