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,22 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'classifier'
|
3
|
+
|
4
|
+
class SourceClassifier
|
5
|
+
|
6
|
+
attr_reader :training_file
|
7
|
+
|
8
|
+
def initialize(training_file=nil)
|
9
|
+
training_file = File.join(File.dirname(__FILE__), '..', 'trainer.bin') unless training_file
|
10
|
+
@training_file = training_file
|
11
|
+
open(@training_file, "r") { |f| @c = Marshal.load(f)}
|
12
|
+
end
|
13
|
+
|
14
|
+
def languages
|
15
|
+
@c.categories
|
16
|
+
end
|
17
|
+
|
18
|
+
def identify(str)
|
19
|
+
@c.classify(str)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/lib/trainer.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'classifier'
|
3
|
+
require 'ruby-debug'
|
4
|
+
|
5
|
+
|
6
|
+
class Trainer
|
7
|
+
attr_reader :classifier
|
8
|
+
|
9
|
+
def initialize(sources_dir, output_dir)
|
10
|
+
@classifier = train(sources_dir,output_dir)
|
11
|
+
end
|
12
|
+
|
13
|
+
def train(sources_dir,output_dir)
|
14
|
+
b = Classifier::Bayes.new
|
15
|
+
|
16
|
+
languages = Trainer.languages_for(sources_dir)
|
17
|
+
|
18
|
+
languages.each do |language|
|
19
|
+
b.add_category language
|
20
|
+
|
21
|
+
files = Trainer.files_for(sources_dir,language)
|
22
|
+
files.each do |f|
|
23
|
+
file_content = File.read(f)
|
24
|
+
b.train language, file_content
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
marshal(output_dir,b)
|
29
|
+
return b
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.languages_for(sources_dir)
|
33
|
+
languages = Dir["#{sources_dir}/*"].map {|dir| dir.split("/").last}
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.files_for(sources_dir,language)
|
37
|
+
files = Dir["#{sources_dir}/#{language}/*"]
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def marshal(output_dir, data)
|
43
|
+
FileUtils.mkdir_p(output_dir)
|
44
|
+
Dir.chdir(output_dir) do
|
45
|
+
open("trainer.bin", "w") { |f| Marshal.dump(data, f) }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{sourceclassifier}
|
5
|
+
s.version = "0.2.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Chris Lowis"]
|
9
|
+
s.date = %q{2008-12-08}
|
10
|
+
s.description = %q{Determine the programming language used in a sample}
|
11
|
+
s.email = %q{chris.lowis@gmail.com}
|
12
|
+
s.extra_rdoc_files = ["lib/sourceclassifier.rb", "lib/trainer.rb", "README.textile"]
|
13
|
+
s.files = ["examples/example.rb", "lib/sourceclassifier.rb", "lib/trainer.rb", "Rakefile", "README.textile", "sourceclassifier.gemspec", "sources/gcc/ackermann.gcc-2.gcc", "sources/gcc/ackermann.gcc-3.gcc", "sources/gcc/ary.gcc", "sources/gcc/binarytrees.gcc", "sources/gcc/binarytrees.gcc-2.gcc", "sources/gcc/binarytrees.gcc-3.gcc", "sources/gcc/chameneos.gcc", "sources/gcc/chameneos.gcc-2.gcc", "sources/gcc/chameneos.gcc-3.gcc", "sources/gcc/chameneosredux.gcc", "sources/gcc/echo.gcc", "sources/gcc/except.gcc", "sources/gcc/fannkuch.gcc", "sources/gcc/fannkuch.gcc-2.gcc", "sources/gcc/fasta.gcc", "sources/gcc/fasta.gcc-2.gcc", "sources/gcc/fasta.gcc-3.gcc", "sources/gcc/fasta.gcc-4.gcc", "sources/gcc/fibo.gcc", "sources/gcc/harmonic.gcc", "sources/gcc/hash.gcc", "sources/gcc/hash2.gcc", "sources/gcc/heapsort.gcc", "sources/gcc/hello.gcc-2.gcc", "sources/gcc/hello.gcc-3.gcc", "sources/gcc/knucleotide.gcc", "sources/gcc/knucleotide.gcc-2.gcc", "sources/gcc/lists.gcc", "sources/gcc/magicsquares.gcc", "sources/gcc/mandelbrot.gcc", "sources/gcc/mandelbrot.gcc-2.gcc", "sources/gcc/mandelbrot.gcc-3.gcc", "sources/gcc/mandelbrot.gcc-4.gcc", "sources/gcc/matrix.gcc", "sources/gcc/message.gcc-2.gcc", "sources/gcc/message.gcc-3.gcc", "sources/gcc/methcall.gcc", "sources/gcc/moments.gcc", "sources/gcc/nbody.gcc", "sources/gcc/nbody.gcc-2.gcc", "sources/gcc/nbody.gcc-3.gcc", "sources/gcc/nestedloop.gcc", "sources/gcc/nsieve.gcc", "sources/gcc/nsievebits.gcc-2.gcc", "sources/gcc/nsievebits.gcc-3.gcc", "sources/gcc/objinst.gcc", "sources/gcc/partialsums.gcc", "sources/gcc/partialsums.gcc-3.gcc", "sources/gcc/partialsums.gcc-4.gcc", "sources/gcc/pidigits.gcc", "sources/gcc/primes.gcc", "sources/gcc/prodcons.gcc", "sources/gcc/random.gcc", "sources/gcc/raytracer.gcc", "sources/gcc/raytracer.gcc-2.gcc", "sources/gcc/recursive.gcc", "sources/gcc/recursive.gcc-2.gcc", "sources/gcc/regexdna.gcc-2.gcc", "sources/gcc/regexmatch.gcc", "sources/gcc/revcomp.gcc", "sources/gcc/revcomp.gcc-2.gcc", "sources/gcc/revcomp.gcc-4.gcc", "sources/gcc/reversefile.gcc", "sources/gcc/reversefile.gcc-2.gcc", "sources/gcc/sieve.gcc", "sources/gcc/spectralnorm.gcc", "sources/gcc/spellcheck.gcc", "sources/gcc/spellcheck.gcc-2.gcc", "sources/gcc/strcat.gcc", "sources/gcc/sumcol.gcc-2.gcc", "sources/gcc/sumcol.gcc-3.gcc", "sources/gcc/sumcol.gcc-4.gcc", "sources/gcc/sumcol.gcc-5.gcc", "sources/gcc/takfp.gcc", "sources/gcc/tcp-stream.gcc", "sources/gcc/tcpecho.gcc", "sources/gcc/tcpecho.gcc-2.gcc", "sources/gcc/tcprequest.gcc", "sources/gcc/threadring.gcc", "sources/gcc/wc.gcc", "sources/gcc/wc.gcc-2.gcc", "sources/gcc/wc.gcc-3.gcc", "sources/gcc/wordfreq.gcc-2.gcc", "sources/perl/ackermann.perl", "sources/perl/ackermann.perl-2.perl", "sources/perl/ackermann.perl-3.perl", "sources/perl/ackermann.perl-4.perl", "sources/perl/ary.perl", "sources/perl/ary.perl-2.perl", "sources/perl/binarytrees.perl", "sources/perl/binarytrees.perl-2.perl", "sources/perl/chameneos.perl", "sources/perl/echo.perl", "sources/perl/except.perl", "sources/perl/fannkuch.perl", "sources/perl/fannkuch.perl-2.perl", "sources/perl/fasta.perl", "sources/perl/fasta.perl-2.perl", "sources/perl/fasta.perl-4.perl", "sources/perl/fibo.perl", "sources/perl/fibo.perl-2.perl", "sources/perl/fibo.perl-3.perl", "sources/perl/harmonic.perl", "sources/perl/hash.perl", "sources/perl/hash.perl-2.perl", "sources/perl/hash.perl-3.perl", "sources/perl/hash2.perl", "sources/perl/heapsort.perl", "sources/perl/heapsort.perl-2.perl", "sources/perl/hello.perl", "sources/perl/knucleotide.perl-2.perl", "sources/perl/lists.perl", "sources/perl/mandelbrot.perl-2.perl", "sources/perl/matrix.perl", "sources/perl/matrix.perl-2.perl", "sources/perl/matrix.perl-3.perl", "sources/perl/message.perl", "sources/perl/methcall.perl", "sources/perl/moments.perl", "sources/perl/nbody.perl", "sources/perl/nestedloop.perl", "sources/perl/nsieve.perl-2.perl", "sources/perl/nsieve.perl-4.perl", "sources/perl/nsievebits.perl", "sources/perl/objinst.perl", "sources/perl/partialsums.perl-3.perl", "sources/perl/pidigits.perl", "sources/perl/pidigits.perl-2.perl", "sources/perl/process.perl", "sources/perl/prodcons.perl", "sources/perl/random.perl-4.perl", "sources/perl/recursive.perl-2.perl", "sources/perl/regexdna.perl", "sources/perl/regexdna.perl-2.perl", "sources/perl/regexdna.perl-3.perl", "sources/perl/regexdna.perl-4.perl", "sources/perl/regexdna.perl-5.perl", "sources/perl/regexdna.perl-6.perl", "sources/perl/regexmatch.perl", "sources/perl/revcomp.perl-2.perl", "sources/perl/reversefile.perl", "sources/perl/reversefile.perl-2.perl", "sources/perl/reversefile.perl-3.perl", "sources/perl/sieve.perl", "sources/perl/spectralnorm.perl-2.perl", "sources/perl/spellcheck.perl", "sources/perl/strcat.perl", "sources/perl/strcat.perl-2.perl", "sources/perl/sumcol.perl", "sources/perl/takfp.perl", "sources/perl/takfp.perl-3.perl", "sources/perl/tcpecho.perl", "sources/perl/tcprequest.perl", "sources/perl/tcpstream.perl", "sources/perl/threadring.perl", "sources/perl/threadring.perl-2.perl", "sources/perl/wc.perl", "sources/perl/wc.perl-2.perl", "sources/perl/wordfreq.perl", "sources/perl/wordfreq.perl-3.perl", "sources/perl/wordfreq.perl3.perl", "sources/python/ackermann.python", "sources/python/ary.python", "sources/python/binarytrees.python", "sources/python/binarytrees.python-3.python", "sources/python/chameneos.python-6.python", "sources/python/chameneosredux.python", "sources/python/chameneosredux.python-2.python", "sources/python/dispatch.python", "sources/python/dispatch.python-2.python", "sources/python/echo.python", "sources/python/except.python", "sources/python/fannkuch.python", "sources/python/fannkuch.python-2.python", "sources/python/fasta.python-2.python", "sources/python/fibo.python", "sources/python/fibo.python-2.python", "sources/python/fibo.python-3.python", "sources/python/harmonic.python-2.python", "sources/python/hash.python", "sources/python/hash.python-2.python", "sources/python/hash2.python", "sources/python/heapsort.python-3.python", "sources/python/hello.python", "sources/python/implicitode.python", "sources/python/knucleotide.python", "sources/python/lists.python", "sources/python/magicsquares.python", "sources/python/mandelbrot.python", "sources/python/mandelbrot.python-2.python", "sources/python/mandelbrot.python-3.python", "sources/python/matrix.python", "sources/python/matrix.python-2.python", "sources/python/message.python", "sources/python/message.python-2.python", "sources/python/message.python-3.python", "sources/python/meteor.python", "sources/python/meteor.python-2.python", "sources/python/methcall.python", "sources/python/moments.python", "sources/python/nbody.python", "sources/python/nbody.python-2.python", "sources/python/nestedloop.python", "sources/python/nsieve.python", "sources/python/nsieve.python-2.python", "sources/python/nsieve.python-4.python", "sources/python/nsievebits.python", "sources/python/nsievebits.python-2.python", "sources/python/objinst.python", "sources/python/partialsums.python", "sources/python/partialsums.python-2.python", "sources/python/partialsums.python-3.python", "sources/python/pidigits.python", "sources/python/pidigits.python-3.python", "sources/python/pidigits.python-4.python", "sources/python/process.python", "sources/python/process.python-2.python", "sources/python/prodcons.python", "sources/python/prodcons.python-2.python", "sources/python/random.python", "sources/python/raytracer.python", "sources/python/recursive.python", "sources/python/regexdna.python", "sources/python/regexdna.python-2.python", "sources/python/regexmatch.python", "sources/python/revcomp.python-3.python", "sources/python/reversefile.python", "sources/python/reversefile.python-2.python", "sources/python/reversefile.python-3.python", "sources/python/sieve.python", "sources/python/spectralnorm.python-2.python", "sources/python/spellcheck.python", "sources/python/strcat.python", "sources/python/strcat.python-2.python", "sources/python/sumcol.python-2.python", "sources/python/sumcol.python-3.python", "sources/python/takfp.python", "sources/python/tcpecho.python", "sources/python/tcprequest.python", "sources/python/tcpstream.python", "sources/python/threadring.python", "sources/python/threadring.python-2.python", "sources/python/threadring.python-3.python", "sources/python/wc.python-2.python", "sources/python/wordfreq.python", "sources/python/wordfreq.python-2.python", "sources/python/wordfreq.python-3.python", "sources/python/wordfreq.python-4.python", "sources/python/wordfreq.python-5.python", "sources/ruby/ackermann.ruby", "sources/ruby/ackermann.ruby-5.ruby", "sources/ruby/ary.ruby", "sources/ruby/binarytrees.ruby-2.ruby", "sources/ruby/chameneos.ruby-2.ruby", "sources/ruby/dispatch.ruby", "sources/ruby/echo.ruby", "sources/ruby/except.ruby", "sources/ruby/except.ruby-2.ruby", "sources/ruby/fannkuch.ruby", "sources/ruby/fasta.ruby", "sources/ruby/fibo.ruby", "sources/ruby/harmonic.ruby-2.ruby", "sources/ruby/hash.ruby", "sources/ruby/hash2.ruby", "sources/ruby/heapsort.ruby", "sources/ruby/hello.ruby", "sources/ruby/knucleotide.ruby-2.ruby", "sources/ruby/lists.ruby", "sources/ruby/mandelbrot.ruby-3.ruby", "sources/ruby/matrix.ruby", "sources/ruby/matrix.ruby-2.ruby", "sources/ruby/message.ruby", "sources/ruby/message.ruby-2.ruby", "sources/ruby/meteor.ruby", "sources/ruby/meteor.ruby-2.ruby", "sources/ruby/methcall.ruby", "sources/ruby/methcall.ruby-2.ruby", "sources/ruby/moments.ruby", "sources/ruby/nbody.ruby-2.ruby", "sources/ruby/nestedloop.ruby", "sources/ruby/nsieve.ruby", "sources/ruby/nsieve.ruby-2.ruby", "sources/ruby/nsievebits.ruby-2.ruby", "sources/ruby/objinst.ruby", "sources/ruby/partialsums.ruby", "sources/ruby/pidigits.ruby", "sources/ruby/pidigits.ruby-2.ruby", "sources/ruby/prodcons.ruby", "sources/ruby/random.ruby", "sources/ruby/recursive.ruby-2.ruby", "sources/ruby/regexdna.ruby", "sources/ruby/regexdna.ruby-2.ruby", "sources/ruby/regexmatch.ruby", "sources/ruby/revcomp.ruby", "sources/ruby/reversefile.ruby", "sources/ruby/sieve.ruby", "sources/ruby/spectralnorm.ruby", "sources/ruby/spellcheck.ruby", "sources/ruby/spellcheck.ruby-2.ruby", "sources/ruby/strcat.ruby", "sources/ruby/strcat.ruby-2.ruby", "sources/ruby/sumcol.ruby", "sources/ruby/sumcol.ruby-2.ruby", "sources/ruby/takfp.ruby", "sources/ruby/tcpecho.ruby", "sources/ruby/tcprequest.ruby", "sources/ruby/tcpstream.ruby", "sources/ruby/threadring.ruby", "sources/ruby/threadring.ruby-2.ruby", "sources/ruby/wc.ruby", "sources/ruby/wordfreq.ruby", "sources/ruby/wordfreq.ruby2.ruby", "test/fixtures/sources/gcc/ackermann.gcc-2.gcc", "test/fixtures/sources/python/ackermann.python", "test/fixtures/sources/ruby/ackermann.ruby", "test/test_source_classifier.rb", "test/test_trainer.rb", "trainer.bin", "Manifest"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://github.com/chrislo/sourceclassifier/tree/master}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Sourceclassifier", "--main", "README.textile"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{sourceclassifier}
|
19
|
+
s.rubygems_version = %q{1.3.0}
|
20
|
+
s.summary = %q{Determine the programming language used in a sample}
|
21
|
+
s.test_files = ["test/test_source_classifier.rb", "test/test_trainer.rb"]
|
22
|
+
|
23
|
+
if s.respond_to? :specification_version then
|
24
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
25
|
+
s.specification_version = 2
|
26
|
+
|
27
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
28
|
+
s.add_runtime_dependency(%q<classifier>, [">= 0"])
|
29
|
+
else
|
30
|
+
s.add_dependency(%q<classifier>, [">= 0"])
|
31
|
+
end
|
32
|
+
else
|
33
|
+
s.add_dependency(%q<classifier>, [">= 0"])
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
/* -*- mode: c -*-
|
2
|
+
* $Id: ackermann.gcc-2.gcc,v 1.1 2005-03-15 06:17:49 bfulgham Exp $
|
3
|
+
* http://shootout.alioth.debian.org/
|
4
|
+
*
|
5
|
+
* Modified to use C inline and unroll of recursive functions
|
6
|
+
* by Paul Hsieh
|
7
|
+
*/
|
8
|
+
|
9
|
+
#include <stdio.h>
|
10
|
+
#include <stdlib.h>
|
11
|
+
#include <limits.h>
|
12
|
+
#if (defined(__linux) || defined(__unix) || defined(__GNUC__))
|
13
|
+
#include <unistd.h>
|
14
|
+
#elif (defined(WIN32) || defined(_WIN32) || defined(__WIN32) ||
|
15
|
+
defined(_WINDOWS) || defined(_WINDOWS_))
|
16
|
+
#include <windows.h>
|
17
|
+
#endif
|
18
|
+
|
19
|
+
#define glueAckExpand(m) Ack##m
|
20
|
+
#define glueAck(m) glueAckExpand(m)
|
21
|
+
|
22
|
+
#define Ack0(N) ((N)+1)
|
23
|
+
|
24
|
+
#define AckDecl(M,M1) \
|
25
|
+
static int glueAck(M) (int N) { \
|
26
|
+
if (0 == N) return glueAck(M1) (1); \
|
27
|
+
if (1 == N) return glueAck(M1) (glueAck(M1) (1)); \
|
28
|
+
return glueAck(M1) (glueAck(M) (N-1)); \
|
29
|
+
}
|
30
|
+
|
31
|
+
AckDecl(1,0)
|
32
|
+
AckDecl(2,1)
|
33
|
+
AckDecl(3,2)
|
34
|
+
AckDecl(4,3)
|
35
|
+
|
36
|
+
/* Removes the overhead of passing around and M parameter,
|
37
|
+
when it is low (which is the only way this can actually
|
38
|
+
complete execution.) */
|
39
|
+
|
40
|
+
int Ack (int M, int N) {
|
41
|
+
switch (M) {
|
42
|
+
case 0: return Ack0 (N);
|
43
|
+
case 1: return Ack1 (N);
|
44
|
+
case 2: return Ack2 (N);
|
45
|
+
case 3: return Ack3 (N);
|
46
|
+
case 4: return Ack4 (N);
|
47
|
+
}
|
48
|
+
|
49
|
+
if (0 == N) return Ack (M-1, 1);
|
50
|
+
if (1 == N) return Ack (M-1, Ack (M-1, 1));
|
51
|
+
return Ack (M-1, Ack (M, N-1));
|
52
|
+
}
|
53
|
+
|
54
|
+
/* Not called. Uses full mathematical simplfications when
|
55
|
+
its possible and unravells the recursion on the N
|
56
|
+
parameter into a for() loop (so the stack level never
|
57
|
+
goes deeper than O(M)). */
|
58
|
+
|
59
|
+
int AckCheat (int M, int N) {
|
60
|
+
switch (M) {
|
61
|
+
case 0: return Ack0 (N);
|
62
|
+
case 1: return N + 2;
|
63
|
+
case 2: return 2*N + 3;
|
64
|
+
case 3: if (N < (CHAR_BIT * sizeof(int) - 3))
|
65
|
+
return (1 << (N+3))-3;
|
66
|
+
}
|
67
|
+
|
68
|
+
if (0 == N) return AckCheat (M-1, 1);
|
69
|
+
if (1 == N) return AckCheat (M-1, AckCheat (M-1, 1));
|
70
|
+
|
71
|
+
{
|
72
|
+
int a = AckCheat (M-1, 1);
|
73
|
+
for (;N; N--) a = AckCheat (M-1, a);
|
74
|
+
return a;
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
int main(int argc, char *argv[]) {
|
79
|
+
int m = ((argc >= 3) ? atoi(argv[argc-2]) : 3);
|
80
|
+
int n = ((argc >= 2) ? atoi(argv[argc-1]) : 1);
|
81
|
+
|
82
|
+
printf("Ack(%d,%d): %d\n", m, n, Ack (m, n));
|
83
|
+
|
84
|
+
/* sleep long enough so we can measure memory usage */
|
85
|
+
#if (defined(__linux) || defined(__unix) || defined(__GNUC__))
|
86
|
+
sleep(1);
|
87
|
+
#elif (defined(WIN32) || defined(_WIN32) || defined(__WIN32) ||
|
88
|
+
defined(_WINDOWS) || defined(_WINDOWS_))
|
89
|
+
Sleep(1000);
|
90
|
+
#endif
|
91
|
+
|
92
|
+
return(0);
|
93
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
/* -*- mode: c -*-
|
2
|
+
* $Id: ackermann.gcc-3.gcc,v 1.1 2005-04-27 05:21:30 igouy-guest Exp $
|
3
|
+
* http://www.bagley.org/~doug/shootout/
|
4
|
+
*
|
5
|
+
* modified by Isaac Gouy
|
6
|
+
*/
|
7
|
+
|
8
|
+
#include <stdio.h>
|
9
|
+
#include <stdlib.h>
|
10
|
+
#include <unistd.h>
|
11
|
+
|
12
|
+
int Ack(int M, int N) { return(M ? (Ack(M-1,N ? Ack(M,(N-1)) : 1)) : N+1); }
|
13
|
+
|
14
|
+
int main(int argc, char *argv[]) {
|
15
|
+
int n = ((argc == 2) ? atoi(argv[1]) : 1);
|
16
|
+
|
17
|
+
printf("Ack(3,%d): %d\n", n, Ack(3, n));
|
18
|
+
return(0);
|
19
|
+
}
|
20
|
+
|
data/sources/gcc/ary.gcc
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
/* -*- mode: c -*-
|
2
|
+
* $Id: ary.gcc,v 1.2 2004-05-22 07:25:00 bfulgham Exp $
|
3
|
+
* http://www.bagley.org/~doug/shootout/
|
4
|
+
*
|
5
|
+
* this program is modified from:
|
6
|
+
* http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html
|
7
|
+
* Timing Trials, or, the Trials of Timing: Experiments with Scripting
|
8
|
+
* and User-Interface Languages</a> by Brian W. Kernighan and
|
9
|
+
* Christopher J. Van Wyk.
|
10
|
+
*
|
11
|
+
* I added free() to deallocate memory.
|
12
|
+
*/
|
13
|
+
|
14
|
+
#include <stdio.h>
|
15
|
+
#include <stdlib.h>
|
16
|
+
|
17
|
+
int
|
18
|
+
main(int argc, char *argv[]) {
|
19
|
+
int n = ((argc == 2) ? atoi(argv[1]) : 1);
|
20
|
+
int i, k, *x, *y;
|
21
|
+
|
22
|
+
x = (int *) calloc(n, sizeof(int));
|
23
|
+
y = (int *) calloc(n, sizeof(int));
|
24
|
+
|
25
|
+
for (i = 0; i < n; i++) {
|
26
|
+
x[i] = i + 1;
|
27
|
+
}
|
28
|
+
for (k=0; k<1000; k++) {
|
29
|
+
for (i = n-1; i >= 0; i--) {
|
30
|
+
y[i] += x[i];
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
fprintf(stdout, "%d %d\n", y[0], y[n-1]);
|
35
|
+
|
36
|
+
free(x);
|
37
|
+
free(y);
|
38
|
+
|
39
|
+
return(0);
|
40
|
+
}
|
@@ -0,0 +1,136 @@
|
|
1
|
+
/* The Computer Language Shootout Benchmarks
|
2
|
+
http://shootout.alioth.debian.org/
|
3
|
+
|
4
|
+
contributed by Kevin Carson
|
5
|
+
compilation:
|
6
|
+
gcc -O3 -fomit-frame-pointer -funroll-loops -static binary-trees.c -lm
|
7
|
+
icc -O3 -ip -unroll -static binary-trees.c -lm
|
8
|
+
*/
|
9
|
+
|
10
|
+
#include <malloc.h>
|
11
|
+
#include <math.h>
|
12
|
+
#include <stdio.h>
|
13
|
+
#include <stdlib.h>
|
14
|
+
|
15
|
+
|
16
|
+
typedef struct tn {
|
17
|
+
struct tn* left;
|
18
|
+
struct tn* right;
|
19
|
+
long item;
|
20
|
+
} treeNode;
|
21
|
+
|
22
|
+
|
23
|
+
treeNode* NewTreeNode(treeNode* left, treeNode* right, long item)
|
24
|
+
{
|
25
|
+
treeNode* new;
|
26
|
+
|
27
|
+
new = (treeNode*)malloc(sizeof(treeNode));
|
28
|
+
|
29
|
+
new->left = left;
|
30
|
+
new->right = right;
|
31
|
+
new->item = item;
|
32
|
+
|
33
|
+
return new;
|
34
|
+
} /* NewTreeNode() */
|
35
|
+
|
36
|
+
|
37
|
+
long ItemCheck(treeNode* tree)
|
38
|
+
{
|
39
|
+
if (tree->left == NULL)
|
40
|
+
return tree->item;
|
41
|
+
else
|
42
|
+
return tree->item + ItemCheck(tree->left) - ItemCheck(tree->right);
|
43
|
+
} /* ItemCheck() */
|
44
|
+
|
45
|
+
|
46
|
+
treeNode* BottomUpTree(long item, unsigned depth)
|
47
|
+
{
|
48
|
+
if (depth > 0)
|
49
|
+
return NewTreeNode
|
50
|
+
(
|
51
|
+
BottomUpTree(2 * item - 1, depth - 1),
|
52
|
+
BottomUpTree(2 * item, depth - 1),
|
53
|
+
item
|
54
|
+
);
|
55
|
+
else
|
56
|
+
return NewTreeNode(NULL, NULL, item);
|
57
|
+
} /* BottomUpTree() */
|
58
|
+
|
59
|
+
|
60
|
+
void DeleteTree(treeNode* tree)
|
61
|
+
{
|
62
|
+
if (tree->left != NULL)
|
63
|
+
{
|
64
|
+
DeleteTree(tree->left);
|
65
|
+
DeleteTree(tree->right);
|
66
|
+
}
|
67
|
+
|
68
|
+
free(tree);
|
69
|
+
} /* DeleteTree() */
|
70
|
+
|
71
|
+
|
72
|
+
int main(int argc, char* argv[])
|
73
|
+
{
|
74
|
+
unsigned N, depth, minDepth, maxDepth, stretchDepth;
|
75
|
+
treeNode *stretchTree, *longLivedTree, *tempTree;
|
76
|
+
|
77
|
+
N = atol(argv[1]);
|
78
|
+
|
79
|
+
minDepth = 4;
|
80
|
+
|
81
|
+
if ((minDepth + 2) > N)
|
82
|
+
maxDepth = minDepth + 2;
|
83
|
+
else
|
84
|
+
maxDepth = N;
|
85
|
+
|
86
|
+
stretchDepth = maxDepth + 1;
|
87
|
+
|
88
|
+
stretchTree = BottomUpTree(0, stretchDepth);
|
89
|
+
printf
|
90
|
+
(
|
91
|
+
"stretch tree of depth %u\t check: %li\n",
|
92
|
+
stretchDepth,
|
93
|
+
ItemCheck(stretchTree)
|
94
|
+
);
|
95
|
+
|
96
|
+
DeleteTree(stretchTree);
|
97
|
+
|
98
|
+
longLivedTree = BottomUpTree(0, maxDepth);
|
99
|
+
|
100
|
+
for (depth = minDepth; depth <= maxDepth; depth += 2)
|
101
|
+
{
|
102
|
+
long i, iterations, check;
|
103
|
+
|
104
|
+
iterations = pow(2, maxDepth - depth + minDepth);
|
105
|
+
|
106
|
+
check = 0;
|
107
|
+
|
108
|
+
for (i = 1; i <= iterations; i++)
|
109
|
+
{
|
110
|
+
tempTree = BottomUpTree(i, depth);
|
111
|
+
check += ItemCheck(tempTree);
|
112
|
+
DeleteTree(tempTree);
|
113
|
+
|
114
|
+
tempTree = BottomUpTree(-i, depth);
|
115
|
+
check += ItemCheck(tempTree);
|
116
|
+
DeleteTree(tempTree);
|
117
|
+
} /* for(i = 1...) */
|
118
|
+
|
119
|
+
printf
|
120
|
+
(
|
121
|
+
"%li\t trees of depth %u\t check: %li\n",
|
122
|
+
iterations * 2,
|
123
|
+
depth,
|
124
|
+
check
|
125
|
+
);
|
126
|
+
} /* for(depth = minDepth...) */
|
127
|
+
|
128
|
+
printf
|
129
|
+
(
|
130
|
+
"long lived tree of depth %u\t check: %li\n",
|
131
|
+
maxDepth,
|
132
|
+
ItemCheck(longLivedTree)
|
133
|
+
);
|
134
|
+
|
135
|
+
return 0;
|
136
|
+
} /* main() */
|
@@ -0,0 +1,140 @@
|
|
1
|
+
/* The Computer Language Shootout Benchmarks
|
2
|
+
http://shootout.alioth.debian.org/
|
3
|
+
|
4
|
+
contributed by Benoit Hudson based on a contribution of Kevin Carson
|
5
|
+
compilation:
|
6
|
+
gcc -O3 -fomit-frame-pointer binary-trees-2.c
|
7
|
+
*/
|
8
|
+
|
9
|
+
#include <stdlib.h>
|
10
|
+
#include <stdio.h>
|
11
|
+
|
12
|
+
typedef struct tn {
|
13
|
+
struct tn* left;
|
14
|
+
struct tn* right;
|
15
|
+
long item;
|
16
|
+
} treeNode;
|
17
|
+
|
18
|
+
static treeNode *freelist = 0;
|
19
|
+
|
20
|
+
treeNode* NewTreeNode(treeNode* left, treeNode* right, long item)
|
21
|
+
{
|
22
|
+
treeNode* new;
|
23
|
+
|
24
|
+
if (NULL == freelist) {
|
25
|
+
new = (treeNode*)malloc(sizeof(treeNode));
|
26
|
+
} else {
|
27
|
+
new = freelist;
|
28
|
+
freelist = freelist -> left;
|
29
|
+
}
|
30
|
+
|
31
|
+
new->left = left;
|
32
|
+
new->right = right;
|
33
|
+
new->item = item;
|
34
|
+
|
35
|
+
return new;
|
36
|
+
} /* NewTreeNode() */
|
37
|
+
|
38
|
+
|
39
|
+
long ItemCheck(treeNode* tree)
|
40
|
+
{
|
41
|
+
if (tree->left == NULL)
|
42
|
+
return tree->item;
|
43
|
+
else
|
44
|
+
return tree->item + ItemCheck(tree->left) - ItemCheck(tree->right);
|
45
|
+
} /* ItemCheck() */
|
46
|
+
|
47
|
+
|
48
|
+
treeNode* BottomUpTree(long item, unsigned depth)
|
49
|
+
{
|
50
|
+
if (depth > 0)
|
51
|
+
return NewTreeNode
|
52
|
+
(
|
53
|
+
BottomUpTree(2 * item - 1, depth - 1),
|
54
|
+
BottomUpTree(2 * item, depth - 1),
|
55
|
+
item
|
56
|
+
);
|
57
|
+
else
|
58
|
+
return NewTreeNode(NULL, NULL, item);
|
59
|
+
} /* BottomUpTree() */
|
60
|
+
|
61
|
+
|
62
|
+
void DeleteTree(treeNode* tree)
|
63
|
+
{
|
64
|
+
if (tree->left != NULL)
|
65
|
+
{
|
66
|
+
DeleteTree(tree->left);
|
67
|
+
DeleteTree(tree->right);
|
68
|
+
}
|
69
|
+
|
70
|
+
tree -> left = freelist;
|
71
|
+
freelist = tree;
|
72
|
+
} /* DeleteTree() */
|
73
|
+
|
74
|
+
|
75
|
+
int main(int argc, char* argv[])
|
76
|
+
{
|
77
|
+
unsigned N, depth, minDepth, maxDepth, stretchDepth;
|
78
|
+
treeNode *stretchTree, *longLivedTree, *tempTree;
|
79
|
+
|
80
|
+
N = atol(argv[1]);
|
81
|
+
|
82
|
+
minDepth = 4;
|
83
|
+
|
84
|
+
if ((minDepth + 2) > N)
|
85
|
+
maxDepth = minDepth + 2;
|
86
|
+
else
|
87
|
+
maxDepth = N;
|
88
|
+
|
89
|
+
stretchDepth = maxDepth + 1;
|
90
|
+
|
91
|
+
stretchTree = BottomUpTree(0, stretchDepth);
|
92
|
+
printf
|
93
|
+
(
|
94
|
+
"stretch tree of depth %u\t check: %li\n",
|
95
|
+
stretchDepth,
|
96
|
+
ItemCheck(stretchTree)
|
97
|
+
);
|
98
|
+
|
99
|
+
DeleteTree(stretchTree);
|
100
|
+
|
101
|
+
longLivedTree = BottomUpTree(0, maxDepth);
|
102
|
+
|
103
|
+
for (depth = minDepth; depth <= maxDepth; depth += 2)
|
104
|
+
{
|
105
|
+
long i, iterations, check;
|
106
|
+
|
107
|
+
iterations = 1 << (maxDepth - depth + minDepth);
|
108
|
+
|
109
|
+
check = 0;
|
110
|
+
|
111
|
+
for (i = 1; i <= iterations; i++)
|
112
|
+
{
|
113
|
+
tempTree = BottomUpTree(i, depth);
|
114
|
+
check += ItemCheck(tempTree);
|
115
|
+
DeleteTree(tempTree);
|
116
|
+
|
117
|
+
tempTree = BottomUpTree(-i, depth);
|
118
|
+
check += ItemCheck(tempTree);
|
119
|
+
DeleteTree(tempTree);
|
120
|
+
} /* for(i = 1...) */
|
121
|
+
|
122
|
+
printf
|
123
|
+
(
|
124
|
+
"%li\t trees of depth %u\t check: %li\n",
|
125
|
+
iterations * 2,
|
126
|
+
depth,
|
127
|
+
check
|
128
|
+
);
|
129
|
+
} /* for(depth = minDepth...) */
|
130
|
+
|
131
|
+
printf
|
132
|
+
(
|
133
|
+
"long lived tree of depth %u\t check: %li\n",
|
134
|
+
maxDepth,
|
135
|
+
ItemCheck(longLivedTree)
|
136
|
+
);
|
137
|
+
|
138
|
+
return 0;
|
139
|
+
} /* main() */
|
140
|
+
|