balanced_ternary_integer 0.1.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -1
- data/Rakefile +6 -1
- data/balanced_ternary_integer.gemspec +2 -0
- data/ext/balanced_ternary_integer/balanced_ternary_integer.c +8 -0
- data/ext/balanced_ternary_integer/balanced_ternary_integer.h +3 -0
- data/ext/balanced_ternary_integer/extconf.rb +4 -0
- data/ext/balanced_ternary_integer/to_int.c +35 -0
- data/lib/balanced_ternary_integer.rb +1 -10
- data/lib/balanced_ternary_integer/version.rb +1 -1
- metadata +23 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2bbe5fbcb23d5d4abf13b8a09caee8872dfee3ad60da15b01034b6faed8a96c
|
4
|
+
data.tar.gz: a3435a829155844b16d210d910b7e67344ce37ab5b2a6360736595efa4f1605d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fecd7b75c16afb2d808498e016664f1ff8c569316c6d7e69aa52766dac5cde8940da3dee0e288f695bd02f977cae8aa661f781af7e162ddd935a6dfb738238ff
|
7
|
+
data.tar.gz: fac248f8af6dfdccd1c5f84db77f5d7dbc0c53c1a4e8bba602158c63549dc7cd0af58e35c339515fa900289d5be2944808154e4f362aa4fd3c94b9c31f0f8b74
|
data/CHANGELOG.md
CHANGED
@@ -1,2 +1,8 @@
|
|
1
1
|
### 0.1.0 - 2020-04-20
|
2
|
-
- Implemented BalancedTernaryInteger.from_int and BalancedTernaryInteger.to_int
|
2
|
+
- Implemented BalancedTernaryInteger.from_int and BalancedTernaryInteger.to_int
|
3
|
+
|
4
|
+
### 0.2.0 - 2020-05-07
|
5
|
+
- Translated BalancedTernaryInteger.to_int to C
|
6
|
+
|
7
|
+
### 0.2.1 - 2020-05-07
|
8
|
+
- Fix gemspec for native extension
|
data/Rakefile
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
2
|
require "rspec/core/rake_task"
|
3
|
+
require "rake/extensiontask"
|
3
4
|
|
4
5
|
RSpec::Core::RakeTask.new(:spec)
|
5
6
|
|
6
|
-
task :
|
7
|
+
task :build => :compile
|
8
|
+
|
9
|
+
Rake::ExtensionTask.new("balanced_ternary_integer") do |ext|
|
10
|
+
ext.lib_dir = "lib/balanced_ternary_integer" # .co - file
|
11
|
+
end
|
@@ -26,10 +26,12 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.bindir = "exe"
|
27
27
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
28
|
spec.require_paths = ["lib"]
|
29
|
+
spec.extensions = %w[ext/balanced_ternary_integer/extconf.rb]
|
29
30
|
|
30
31
|
spec.add_development_dependency "bundler", "~> 1.17"
|
31
32
|
spec.add_development_dependency "rake", ">= 12.3.3"
|
32
33
|
spec.add_development_dependency "rspec", "~> 3.0"
|
34
|
+
spec.add_development_dependency "rake-compiler", "~> 1.1"
|
33
35
|
|
34
36
|
spec.required_ruby_version = '>= 2.4.0'
|
35
37
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
#include "balanced_ternary_integer.h"
|
2
|
+
|
3
|
+
short char_to_num(char digit) {
|
4
|
+
switch(digit) {
|
5
|
+
case 'T':
|
6
|
+
return -1;
|
7
|
+
case '0':
|
8
|
+
return 0;
|
9
|
+
case '1':
|
10
|
+
return 1;
|
11
|
+
default:
|
12
|
+
rb_raise(rb_eArgError, "String must contain only 0, 1, T symbols");
|
13
|
+
}
|
14
|
+
}
|
15
|
+
|
16
|
+
VALUE rb_balanced_ternary_integer_to_int(VALUE self, VALUE rb_string) {
|
17
|
+
Check_Type(rb_string, T_STRING); // raise TypeError (wrong argument type Integer (expected String))
|
18
|
+
|
19
|
+
char *chars = RSTRING_PTR(rb_string);
|
20
|
+
long len = RSTRING_LEN(rb_string);
|
21
|
+
VALUE exp = INT2FIX(1);
|
22
|
+
VALUE result = INT2FIX(0);
|
23
|
+
short digit;
|
24
|
+
|
25
|
+
for (int i = len - 1; i >= 0; i--) {
|
26
|
+
digit = INT2NUM(char_to_num(chars[i]));
|
27
|
+
result = rb_funcall(result, '+', 1, rb_funcall(digit, '*', 1, exp)); // result += digit * exp
|
28
|
+
exp = rb_funcall(exp, '*', 1, INT2FIX(3)); // множитель (1, 3, 9 и тд)
|
29
|
+
}
|
30
|
+
return result;
|
31
|
+
}
|
32
|
+
|
33
|
+
void Init_to_int(VALUE mBalancedTernaryInteger) {
|
34
|
+
rb_define_singleton_method(mBalancedTernaryInteger, "to_int", rb_balanced_ternary_integer_to_int, 1);
|
35
|
+
}
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'balanced_ternary_integer/version'
|
2
|
+
require 'balanced_ternary_integer/balanced_ternary_integer' # .co file
|
2
3
|
|
3
4
|
module BalancedTernaryInteger
|
4
5
|
class << self
|
@@ -13,15 +14,5 @@ module BalancedTernaryInteger
|
|
13
14
|
end until number.zero?
|
14
15
|
digits
|
15
16
|
end
|
16
|
-
|
17
|
-
def to_int(string)
|
18
|
-
raise ArgumentError, "String expected, got #{string.class}" unless string.instance_of?(String)
|
19
|
-
raise ArgumentError, 'String must contain only 0, 1, T symbols' unless string.match?(/\A[01T]*\z/)
|
20
|
-
|
21
|
-
string.chars.reverse.map.with_index.sum do |char, index|
|
22
|
-
num = char == 'T' ? -1 : char.to_i
|
23
|
-
num * 3**index
|
24
|
-
end
|
25
|
-
end
|
26
17
|
end
|
27
18
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: balanced_ternary_integer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vasiliy Petrov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-05-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,11 +52,26 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake-compiler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.1'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.1'
|
55
69
|
description: Conversion integers from/to balanced ternary and decimal numeral system.
|
56
70
|
email:
|
57
71
|
- saugardas@yandex.ru
|
58
72
|
executables: []
|
59
|
-
extensions:
|
73
|
+
extensions:
|
74
|
+
- ext/balanced_ternary_integer/extconf.rb
|
60
75
|
extra_rdoc_files: []
|
61
76
|
files:
|
62
77
|
- ".gitignore"
|
@@ -69,6 +84,10 @@ files:
|
|
69
84
|
- balanced_ternary_integer.gemspec
|
70
85
|
- bin/console
|
71
86
|
- bin/setup
|
87
|
+
- ext/balanced_ternary_integer/balanced_ternary_integer.c
|
88
|
+
- ext/balanced_ternary_integer/balanced_ternary_integer.h
|
89
|
+
- ext/balanced_ternary_integer/extconf.rb
|
90
|
+
- ext/balanced_ternary_integer/to_int.c
|
72
91
|
- lib/balanced_ternary_integer.rb
|
73
92
|
- lib/balanced_ternary_integer/version.rb
|
74
93
|
homepage: https://github.com/Saugardas/balanced_ternary_integer
|
@@ -91,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
110
|
- !ruby/object:Gem::Version
|
92
111
|
version: '0'
|
93
112
|
requirements: []
|
94
|
-
rubygems_version: 3.
|
113
|
+
rubygems_version: 3.1.2
|
95
114
|
signing_key:
|
96
115
|
specification_version: 4
|
97
116
|
summary: Conversion integers from/to balanced ternary and decimal numeral system.
|