Advertisement
am1x

mtonum001.cpp

Jan 1st, 2023 (edited)
1,342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <inttypes.h>
  3. #include <assert.h>
  4.  
  5. const uint32_t N = 100000000;
  6. const uint32_t M = 12, Z = 26, H = M + 1;
  7.  
  8. static const char* mnames[M + 1] = {
  9.     "",
  10.     "Jan",
  11.     "Feb",
  12.     "Mar",
  13.     "Apr",
  14.     "May",
  15.     "Jun",
  16.     "Jul",
  17.     "Aug",
  18.     "Sep",
  19.     "Oct",
  20.     "Nov",
  21.     "Dec"
  22. };
  23.  
  24.  
  25. static const uint32_t mhash[H] = {0, 2, 6, 1, 10, 4, 7, 11, 3, 8, 9, 12, 5};
  26.  
  27. uint32_t month_to_num(const unsigned char *s)
  28. {
  29.     uint32_t c0 = s[0] - 'A';
  30.     if (Z <= c0)
  31.         return 0;
  32.     uint32_t c1 = s[1] - 'a';
  33.     if (Z <= c1)
  34.         return 0;
  35.     uint32_t c2 = s[2] - 'a';
  36.     if (Z <= c2)
  37.         return 0;
  38.     if (s[3])
  39.         return 0;
  40.     uint32_t z = ((1431655833 * c2) ^ c1) % H;
  41.     if (z == 0)
  42.         return z;
  43.     z = mhash[z];
  44.     assert (0 < z && z <= M);
  45.     const unsigned char *s0 = (const unsigned char *) mnames[z];
  46.     if (s[0] != s0[0] || s[1] != s0[1] || s[2] != s0[2])
  47.         return 0;
  48.     return z;
  49. }
  50.  
  51.  
  52. int main()
  53. {
  54.     uint32_t acc = 0;
  55.     for (uint32_t j = 0; j < N; j++) {
  56.         for (uint32_t i = 0; i <= M; i++) {
  57.             acc += month_to_num((const unsigned char*) mnames[i]);
  58.         }
  59.     }
  60.  
  61.     printf("acc = %u \n", acc);
  62.     for (uint32_t i = 0; i <= M; i++) {
  63.         uint32_t z = month_to_num((const unsigned char*) mnames[i]);
  64.         printf("%3s %2u %2u \n", mnames[i], z, i);
  65.     }
  66.  
  67.     return 0;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement