tags:

views:

191

answers:

4

i have following code

uint32 joaat_hash(uchar *key, size_t len)
  {
    uint32 hash = 0;
    size_t i;

    for (i = 0; i < len; i++)
    {
        hash += key[i];
        hash += (hash << 10);
        hash ^= (hash >> 6);
    }
    hash += (hash << 3);
    hash ^= (hash >> 11);
    hash += (hash << 15);
    return hash;
}

what will be it's equivalent code in c++? i means data types

1>------ Build started: Project: hash_functions, Configuration: Debug Win32 ------
1>  hash_function.cpp
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2146: syntax error : missing ';' before identifier 'joaat_hash'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2065: 'uchar' : undeclared identifier
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2065: 'key' : undeclared identifier
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2275: 'size_t' : illegal use of this type as an expression
1>          c:\users\david\documents\visual studio 2010\projects\hash_functions\predefined c++ types (compiler internal)(19) : see declaration of 'size_t'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2146: syntax error : missing ')' before identifier 'len'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2078: too many initializers
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2275: 'size_t' : illegal use of this type as an expression
1>          c:\users\david\documents\visual studio 2010\projects\hash_functions\predefined c++ types (compiler internal)(19) : see declaration of 'size_t'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(4): error C2143: syntax error : missing ';' before '{'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(4): error C2447: '{' : missing function header (old-style formal list?)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
+1  A: 

Add these to the top of your code:

typedef unsigned char uchar;
typedef unsigned long uint32;  // likely, but not guarenteed.
//typedef unsigned int size_t;  // should be defined in stdlib.h
James Curran
This won't work for all systems. Check to see what size of integer you've got that's 32 bits.
David Thornley
-1: 32 bit longs, even on 64 bit systems is a Window-ism (LLP64 model). They couldn't support the more flexible LP64 model, because everyone hardcoded the "likely" assumption that longs are 32 bit, *just like you just told the OP*.
Luther Blissett
@Luther: a) any given computer in the world is "likely" to be running Windows. b) the OP's computer *is* windows (see his error messages), c) That's why I pointer out "not guarenteed".
James Curran
Any given computer in the world is likely *not* to be running Windows, because micro-controller (which are programmed in C and C++ these days) and embedded systems outnumber desktop-computers by 1-2 magnitudes. Teaching people to make frank assumptions leads them to the way too long and winding road to become better programmers.
Luther Blissett
But the point is that I needed to put *some* type there. I used the type that was correct for the computer the OP was actually using, and gave a warning that it won't apply universally. What more do you want?
James Curran
A: 

That, to me, seems to already be a C++ code. I wonder why you think it's not.
But since C++ doesn't have uint and uchar explicitly defined you'll need to either have this piece of code before that function.
typedef unsigned int uint32;
typedef unsigned char uchar;

Otherwise, rewrite the function like:

unsigned int joaat_hash(unsigned char *key, size_t len)
{
    unsigned int hash = 0;
    size_t i;

    for (i = 0; i < len; i++)
    {
        hash += key[i];
        hash += (hash << 10);
        hash ^= (hash >> 6);
    }
    hash += (hash << 3);
    hash ^= (hash >> 11);
    hash += (hash << 15);
    return hash;
}
Vite Falcon
@James Curran is right you cannot assume anything about the size of the an `int`.
Thomas Børlum
@Thomas: Or a long. Use stdint.h if available, and the changes suggested by KillianDS, or check the documentation for your implementation.
David Thornley
+2  A: 

If by "equivalent code in C++" you mean if this should be refactored to use language constructs only available in C++ such as templates or classes, I would say it doesn't.

There are no obvious invariants in the function so it doesn't really form the basis of an object.

The nature of the data to be processed doesn't scale to arbitrary types so templates don't seem a good fit either.

I think the comment suggesting the use of stdint.h is very sensible advice and I would also replace those magic numbers with something more implicitly meaningful (int const's, etc.).

Amardeep
+1 for constants.
Dacav
+5  A: 

Include <cstdint> (compiler must support C++0x) and change following datatypes:

  • uint32 -> uint32_t (or std::uint32_t)
  • uchar -> unsigned char

As mentioned in comment, cstdint won't be available yet in all compilers, most of the time you can use stdint.h however (the normal C99 header, not in std namespace).

KillianDS
+1. AFAIK `<cstdint>` is not usable on some compilers. You may prefer to use `<stdint.h>` instead.Also if so you can use `uint8_t` to cover `uchar`.
Dacav
Why must a C++ compiler support C99? C++0x will include stdint.h, but is that the current standard? (I don't have a C++03 standard handy.)
David Thornley
stdint.h is, like cstdint a C99 header. Visual Studios before C++0x havn't got any of these.
Luther Blissett