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. reverse lookup in dictionary

reverse lookup in dictionary

Scheduled Pinned Locked Moved C#
9 Posts 5 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.
  • V Offline
    V Offline
    vytheese
    wrote on last edited by
    #1

    Is there any data structure avilable out of the box in Framework 3.5, kind of reverse lookup dictionary.

    Dictionary< key1, key2 > d = new Dictionary< key1, key2>();
    Key2 k2 = d[key1]; // normal
    Key1 k1 = d[key2]; // specify the key2 and get key1 -- needed

    Thanks

    Regards, Vythees Miles to go before sleep...

    L P 2 Replies Last reply
    0
    • V vytheese

      Is there any data structure avilable out of the box in Framework 3.5, kind of reverse lookup dictionary.

      Dictionary< key1, key2 > d = new Dictionary< key1, key2>();
      Key2 k2 = d[key1]; // normal
      Key1 k1 = d[key2]; // specify the key2 and get key1 -- needed

      Thanks

      Regards, Vythees Miles to go before sleep...

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      There is no reverse lookup in a dictionary because one value can lead to multiple keys, but not the other way round. To get a list of keys that result in a specific value you can use this:

      var keys = from key in dic.Keys
      where dic[key] == somevalue
      select key;

      regards

      V 1 Reply Last reply
      0
      • L Lost User

        There is no reverse lookup in a dictionary because one value can lead to multiple keys, but not the other way round. To get a list of keys that result in a specific value you can use this:

        var keys = from key in dic.Keys
        where dic[key] == somevalue
        select key;

        regards

        V Offline
        V Offline
        vytheese
        wrote on last edited by
        #3

        Thanks, but in my case both are unique.

        Regards, Vythees Miles to go before sleep...

        L 1 Reply Last reply
        0
        • V vytheese

          Thanks, but in my case both are unique.

          Regards, Vythees Miles to go before sleep...

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          vytheese wrote:

          but in my case both are unique.

          In this case I would derive a class from IDictionary that will internally hold two dictionaries, one for key->value and one for value->key. Something like:

          public class TwoWayDictionary<K, V> : IDictionary<K, V>

          Override the methods like Add, Contains etc to allow adding, removing and searching by key and value in both of the dictionaries. regards

          V 1 Reply Last reply
          0
          • L Lost User

            vytheese wrote:

            but in my case both are unique.

            In this case I would derive a class from IDictionary that will internally hold two dictionaries, one for key->value and one for value->key. Something like:

            public class TwoWayDictionary<K, V> : IDictionary<K, V>

            Override the methods like Add, Contains etc to allow adding, removing and searching by key and value in both of the dictionaries. regards

            V Offline
            V Offline
            vytheese
            wrote on last edited by
            #5

            The wrapper will ease from access point of view but Again I will end up in performance issue while searching from value point.

            Regards, Vythees Miles to go before sleep...

            B 1 Reply Last reply
            0
            • V vytheese

              The wrapper will ease from access point of view but Again I will end up in performance issue while searching from value point.

              Regards, Vythees Miles to go before sleep...

              B Offline
              B Offline
              Brady Kelly
              wrote on last edited by
              #6

              Why? You will be looking up the value as a key in your second dictionary.

              Unscrambling Eggs: Decompiling ASP.NET

              V 1 Reply Last reply
              0
              • B Brady Kelly

                Why? You will be looking up the value as a key in your second dictionary.

                Unscrambling Eggs: Decompiling ASP.NET

                V Offline
                V Offline
                vytheese
                wrote on last edited by
                #7

                Good, Sorry I failed to notice that. But in any case its only a wrapper for me. What I am looking for is single dictionary with key, values both indexed or hashed. since in my scenario the collection I am expecting is huge and time for process is having little window. so I want to get the feasible data structure that will helps me in both (space/time). But with the time contstraint for my execution I have to trade between space vs time for a intial release. Grazie.

                Regards, Vythees Miles to go before sleep...

                L 1 Reply Last reply
                0
                • V vytheese

                  Good, Sorry I failed to notice that. But in any case its only a wrapper for me. What I am looking for is single dictionary with key, values both indexed or hashed. since in my scenario the collection I am expecting is huge and time for process is having little window. so I want to get the feasible data structure that will helps me in both (space/time). But with the time contstraint for my execution I have to trade between space vs time for a intial release. Grazie.

                  Regards, Vythees Miles to go before sleep...

                  L Offline
                  L Offline
                  leppie
                  wrote on last edited by
                  #8

                  vytheese wrote:

                  What I am looking for is single dictionary with key, values both indexed or hashed.

                  That exactly what a wrapper class with 2 dictionaries will do...

                  xacc.ide - now with TabsToSpaces support
                  IronScheme - 1.0 alpha 4a out now (29 May 2008)

                  1 Reply Last reply
                  0
                  • V vytheese

                    Is there any data structure avilable out of the box in Framework 3.5, kind of reverse lookup dictionary.

                    Dictionary< key1, key2 > d = new Dictionary< key1, key2>();
                    Key2 k2 = d[key1]; // normal
                    Key1 k1 = d[key2]; // specify the key2 and get key1 -- needed

                    Thanks

                    Regards, Vythees Miles to go before sleep...

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Yeah, I just use two Dictionaries.

                    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