#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Hash table for ip addreses


    hi guys,

    I need to store IP addresses in a hash table and count the number of messages they send.
    (Unknown number of IPs or messages)

    I don't know what is the fastest data structure in this case (an array, a tree...).

    Also i need a good hash function for the IP addresses.

    Thanks in advance...
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    If you are using C++ just use std::map. If you do it in C (and don't use a library) you are going to be writing 100's of lines of code that all has to work perfectly, not simple or easy even for experts.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Originally Posted by mitakeet
    If you are using C++ just use std::map. If you do it in C (and don't use a library) you are going to be writing 100's of lines of code that all has to work perfectly, not simple or easy even for experts.
    On a *nix system with a C compiler, you can use hcreate(), hsearch() and hfree(). Alternatively, you could use a third party library such as Apache's APR library (specifically, the apr_hash_xx routines), which is portable across multiple platforms.

    For C++, you could use std::unordered_map instead of std::map if you have a C++ 0x standard compliant compiler. For older C++ compilers, most of them support a std::hash_map template even though it wasn't part of the standard :). std::hash_map is faster than std::map (because std::map sorts on every item insert)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo