|
To make that efficiently (without looping through the items in the collection), you need to keep another dictionary where you switch the key and value.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Great. Do you mean putting another dictionary instance which is not visible to outside of class. When new items are added/removed from the main dictionary, this internal one also should synchronize. Right ?
|
|
|
|
|
Yes, that will be the best.
|
|
|
|
|
Thanks. How about the performance if collection is big ?
|
|
|
|
|
N a v a n e e t h wrote: Do you mean putting another dictionary instance which is not visible to outside of class.
Basically, yes. If you want to access items both on key and value, I suggest that you make a class that encapsulates two dictionaries, and only expose methods for adding, removing and do key and value lookups.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Okay. Thanks for that. Will it be an performance overhead if collection is big ?
|
|
|
|
|
N a v a n e e t h wrote: Will it be an performance overhead if collection is big ?
There will be some memory overhead, as a dictionary uses about 20 bytes to keep track of each entry.
Accessing items by key is a O(1) operation, so the size of the dictionary doesn't affect the time to fetch an item (assuming of course that the type of the key doesn't have a lousy algorithm for producing hash values).
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thanks again guffa. I will go with the solution you suggested.
Guffa wrote: as a dictionary uses about 20 bytes
I am wondering how you are getting the memory details ?
|
|
|
|
|
A dictionary contains two arrays, a bucket index array and an entry array. The bucket index array is an int array and the entry array is an array of the type Entry:
[StructLayout(LayoutKind.Sequential)]
private struct Entry {
public int hashCode;
public int next;
public TKey key;
public TValue value;
}
If the key and the value are reference types, on a 32-bit system the Entry structure will be 4+4+4+4 = 16 bytes. The two arrays always has the same number of items, so for each entry there is one item in the bucket index array (int) and one item in the entry array, which will add up to 20 bytes in this case.
The size of the Entry structure varies depending on the system and data types. For example:
On a 64-bit system, if the key and value are reference types, the Entry structure will be 4+4+8+8 = 24 bytes.
If the key is an Int64 and the value is a Decimal, the Entry structure will be 4+4+8+16 = 32 bytes.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
|
Yeah, for constant fetch time, you could do what Guffa said - have two dictionaries, one keyed on your key and one keyed on your value.
If it's okay for fetch time for values to be linear, you could simply pack the key along with the value, internally (i.e Dictionary<key, pair<key,="" value="">>). That way, when you iterate through values and find a value, you can get the associated key right away.
|
|
|
|
|
S. Senthil Kumar wrote: have two dictionaries, one keyed on your key and one keyed on your value.
Thanks. If my Dictionary contains large number of items, will this method makes any memory issues ?
S. Senthil Kumar wrote: you could simply pack the key along with the value, internally (i.e Dictionary> [Wink] . That way, when you iterate through values and find a value, you can get the associated key right away.
This is good method. But I can't use it as my key and value are isolated (code I have shown in the last message is for demonstration).
Thanks senthil for the reply
|
|
|
|
|
Hi all!
I was wondering if anyone could assist me...
In a file, I have a large amount of strings, each separated by a comma.
In my current example, I have a LARGE string value, containing 1343 different string values, and a total of 573033 values (All different lengths) - Created by a program.
I'm wondering if there's and easy way to find which string value, when removed, will shorten the list the most (I'm messing around with compression )
Eg:
a,asdasd,asdasd,a,b <--- 3 different string values, 5 values
In the above example, both "asdasd" and "a" each appear twice, but if I remove "asdasd" from the example, it will be shortened the most.
I've tried using the dictionary class to count the amount of times a value appears (Which works...), but it still doesn't tell me the largest value, which appears the most times.
I was thinking about iterating through my result from the dictionary class, then multiplying the string length by the amount of times it appears to get a result (Which might work...), but I was wondering if there was any other way to do it...
Any help would be appreciated
-= Reelix =-
|
|
|
|
|
Reelix wrote: I've tried using the dictionary class to count the amount of times a value appears (Which works...), but it still doesn't tell me the largest value, which appears the most times.
Don't count the number of times the strings occur, count the total number of characters that they use.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
That'd only give me half of what I need.
Eg:
a,123321,a,b,123321,a,a would return
1, 6, 1, 1, 6, 1,1
Still doesn't tell me which occurs the most often :/
I pretty much need to input
a,123321,a,b,123321, a, a
and it outputs
123321 (Being the longest string, which appears most often)
See that even though the a's appear 4 times, the total length they take up is (4 * 1) = 4 spaces, as opposed to 123321, which takes up (6 * 2) = 12 spaces.
-= Reelix =-
|
|
|
|
|
Looping through the strings and collecting the string lengths should give you a collection that looks like this:
"a": 4
"123321": 12
"b": 1
The number is the total number of characters that all copies of the string uses. Then you just have to pick the item with the largest number.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
There are more available dlls to add under windows\assembly folder. But adding presentationcore.dll alone asks that there are some unknown classes. Is there a way to specify some paths under VS2008 to work with them?
chesnokov
|
|
|
|
|
i had a snap shot of error if anybody can solve plz tell me about this error.
when i start my application it gives an application error rather than executing,"the application failed to initialize properly 0xc000007b applicaton error"
|
|
|
|
|
If you also have the code of that application than why not debug that. and see on which line it throws an exception.
|
|
|
|
|
I want to run an application in Vista.
How to run as administrator automatic?
|
|
|
|
|
You have to inject a manifest containing the desired execution level (requireAdministrator).
If you search with these keywords you'll find all you need.
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
There are n number of clients and all are accessing one server. Data, which is in dataset(disconnected mode) shows at client machine. If one of the client changes the data at his local machine, it should reflect in other systems also, without refreshing the data.
Please let me know How to handle this scenario in order to avoid redundant data. Hope my question is clear.
|
|
|
|
|
Krish Agn wrote: it should reflect in other systems also, without refreshing the data
By magic. Thats the only way if you don't want to refresh the data.
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Ok. Or else, need to restrict other users not to make any changes(insert, delete, update) while one client is modifying the data.
|
|
|
|
|
As you are working with disconnected datasets basically you can't. What you need to do is to check if changes have been made when updating the main database. This is usually done by timestamps or record version numbers.
Hope this helps
Bob
Ashfield Consultants Ltd
|
|
|
|