chrislo-sourceclassifier 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +325 -0
- data/README.textile +51 -0
- data/Rakefile +64 -0
- data/examples/example.rb +22 -0
- data/lib/sourceclassifier.rb +22 -0
- data/lib/trainer.rb +51 -0
- data/sourceclassifier.gemspec +35 -0
- data/sources/gcc/ackermann.gcc-2.gcc +93 -0
- data/sources/gcc/ackermann.gcc-3.gcc +20 -0
- data/sources/gcc/ary.gcc +40 -0
- data/sources/gcc/binarytrees.gcc +136 -0
- data/sources/gcc/binarytrees.gcc-2.gcc +140 -0
- data/sources/gcc/binarytrees.gcc-3.gcc +134 -0
- data/sources/gcc/chameneos.gcc +117 -0
- data/sources/gcc/chameneos.gcc-2.gcc +134 -0
- data/sources/gcc/chameneos.gcc-3.gcc +120 -0
- data/sources/gcc/chameneosredux.gcc +306 -0
- data/sources/gcc/echo.gcc +144 -0
- data/sources/gcc/except.gcc +52 -0
- data/sources/gcc/fannkuch.gcc +105 -0
- data/sources/gcc/fannkuch.gcc-2.gcc +94 -0
- data/sources/gcc/fasta.gcc +158 -0
- data/sources/gcc/fasta.gcc-2.gcc +132 -0
- data/sources/gcc/fasta.gcc-3.gcc +145 -0
- data/sources/gcc/fasta.gcc-4.gcc +141 -0
- data/sources/gcc/fibo.gcc +20 -0
- data/sources/gcc/harmonic.gcc +22 -0
- data/sources/gcc/hash.gcc +31 -0
- data/sources/gcc/hash2.gcc +38 -0
- data/sources/gcc/heapsort.gcc +72 -0
- data/sources/gcc/hello.gcc-2.gcc +12 -0
- data/sources/gcc/hello.gcc-3.gcc +20 -0
- data/sources/gcc/knucleotide.gcc +173 -0
- data/sources/gcc/knucleotide.gcc-2.gcc +168 -0
- data/sources/gcc/lists.gcc +226 -0
- data/sources/gcc/magicsquares.gcc +378 -0
- data/sources/gcc/mandelbrot.gcc +84 -0
- data/sources/gcc/mandelbrot.gcc-2.gcc +63 -0
- data/sources/gcc/mandelbrot.gcc-3.gcc +91 -0
- data/sources/gcc/mandelbrot.gcc-4.gcc +78 -0
- data/sources/gcc/matrix.gcc +66 -0
- data/sources/gcc/message.gcc-2.gcc +72 -0
- data/sources/gcc/message.gcc-3.gcc +64 -0
- data/sources/gcc/methcall.gcc +89 -0
- data/sources/gcc/moments.gcc +120 -0
- data/sources/gcc/nbody.gcc +142 -0
- data/sources/gcc/nbody.gcc-2.gcc +143 -0
- data/sources/gcc/nbody.gcc-3.gcc +142 -0
- data/sources/gcc/nestedloop.gcc +25 -0
- data/sources/gcc/nsieve.gcc +35 -0
- data/sources/gcc/nsievebits.gcc-2.gcc +37 -0
- data/sources/gcc/nsievebits.gcc-3.gcc +53 -0
- data/sources/gcc/objinst.gcc +95 -0
- data/sources/gcc/partialsums.gcc +86 -0
- data/sources/gcc/partialsums.gcc-3.gcc +81 -0
- data/sources/gcc/partialsums.gcc-4.gcc +68 -0
- data/sources/gcc/pidigits.gcc +105 -0
- data/sources/gcc/primes.gcc +75 -0
- data/sources/gcc/prodcons.gcc +86 -0
- data/sources/gcc/random.gcc +29 -0
- data/sources/gcc/raytracer.gcc +125 -0
- data/sources/gcc/raytracer.gcc-2.gcc +181 -0
- data/sources/gcc/recursive.gcc +68 -0
- data/sources/gcc/recursive.gcc-2.gcc +55 -0
- data/sources/gcc/regexdna.gcc-2.gcc +126 -0
- data/sources/gcc/regexmatch.gcc +136 -0
- data/sources/gcc/revcomp.gcc +85 -0
- data/sources/gcc/revcomp.gcc-2.gcc +88 -0
- data/sources/gcc/revcomp.gcc-4.gcc +71 -0
- data/sources/gcc/reversefile.gcc +103 -0
- data/sources/gcc/reversefile.gcc-2.gcc +56 -0
- data/sources/gcc/sieve.gcc +34 -0
- data/sources/gcc/spectralnorm.gcc +54 -0
- data/sources/gcc/spellcheck.gcc +72 -0
- data/sources/gcc/spellcheck.gcc-2.gcc +61 -0
- data/sources/gcc/strcat.gcc +38 -0
- data/sources/gcc/sumcol.gcc-2.gcc +98 -0
- data/sources/gcc/sumcol.gcc-3.gcc +22 -0
- data/sources/gcc/sumcol.gcc-4.gcc +18 -0
- data/sources/gcc/sumcol.gcc-5.gcc +32 -0
- data/sources/gcc/takfp.gcc +23 -0
- data/sources/gcc/tcp-stream.gcc +122 -0
- data/sources/gcc/tcpecho.gcc +122 -0
- data/sources/gcc/tcpecho.gcc-2.gcc +136 -0
- data/sources/gcc/tcprequest.gcc +122 -0
- data/sources/gcc/threadring.gcc +73 -0
- data/sources/gcc/wc.gcc +52 -0
- data/sources/gcc/wc.gcc-2.gcc +40 -0
- data/sources/gcc/wc.gcc-3.gcc +46 -0
- data/sources/gcc/wordfreq.gcc-2.gcc +85 -0
- data/sources/perl/ackermann.perl +28 -0
- data/sources/perl/ackermann.perl-2.perl +25 -0
- data/sources/perl/ackermann.perl-3.perl +20 -0
- data/sources/perl/ackermann.perl-4.perl +18 -0
- data/sources/perl/ary.perl +25 -0
- data/sources/perl/ary.perl-2.perl +23 -0
- data/sources/perl/binarytrees.perl +66 -0
- data/sources/perl/binarytrees.perl-2.perl +71 -0
- data/sources/perl/chameneos.perl +67 -0
- data/sources/perl/echo.perl +81 -0
- data/sources/perl/except.perl +73 -0
- data/sources/perl/fannkuch.perl +44 -0
- data/sources/perl/fannkuch.perl-2.perl +38 -0
- data/sources/perl/fasta.perl +112 -0
- data/sources/perl/fasta.perl-2.perl +135 -0
- data/sources/perl/fasta.perl-4.perl +122 -0
- data/sources/perl/fibo.perl +15 -0
- data/sources/perl/fibo.perl-2.perl +0 -0
- data/sources/perl/fibo.perl-3.perl +0 -0
- data/sources/perl/harmonic.perl +7 -0
- data/sources/perl/hash.perl +23 -0
- data/sources/perl/hash.perl-2.perl +17 -0
- data/sources/perl/hash.perl-3.perl +24 -0
- data/sources/perl/hash2.perl +16 -0
- data/sources/perl/heapsort.perl +65 -0
- data/sources/perl/heapsort.perl-2.perl +0 -0
- data/sources/perl/hello.perl +5 -0
- data/sources/perl/knucleotide.perl-2.perl +30 -0
- data/sources/perl/lists.perl +48 -0
- data/sources/perl/mandelbrot.perl-2.perl +32 -0
- data/sources/perl/matrix.perl +59 -0
- data/sources/perl/matrix.perl-2.perl +0 -0
- data/sources/perl/matrix.perl-3.perl +0 -0
- data/sources/perl/message.perl +27 -0
- data/sources/perl/methcall.perl +66 -0
- data/sources/perl/moments.perl +44 -0
- data/sources/perl/nbody.perl +108 -0
- data/sources/perl/nestedloop.perl +28 -0
- data/sources/perl/nsieve.perl-2.perl +41 -0
- data/sources/perl/nsieve.perl-4.perl +43 -0
- data/sources/perl/nsievebits.perl +37 -0
- data/sources/perl/objinst.perl +73 -0
- data/sources/perl/partialsums.perl-3.perl +31 -0
- data/sources/perl/pidigits.perl +52 -0
- data/sources/perl/pidigits.perl-2.perl +47 -0
- data/sources/perl/process.perl +50 -0
- data/sources/perl/prodcons.perl +47 -0
- data/sources/perl/random.perl-4.perl +17 -0
- data/sources/perl/recursive.perl-2.perl +57 -0
- data/sources/perl/regexdna.perl +48 -0
- data/sources/perl/regexdna.perl-2.perl +43 -0
- data/sources/perl/regexdna.perl-3.perl +50 -0
- data/sources/perl/regexdna.perl-4.perl +49 -0
- data/sources/perl/regexdna.perl-5.perl +42 -0
- data/sources/perl/regexdna.perl-6.perl +43 -0
- data/sources/perl/regexmatch.perl +35 -0
- data/sources/perl/revcomp.perl-2.perl +34 -0
- data/sources/perl/reversefile.perl +8 -0
- data/sources/perl/reversefile.perl-2.perl +0 -0
- data/sources/perl/reversefile.perl-3.perl +0 -0
- data/sources/perl/sieve.perl +23 -0
- data/sources/perl/spectralnorm.perl-2.perl +54 -0
- data/sources/perl/spellcheck.perl +24 -0
- data/sources/perl/strcat.perl +13 -0
- data/sources/perl/strcat.perl-2.perl +0 -0
- data/sources/perl/sumcol.perl +8 -0
- data/sources/perl/takfp.perl +23 -0
- data/sources/perl/takfp.perl-3.perl +20 -0
- data/sources/perl/tcpecho.perl +61 -0
- data/sources/perl/tcprequest.perl +61 -0
- data/sources/perl/tcpstream.perl +61 -0
- data/sources/perl/threadring.perl +55 -0
- data/sources/perl/threadring.perl-2.perl +43 -0
- data/sources/perl/wc.perl +20 -0
- data/sources/perl/wc.perl-2.perl +14 -0
- data/sources/perl/wordfreq.perl +22 -0
- data/sources/perl/wordfreq.perl-3.perl +0 -0
- data/sources/perl/wordfreq.perl3.perl +0 -0
- data/sources/python/ackermann.python +21 -0
- data/sources/python/ary.python +19 -0
- data/sources/python/binarytrees.python +39 -0
- data/sources/python/binarytrees.python-3.python +44 -0
- data/sources/python/chameneos.python-6.python +73 -0
- data/sources/python/chameneosredux.python +126 -0
- data/sources/python/chameneosredux.python-2.python +122 -0
- data/sources/python/dispatch.python +176 -0
- data/sources/python/dispatch.python-2.python +136 -0
- data/sources/python/echo.python +64 -0
- data/sources/python/except.python +62 -0
- data/sources/python/fannkuch.python +50 -0
- data/sources/python/fannkuch.python-2.python +54 -0
- data/sources/python/fasta.python-2.python +79 -0
- data/sources/python/fibo.python +17 -0
- data/sources/python/fibo.python-2.python +0 -0
- data/sources/python/fibo.python-3.python +0 -0
- data/sources/python/harmonic.python-2.python +9 -0
- data/sources/python/hash.python +21 -0
- data/sources/python/hash.python-2.python +0 -0
- data/sources/python/hash2.python +30 -0
- data/sources/python/heapsort.python-3.python +66 -0
- data/sources/python/hello.python +5 -0
- data/sources/python/implicitode.python +231 -0
- data/sources/python/knucleotide.python +55 -0
- data/sources/python/lists.python +44 -0
- data/sources/python/magicsquares.python +145 -0
- data/sources/python/mandelbrot.python +44 -0
- data/sources/python/mandelbrot.python-2.python +35 -0
- data/sources/python/mandelbrot.python-3.python +46 -0
- data/sources/python/matrix.python +34 -0
- data/sources/python/matrix.python-2.python +23 -0
- data/sources/python/message.python +24 -0
- data/sources/python/message.python-2.python +20 -0
- data/sources/python/message.python-3.python +19 -0
- data/sources/python/meteor.python +210 -0
- data/sources/python/meteor.python-2.python +192 -0
- data/sources/python/methcall.python +51 -0
- data/sources/python/moments.python +65 -0
- data/sources/python/nbody.python +123 -0
- data/sources/python/nbody.python-2.python +120 -0
- data/sources/python/nestedloop.python +24 -0
- data/sources/python/nsieve.python +27 -0
- data/sources/python/nsieve.python-2.python +23 -0
- data/sources/python/nsieve.python-4.python +25 -0
- data/sources/python/nsievebits.python +27 -0
- data/sources/python/nsievebits.python-2.python +43 -0
- data/sources/python/objinst.python +53 -0
- data/sources/python/partialsums.python +37 -0
- data/sources/python/partialsums.python-2.python +35 -0
- data/sources/python/partialsums.python-3.python +48 -0
- data/sources/python/pidigits.python +38 -0
- data/sources/python/pidigits.python-3.python +63 -0
- data/sources/python/pidigits.python-4.python +24 -0
- data/sources/python/process.python +51 -0
- data/sources/python/process.python-2.python +133 -0
- data/sources/python/prodcons.python +51 -0
- data/sources/python/prodcons.python-2.python +0 -0
- data/sources/python/random.python +27 -0
- data/sources/python/raytracer.python +203 -0
- data/sources/python/recursive.python +35 -0
- data/sources/python/regexdna.python +39 -0
- data/sources/python/regexdna.python-2.python +34 -0
- data/sources/python/regexmatch.python +36 -0
- data/sources/python/revcomp.python-3.python +31 -0
- data/sources/python/reversefile.python +13 -0
- data/sources/python/reversefile.python-2.python +0 -0
- data/sources/python/reversefile.python-3.python +0 -0
- data/sources/python/sieve.python +50 -0
- data/sources/python/spectralnorm.python-2.python +36 -0
- data/sources/python/spellcheck.python +17 -0
- data/sources/python/strcat.python +35 -0
- data/sources/python/strcat.python-2.python +0 -0
- data/sources/python/sumcol.python-2.python +0 -0
- data/sources/python/sumcol.python-3.python +0 -0
- data/sources/python/takfp.python +19 -0
- data/sources/python/tcpecho.python +67 -0
- data/sources/python/tcprequest.python +67 -0
- data/sources/python/tcpstream.python +67 -0
- data/sources/python/threadring.python +47 -0
- data/sources/python/threadring.python-2.python +40 -0
- data/sources/python/threadring.python-3.python +34 -0
- data/sources/python/wc.python-2.python +19 -0
- data/sources/python/wordfreq.python +43 -0
- data/sources/python/wordfreq.python-2.python +0 -0
- data/sources/python/wordfreq.python-3.python +28 -0
- data/sources/python/wordfreq.python-4.python +38 -0
- data/sources/python/wordfreq.python-5.python +39 -0
- data/sources/ruby/ackermann.ruby +17 -0
- data/sources/ruby/ackermann.ruby-5.ruby +153 -0
- data/sources/ruby/ary.ruby +22 -0
- data/sources/ruby/binarytrees.ruby-2.ruby +55 -0
- data/sources/ruby/chameneos.ruby-2.ruby +71 -0
- data/sources/ruby/dispatch.ruby +114 -0
- data/sources/ruby/echo.ruby +41 -0
- data/sources/ruby/except.ruby +61 -0
- data/sources/ruby/except.ruby-2.ruby +61 -0
- data/sources/ruby/fannkuch.ruby +42 -0
- data/sources/ruby/fasta.ruby +81 -0
- data/sources/ruby/fibo.ruby +15 -0
- data/sources/ruby/harmonic.ruby-2.ruby +15 -0
- data/sources/ruby/hash.ruby +19 -0
- data/sources/ruby/hash2.ruby +23 -0
- data/sources/ruby/heapsort.ruby +55 -0
- data/sources/ruby/hello.ruby +6 -0
- data/sources/ruby/knucleotide.ruby-2.ruby +44 -0
- data/sources/ruby/lists.ruby +46 -0
- data/sources/ruby/mandelbrot.ruby-3.ruby +63 -0
- data/sources/ruby/matrix.ruby +40 -0
- data/sources/ruby/matrix.ruby-2.ruby +30 -0
- data/sources/ruby/message.ruby +29 -0
- data/sources/ruby/message.ruby-2.ruby +24 -0
- data/sources/ruby/meteor.ruby +386 -0
- data/sources/ruby/meteor.ruby-2.ruby +561 -0
- data/sources/ruby/methcall.ruby +58 -0
- data/sources/ruby/methcall.ruby-2.ruby +54 -0
- data/sources/ruby/moments.ruby +64 -0
- data/sources/ruby/nbody.ruby-2.ruby +145 -0
- data/sources/ruby/nestedloop.ruby +22 -0
- data/sources/ruby/nsieve.ruby +36 -0
- data/sources/ruby/nsieve.ruby-2.ruby +25 -0
- data/sources/ruby/nsievebits.ruby-2.ruby +42 -0
- data/sources/ruby/objinst.ruby +58 -0
- data/sources/ruby/partialsums.ruby +39 -0
- data/sources/ruby/pidigits.ruby +92 -0
- data/sources/ruby/pidigits.ruby-2.ruby +109 -0
- data/sources/ruby/prodcons.ruby +41 -0
- data/sources/ruby/random.ruby +17 -0
- data/sources/ruby/recursive.ruby-2.ruby +53 -0
- data/sources/ruby/regexdna.ruby +32 -0
- data/sources/ruby/regexdna.ruby-2.ruby +38 -0
- data/sources/ruby/regexmatch.ruby +33 -0
- data/sources/ruby/revcomp.ruby +28 -0
- data/sources/ruby/reversefile.ruby +7 -0
- data/sources/ruby/sieve.ruby +30 -0
- data/sources/ruby/spectralnorm.ruby +48 -0
- data/sources/ruby/spellcheck.ruby +18 -0
- data/sources/ruby/spellcheck.ruby-2.ruby +0 -0
- data/sources/ruby/strcat.ruby +12 -0
- data/sources/ruby/strcat.ruby-2.ruby +12 -0
- data/sources/ruby/sumcol.ruby +12 -0
- data/sources/ruby/sumcol.ruby-2.ruby +5 -0
- data/sources/ruby/takfp.ruby +15 -0
- data/sources/ruby/tcpecho.ruby +45 -0
- data/sources/ruby/tcprequest.ruby +45 -0
- data/sources/ruby/tcpstream.ruby +45 -0
- data/sources/ruby/threadring.ruby +61 -0
- data/sources/ruby/threadring.ruby-2.ruby +33 -0
- data/sources/ruby/wc.ruby +15 -0
- data/sources/ruby/wordfreq.ruby +17 -0
- data/sources/ruby/wordfreq.ruby2.ruby +0 -0
- data/test/fixtures/sources/gcc/ackermann.gcc-2.gcc +93 -0
- data/test/fixtures/sources/python/ackermann.python +21 -0
- data/test/fixtures/sources/ruby/ackermann.ruby +17 -0
- data/test/test_source_classifier.rb +40 -0
- data/test/test_trainer.rb +34 -0
- data/trainer.bin +1193 -0
- metadata +393 -0
@@ -0,0 +1,134 @@
|
|
1
|
+
/* The Computer Language Benchmarks Game
|
2
|
+
http://shootout.alioth.debian.org/
|
3
|
+
|
4
|
+
contributed by Kevin Carson
|
5
|
+
modified by Anthony Piron
|
6
|
+
*/
|
7
|
+
|
8
|
+
#include <malloc.h>
|
9
|
+
#include <math.h>
|
10
|
+
#include <stdio.h>
|
11
|
+
#include <stdlib.h>
|
12
|
+
|
13
|
+
|
14
|
+
typedef struct tn {
|
15
|
+
struct tn* left;
|
16
|
+
struct tn* right;
|
17
|
+
long item;
|
18
|
+
} treeNode;
|
19
|
+
|
20
|
+
|
21
|
+
treeNode* InitTreeNode(treeNode* new, treeNode* left, treeNode* right, long item)
|
22
|
+
{
|
23
|
+
new->left = left;
|
24
|
+
new->right = right;
|
25
|
+
new->item = item;
|
26
|
+
|
27
|
+
return new;
|
28
|
+
} /* NewTreeNode() */
|
29
|
+
|
30
|
+
|
31
|
+
long ItemCheck(treeNode* tree)
|
32
|
+
{
|
33
|
+
if (tree->left == NULL)
|
34
|
+
return tree->item;
|
35
|
+
else
|
36
|
+
return tree->item + ItemCheck(tree->left) - ItemCheck(tree->right);
|
37
|
+
} /* ItemCheck() */
|
38
|
+
|
39
|
+
|
40
|
+
treeNode* BottomUpTreeBis(treeNode array[], long index,
|
41
|
+
long item, unsigned depth)
|
42
|
+
{
|
43
|
+
if (depth > 0)
|
44
|
+
return InitTreeNode
|
45
|
+
(
|
46
|
+
array + index,
|
47
|
+
BottomUpTreeBis(array, index*2+1, 2 * item - 1, depth - 1),
|
48
|
+
BottomUpTreeBis(array,index*2+2, 2 * item, depth - 1),
|
49
|
+
item
|
50
|
+
);
|
51
|
+
else
|
52
|
+
return InitTreeNode( array+index,NULL, NULL, item);
|
53
|
+
} /* BottomUpTree() */
|
54
|
+
|
55
|
+
treeNode* BottomUpTree(long item, unsigned depth)
|
56
|
+
{
|
57
|
+
treeNode* array = malloc(sizeof(treeNode)* pow(2,depth+1) );
|
58
|
+
|
59
|
+
return BottomUpTreeBis(array,0, item, depth);
|
60
|
+
}
|
61
|
+
|
62
|
+
void DeleteTree(treeNode* tree)
|
63
|
+
{
|
64
|
+
|
65
|
+
free(tree);
|
66
|
+
|
67
|
+
} /* DeleteTree() */
|
68
|
+
|
69
|
+
|
70
|
+
int main(int argc, char* argv[])
|
71
|
+
{
|
72
|
+
unsigned N, depth, minDepth, maxDepth, stretchDepth;
|
73
|
+
treeNode *stretchTree, *longLivedTree, *tempTree;
|
74
|
+
|
75
|
+
N = atol(argv[1]);
|
76
|
+
|
77
|
+
minDepth = 4;
|
78
|
+
|
79
|
+
if ((minDepth + 2) > N)
|
80
|
+
maxDepth = minDepth + 2;
|
81
|
+
else
|
82
|
+
maxDepth = N;
|
83
|
+
|
84
|
+
stretchDepth = maxDepth + 1;
|
85
|
+
|
86
|
+
stretchTree = BottomUpTree(0, stretchDepth);
|
87
|
+
printf
|
88
|
+
(
|
89
|
+
"stretch tree of depth %u\t check: %li\n",
|
90
|
+
stretchDepth,
|
91
|
+
ItemCheck(stretchTree)
|
92
|
+
);
|
93
|
+
|
94
|
+
DeleteTree(stretchTree);
|
95
|
+
|
96
|
+
longLivedTree = BottomUpTree(0, maxDepth);
|
97
|
+
|
98
|
+
for (depth = minDepth; depth <= maxDepth; depth += 2)
|
99
|
+
{
|
100
|
+
long i, iterations, check;
|
101
|
+
|
102
|
+
iterations = pow(2, maxDepth - depth + minDepth);
|
103
|
+
|
104
|
+
check = 0;
|
105
|
+
|
106
|
+
for (i = 1; i <= iterations; i++)
|
107
|
+
{
|
108
|
+
tempTree = BottomUpTree(i, depth);
|
109
|
+
check += ItemCheck(tempTree);
|
110
|
+
DeleteTree(tempTree);
|
111
|
+
|
112
|
+
tempTree = BottomUpTree(-i, depth);
|
113
|
+
check += ItemCheck(tempTree);
|
114
|
+
DeleteTree(tempTree);
|
115
|
+
} /* for(i = 1...) */
|
116
|
+
|
117
|
+
printf
|
118
|
+
(
|
119
|
+
"%li\t trees of depth %u\t check: %li\n",
|
120
|
+
iterations * 2,
|
121
|
+
depth,
|
122
|
+
check
|
123
|
+
);
|
124
|
+
} /* for(depth = minDepth...) */
|
125
|
+
|
126
|
+
printf
|
127
|
+
(
|
128
|
+
"long lived tree of depth %u\t check: %li\n",
|
129
|
+
maxDepth,
|
130
|
+
ItemCheck(longLivedTree)
|
131
|
+
);
|
132
|
+
|
133
|
+
return 0;
|
134
|
+
} /* main() */
|
@@ -0,0 +1,117 @@
|
|
1
|
+
/* The Computer Language Shootout
|
2
|
+
http://shootout.alioth.debian.org/
|
3
|
+
|
4
|
+
contributed by Josh Goldfoot
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include <semaphore.h>
|
8
|
+
#include <pthread.h>
|
9
|
+
#include <stdio.h>
|
10
|
+
#include <stdlib.h>
|
11
|
+
|
12
|
+
typedef enum { Blue, Red, Yellow, Faded } color;
|
13
|
+
|
14
|
+
sem_t AtMostTwo;
|
15
|
+
sem_t Mutex;
|
16
|
+
sem_t SemPriv;
|
17
|
+
|
18
|
+
int mpStatus = 1;
|
19
|
+
color aColor, bColor;
|
20
|
+
long meetingsLeft;
|
21
|
+
long reports[4];
|
22
|
+
|
23
|
+
color complementaryColor (color c1, color c2)
|
24
|
+
{
|
25
|
+
if (c2 == Faded) return Faded;
|
26
|
+
if (c1 == c2) return c1;
|
27
|
+
switch (c1)
|
28
|
+
{
|
29
|
+
case Blue:
|
30
|
+
return c2 == Red ? Yellow : Red;
|
31
|
+
case Red:
|
32
|
+
return c2 == Blue ? Yellow : Blue;
|
33
|
+
case Yellow:
|
34
|
+
return c2 == Blue ? Red : Blue;
|
35
|
+
default:
|
36
|
+
return c1;
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
color Cooperation (int id, color c)
|
41
|
+
{
|
42
|
+
color otherColor;
|
43
|
+
|
44
|
+
sem_wait (&AtMostTwo);
|
45
|
+
sem_wait (&Mutex);
|
46
|
+
switch (mpStatus)
|
47
|
+
{
|
48
|
+
case 1:
|
49
|
+
aColor = c;
|
50
|
+
mpStatus = 2;
|
51
|
+
sem_post (&Mutex);
|
52
|
+
sem_wait (&SemPriv);
|
53
|
+
otherColor = bColor;
|
54
|
+
sem_post (&Mutex);
|
55
|
+
sem_post (&AtMostTwo);
|
56
|
+
sem_post (&AtMostTwo);
|
57
|
+
break;
|
58
|
+
case 2:
|
59
|
+
mpStatus = (--meetingsLeft > 0 ? 1 : 3);
|
60
|
+
bColor = c;
|
61
|
+
otherColor = aColor;
|
62
|
+
sem_post (&SemPriv);
|
63
|
+
break;
|
64
|
+
case 3:
|
65
|
+
default:
|
66
|
+
otherColor = Faded;
|
67
|
+
sem_post (&Mutex);
|
68
|
+
sem_post (&AtMostTwo);
|
69
|
+
}
|
70
|
+
return otherColor;
|
71
|
+
}
|
72
|
+
|
73
|
+
void chameneosCode (void *args)
|
74
|
+
{
|
75
|
+
int myId;
|
76
|
+
long meetings;
|
77
|
+
color myColor, otherColor;
|
78
|
+
sscanf ((char *) args, "%d %d", &myId, &myColor);
|
79
|
+
meetings = 0;
|
80
|
+
while (myColor != Faded)
|
81
|
+
{
|
82
|
+
otherColor = Cooperation (myId, myColor);
|
83
|
+
myColor = complementaryColor (myColor, otherColor);
|
84
|
+
meetings++;
|
85
|
+
}
|
86
|
+
reports[myId] = meetings - 1;
|
87
|
+
}
|
88
|
+
|
89
|
+
int main (int argc, char *argv[])
|
90
|
+
{
|
91
|
+
color tabColor[4] = { Blue, Red, Yellow, Blue };
|
92
|
+
pthread_t tabPid[4];
|
93
|
+
char theArgs[32][4];
|
94
|
+
int i;
|
95
|
+
|
96
|
+
sem_init (&AtMostTwo, 0, 2);
|
97
|
+
sem_init (&Mutex, 0, 1);
|
98
|
+
sem_init (&SemPriv, 0, 0);
|
99
|
+
|
100
|
+
meetingsLeft = (argc > 1) ? atoi (argv[1]) : 1000000;
|
101
|
+
|
102
|
+
for (i = 0; i < 4; i++)
|
103
|
+
{
|
104
|
+
sprintf (theArgs[i], "%d %d", i, tabColor[i]);
|
105
|
+
pthread_create (&tabPid[i], NULL, (void *(*)(void *)) chameneosCode,
|
106
|
+
theArgs[i]);
|
107
|
+
}
|
108
|
+
for (i = 0; i < 4; i++)
|
109
|
+
pthread_join (tabPid[i], NULL);
|
110
|
+
|
111
|
+
long sum = reports[0];
|
112
|
+
for (i = 1; i < 4; i++)
|
113
|
+
sum += reports[i];
|
114
|
+
printf ("%d\n", sum);
|
115
|
+
|
116
|
+
return 0;
|
117
|
+
}
|
@@ -0,0 +1,134 @@
|
|
1
|
+
/* The Computer Language Benchmarks Game
|
2
|
+
http://shootout.alioth.debian.org/
|
3
|
+
|
4
|
+
Written by Jorge Peixoto de Morais Neto
|
5
|
+
based on code by Josh Goldfoot */
|
6
|
+
|
7
|
+
#include <stdio.h>
|
8
|
+
#include <stdlib.h>
|
9
|
+
#include <pthread.h>
|
10
|
+
#include <semaphore.h>
|
11
|
+
#include <err.h>
|
12
|
+
|
13
|
+
static sem_t mutex, second_creature;
|
14
|
+
|
15
|
+
static unsigned long meetingsleft;
|
16
|
+
|
17
|
+
typedef enum { Blue, Red, Yellow, Faded } color_t;
|
18
|
+
typedef struct {
|
19
|
+
unsigned long *meetingsp;
|
20
|
+
color_t color;
|
21
|
+
} creature_t;
|
22
|
+
|
23
|
+
/* When this function is called with two different colors, it must return the other color */
|
24
|
+
/* We assume that none of the colors is Faded */
|
25
|
+
/* If it is called with two equal colors, the specification allows us to return any color. */
|
26
|
+
/* In this case, we return Red if both colors are Blue, Yellow if both are Red and Blue if both are Yellow */
|
27
|
+
static color_t complementarycolor (color_t c1, color_t c2) {
|
28
|
+
switch (c1) {
|
29
|
+
case Blue:
|
30
|
+
return c2 == Red ? Yellow : Red;
|
31
|
+
case Red:
|
32
|
+
return c2 == Yellow ? Blue : Yellow;
|
33
|
+
case Yellow:
|
34
|
+
default:
|
35
|
+
return c2 == Blue ? Red : Blue;
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
/* This is the meeting place. Multiple threads call this function. */
|
40
|
+
/* This function returns the new color the creature must assume after the meeting*/
|
41
|
+
/* The first creature locks mutex, sets color1 = color, updates mp_state, */
|
42
|
+
/* unlocks the mutex and waits to lock second_creature */
|
43
|
+
/* The second creature locks mutex, sets othercolor=color1 and color2 = color , */
|
44
|
+
/* decrements meetingsleft, updates mp_state, unlocks second_creature and returns */
|
45
|
+
/* The first creature sets othercolor = color2, unlocks mutex and returns */
|
46
|
+
/* mp_state is EMPTY again, the two semaphores are back to initial values and the cycle continues */
|
47
|
+
/* until meetingsleft reaches 0. */
|
48
|
+
static color_t new_color (color_t color) {
|
49
|
+
/* Meeting place state*/
|
50
|
+
static enum {
|
51
|
+
MEETINGS_LIMIT_REACHED = -1,
|
52
|
+
EMPTY,
|
53
|
+
ONE_CREATURE
|
54
|
+
} mp_state = EMPTY;
|
55
|
+
|
56
|
+
static color_t color1, color2;
|
57
|
+
color_t othercolor;
|
58
|
+
sem_wait (&mutex);
|
59
|
+
switch (mp_state) {
|
60
|
+
case EMPTY:
|
61
|
+
color1 = color;
|
62
|
+
mp_state = ONE_CREATURE;
|
63
|
+
sem_post (&mutex);
|
64
|
+
sem_wait (&second_creature);
|
65
|
+
othercolor = color2;
|
66
|
+
sem_post (&mutex);
|
67
|
+
break;
|
68
|
+
case ONE_CREATURE:
|
69
|
+
othercolor = color1;
|
70
|
+
color2 = color;
|
71
|
+
mp_state = (--meetingsleft ? EMPTY : MEETINGS_LIMIT_REACHED);
|
72
|
+
sem_post (&second_creature);
|
73
|
+
break;
|
74
|
+
case MEETINGS_LIMIT_REACHED:
|
75
|
+
default:
|
76
|
+
sem_post (&mutex);
|
77
|
+
return Faded;
|
78
|
+
}
|
79
|
+
return complementarycolor (color, othercolor);
|
80
|
+
}
|
81
|
+
|
82
|
+
/* This function simulates a creature. */
|
83
|
+
static void *run_creature (void *voidpme) {
|
84
|
+
creature_t const *me = voidpme;
|
85
|
+
unsigned long meetings = 0;
|
86
|
+
color_t color = me->color;
|
87
|
+
do {
|
88
|
+
meetings++;
|
89
|
+
color = new_color (color);
|
90
|
+
} while (color != Faded);
|
91
|
+
/* The '-1' compensates the fact that the meetings variable counts the meeting */
|
92
|
+
/* in wich the creature becomes faded (but it shouldn't) */
|
93
|
+
*me->meetingsp = meetings - 1;
|
94
|
+
static int zero = 0;
|
95
|
+
/* We return a pointer to 0, meaning success. */
|
96
|
+
return &zero;
|
97
|
+
}
|
98
|
+
|
99
|
+
/* This function sets loose four creatures and returns the sum of reported meetings*/
|
100
|
+
static unsigned long meetings_of_four_creatures (void) {
|
101
|
+
sem_init (&mutex, 0, 1);
|
102
|
+
sem_init (&second_creature, 0, 0);
|
103
|
+
|
104
|
+
unsigned long reports[4];
|
105
|
+
creature_t creatures[4] = {{&reports[0], Blue},
|
106
|
+
{&reports[1], Red},
|
107
|
+
{&reports[2], Yellow},
|
108
|
+
{&reports[3], Blue}};
|
109
|
+
pthread_t pids[4];
|
110
|
+
int i;
|
111
|
+
for (i = 0; i < 4; i++)
|
112
|
+
pthread_create (&pids[i], NULL, run_creature, &creatures[i]);
|
113
|
+
|
114
|
+
for (i = 0; i < 4; i++)
|
115
|
+
pthread_join (pids[i], NULL);
|
116
|
+
|
117
|
+
unsigned long sum = reports[0];
|
118
|
+
for (i = 1; i < 4; i++)
|
119
|
+
sum += reports[i];
|
120
|
+
return sum;
|
121
|
+
}
|
122
|
+
|
123
|
+
int main (int argc, char const **argv) {
|
124
|
+
meetingsleft = 1e6;
|
125
|
+
if (argc > 1) {
|
126
|
+
char *tail;
|
127
|
+
meetingsleft = strtoul (argv[1], &tail, 0);
|
128
|
+
if (tail == argv[1])
|
129
|
+
errx (1, "Could not convert \"%s\" to an unsigned long integer", argv[1]);
|
130
|
+
}
|
131
|
+
unsigned long sum = meetings_of_four_creatures ();
|
132
|
+
printf ("%lu\n", sum);
|
133
|
+
return 0;
|
134
|
+
}
|
@@ -0,0 +1,120 @@
|
|
1
|
+
/* The Computer Language Benchmarks Game
|
2
|
+
http://shootout.alioth.debian.org/
|
3
|
+
|
4
|
+
Written by Jorge Peixoto de Morais Neto
|
5
|
+
based on code by Josh Goldfoot
|
6
|
+
*/
|
7
|
+
|
8
|
+
#include <stdio.h>
|
9
|
+
#include <stdlib.h>
|
10
|
+
#include <pthread.h>
|
11
|
+
#include <sched.h>
|
12
|
+
#include <stdbool.h>
|
13
|
+
#include <err.h>
|
14
|
+
static unsigned long meetingsleft;
|
15
|
+
|
16
|
+
typedef enum { Blue, Red, Yellow, Faded } color_t;
|
17
|
+
typedef struct {
|
18
|
+
color_t color;
|
19
|
+
bool waiting_partner;
|
20
|
+
} creature_t;
|
21
|
+
|
22
|
+
/* When this function is called with two different colors, it must return the other color */
|
23
|
+
/* We assume that none of the colors is Faded */
|
24
|
+
/* If it is called with two equal colors, the specification allows us to return any color. */
|
25
|
+
/* In this case, we return Red if both colors are Blue, Yellow if both are Red and Blue if both are Yellow */
|
26
|
+
static color_t complementarycolor (color_t c1, color_t c2) {
|
27
|
+
switch (c1) {
|
28
|
+
case Yellow:
|
29
|
+
return c2 == Blue ? Red : Blue;
|
30
|
+
case Red:
|
31
|
+
return c2 == Yellow ? Blue : Yellow;
|
32
|
+
default:
|
33
|
+
case Blue:
|
34
|
+
return c2 == Red ? Yellow : Red;
|
35
|
+
}
|
36
|
+
}
|
37
|
+
|
38
|
+
/* This is the meeting place. The threads simulating creatures call this function. */
|
39
|
+
/* This function takes a pointer to the creaure data */
|
40
|
+
/* and writes to the color field the new color the creature must assume */
|
41
|
+
static void meeting_place (creature_t volatile *creature_p) {
|
42
|
+
/* A creature in the meeting place waiting for a partner. NULL means */
|
43
|
+
/* there is no such creature */
|
44
|
+
static creature_t volatile *waiting_creature_p = NULL;
|
45
|
+
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
46
|
+
pthread_mutex_lock (&mutex);
|
47
|
+
if (!waiting_creature_p) {
|
48
|
+
if (meetingsleft) {
|
49
|
+
waiting_creature_p = creature_p;
|
50
|
+
creature_p->waiting_partner = true;
|
51
|
+
pthread_mutex_unlock(&mutex);
|
52
|
+
while (creature_p->waiting_partner)
|
53
|
+
sched_yield ();
|
54
|
+
} else {
|
55
|
+
pthread_mutex_unlock (&mutex);
|
56
|
+
creature_p->color = Faded;
|
57
|
+
}
|
58
|
+
} else {
|
59
|
+
color_t newcolor =
|
60
|
+
complementarycolor (creature_p->color, waiting_creature_p->color);
|
61
|
+
creature_p->color = newcolor;
|
62
|
+
waiting_creature_p->color = newcolor;
|
63
|
+
waiting_creature_p->waiting_partner = false;
|
64
|
+
waiting_creature_p = NULL;
|
65
|
+
meetingsleft--;
|
66
|
+
pthread_mutex_unlock (&mutex);
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
/* This function simulates a creature. */
|
71
|
+
/* It returns a pointer to the number of meetings this creature performed */
|
72
|
+
static void *run_creature (void *me_voidp) {
|
73
|
+
creature_t *me_p = me_voidp;
|
74
|
+
unsigned long meetings = 0;
|
75
|
+
do {
|
76
|
+
meetings++;
|
77
|
+
meeting_place (me_p);
|
78
|
+
} while (me_p->color != Faded);
|
79
|
+
unsigned long *meetings_p = malloc (sizeof *meetings_p);
|
80
|
+
/* The '-1' compensates the fact that the meetings variable counts the meeting */
|
81
|
+
/* in wich the creature becomes faded (but it shouldn't) */
|
82
|
+
*meetings_p = meetings - 1;
|
83
|
+
return meetings_p;
|
84
|
+
}
|
85
|
+
|
86
|
+
#define NCREATURES 4
|
87
|
+
/* This function unleashes NCREATURES creatures and returns the sum of reported meetings */
|
88
|
+
static unsigned long meetings_of_creatures (void) {
|
89
|
+
creature_t creatures[NCREATURES];
|
90
|
+
creatures[0].color = Blue;
|
91
|
+
creatures[1].color = Red;
|
92
|
+
creatures[2].color = Yellow;
|
93
|
+
creatures[3].color = Blue;
|
94
|
+
|
95
|
+
pthread_t pids[NCREATURES];
|
96
|
+
int i;
|
97
|
+
for (i = NCREATURES - 1; i >= 0; i--)
|
98
|
+
pthread_create (&pids[i], NULL, run_creature, &creatures[i]);
|
99
|
+
|
100
|
+
unsigned long sum = 0;
|
101
|
+
for (i = NCREATURES - 1; i >= 0; i--) {
|
102
|
+
void *result_p;
|
103
|
+
pthread_join (pids[i], &result_p);
|
104
|
+
sum += *(unsigned long *) result_p;
|
105
|
+
}
|
106
|
+
return sum;
|
107
|
+
}
|
108
|
+
|
109
|
+
int main (int argc, char **argv) {
|
110
|
+
meetingsleft = 1e6;
|
111
|
+
if (argc > 1) {
|
112
|
+
char *tail;
|
113
|
+
meetingsleft = strtoul (argv[1], &tail, 0);
|
114
|
+
if (tail == argv[1])
|
115
|
+
errx (1, "Could not convert \"%s\" to an unsigned long integer", argv[1]);
|
116
|
+
}
|
117
|
+
unsigned long sum = meetings_of_creatures ();
|
118
|
+
printf ("%lu\n", sum);
|
119
|
+
return 0;
|
120
|
+
}
|