Results 1 to 16 of 16

Thread: hash_multimap

  1. #1
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default hash_multimap

    How hard is it to roll up our own small hash_multimap ? I am working on a small project, I wouldn't need anything besides the standard STL (hash_multimap under STL would have been so great).
    I don't want to use Boost just for hash_multimap, if I don't have any alternative I might use it though. So what are other alternatives ?

    BTW, I just need the map for hash_multimap<string, vector<string> >.
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hash_multimap

    wouldn't QMultiHash fit???
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hash_multimap

    yes, QMultiHash is so nice isn't it . But I wouldn't want to use it just for hashmap,then I would have to rely on the library files... Is there anything else in a single or two header files that could be useful...

    How hard is it to roll our own ? I know that if the hash function is not good then the hashmap could perform worse than map. So, guess there is math involved in it....
    Last edited by ct; 28th July 2007 at 17:24. Reason: bad english
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  4. #4
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hash_multimap

    if you are already using STD (string and vector are from STD right?) why not using the hash_multimap from STD? if however you can't afford neither STD nor Qt then you'll have a hard time creating your own... maybe a quick look at Qt sources would help you...
    Current Qt projects : QCodeEdit, RotiDeCode

  5. #5
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hash_multimap

    if you are already using STD (string and vector are from STD right?) why not using the hash_multimap from STD?
    It's STL ( Standard Template Library) and hash_multimap is not a part of STL standard as far as I know.
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  6. #6
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hash_multimap

    Quote Originally Posted by ct View Post
    It's STL ( Standard Template Library)
    Whoops... yep! sorry, little confusion...

    Quote Originally Posted by ct View Post
    and hash_multimap is not a part of STL standard as far as I know.
    right. I'm afraid I misunderstood your first post... I thought hash_multimap was something from STL that you couldn't afford using...

    Again I'd suggest having a look at Qt sources... Being a template class it *should* occupy a single file. However the use of implicit sharing makes it much trickier but once you get rid of all these (useless?) portions of code it should fit your purposes...
    Current Qt projects : QCodeEdit, RotiDeCode

  7. #7
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hash_multimap

    I am confused though if I should go through the trouble of filtering out the hash multimap.
    Should I use the multimap that is already there in the STL or go for hash_map.

    The array size will be around 100,000 and i might need to find about 10 items in that array...would the lookups be huge for a map ?
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  8. #8
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hash_multimap

    Quote Originally Posted by ct View Post
    I am confused though if I should go through the trouble of filtering out the hash multimap.
    Should I use the multimap that is already there in the STL or go for hash_map.

    The array size will be around 100,000 and i might need to find about 10 items in that array...would the lookups be huge for a map ?
    I don't know how well the STL multi_map performs... The thing is that a map orders its elements so it is way slower when inserting items, especially with as much items as you plan to add. As for the lookup, hash-based structures are said to be faster but a map-based, being sorted, uses binary search which can actually turn out to be faster if the number of lookups is relatively small...
    Current Qt projects : QCodeEdit, RotiDeCode

  9. #9
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: hash_multimap

    http://code.google.com/p/google-sparsehash/
    Just found the link accidently
    Last edited by Gopala Krishna; 5th August 2007 at 20:12.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: hash_multimap

    You can always use a standard hash and keep there not single items, but lists of items. Something like: std::hash_map<std::list<Item> >

    BTW. Are you sure hash_multimap is not available (I know it's not part of the standard, but for instance my system supports it)?
    http://www.sgi.com/tech/stl/hash_multimap.html

  11. #11
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hash_multimap


    BTW. Are you sure hash_multimap is not available (I know it's not part of the standard, but for instance my system supports it)?
    yes I think both g++ and VC++ has support for multimap but the usage varies. I needed something that would compile for both the compilers.
    However, map and multimap should suffice my needs at the moment with O(logn) time complexity. I had mistakenly taken the time complexity of STL's map as O(n)..I was so wrong.. O (log n) is still fast..
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: hash_multimap

    STL on Linux is different than STL on Windows - these are completely different implementations, so you'll have differences in all other classes as well (probably including std::vector). The API should be the same on both platforms.

  13. #13
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hash_multimap

    Wow, you are right. The API of both g++ and vc++ is same; just have to insert the required headers. I have the same program working for both g++ and vc++ 2005 right now.
    Here is the code:

    Qt Code:
    1. //VC++
    2. #include<iostream>
    3. #include<cstdio>
    4. #include<hash_map>
    5. using namespace std;
    6. using namespace stdext;
    7. int main(int argc,char** argv)
    8. {
    9. hash_map<int,int> mapping;
    10. hash_map<int,int>::const_iterator miter;
    11.  
    12. mapping.insert(pair<int,int>(1,100));
    13. mapping.insert(pair<int,int>(2,200));
    14. miter = mapping.find(1);
    15.  
    16. if(miter == mapping.end())
    17. cout << "Not found";
    18. else
    19. cout << miter->first << " = " << miter->second;
    20.  
    21.  
    22. getchar();
    23. return 0;
    24. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //g++
    2. #include<iostream>
    3. #include<cstdio>
    4. #include<ext/hash_map>
    5. using namespace std;
    6. using namespace __gnu_cxx;
    7. int main(int argc,char** argv)
    8. {
    9. hash_map<int,int> mapping;
    10. hash_map<int,int>::const_iterator miter;
    11.  
    12. mapping.insert(pair<int,int>(1,100));
    13. mapping.insert(pair<int,int>(2,200));
    14. miter = mapping.find(1);
    15.  
    16. if(miter == mapping.end())
    17. cout << "Not found";
    18. else
    19. cout << miter->first << " = " << miter->second;
    20.  
    21.  
    22. getchar();
    23. return 0;
    24. }
    To copy to clipboard, switch view to plain text mode 

    However I am running into some problem with hash_map<string,string> for both the compilers. I am sure there is some compare function that needs to be defined for strings or is there something already available ?
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  14. #14
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hash_multimap

    BTW, how can I combine both these files into one so that both the compilers can compile it. Is there some flag that needs to be checked to find out if the compiler is VC++ or g++ ?
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  15. #15
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: hash_multimap

    Qt Code:
    1. #ifdef Q_CC_MSVC
    2. #include<hash_map>
    3. using namespace stdext;
    4. #endif
    5.  
    6. #ifdef Q_CC_GNU
    7. #include<ext/hash_map>
    8. using namespace __gnu_cxx;
    9. #endif
    To copy to clipboard, switch view to plain text mode 

  16. #16
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hash_multimap

    Quote Originally Posted by spud View Post
    Qt Code:
    1. #ifdef Q_CC_MSVC
    2. #include<hash_map>
    3. using namespace stdext;
    4. #endif
    5.  
    6. #ifdef Q_CC_GNU
    7. #include<ext/hash_map>
    8. using namespace __gnu_cxx;
    9. #endif
    To copy to clipboard, switch view to plain text mode 

    That certainly didn't work. Is that some QT preprocessor that does the same thing? I opened up the glut.h file and saw how they have done it so I got upto here.

    Qt Code:
    1. #include<iostream>
    2. #include<cstdio>
    3.  
    4. #if defined(__MINGW32__)
    5. #include<ext/hash_map>
    6. using namespace __gnu_cxx;
    7. #endif
    8.  
    9. #if defined(_MSC_VER)
    10. #include<hash_map>
    11. using namespace stdext;
    12. #endif
    13.  
    14. #include<iostream>
    15. #include<cstdio>
    16.  
    17. using namespace std;
    18.  
    19. int main(int argc,char** argv)
    20. {
    21. hash_map<int,int> mapping;
    22. hash_map<int,int>::const_iterator miter;
    23.  
    24. mapping.insert(pair<int,int>(1,100));
    25. mapping.insert(pair<int,int>(2,200));
    26. miter = mapping.find(1);
    27.  
    28. if(miter == mapping.end())
    29. cout << "Not found";
    30. else
    31. cout << miter->first << " = " << miter->second;
    32.  
    33.  
    34. getchar();
    35. return 0;
    36. }
    To copy to clipboard, switch view to plain text mode 
    The preprocessors have always confused me a bit. Is there a list of all the preprocessors that can be used (at least the more common ones ). Can anyone point to some good resource about types of general preprocessors or may be list some common ones out here ?
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.