That's very inefficient use of storage. Let hash function is h, hash table contains 0 to n-1 slots. In Star Trek TNG Episode 11 "The Big Goodbye", why would the people inside of the holodeck "vanish" if the program aborts? I've edited the post. Your compiler may be smart enough to spot the pattern and convert it to more efficient form (e.g. Yes, mixing in other operations would help. The inner loop is supposed to do. Making statements based on opinion; back them up with references or personal experience. That is to say, because your. How come it's actually Black with the advantage here? My goal is to be simple and performatic. To become a better guitar player or musician, how do you balance your practice/training on lead playing and rhythm playing? You should use strlen() to compute the length of strings. Use MathJax to format equations. also contains memset() - don't reimplement it yourself. end-of-world/alien invasion of NYC story. This is exactly what strlen() (in ) is for: BTW, it's probably worth using size_t (or at least unsigned types) for HASH_LENGTH and varLen, too, and for the indexing iterators i and j. Old Budrys(?) This function is treated specially by the compiler. site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. The return type can remain char *. For example your loop to find inputLen could be replaced by a call to something like strlen and your loop to zero the array with a call to memset. Is it for something else? MathJax reference. How were these constants generated? What is the purpose of this hash function? Asking for help, clarification, or responding to other answers. Thanks for contributing an answer to Code Review Stack Exchange! I have only a few comments about your code, otherwise, it looks good. On systems where char is an unsigned type, this may be true much more often than on those where it's signed - that looks like a bug. Why does Lovecraft write that Mount Nansen (approx. It's possible to write it shorter and cleaner. Should live sessions be recorded for students when teaching a math course online? That's a serious bug. ', and anything with ASCII value less than 48) you will get a negative result and when you add it to the hash it will be sign-extended and converted to a huge unsigned value, something like 0xffffffffffffffxx? Performance seems decent. We have a very low density of outputs. I'm in doubt. TL;DR: The algorithm receives a string as input, allocate another string to be the final digest, and start working on them. I like the attention to zeroing out your digest before you start. Are Van Der Waals Forces the Similar to Van der Waal Equation? Why is "threepenny" pronounced as THREP.NI? A simple hash function from Robert Sedgwicks Algorithms in C book. I highly doubt I was the first one to think of this. Use MathJax to format equations. char vars[] = { 0xA6, ... remains problematic as when char is signed, conversion of an out-of-range value to char is implementation defined and may not perform as desired. 1. You will loose a lot of potential reviewers if they have to infer that knowledge from the code. MathJax reference. 9000 ft.) is 15,000 feet high? Is there a name for this algorithm? What is this hole above the intake of engines of Mil helicopters? The XOR loops don't seem to consider any of the input string after the first HASH_LENGTH characters. To subscribe to this RSS feed, copy and paste this URL into your RSS reader. Making statements based on opinion; back them up with references or personal experience. char *digest as unsigned char *digest would negate the need for abs() in abs(digest[i]), Fundamental issues as code uses char rather than unsigned char. Find the coordinates of a hand drawn curve. If we declare malloc() before we use it, we won't need to cast its result: Note that sizeof (char) cannot be other than 1, since sizeof works in units of char. By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. We really need to start by defining the kind of hash function we've created - this doesn't appear to be a useful cryptographic (one-way) hash function, yet creating strings as output seems to be a poor choice for hashes used for performance purposes (as keys in unordered maps and sets, for example). First, as did owensss notice, the variable hashval is not initialized. I added locking on hash buckets which only minorly affects performance, and allows safe retrieval and storingof key value pairs. The nested loops mean we're doing much more work than traditional hash functions, which examine each input character just once (and are O(n) in the length of input). As for the hashing itself, I'm sure that the output looks fairly random, but it's actually doing a lot of work for the level of hashing that it supplies. Has the European Union taken any concrete steps towards reducing its economic dependency on China? ', '! Just state what your code does. Stack Exchange network consists of 176 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. I kinda tried to do everything from scratch, without many external libraries, just for studying. You don't need to know the string length. Simple hashing and salting algorithm. If we substitute ASCII codes for these characters, then we get 90 - 48, this is equal to 42 which is ASCII code for '*' character. Hash (key) = Elements % table size; 2 = 42 % 10; 8 = 78 % 10; 9 = 89 % 10; 4 = 64 % 10; The table representation can be seen as below: rev 2020.11.24.38066, The best answers are voted up and rise to the top, Code Review Stack Exchange works best with JavaScript enabled, Start here for a quick overview of the site, Detailed answers to any questions you might have, Discuss the workings and policies of this site, Learn more about Stack Overflow the company, Learn more about hiring developers or posting ads with us, @AlekseyDemakov Yes, you are correct about, Also, we shouldn't assume that the runtime will always call. Well, suppose at some moment c == 'Z', so this expression amounts to 'Z' - '0'. Which is the practical difference between a server and a web server? I also quite like your use of comments. Saving 1… Asking for help, clarification, or responding to other answers. Example: elements to be placed in a hash table are 42,78,89,64 and let’s take table size as 10. Don't check for NULL pointer argument. So you have transformed 'Z' into '*'. As for the alphabet thing, the standard option is to separate out the hash calculation (which should be a nice even spread across all bits) from the conversion to something visible for debugging. To learn more, see our tips on writing great answers. It's not quite clear what do you mean by "ASCII decimal value". What does it mean by "Selling one’s soul to Devil"? It's worth a comment explaining how these improve the algorithm, because it's not obvious to a casual reader. You can create a hash table, and add strings, long ints, doubles and pointers to it, keyed by stringsor long ints. Note that the C library functions perform internally as if char was unsigned char even when char is signed. alphabet and vars can be shared across all invocations, so should be declared static.
2020 simple hash function c#