fast-stats 1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2333c25bd11e2269552ec7a993f4669e7678a46e
4
+ data.tar.gz: 24b6f8bfcb9394f491696f0db9a8c821b5fce972
5
+ SHA512:
6
+ metadata.gz: bb6441cdf70869f5520ae8c27ff87df3d7b9c3c662186ee48e870ee8532f2dac9fbb32b9ede05c5d7c8d385893775c66af0b552211acfc3d9e845f235d601132
7
+ data.tar.gz: d1d9c73c37d20620009fa0db0ece7039757c41c5096916c5eb1bf106ba2f2c7cef16f6912735d6f4d6e838b154bf26368b38f3fb8dccb299036b42ff987b60fa
data/Makefile ADDED
@@ -0,0 +1,23 @@
1
+ .PHONY: all build test copy-lib-files
2
+
3
+ GEMVERSION = $(shell ruby -e 'require "./lib/stats/version.rb"; puts StatsGem::VERSION')
4
+ $(info gem version is $(GEMVERSION))
5
+
6
+ all: build test
7
+
8
+ build: copy-lib-files
9
+ gem build fast-stats.gemspec
10
+ gem install fast-stats-$(GEMVERSION).gem
11
+
12
+ copy-lib-files:
13
+ if [ ! -d ext/stats/stats ]; then mkdir ext/stats/stats; fi
14
+ cp ../include/stats/*.h ext/stats/stats
15
+ cp ../src/*.c ext/stats
16
+ cp ../ext/*.c ext/stats
17
+ cp ../ext/*.h ext/stats
18
+
19
+ test:
20
+ ruby ./test/test_stats.rb
21
+
22
+ benchmark:
23
+ ruby ./test/benchmark_stats.rb
@@ -0,0 +1,12 @@
1
+ error.c
2
+ hash.c
3
+ lock.c
4
+ mt19937.c
5
+ mt19937.h
6
+ semaphore.c
7
+ shared_mem.c
8
+ stats
9
+ stats.c
10
+ strlcat.c
11
+ strlcpy.c
12
+
data/ext/stats/error.c ADDED
@@ -0,0 +1,42 @@
1
+ /* error.c */
2
+
3
+ #include "stats/error.h"
4
+
5
+ const char * error_message(int code)
6
+ {
7
+ switch (code)
8
+ {
9
+ case S_OK: return "S_OK";
10
+
11
+ case ERROR_FAIL: return "ERRROR_FAIL";
12
+ case ERROR_INVALID_PARAMETERS: return "ERROR_INVALID_PARAMETERS";
13
+ case ERROR_MEMORY: return "ERROR_MEMORY";
14
+
15
+ case ERROR_SHARED_MEM_NAME_TOO_LONG: return "ERROR_SHARED_MEM_NAME_TOO_LONG";
16
+ case ERROR_SHARED_MEM_CANNOT_CREATE_DIRECTORY: return "ERROR_SHARED_MEM_CANNOT_CREATE_DIRECTORY";
17
+ case ERROR_SHARED_MEM_PATH_NOT_DIRECTORY: return "ERROR_SHARED_MEM_PATH_NOT_DIRECTORY";
18
+ case ERROR_SHARED_MEM_CANNOT_CREATE_PATH: return "ERROR_SHARED_MEM_CANNOT_CREATE_PATH";
19
+ case ERROR_SHARED_MEM_CANNOT_CREATE_IPC_TOKEN: return "ERROR_SHARED_MEM_CANNOT_CREATE_IPC_TOKEN";
20
+ case ERROR_SHARED_MEM_ALREADY_EXISTS: return "ERROR_SHARED_MEM_ALREADY_EXISTS";
21
+ case ERROR_SHARED_MEM_DOES_NOT_EXIST: return "ERROR_SHARED_MEM_DOES_NOT_EXIST";
22
+ case ERROR_SHARED_MEM_INVALID_SIZE: return "ERROR_SHARED_MEM_INVALID_SIZE";
23
+ case ERROR_SHARED_MEM_CANNOT_STAT: return "ERROR_SHARED_MEM_CANNOT_STAT";
24
+ case ERROR_SHARED_MEM_CANNOT_OPEN: return "ERROR_SHARED_MEM_CANNOT_OPEN";
25
+ case ERROR_SHARED_MEM_CANNOT_ATTACH: return "ERROR_SHARED_MEM_CANNOT_ATTACH";
26
+
27
+ case ERROR_SEMAPHORE_NAME_TOO_LONG: return "ERROR_SEMAPHORE_NAME_TOO_LONG";
28
+ case ERROR_SEMAPHORE_CANNOT_CREATE_DIRECTORY: return "ERROR_SEMAPHORE_CANNOT_CREATE_DIRECTORY";
29
+ case ERROR_SEMAPHORE_PATH_NOT_DIRECTORY: return "ERROR_SEMAPHORE_PATH_NOT_DIRECTORY";
30
+ case ERROR_SEMAPHORE_CANNOT_CREATE_PATH: return "ERROR_SEMAPHORE_CANNOT_CREATE_PATH";
31
+ case ERROR_SEMAPHORE_CANNOT_CREATE_IPC_TOKEN: return "ERROR_SEMAPHORE_CANNOT_CREATE_IPC_TOKEN";
32
+ case ERROR_SEMAPHORE_ALREADY_EXISTS: return "ERROR_SEMAPHORE_ALREADY_EXISTS";
33
+ case ERROR_SEMAPHORE_DOES_NOT_EXIST: return "ERROR_SEMAPHORE_DOES_NOT_EXIST";
34
+ case ERROR_SEMAPHORE_INVALID_SIZE: return "ERROR_SEMAPHORE_INVALID_SIZE";
35
+ case ERROR_SEMAPHORE_CANNOT_OPEN: return "ERROR_SEMAPHORE_CANNOT_OPEN";
36
+
37
+ case ERROR_STATS_CANNOT_ALLOCATE_COUNTER: return "ERROR_STATS_CANNOT_ALLOCATE_COUNTER";
38
+ case ERROR_STATS_KEY_TOO_LONG: return "ERROR_STATS_KEY_TOO_LONG";
39
+
40
+ }
41
+ return "UNKNOWN_ERROR";
42
+ }
@@ -0,0 +1,18 @@
1
+ require 'mkmf'
2
+ require 'rbconfig'
3
+
4
+ $CFLAGS << ' -Wall -Wno-multichar'
5
+ $CFLAGS << ' -Wextra -O0 -ggdb3' if ENV['DEBUG']
6
+
7
+ $CFLAGS << ' -DDEBUG=1' if ENV['DEBUG']
8
+ $CFLAGS << ' -DDEBUG=0' unless ENV['DEBUG']
9
+
10
+ $uname = `uname -a`
11
+ if /linux/i =~ $uname
12
+ $CFLAGS << ' -DLINUX'
13
+ have_library('rt','clock_gettime') && append_library($libs,'rt')
14
+ end
15
+
16
+ $CFLAGS << ' -DDARWIN' if /darwin/i =~ $uname
17
+
18
+ create_makefile('stats/stats')
data/ext/stats/hash.c ADDED
@@ -0,0 +1,66 @@
1
+ /*
2
+ * Paul Hsieh's SuperFastHash function
3
+ *
4
+ * http://www.azillionmonkeys.com/qed/hash.html
5
+ *
6
+ * Released under LGPL 2.1
7
+ */
8
+
9
+ #include "stats/hash.h"
10
+
11
+ #undef get16bits
12
+ #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
13
+ #define get16bits(d) (*((const uint16_t *) (d)))
14
+ #endif
15
+
16
+ #if !defined (get16bits)
17
+ #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
18
+ +(uint32_t)(((const uint8_t *)(d))[0]) )
19
+ #endif
20
+
21
+ uint32_t fast_hash(const char * data, int len)
22
+ {
23
+ uint32_t hash = len, tmp;
24
+ int rem;
25
+
26
+ if (len <= 0 || data == 0)
27
+ return 0;
28
+
29
+ rem = len & 3;
30
+ len >>= 2;
31
+
32
+ /* Main loop */
33
+ for (;len > 0; len--) {
34
+ hash += get16bits (data);
35
+ tmp = (get16bits (data+2) << 11) ^ hash;
36
+ hash = (hash << 16) ^ tmp;
37
+ data += 2*sizeof (uint16_t);
38
+ hash += hash >> 11;
39
+ }
40
+
41
+ /* Handle end cases */
42
+ switch (rem) {
43
+ case 3: hash += get16bits (data);
44
+ hash ^= hash << 16;
45
+ hash ^= ((signed char)data[sizeof (uint16_t)]) << 18;
46
+ hash += hash >> 11;
47
+ break;
48
+ case 2: hash += get16bits (data);
49
+ hash ^= hash << 11;
50
+ hash += hash >> 17;
51
+ break;
52
+ case 1: hash += (signed char)*data;
53
+ hash ^= hash << 10;
54
+ hash += hash >> 1;
55
+ }
56
+
57
+ /* Force "avalanching" of final 127 bits */
58
+ hash ^= hash << 3;
59
+ hash += hash >> 5;
60
+ hash ^= hash << 4;
61
+ hash += hash >> 17;
62
+ hash ^= hash << 25;
63
+ hash += hash >> 6;
64
+
65
+ return hash;
66
+ }
data/ext/stats/lock.c ADDED
@@ -0,0 +1,39 @@
1
+ /* lock.c */
2
+
3
+ #include <stdio.h>
4
+ #include <stdlib.h>
5
+
6
+ #include "stats/error.h"
7
+ #include "stats/lock.h"
8
+
9
+ int lock_create(const char * name, struct lock **lock_out)
10
+ {
11
+ struct lock * lock;
12
+ int err;
13
+
14
+ if (lock_out == NULL)
15
+ return ERROR_INVALID_PARAMETERS;
16
+
17
+ lock = malloc(sizeof(struct lock));
18
+ if (!lock)
19
+ return ERROR_MEMORY;
20
+
21
+ err = lock_init(lock,name);
22
+ if (err == S_OK)
23
+ {
24
+ *lock_out = lock;
25
+ }
26
+ else
27
+ {
28
+ free(lock);
29
+ *lock_out = NULL;
30
+ }
31
+
32
+ return err;
33
+ }
34
+
35
+
36
+ void lock_free(struct lock *lock)
37
+ {
38
+ free(lock);
39
+ }
@@ -0,0 +1,209 @@
1
+ /*
2
+ A C-program for MT19937, with initialization improved 2002/1/26.
3
+ Coded by Takuji Nishimura and Makoto Matsumoto.
4
+
5
+ Before using, initialize the state by using init_genrand(seed)
6
+ or init_by_array(init_key, key_length).
7
+
8
+ Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
9
+ All rights reserved.
10
+
11
+ Redistribution and use in source and binary forms, with or without
12
+ modification, are permitted provided that the following conditions
13
+ are met:
14
+
15
+ 1. Redistributions of source code must retain the above copyright
16
+ notice, this list of conditions and the following disclaimer.
17
+
18
+ 2. Redistributions in binary form must reproduce the above copyright
19
+ notice, this list of conditions and the following disclaimer in the
20
+ documentation and/or other materials provided with the distribution.
21
+
22
+ 3. The names of its contributors may not be used to endorse or promote
23
+ products derived from this software without specific prior written
24
+ permission.
25
+
26
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
+
38
+
39
+ Any feedback is very welcome.
40
+ http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
41
+ email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
42
+ */
43
+
44
+ #include <stdio.h>
45
+ #include "mt19937.h"
46
+
47
+ /* Period parameters */
48
+ #define N 624
49
+ #define M 397
50
+ #define MATRIX_A 0x9908b0dfUL /* constant vector a */
51
+ #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
52
+ #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
53
+
54
+ static unsigned long mt[N]; /* the array for the state vector */
55
+ static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
56
+
57
+ /* initializes mt[N] with a seed */
58
+ void init_genrand(unsigned long s)
59
+ {
60
+ mt[0]= s & 0xffffffffUL;
61
+ for (mti=1; mti<N; mti++) {
62
+ mt[mti] =
63
+ (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
64
+ /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
65
+ /* In the previous versions, MSBs of the seed affect */
66
+ /* only MSBs of the array mt[]. */
67
+ /* 2002/01/09 modified by Makoto Matsumoto */
68
+ mt[mti] &= 0xffffffffUL;
69
+ /* for >32 bit machines */
70
+ }
71
+ }
72
+
73
+ /* initialize by an array with array-length */
74
+ /* init_key is the array for initializing keys */
75
+ /* key_length is its length */
76
+ /* slight change for C++, 2004/2/26 */
77
+ void init_by_array(unsigned long init_key[], int key_length)
78
+ {
79
+ int i, j, k;
80
+ init_genrand(19650218UL);
81
+ i=1; j=0;
82
+ k = (N>key_length ? N : key_length);
83
+ for (; k; k--) {
84
+ mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
85
+ + init_key[j] + j; /* non linear */
86
+ mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
87
+ i++; j++;
88
+ if (i>=N) { mt[0] = mt[N-1]; i=1; }
89
+ if (j>=key_length) j=0;
90
+ }
91
+ for (k=N-1; k; k--) {
92
+ mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
93
+ - i; /* non linear */
94
+ mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
95
+ i++;
96
+ if (i>=N) { mt[0] = mt[N-1]; i=1; }
97
+ }
98
+
99
+ mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
100
+ }
101
+
102
+ /* generates a random number on [0,0xffffffff]-interval */
103
+ unsigned long genrand_int32(void)
104
+ {
105
+ unsigned long y;
106
+ static unsigned long mag01[2]={0x0UL, MATRIX_A};
107
+ /* mag01[x] = x * MATRIX_A for x=0,1 */
108
+
109
+ if (mti >= N) { /* generate N words at one time */
110
+ int kk;
111
+
112
+ if (mti == N+1) /* if init_genrand() has not been called, */
113
+ init_genrand(5489UL); /* a default initial seed is used */
114
+
115
+ for (kk=0;kk<N-M;kk++) {
116
+ y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
117
+ mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
118
+ }
119
+ for (;kk<N-1;kk++) {
120
+ y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
121
+ mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
122
+ }
123
+ y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
124
+ mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
125
+
126
+ mti = 0;
127
+ }
128
+
129
+ y = mt[mti++];
130
+
131
+ /* Tempering */
132
+ y ^= (y >> 11);
133
+ y ^= (y << 7) & 0x9d2c5680UL;
134
+ y ^= (y << 15) & 0xefc60000UL;
135
+ y ^= (y >> 18);
136
+
137
+ return y;
138
+ }
139
+
140
+ /* generates a random number on [0,0x7fffffff]-interval */
141
+ long genrand_int31(void)
142
+ {
143
+ return (long)(genrand_int32()>>1);
144
+ }
145
+
146
+ /* generates a random number on [0,1]-real-interval */
147
+ double genrand_real1(void)
148
+ {
149
+ return genrand_int32()*(1.0/4294967295.0);
150
+ /* divided by 2^32-1 */
151
+ }
152
+
153
+ /* generates a random number on [0,1)-real-interval */
154
+ double genrand_real2(void)
155
+ {
156
+ return genrand_int32()*(1.0/4294967296.0);
157
+ /* divided by 2^32 */
158
+ }
159
+
160
+ /* generates a random number on (0,1)-real-interval */
161
+ double genrand_real3(void)
162
+ {
163
+ return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0);
164
+ /* divided by 2^32 */
165
+ }
166
+
167
+ /* generates a random number on [0,1) with 53-bit resolution*/
168
+ double genrand_res53(void)
169
+ {
170
+ unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6;
171
+ return(a*67108864.0+b)*(1.0/9007199254740992.0);
172
+ }
173
+ /* These real versions are due to Isaku Wada, 2002/01/09 added */
174
+
175
+
176
+ void init_rng()
177
+ {
178
+ unsigned long seeds[16];
179
+ FILE * fp;
180
+
181
+ fp = fopen("/dev/urandom", "r");
182
+ if (fp)
183
+ {
184
+ fread(seeds,sizeof(unsigned long),16,fp);
185
+ fclose(fp);
186
+ }
187
+
188
+ init_by_array(seeds,16);
189
+ }
190
+
191
+ #if 0
192
+ int main(void)
193
+ {
194
+ int i;
195
+ unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4;
196
+ init_by_array(init, length);
197
+ printf("1000 outputs of genrand_int32()\n");
198
+ for (i=0; i<1000; i++) {
199
+ printf("%10lu ", genrand_int32());
200
+ if (i%5==4) printf("\n");
201
+ }
202
+ printf("\n1000 outputs of genrand_real2()\n");
203
+ for (i=0; i<1000; i++) {
204
+ printf("%10.8f ", genrand_real2());
205
+ if (i%5==4) printf("\n");
206
+ }
207
+ return 0;
208
+ }
209
+ #endif
@@ -0,0 +1,16 @@
1
+ /* mt19937.h */
2
+
3
+ #ifndef _MT19937_H_
4
+ #define _MT19937_H_
5
+
6
+ void init_rng();
7
+ void init_genrand(unsigned long s);
8
+ void init_by_array(unsigned long init_key[], int key_length);
9
+ unsigned long genrand_int32(void);
10
+ long genrand_int31(void);
11
+ double genrand_real1(void);
12
+ double genrand_real2(void);
13
+ double genrand_real3(void);
14
+ double genrand_res53(void);
15
+
16
+ #endif