n_adic_number 0.0.1

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: 73c03a05fd526fed55b1ccab547829006a268563
4
+ data.tar.gz: 38b9607a2dc394e22aeafc3f49fc0736342f4f58
5
+ SHA512:
6
+ metadata.gz: 9701f7878f411ae5fe080b33411b1ac1fb4ad97b9bdfa3acdd6e2658b76f5383c13a5ca4141daf3a9532aa271841861a323122fa95c47cebed8cc2fec063848c
7
+ data.tar.gz: 20fdb12b6e9a0ecc49672158caf6083bfe385079747ac3ab95b8021146db6924296cee2717d2db06c609ccac56f64d77bbfa8f640c6315370e34dcdf64dfb1a9
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in n_adic_number.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 tsukasaoishi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # NAdicNumber
2
+
3
+ NAdicNumber treats N-adic Number.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'n_adic_number'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install n_adic_number
18
+
19
+ ## Usage
20
+ NAdicNumber::Base.mapping is setting map table of N-adic Number.
21
+ For example, 16-adic:
22
+
23
+ class NumberObBase16 < NAdicNumber::Base
24
+ mapping ["0".."9", "a".."f"].map{|r| r.to_s}.flatten
25
+ end
26
+
27
+ For example, 94-adic:
28
+
29
+ class NumberObBase94 < NAdicNumber::Base
30
+ mapping (33..126).map{|i| i.chr}
31
+ end
32
+
33
+ Sample:
34
+
35
+ class NumberOfBase62 < NAdicNumber::Base
36
+ mapping ["0".."9", "A".."Z", "a".."z"].map{|r| r.to_a}.flatter
37
+ end
38
+
39
+ one = NumberOfBase62.new(1)
40
+ sixty_two = NumberOfBase62.new(62)
41
+
42
+ one.to_s #=> "1"
43
+ sixty_two.to_s #=> "z"
44
+ one.to_i #=> 1
45
+ sixty_two.to_i => 62
46
+
47
+ sum = one + sixty_two
48
+ sum.to_i #=> 63
49
+ sum.to_s #=> "10"
50
+
51
+ sum += 100
52
+ sum.to_s #=> "2d"
53
+ sum.to_i #=> 163
54
+
55
+ all_a = NumberOfBase62.new("aaaaa")
56
+ all_a.to_s #=> "aaaaa"
57
+ all_a.to_i #=> 540668556
58
+
59
+
60
+
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,99 @@
1
+ module NAdicNumber
2
+ class Base
3
+ class << self
4
+ attr_reader :base_num, :map_table, :reverse_map
5
+
6
+ def mapping(ary)
7
+ raise ArgumentError, "map_table is not Array" unless ary.is_a?(Array)
8
+ raise ArgumentError, "map_table is empty" if ary.empty?
9
+ raise ArgementError, "map_table is not unique" if ary.uniq!
10
+
11
+ @map_table = ary
12
+ @base_num = ary.size
13
+ @reverse_map = {}
14
+ ary.each_with_index{|ch,i| @reverse_map[ch] = i}
15
+ @keta = (0..9).map{|i| @base_num ** i}
16
+ nil
17
+ end
18
+
19
+ def keta(pos)
20
+ @keta[pos] ||= @base_num ** pos
21
+ end
22
+ end
23
+
24
+ def initialize(seed)
25
+ raise "Not define map_table" unless base_num
26
+
27
+ case seed
28
+ when Fixnum
29
+ raise ArgumentError if seed < 0
30
+ @integer = seed
31
+ when String
32
+ @str_chars = seed.reverse.chars
33
+ raise ArgumentError if @str_chars.one?{|ch| !reverse_map.has_key?(ch)}
34
+ @string = seed
35
+ else
36
+ raise ArgumentError
37
+ end
38
+ end
39
+
40
+ def to_s
41
+ @string ||= make_string
42
+ end
43
+
44
+ def to_i
45
+ @integer ||= make_integer
46
+ end
47
+
48
+ %w|+ - * /|.each do |op|
49
+ define_method(op) do |something|
50
+ new_val = self.to_i.__send__(op, something.to_i)
51
+ self.class.new(new_val)
52
+ end
53
+ end
54
+
55
+ def inspect
56
+ %Q|#<#{self.class.name}:0x#{"%014x" % (self.object_id << 1)} base:#{base_num} num:#{to_s} integer:#{to_i}>|
57
+ end
58
+
59
+ private
60
+
61
+ def make_string
62
+ if @integer == 0
63
+ map_table.first.dup
64
+ else
65
+ string = ""
66
+ work = @integer
67
+ while work != 0
68
+ work, num = work.divmod(base_num)
69
+ string << map_table[num]
70
+ end
71
+ string.reverse
72
+ end
73
+ end
74
+
75
+ def make_integer
76
+ integer = 0
77
+ @str_chars.each_with_index do |ch, index|
78
+ integer += reverse_map[ch] * keta(index)
79
+ end
80
+ integer
81
+ end
82
+
83
+ def base_num
84
+ self.class.base_num
85
+ end
86
+
87
+ def keta(pos)
88
+ self.class.keta(pos)
89
+ end
90
+
91
+ def map_table
92
+ self.class.map_table
93
+ end
94
+
95
+ def reverse_map
96
+ self.class.reverse_map
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,3 @@
1
+ module NAdicNumber
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1 @@
1
+ require 'n_adic_number/base'
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'n_adic_number/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "n_adic_number"
8
+ spec.version = NAdicNumber::VERSION
9
+ spec.authors = ["tsukasaoishi"]
10
+ spec.email = ["tsukasa.oishi@gmail.com"]
11
+ spec.description = %q{https://github.com/tsukasaoishi/n_adic_number}
12
+ spec.summary = %q{NAdicNumber treats N-adic Number}
13
+ spec.homepage = "https://github.com/tsukasaoishi/n_adic_number"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,62 @@
1
+ require 'benchmark'
2
+ require File.expand_path('../../lib/n_adic_number/base.rb', __FILE__)
3
+
4
+ N = NAdicNumber::Base
5
+ N.mapping ["0".."9", "a".."z"].map{|r| r.to_a}.flatten
6
+
7
+ TEST_COUNT = 100000
8
+ sample_list = []
9
+ TEST_COUNT.times{|n| sample_list << n.to_s(36)}
10
+ int_list = []
11
+ TEST_COUNT.times{|n| int_list << N.new(n)}
12
+ str_list = sample_list.map{|n| N.new(n)}
13
+
14
+ Benchmark.bm(10) do |x|
15
+ x.report("init_i") do
16
+ TEST_COUNT.times do |n|
17
+ N.new(n)
18
+ end
19
+ end
20
+
21
+ x.report("init_s") do
22
+ sample_list.each do |n|
23
+ N.new(n)
24
+ end
25
+ end
26
+
27
+ x.report("to_s_from_s") do
28
+ str_list.each do |n|
29
+ n.to_s
30
+ end
31
+ end
32
+
33
+ x.report("to_s_from_i") do
34
+ int_list.each do |n|
35
+ n.to_s
36
+ end
37
+ end
38
+
39
+ x.report("to_i_from_s") do
40
+ str_list.each do |n|
41
+ n.to_i
42
+ end
43
+ end
44
+
45
+ x.report("to_i_from_i") do
46
+ int_list.each do |n|
47
+ n.to_i
48
+ end
49
+ end
50
+
51
+ x.report("smpl_s") do
52
+ TEST_COUNT.times do |n|
53
+ n.to_s(36)
54
+ end
55
+ end
56
+
57
+ x.report("smpl_i") do
58
+ sample_list.each do |n|
59
+ n.to_i(36)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,61 @@
1
+ # coding: utf-8
2
+
3
+ require_relative './test_helper'
4
+
5
+ class TestNAdicNumner < MiniTest::Unit::TestCase
6
+ N = NAdicNumber::Base
7
+
8
+ def setup
9
+ @map_table = ["0".."9", "A".."Z", "a".."z"].map{|r| r.to_a}.flatten
10
+ #@map_table = ("あ".."ん").to_a
11
+ N.mapping @map_table
12
+ end
13
+
14
+ def test_zero
15
+ assert_equal N.new(0).to_s, @map_table.first
16
+ assert_equal N.new(@map_table.first).to_i, 0
17
+ end
18
+
19
+ def test_last
20
+ assert_equal N.new(@map_table.size - 1).to_s, @map_table.last
21
+ assert_equal N.new(@map_table.last).to_i, @map_table.size - 1
22
+ end
23
+
24
+ def test_border
25
+ str = @map_table[0,2].reverse.join
26
+ assert_equal N.new(@map_table.size).to_s, str
27
+ assert_equal N.new(str).to_i, @map_table.size
28
+ end
29
+
30
+ def test_most_of_all
31
+ base = [0, 2 ** 32]
32
+ 32.times do |base|
33
+ pos = 2 ** base
34
+ (pos..(pos + 10000)).each do |num|
35
+ inst = N.new(num)
36
+ int = inst.to_i
37
+ assert_equal num, int
38
+ end
39
+ end
40
+ end
41
+
42
+ def test_add
43
+ inst = N.new(100) + 50
44
+ assert_equal 100 + 50, inst.to_i
45
+ end
46
+
47
+ def test_subtract
48
+ inst = N.new(100) - 50
49
+ assert_equal 100 - 50, inst.to_i
50
+ end
51
+
52
+ def test_multiply
53
+ inst = N.new(100) * 50
54
+ assert_equal 100 * 50, inst.to_i
55
+ end
56
+
57
+ def test_divide
58
+ inst = N.new(100) / 50
59
+ assert_equal 100 / 50, inst.to_i
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require File.expand_path('../../lib/n_adic_number/base.rb', __FILE__)
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: n_adic_number
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - tsukasaoishi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: https://github.com/tsukasaoishi/n_adic_number
42
+ email:
43
+ - tsukasa.oishi@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/n_adic_number.rb
54
+ - lib/n_adic_number/base.rb
55
+ - lib/n_adic_number/version.rb
56
+ - n_adic_number.gemspec
57
+ - test/bench_test.rb
58
+ - test/n_adic_number_test.rb
59
+ - test/test_helper.rb
60
+ homepage: https://github.com/tsukasaoishi/n_adic_number
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.0.0
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: NAdicNumber treats N-adic Number
84
+ test_files:
85
+ - test/bench_test.rb
86
+ - test/n_adic_number_test.rb
87
+ - test/test_helper.rb