am1x icon

mtonum001.cpp

am1x | PRO | 01/01/23 05:32:24 PM UTC (Edited) | 0 ⭐ | 1815 👁️ | Never ⏰ | []
C++ |

1.24 KB

|

None

|

0 👍

/

0 👎

#include <stdio.h>
#include <inttypes.h>
#include <assert.h>
 
const uint32_t N = 100000000;
const uint32_t M = 12, Z = 26, H = M + 1;
 
static const char* mnames[M + 1] = {
    "",
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec"
};
 
 
static const uint32_t mhash[H] = {0, 2, 6, 1, 10, 4, 7, 11, 3, 8, 9, 12, 5};
 
uint32_t month_to_num(const unsigned char *s)
{
    uint32_t c0 = s[0] - 'A';
    if (Z <= c0)
        return 0;
    uint32_t c1 = s[1] - 'a';
    if (Z <= c1)
        return 0;
    uint32_t c2 = s[2] - 'a';
    if (Z <= c2)
        return 0;
    if (s[3])
        return 0;
    uint32_t z = ((1431655833 * c2) ^ c1) % H;
    if (z == 0)
        return z;
    z = mhash[z];
    assert (0 < z && z <= M);
    const unsigned char *s0 = (const unsigned char *) mnames[z];
    if (s[0] != s0[0] || s[1] != s0[1] || s[2] != s0[2])
        return 0;
    return z;
}
 
 
int main()
{
    uint32_t acc = 0;
    for (uint32_t j = 0; j < N; j++) {
        for (uint32_t i = 0; i <= M; i++) {
            acc += month_to_num((const unsigned char*) mnames[i]);
        }
    }
 
    printf("acc = %u \n", acc);
    for (uint32_t i = 0; i <= M; i++) {
        uint32_t z = month_to_num((const unsigned char*) mnames[i]);
        printf("%3s %2u %2u \n", mnames[i], z, i);
    }
 
    return 0;
}
 

Comments