Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Fastest way to modify the value in the dictionary

Fastest way to modify the value in the dictionary

Scheduled Pinned Locked Moved C#
databasehelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gilbert Consellado
    wrote on last edited by
    #1

    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.

    B 1 Reply Last reply
    0
    • G Gilbert Consellado

      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.

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #2

      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.

      G 2 Replies Last reply
      0
      • B BillWoodruff

        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.

        G Offline
        G Offline
        Gilbert Consellado
        wrote on last edited by
        #3

        Thank you, Ill try your suggestion.

        1 Reply Last reply
        0
        • B BillWoodruff

          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.

          G Offline
          G Offline
          Gilbert Consellado
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups