File: 027.c

package info (click to toggle)
cfengine3 3.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,552 kB
  • sloc: ansic: 163,161; sh: 10,296; python: 2,950; makefile: 1,744; lex: 784; yacc: 633; perl: 211; pascal: 157; xml: 21; sed: 13
file content (60 lines) | stat: -rw-r--r-- 1,337 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define SPLAY_PSEUDO_RANDOM_CONSTANT 8192

#define HOURLY 12
main()
{
    char s[] = "a";
    char c;
    int hash, box, minblocks;
    int period = HOURLY - 1;
    char *boxes[HOURLY];
    for (c = 0; c < HOURLY; c++)
    {
        boxes[c] = 0;
    }
    for (c = 'a'; c <= 'z'; c++)
    {
        *s = c;
        // The block algorithm is copied from evalfunction.c
        hash = OatHash(s);
        box =
            (int) (0.5 + period * hash / (double) SPLAY_PSEUDO_RANDOM_CONSTANT);
        minblocks = box % HOURLY;
        // Back to original code
        if (!boxes[minblocks])
        {
            boxes[minblocks] = strncpy((char *) malloc(2), s, 2);
        }
    }
    printf("    \"ok\" xor => {\n");
    for (c = 0; c < HOURLY; c++)
    {
        printf("\tsplayclass(\"%s\",\"hourly\"), # Box %d\n", boxes[c], c);
    }
    printf("\t};\n");
}

// This is copied from files_hashes.c
int OatHash(char *key)
{
    unsigned int hashtablesize = SPLAY_PSEUDO_RANDOM_CONSTANT;
    unsigned char *p = key;
    unsigned h = 0;
    int i, len = strlen(key);

    for (i = 0; i < len; i++)
    {
        h += p[i];
        h += (h << 10);
        h ^= (h >> 6);
    }

    h += (h << 3);
    h ^= (h >> 11);
    h += (h << 15);

    return (h & (hashtablesize - 1));
}