diff --git a/clock/main.c b/clock/main.c index 1477b11..b83977d 100644 --- a/clock/main.c +++ b/clock/main.c @@ -1,6 +1,8 @@ +#define _CRT_SECURE_NO_WARNINGS #include #include #include +#include #include "../progressbar test/progressbar.h" // \033[ = CSI, CSI ?25l hides the cursor, CSI ?25h shows the cursor @@ -22,9 +24,9 @@ int main() { #ifdef WIN32 printf("ITS WINDOWS!\n"); #endif - + struct progressbar pb; - int n = 10, sl = 1; + int n = 1, sl = 1; printf(HIDE_CURSOR); printf("\033[385;31m"); progressbar_start(&pb, n * sl); @@ -57,12 +59,12 @@ int main() { printf("\033[385;%dm", i); for (int j = 30; j <= 37; j++) { printf("\033[385;%dm", j); - printf("%2X ", count); + printf("%02X ", count); count++; } for (int j = 90; j <= 97; j++) { printf("\033[385;%dm", j); - printf("%2X ", count); + printf("%02X ", count); count++; } printf("\n"); @@ -71,12 +73,12 @@ int main() { printf("\033[385;%dm", i); for (int j = 30; j <= 37; j++) { printf("\033[385;%dm", j); - printf("%2X ", count); + printf("%02X ", count); count++; } for (int j = 90; j <= 97; j++) { printf("\033[385;%dm", j); - printf("%2X ", count); + printf("%02X ", count); count++; } printf("\n"); diff --git a/hash functions/fnv.h b/hash functions/fnv.h new file mode 100644 index 0000000..2083a4a --- /dev/null +++ b/hash functions/fnv.h @@ -0,0 +1,249 @@ +/* + * fnv - Fowler/Noll/Vo- hash code + * + * @(#) $Revision: 5.4 $ + * @(#) $Id: fnv.h,v 5.4 2009/07/30 22:49:13 chongo Exp $ + * @(#) $Source: /usr/local/src/cmd/fnv/RCS/fnv.h,v $ + * + *** + * + * Fowler/Noll/Vo- hash + * + * The basis of this hash algorithm was taken from an idea sent + * as reviewer comments to the IEEE POSIX P1003.2 committee by: + * + * Phong Vo (http://www.research.att.com/info/kpv/) + * Glenn Fowler (http://www.research.att.com/~gsf/) + * + * In a subsequent ballot round: + * + * Landon Curt Noll (http://www.isthe.com/chongo/) + * + * improved on their algorithm. Some people tried this hash + * and found that it worked rather well. In an EMail message + * to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash. + * + * FNV hashes are designed to be fast while maintaining a low + * collision rate. The FNV speed allows one to quickly hash lots + * of data while maintaining a reasonable collision rate. See: + * + * http://www.isthe.com/chongo/tech/comp/fnv/index.html + * + * for more details as well as other forms of the FNV hash. + * + *** + * + * NOTE: The FNV-0 historic hash is not recommended. One should use + * the FNV-1 hash instead. + * + * To use the 32 bit FNV-0 historic hash, pass FNV0_32_INIT as the + * Fnv32_t hashval argument to fnv_32_buf() or fnv_32_str(). + * + * To use the 64 bit FNV-0 historic hash, pass FNV0_64_INIT as the + * Fnv64_t hashval argument to fnv_64_buf() or fnv_64_str(). + * + * To use the recommended 32 bit FNV-1 hash, pass FNV1_32_INIT as the + * Fnv32_t hashval argument to fnv_32_buf() or fnv_32_str(). + * + * To use the recommended 64 bit FNV-1 hash, pass FNV1_64_INIT as the + * Fnv64_t hashval argument to fnv_64_buf() or fnv_64_str(). + * + * To use the recommended 32 bit FNV-1a hash, pass FNV1_32A_INIT as the + * Fnv32_t hashval argument to fnv_32a_buf() or fnv_32a_str(). + * + * To use the recommended 64 bit FNV-1a hash, pass FNV1A_64_INIT as the + * Fnv64_t hashval argument to fnv_64a_buf() or fnv_64a_str(). + * + *** + * + * Please do not copyright this code. This code is in the public domain. + * + * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO + * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF + * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + * + * By: + * chongo /\oo/\ + * http://www.isthe.com/chongo/ + * + * Share and Enjoy! :-) + */ + +#if !defined(__FNV_H__) +#define __FNV_H__ + +#include + +#define FNV_VERSION "5.0.2" /* @(#) FNV Version */ + + +/* + * 32 bit FNV-0 hash type + */ +typedef u_int32_t Fnv32_t; + + +/* + * 32 bit FNV-0 zero initial basis + * + * This historic hash is not recommended. One should use + * the FNV-1 hash and initial basis instead. + */ +#define FNV0_32_INIT ((Fnv32_t)0) + + +/* + * 32 bit FNV-1 and FNV-1a non-zero initial basis + * + * The FNV-1 initial basis is the FNV-0 hash of the following 32 octets: + * + * chongo /\../\ + * + * NOTE: The \'s above are not back-slashing escape characters. + * They are literal ASCII backslash 0x5c characters. + * + * NOTE: The FNV-1a initial basis is the same value as FNV-1 by definition. + */ +#define FNV1_32_INIT ((Fnv32_t)0x811c9dc5) +#define FNV1_32A_INIT FNV1_32_INIT + + +/* + * determine how 64 bit unsigned values are represented + */ +#include "longlong.h" + + +/* + * 64 bit FNV-0 hash + */ +#if defined(HAVE_64BIT_LONG_LONG) +typedef u_int64_t Fnv64_t; +#else /* HAVE_64BIT_LONG_LONG */ +typedef struct { + u_int32_t w32[2]; /* w32[0] is low order, w32[1] is high order word */ +} Fnv64_t; +#endif /* HAVE_64BIT_LONG_LONG */ + + +/* + * 64 bit FNV-0 zero initial basis + * + * This historic hash is not recommended. One should use + * the FNV-1 hash and initial basis instead. + */ +#if defined(HAVE_64BIT_LONG_LONG) +#define FNV0_64_INIT ((Fnv64_t)0) +#else /* HAVE_64BIT_LONG_LONG */ +extern const Fnv64_t fnv0_64_init; +#define FNV0_64_INIT (fnv0_64_init) +#endif /* HAVE_64BIT_LONG_LONG */ + + +/* + * 64 bit FNV-1 non-zero initial basis + * + * The FNV-1 initial basis is the FNV-0 hash of the following 32 octets: + * + * chongo /\../\ + * + * NOTE: The \'s above are not back-slashing escape characters. + * They are literal ASCII backslash 0x5c characters. + * + * NOTE: The FNV-1a initial basis is the same value as FNV-1 by definition. + */ +#if defined(HAVE_64BIT_LONG_LONG) +#define FNV1_64_INIT ((Fnv64_t)0xcbf29ce484222325ULL) +#define FNV1A_64_INIT FNV1_64_INIT +#else /* HAVE_64BIT_LONG_LONG */ +extern const fnv1_64_init; +extern const Fnv64_t fnv1a_64_init; +#define FNV1_64_INIT (fnv1_64_init) +#define FNV1A_64_INIT (fnv1a_64_init) +#endif /* HAVE_64BIT_LONG_LONG */ + + +/* + * hash types + */ +enum fnv_type { + FNV_NONE = 0, /* invalid FNV hash type */ + FNV0_32 = 1, /* FNV-0 32 bit hash */ + FNV1_32 = 2, /* FNV-1 32 bit hash */ + FNV1a_32 = 3, /* FNV-1a 32 bit hash */ + FNV0_64 = 4, /* FNV-0 64 bit hash */ + FNV1_64 = 5, /* FNV-1 64 bit hash */ + FNV1a_64 = 6, /* FNV-1a 64 bit hash */ +}; + + +/* + * these test vectors are used as part o the FNV test suite + */ +struct test_vector { + void *buf; /* start of test vector buffer */ + int len; /* length of test vector */ +}; +struct fnv0_32_test_vector { + struct test_vector *test; /* test vector buffer to hash */ + Fnv32_t fnv0_32; /* expected FNV-0 32 bit hash value */ +}; +struct fnv1_32_test_vector { + struct test_vector *test; /* test vector buffer to hash */ + Fnv32_t fnv1_32; /* expected FNV-1 32 bit hash value */ +}; +struct fnv1a_32_test_vector { + struct test_vector *test; /* test vector buffer to hash */ + Fnv32_t fnv1a_32; /* expected FNV-1a 32 bit hash value */ +}; +struct fnv0_64_test_vector { + struct test_vector *test; /* test vector buffer to hash */ + Fnv64_t fnv0_64; /* expected FNV-0 64 bit hash value */ +}; +struct fnv1_64_test_vector { + struct test_vector *test; /* test vector buffer to hash */ + Fnv64_t fnv1_64; /* expected FNV-1 64 bit hash value */ +}; +struct fnv1a_64_test_vector { + struct test_vector *test; /* test vector buffer to hash */ + Fnv64_t fnv1a_64; /* expected FNV-1a 64 bit hash value */ +}; + + +/* + * external functions + */ +/* hash_32.c */ +extern Fnv32_t fnv_32_buf(void *buf, size_t len, Fnv32_t hashval); +extern Fnv32_t fnv_32_str(char *buf, Fnv32_t hashval); + +/* hash_32a.c */ +extern Fnv32_t fnv_32a_buf(void *buf, size_t len, Fnv32_t hashval); +extern Fnv32_t fnv_32a_str(char *buf, Fnv32_t hashval); + +/* hash_64.c */ +extern Fnv64_t fnv_64_buf(void *buf, size_t len, Fnv64_t hashval); +extern Fnv64_t fnv_64_str(char *buf, Fnv64_t hashval); + +/* hash_64a.c */ +extern Fnv64_t fnv_64a_buf(void *buf, size_t len, Fnv64_t hashval); +extern Fnv64_t fnv_64a_str(char *buf, Fnv64_t hashval); + +/* test_fnv.c */ +extern struct test_vector fnv_test_str[]; +extern struct fnv0_32_test_vector fnv0_32_vector[]; +extern struct fnv1_32_test_vector fnv1_32_vector[]; +extern struct fnv1a_32_test_vector fnv1a_32_vector[]; +extern struct fnv0_64_test_vector fnv0_64_vector[]; +extern struct fnv1_64_test_vector fnv1_64_vector[]; +extern struct fnv1a_64_test_vector fnv1a_64_vector[]; +extern void unknown_hash_type(char *prog, enum fnv_type type, int code); +extern void print_fnv32(Fnv32_t hval, Fnv32_t mask, int verbose, char *arg); +extern void print_fnv64(Fnv64_t hval, Fnv64_t mask, int verbose, char *arg); + + +#endif /* __FNV_H__ */ diff --git a/hash functions/fnvhash_32a.c b/hash functions/fnvhash_32a.c new file mode 100644 index 0000000..ebf80b2 --- /dev/null +++ b/hash functions/fnvhash_32a.c @@ -0,0 +1,129 @@ +/* + * hash_32 - 32 bit Fowler/Noll/Vo FNV-1a hash code + * + * @(#) $Revision: 5.1 $ + * @(#) $Id: hash_32a.c,v 5.1 2009/06/30 09:13:32 chongo Exp $ + * @(#) $Source: /usr/local/src/cmd/fnv/RCS/hash_32a.c,v $ + * + *** + * + * Fowler/Noll/Vo hash + * + * The basis of this hash algorithm was taken from an idea sent + * as reviewer comments to the IEEE POSIX P1003.2 committee by: + * + * Phong Vo (http://www.research.att.com/info/kpv/) + * Glenn Fowler (http://www.research.att.com/~gsf/) + * + * In a subsequent ballot round: + * + * Landon Curt Noll (http://www.isthe.com/chongo/) + * + * improved on their algorithm. Some people tried this hash + * and found that it worked rather well. In an EMail message + * to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash. + * + * FNV hashes are designed to be fast while maintaining a low + * collision rate. The FNV speed allows one to quickly hash lots + * of data while maintaining a reasonable collision rate. See: + * + * http://www.isthe.com/chongo/tech/comp/fnv/index.html + * + * for more details as well as other forms of the FNV hash. + *** + * + * To use the recommended 32 bit FNV-1a hash, pass FNV1_32A_INIT as the + * Fnv32_t hashval argument to fnv_32a_buf() or fnv_32a_str(). + * + *** + * + * Please do not copyright this code. This code is in the public domain. + * + * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO + * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF + * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + * + * By: + * chongo /\oo/\ + * http://www.isthe.com/chongo/ + * + * Share and Enjoy! :-) + */ + +#include +#include +#include "fnvhash_32a.h" + +/* + * fnv_32a_buf - perform a 32 bit Fowler/Noll/Vo FNV-1a hash on a buffer + * + * input: + * buf - start of buffer to hash + * len - length of buffer in octets + * hval - previous hash value or FNV1_32A_INIT if first call + * + * returns: + * 32 bit hash as a static hash type + * + * NOTE: To use the recommended 32 bit FNV-1a hash, use FNV1_32A_INIT as the + * hval arg on the first call to either fnv_32a_buf() or fnv_32a_str(). + */ +Fnv32_t fnv_32a_buf(void *buf, size_t len, Fnv32_t hval) +{ + unsigned char *bp = (unsigned char *)buf; /* start of buffer */ + unsigned char *be = bp + len; /* beyond end of buffer */ + + /* + * FNV-1a hash each octet in the buffer + */ + while (bp < be) { + + /* xor the bottom with the current octet */ + hval ^= (Fnv32_t)*bp++; + + /* multiply by the 32 bit FNV magic prime mod 2^32 */ + hval *= FNV_32_PRIME; + } + + /* return our new hash value */ + return hval; +} + + +/* + * fnv_32a_str - perform a 32 bit Fowler/Noll/Vo FNV-1a hash on a string + * + * input: + * str - string to hash + * hval- previous hash value or FNV1_32A_INIT if first call + * + * returns: + * 32 bit hash as a static hash type + * + * NOTE: To use the recommended 32 bit FNV-1a hash, use FNV1_32A_INIT as the + * hval arg on the first call to either fnv_32a_buf() or fnv_32a_str(). + */ +Fnv32_t fnv_32a_str(char *str, Fnv32_t hval) +{ + unsigned char *s = (unsigned char *)str; /* unsigned string */ + + /* + * FNV-1a hash each octet in the buffer + */ + while (*s) { + + /* xor the bottom with the current octet */ + hval ^= (Fnv32_t)*s++; + + /* multiply by the 32 bit FNV magic prime mod 2^32 */ + hval *= FNV_32_PRIME; + + } + + /* return our new hash value */ + return hval; +} diff --git a/hash functions/fnvhash_32a.h b/hash functions/fnvhash_32a.h new file mode 100644 index 0000000..fa71051 --- /dev/null +++ b/hash functions/fnvhash_32a.h @@ -0,0 +1,86 @@ +#pragma once + +/* + * https://en.wikipedia.org/wiki/FowlerNollVo_hash_function + * http://www.isthe.com/chongo/tech/comp/fnv/index.html +*/ + +/* + * To use the recommended 32 bit FNV-1a hash, pass FNV1_32A_INIT as the + * Fnv32_t hashval argument to fnv_32a_buf() or fnv_32a_str(). +*/ + +typedef uint32_t Fnv32_t; + +/* + * 32 bit magic FNV-1a prime +*/ +#define FNV_32_PRIME ((Fnv32_t)0x01000193) + +/* + * 32 bit FNV-1 and FNV-1a non-zero initial basis + * + * The FNV-1 initial basis is the FNV-0 hash of the following 32 octets: + * + * chongo /\../\ + * + * NOTE: The \'s above are not back-slashing escape characters. + * They are literal ASCII backslash 0x5c characters. + * + * NOTE: The FNV-1a initial basis is the same value as FNV-1 by definition. + */ +#define FNV1_32_INIT ((Fnv32_t)0x811c9dc5) +#define FNV1_32A_INIT FNV1_32_INIT + +/* + * If you need an x-bit hash where x is not a power of 2, + * then we recommend that you compute the FNV hash that is just larger than x-bits + * and xor-fold the result down to x-bits.By xor-folding we mean shift the excess + * high order bits down and xor them with the lower x - bits. +*/ + +//For example to produce a 24 bit FNV-1 hash in C we xor-fold fold a 32 bit FNV-1 hash: + +#define MASK_24 (((uint32_t)1<<24)-1) /* i.e., (uint32_t)0xffffff */ +/* +uint32_t hash; +void* data; +size_t data_len; + +hash = fnv_32_buf(data, data_len, FNV1_32_INIT); +hash = (hash >> 24) ^ (hash & MASK_24); +*/ + +#define FNV_24(hash) ((hash >> 24) ^ (hash & MASK_24)) + +//To produce a 16 bit FNV-1 hash in C we xor-fold fold a 32 bit FNV-1 hash: + +#define MASK_16 (((uint32_t)1<<16)-1) /* i.e., (uint32_t)0xffff */ +/* +uint32_t hash; +void* data; +size_t data_len; + +hash = fnv_32_buf(data, data_len, FNV1_32_INIT); +hash = (hash >> 16) ^ (hash & MASK_16); +*/ + +#define FNV_16(hash) ((hash >> 16) ^ (hash & MASK_16)) + +//For tiny x < 16 bit values, we recommend using a 32 bit FNV - 1 hash as follows: + +/* NOTE: for 0 < x < 16 ONLY!!! */ +#define TINY_MASK(x) (((uint32_t)1<<(x))-1) +/* +uint32_t hash; +void* data; +size_t data_len; + +hash = fnv_32_buf(data, data_len, FNV1_32_INIT); +hash = (((hash >> x) ^ hash) & TINY_MASK(x)); +*/ + +#define TINY_FNV(x, hash) (((hash >> x) ^ hash) & TINY_MASK(x)) + +Fnv32_t fnv_32a_buf(void* buf, size_t len, Fnv32_t hval); +Fnv32_t fnv_32a_str(char* str, Fnv32_t hval); diff --git a/hash functions/hash functions.sln b/hash functions/hash functions.sln new file mode 100644 index 0000000..dabaac0 --- /dev/null +++ b/hash functions/hash functions.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35527.113 d17.12 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hash functions", "hash functions.vcxproj", "{61817318-7F41-4087-850B-FEE499E3CE31}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {61817318-7F41-4087-850B-FEE499E3CE31}.Debug|x64.ActiveCfg = Debug|x64 + {61817318-7F41-4087-850B-FEE499E3CE31}.Debug|x64.Build.0 = Debug|x64 + {61817318-7F41-4087-850B-FEE499E3CE31}.Debug|x86.ActiveCfg = Debug|Win32 + {61817318-7F41-4087-850B-FEE499E3CE31}.Debug|x86.Build.0 = Debug|Win32 + {61817318-7F41-4087-850B-FEE499E3CE31}.Release|x64.ActiveCfg = Release|x64 + {61817318-7F41-4087-850B-FEE499E3CE31}.Release|x64.Build.0 = Release|x64 + {61817318-7F41-4087-850B-FEE499E3CE31}.Release|x86.ActiveCfg = Release|Win32 + {61817318-7F41-4087-850B-FEE499E3CE31}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/hash functions/hash functions.vcxproj b/hash functions/hash functions.vcxproj new file mode 100644 index 0000000..be0d42d --- /dev/null +++ b/hash functions/hash functions.vcxproj @@ -0,0 +1,142 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {61817318-7f41-4087-850b-fee499e3ce31} + hashfunctions + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hash functions/hash functions.vcxproj.filters b/hash functions/hash functions.vcxproj.filters new file mode 100644 index 0000000..75d394d --- /dev/null +++ b/hash functions/hash functions.vcxproj.filters @@ -0,0 +1,39 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + \ No newline at end of file diff --git a/hash functions/jhash.c b/hash functions/jhash.c new file mode 100644 index 0000000..44f6c2f --- /dev/null +++ b/hash functions/jhash.c @@ -0,0 +1,75 @@ +#include +#include +#include +#include "jhash.h" + +/* +from https://www.burtleburtle.net/bob/c/lookup2.c +-------------------------------------------------------------------- +jhash() -- hash a variable-length key into a 32-bit value + k : the key (the unaligned variable-length array of bytes) + len : the length of the key, counting by bytes + initval : the previous hash, or an arbitrary value +Returns a 32-bit value. Every bit of the key affects every bit of +the return value. Every 1-bit and 2-bit delta achieves avalanche. +About 36+6len instructions. + +The best hash table sizes are powers of 2. There is no need to do +mod a prime (mod is sooo slow!). If you need less than 32 bits, +use a bitmask. For example, if you need only 10 bits, do + h = (h & hashmask(10)); +In which case, the hash table should have hashsize(10) elements. + +If you are hashing n strings (ub1 **)k, do it like this: + for (i=0, h=0; i= 12) + { + a += (k[0] + ((ub4)k[1] << 8) + ((ub4)k[2] << 16) + ((ub4)k[3] << 24)); + b += (k[4] + ((ub4)k[5] << 8) + ((ub4)k[6] << 16) + ((ub4)k[7] << 24)); + c += (k[8] + ((ub4)k[9] << 8) + ((ub4)k[10] << 16) + ((ub4)k[11] << 24)); + mix(a, b, c); + k += 12; len -= 12; + } + + /*------------------------------------- handle the last 11 bytes */ + c += length; + switch (len) /* all the case statements fall through */ + { + case 11: c += ((ub4)k[10] << 24); + case 10: c += ((ub4)k[9] << 16); + case 9: c += ((ub4)k[8] << 8); + /* the first byte of c is reserved for the length */ + case 8: b += ((ub4)k[7] << 24); + case 7: b += ((ub4)k[6] << 16); + case 6: b += ((ub4)k[5] << 8); + case 5: b += k[4]; + case 4: a += ((ub4)k[3] << 24); + case 3: a += ((ub4)k[2] << 16); + case 2: a += ((ub4)k[1] << 8); + case 1: a += k[0]; + /* case 0: nothing left to add */ + } + mix(a, b, c); + /*-------------------------------------------- report the result */ + return c; +} \ No newline at end of file diff --git a/hash functions/jhash.h b/hash functions/jhash.h new file mode 100644 index 0000000..cd7dac8 --- /dev/null +++ b/hash functions/jhash.h @@ -0,0 +1,48 @@ +#pragma once + +typedef unsigned long int ub4; /* unsigned 4-byte quantities */ +typedef unsigned char ub1; + +#define hashsize(n) ((ub4)1<<(n)) +#define hashmask(n) (hashsize(n)-1) + +/* +-------------------------------------------------------------------- +mix -- mix 3 32-bit values reversibly. +For every delta with one or two bit set, and the deltas of all three + high bits or all three low bits, whether the original value of a,b,c + is almost all zero or is uniformly distributed, +* If mix() is run forward or backward, at least 32 bits in a,b,c + have at least 1/4 probability of changing. +* If mix() is run forward, every bit of c will change between 1/3 and + 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.) +mix() was built out of 36 single-cycle latency instructions in a + structure that could supported 2x parallelism, like so: + a -= b; + a -= c; x = (c>>13); + b -= c; a ^= x; + b -= a; x = (a<<8); + c -= a; b ^= x; + c -= b; x = (b>>13); + ... + Unfortunately, superscalar Pentiums and Sparcs can't take advantage + of that parallelism. They've also turned some of those single-cycle + latency instructions into multi-cycle latency instructions. Still, + this is the fastest good hash I could find. There were about 2^^68 + to choose from. I only looked at a billion or so. +-------------------------------------------------------------------- +*/ +#define mix(a,b,c) \ +{ \ + a -= b; a -= c; a ^= (c>>13); \ + b -= c; b -= a; b ^= (a<<8); \ + c -= a; c -= b; c ^= (b>>13); \ + a -= b; a -= c; a ^= (c>>12); \ + b -= c; b -= a; b ^= (a<<16); \ + c -= a; c -= b; c ^= (b>>5); \ + a -= b; a -= c; a ^= (c>>3); \ + b -= c; b -= a; b ^= (a<<10); \ + c -= a; c -= b; c ^= (b>>15); \ +} + +ub4 jhash(register ub1* k, register ub4 length, register ub4 initval); \ No newline at end of file diff --git a/hash functions/main.c b/hash functions/main.c new file mode 100644 index 0000000..a85b5a4 --- /dev/null +++ b/hash functions/main.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include + +#include "jhash.h" +#include "fnvhash_32a.h" + +// Assumes little endian +void printBits(size_t const size, void const* const ptr) { + unsigned char* b = (unsigned char*)ptr; + unsigned char byte; + int i, j; + + for (i = (int)size - 1; i >= 0; i--) { + for (j = 7; j >= 0; j--) { + byte = (b[i] >> j) & 1; + printf("%u", byte); + } + } + puts(""); +} + +int main() { + char s1[] = "cbbc"; + char s2[] = "cddc"; + Fnv32_t h1 = fnv_32a_str(s1, FNV1_32A_INIT); + Fnv32_t h2 = fnv_32a_str(s2, FNV1_32A_INIT); + Fnv32_t h11 = h1; + + + printBits(4, &h1); + h1 = FNV_16(h1); + h11 = TINY_FNV(15, h1); + printBits(4, &h1); + printBits(4, &h11); + + return 0; +} \ No newline at end of file diff --git a/progressbar test/progressbar.c b/progressbar test/progressbar.c index c175c38..4248adc 100644 --- a/progressbar test/progressbar.c +++ b/progressbar test/progressbar.c @@ -9,7 +9,7 @@ static unsigned get_to_print(unsigned current, unsigned max) { return progressbar_width * current / max; } -unsigned get_percentage(unsigned current, unsigned max) { +static unsigned get_percentage(unsigned current, unsigned max) { return 100 * current / max; } diff --git a/progressbar test/progressbar.h b/progressbar test/progressbar.h index 238f56b..2b82e85 100644 --- a/progressbar test/progressbar.h +++ b/progressbar test/progressbar.h @@ -17,6 +17,4 @@ void progressbar_inc(struct progressbar *pb); // clear line of progress bar void progressbar_clear(); -unsigned get_percentage(unsigned current, unsigned max); - #endif diff --git a/Лекции/FIST2024_OsnProgram_lek_14_ATD_DvoichnoeDerevo_Khesh.pptx b/Лекции/FIST2024_OsnProgram_lek_14_ATD_DvoichnoeDerevo_Khesh.pptx new file mode 100644 index 0000000..3494089 Binary files /dev/null and b/Лекции/FIST2024_OsnProgram_lek_14_ATD_DvoichnoeDerevo_Khesh.pptx differ