Fastest way to modify the value in the dictionary
-
I have a bindinglist then i store the indexes to dictianary for fast access to the bindinglist. then i have something like this
var dIndex = new Dictionary()// the key is the id of the object and the value is the index of the object into the bindinglist;
now i face a problem when removing the item in the bindiglist, because the indexes stored in the dictionary doesnt correspond anymore in the bindinglist. so my solution is to re-arrange the indexes. Then i got this.
int id = thisObject.ID;
if(dIndex.ContainsKey(id))
{
data.RemoveAt(dIndex[id]);//this is removing the item from bindinglist
_dIndex.remove(id);foreach(var key in dIndex.keys)
{
if(dIndex[key] > id)
{
dIndex[Key]--;
}
}
}If I am correct, rearranging the index (the value of the dictionary) using the foreach loop above could result to an O(n). could anyone here can give me much better solution than this. I will appreciate for any advice. Thank you.
-
I have a bindinglist then i store the indexes to dictianary for fast access to the bindinglist. then i have something like this
var dIndex = new Dictionary()// the key is the id of the object and the value is the index of the object into the bindinglist;
now i face a problem when removing the item in the bindiglist, because the indexes stored in the dictionary doesnt correspond anymore in the bindinglist. so my solution is to re-arrange the indexes. Then i got this.
int id = thisObject.ID;
if(dIndex.ContainsKey(id))
{
data.RemoveAt(dIndex[id]);//this is removing the item from bindinglist
_dIndex.remove(id);foreach(var key in dIndex.keys)
{
if(dIndex[key] > id)
{
dIndex[Key]--;
}
}
}If I am correct, rearranging the index (the value of the dictionary) using the foreach loop above could result to an O(n). could anyone here can give me much better solution than this. I will appreciate for any advice. Thank you.
I suggest you use a direct reference to the object(s) in the Binding List for the Value field of the generic Dictionary, rather than an index to their position in a List; since what you will actually store is a pointer, this will not consume much more memory than storing an Int16, or Int32. In my mind, that also raises the question of why use a List when you are using a Dictionary ... however, I can't see your code, and don't know in what other ways you may be using the List.
private Dictionary IDToBindingListObject = new Dictionary();
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
-
I suggest you use a direct reference to the object(s) in the Binding List for the Value field of the generic Dictionary, rather than an index to their position in a List; since what you will actually store is a pointer, this will not consume much more memory than storing an Int16, or Int32. In my mind, that also raises the question of why use a List when you are using a Dictionary ... however, I can't see your code, and don't know in what other ways you may be using the List.
private Dictionary IDToBindingListObject = new Dictionary();
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
Thank you, Ill try your suggestion.
-
I suggest you use a direct reference to the object(s) in the Binding List for the Value field of the generic Dictionary, rather than an index to their position in a List; since what you will actually store is a pointer, this will not consume much more memory than storing an Int16, or Int32. In my mind, that also raises the question of why use a List when you are using a Dictionary ... however, I can't see your code, and don't know in what other ways you may be using the List.
private Dictionary IDToBindingListObject = new Dictionary();
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
Yes you are right, i use the dictionary as pointer to the object, I used the key as the object id and the value as the pointer to the object. I will also use the bindinglist as a datasource. For my first implementation I am aware that it has a memory penalty, and need much more work. I got just worried about this line of code, how would it impact the performance.
foreach(var key in dIndex.keys)
{
if(dIndex[key] > id)
{
dIndex[Key]--;
}
}I think the remove(object) method of bindinglist has already been optimized by MS. Thank you for your suggestion, Ill try it. I think there is no way to achieve O(1) for deleting object on the bindinglist.