abst_int 0.0.3

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3be8352c028620f8caca5fcf4f4a292070e74872
4
+ data.tar.gz: 6067963034b62ef44f552c74fa4ad611907543e8
5
+ SHA512:
6
+ metadata.gz: 5dd3b7a312e3e838b1d27fa011130669ef6d657f03e56c5942a80f34fbdd9e8b908ca7f53b65b3345ab838d9c7199e7fb2d6cda9f89823c1dc5e01354bda6434
7
+ data.tar.gz: d1d864872303440274a85554f0f3118c96aa531f57bfd1dfd879d38d0ecb39c194a6884bd2f29635d987d41d6bfdb591508f311c1a8fc5bfa8e61cd69675bc3b
@@ -0,0 +1 @@
1
+ service_name: travis-ci
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.pryrc ADDED
@@ -0,0 +1,18 @@
1
+ # switch default editor for pry to sublime text
2
+ Pry.config.editor = "sublime"
3
+
4
+ # format prompt to be <Rails version>@<ruby version>(<object>)>
5
+ Pry.config.prompt = proc do |obj, level, _|
6
+ prompt = "\e[1;30m"
7
+ prompt << "#{Rails.version} @ " if defined?(Rails)
8
+ prompt << "#{RUBY_VERSION}"
9
+ "#{prompt} (#{obj})>\e[0m"
10
+ end
11
+
12
+ # use awesome print for all objects in pry
13
+ begin
14
+ require 'awesome_print'
15
+ Pry.config.print = proc { |output, value| output.puts "=> #{ap value}" }
16
+ rescue
17
+ puts "=> Unable to load awesome_print, please type 'gem install awesome_print' or 'sudo gem install awesome_print'."
18
+ end
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1 @@
1
+ 2.1.2
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
4
+ - 2.0.0
5
+ - 1.9.3
6
+ - jruby-19mode
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'coveralls', :require => false
5
+ end
6
+
7
+ # Specify your gem's dependencies in abst_int.gemspec
8
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Yuutetu
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.
@@ -0,0 +1,34 @@
1
+ # AbstInt
2
+
3
+ [![Coverage Status](https://coveralls.io/repos/yuutetu/abst_int/badge.png?branch=master)](https://coveralls.io/r/yuutetu/abst_int?branch=master)
4
+ [![Travis Status](https://travis-ci.org/yuutetu/abst_int.svg?branch=master)](https://travis-ci.org/yuutetu/abst_int)
5
+ [![Code Climate](https://codeclimate.com/github/yuutetu/abst_int.png)](https://codeclimate.com/github/yuutetu/abst_int)
6
+ [![Dependency Status](https://gemnasium.com/yuutetu/abst_int.svg)](https://gemnasium.com/yuutetu/abst_int)
7
+
8
+ TODO: Write a gem description
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'abst_int'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install abst_int
23
+
24
+ ## Usage
25
+
26
+ TODO: Write usage instructions here
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it ( https://github.com/[my-github-username]/abst_int/fork )
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create a new Pull Request
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rubygems'
3
+ require 'rake'
4
+ $:.unshift File.join(File.dirname(__FILE__), "lib")
5
+
6
+ require 'rspec/core'
7
+ require 'rspec/core/rake_task'
8
+
9
+ task :default => :spec
10
+
11
+ desc "Run all specs in spec directory"
12
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'abst_int/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "abst_int"
8
+ spec.version = AbstInt::VERSION
9
+ spec.authors = ["Yuutetu"]
10
+ spec.email = ["yuutetu@gmail.com"]
11
+ spec.summary = %q{Abstract Integer}
12
+ spec.description = %q{AbstInt provide abstract integer. This can be used to test exhaustively.}
13
+ spec.homepage = "https://github.com/yuutetu/abst_int"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "pry-doc"
26
+ end
@@ -0,0 +1,83 @@
1
+ require "abst_int/version"
2
+ require "abst_int/or_set"
3
+ require "abst_int/integer"
4
+
5
+ class AbstInt
6
+
7
+ class MultiResultError < StandardError
8
+ end
9
+
10
+ attr_reader :terms
11
+
12
+ def initialize terms = AbstInt::OrSet.new(1, true)
13
+ @terms = terms
14
+ end
15
+
16
+ def + abst_int_or_int
17
+ terms = to_set abst_int_or_int
18
+ return AbstInt.new(self.terms + terms)
19
+ end
20
+
21
+ def - abst_int_or_int
22
+ terms = to_set abst_int_or_int
23
+ return AbstInt.new(self.terms - terms)
24
+ end
25
+
26
+ def * abst_int_or_int
27
+ terms = to_set abst_int_or_int
28
+ return AbstInt.new(self.terms * terms)
29
+ end
30
+
31
+ def / abst_int_or_int
32
+ raise "not implement"
33
+ end
34
+
35
+ def % num
36
+ return self.terms % num
37
+ end
38
+
39
+ def | abst_int_or_int
40
+ terms = to_set abst_int_or_int
41
+ return AbstInt.new(self.terms | terms)
42
+ end
43
+
44
+ def & abst_int_or_int
45
+ terms = to_set abst_int_or_int
46
+ return AbstInt.new(self.terms & terms)
47
+ end
48
+
49
+ def not
50
+ return AbstInt.new(self.terms.not)
51
+ end
52
+
53
+ def to_s
54
+ @terms.to_s
55
+ end
56
+
57
+ def object
58
+ AbstInt::Integer.new self
59
+ end
60
+
61
+ private
62
+ def to_set abst_int_or_int
63
+ case abst_int_or_int
64
+ when ::Integer
65
+ terms = AbstInt::OrSet.new(abst_int_or_int)
66
+ when AbstInt
67
+ terms = abst_int_or_int.terms
68
+ when AbstInt::Integer
69
+ terms = abst_int_or_int._terms
70
+ end
71
+ return terms
72
+ end
73
+ end
74
+
75
+ class Fixnum
76
+ def mul_with_abst_int num
77
+ return num * self if (num.is_a? AbstInt) || (num.is_a? AbstInt::Integer)
78
+ self.mul_without_abst_int num
79
+ end
80
+
81
+ alias :mul_without_abst_int :*
82
+ alias :* :mul_with_abst_int
83
+ end
@@ -0,0 +1,287 @@
1
+ require "set"
2
+ require "abst_int/or_set"
3
+
4
+ class Object
5
+ def try key, *args
6
+ send key, *args
7
+ end
8
+ end
9
+
10
+ class NilClass
11
+ def try key, *args
12
+ nil
13
+ end
14
+
15
+ def [] some
16
+ return some
17
+ end
18
+
19
+ def + some
20
+ return some
21
+ end
22
+
23
+ def | some
24
+ return some
25
+ end
26
+ end
27
+
28
+ module AbstInt::CalculusModel
29
+ class Dfa
30
+ def initialize
31
+ @states = Set.new
32
+ @initial_state = nil
33
+ @final_states = Set.new
34
+ @inputs = Set.new
35
+ @transition = {}
36
+ end
37
+
38
+ def set_initial state
39
+ @states << state
40
+ @initial_state = state
41
+ end
42
+
43
+ def add_final state
44
+ @states << state
45
+ @final_states << state
46
+ end
47
+
48
+ # [state] 1,2,3
49
+ # [input] 'a', 'b', 'c'
50
+ def add_trans state1, input, state2
51
+ @states << state1 << state2
52
+ @inputs << input
53
+ @transition[state1] ||= {}
54
+ @transition[state1][input] = state2
55
+ end
56
+
57
+ def & another_dfa
58
+ dfa = AbstInt::CalculusModel::Dfa.new
59
+ dfa.set_initial [self.initial_state, another_dfa.initial_state]
60
+ dfa = generate_inter_dfa another_dfa, dfa, [self.initial_state, another_dfa.initial_state], Set.new
61
+ dfa.each_states do |states|
62
+ dfa.add_final states if self.final?(states[0]) && another_dfa.final?(states[1])
63
+ end
64
+ return dfa
65
+ end
66
+
67
+ def not
68
+ cloned_self = self.perfect
69
+ cloned_self.final_states = cloned_self.states - cloned_self.final_states
70
+ return cloned_self
71
+ end
72
+
73
+ def final? state
74
+ @final_states.include? state
75
+ end
76
+
77
+ def each_states &block
78
+ @states.each &block
79
+ end
80
+
81
+ def to_orset
82
+ return generate_orset
83
+ end
84
+
85
+ def equal_transition? dfa
86
+ @transition == dfa.transition
87
+ end
88
+
89
+ def self.filter_dfa
90
+ dfa = AbstInt::CalculusModel::Dfa.new
91
+ dfa.set_initial 'A'
92
+ dfa.add_trans 'A', 'a', 'A'
93
+ dfa.add_trans 'A', 'b', 'B'
94
+ dfa.add_trans 'B', 'b', 'B'
95
+ dfa.add_final 'A'
96
+ dfa.add_final 'B'
97
+ return dfa
98
+ end
99
+
100
+ def accept? str
101
+ result = rec_accept? str, @initial_state
102
+ return result
103
+ end
104
+
105
+ protected
106
+ def rec_accept? str, state
107
+ if str.length == 0
108
+ return @final_states.include?(state)
109
+ else
110
+ head = str[0]
111
+ rest = str[1..-1]
112
+ return nil if @transition.try(:[], state).try(:[], head).nil?
113
+ return self.rec_accept?(rest, @transition.try(:[], state).try(:[], head))
114
+ end
115
+ end
116
+
117
+ def transition
118
+ @transition
119
+ end
120
+
121
+ def initial_state
122
+ @initial_state
123
+ end
124
+
125
+ def final_states
126
+ @final_states
127
+ end
128
+
129
+ def final_states= final_states
130
+ @final_states = final_states
131
+ end
132
+
133
+ def states
134
+ @states
135
+ end
136
+
137
+ def perfect
138
+ cloned_self = AbstInt::CalculusModel::Dfa.new
139
+ cloned_self.set_initial self.initial_state
140
+ self.final_states.each do |state|
141
+ cloned_self.add_final state
142
+ end
143
+ @states.each do |state|
144
+ @inputs.each do |input|
145
+ if @transition.try(:[], state).try(:[], input).nil?
146
+ cloned_self.add_trans state, input, :reject
147
+ else
148
+ cloned_self.add_trans state, input, @transition[state][input]
149
+ end
150
+ end
151
+ end
152
+ return cloned_self
153
+ end
154
+
155
+ private
156
+ def generate_inter_dfa another_dfa, dfa, state, state_set
157
+ return dfa if state_set.include? state
158
+ @inputs.each do |input|
159
+ return dfa if self.transition.try(:[], state[0]).try(:[], input).nil?
160
+ return dfa if another_dfa.transition.try(:[], state[1]).try(:[], input).nil?
161
+ next_state = [self.transition[state[0]][input], another_dfa.transition[state[1]][input]]
162
+ dfa.add_trans state, input, next_state
163
+ dfa = generate_inter_dfa another_dfa, dfa, next_state, (state_set << state)
164
+ end
165
+ return dfa
166
+ end
167
+
168
+ def generate_orset
169
+
170
+ # アルファベットを数字に変更
171
+ current_dfa = AbstInt::CalculusModel::Dfa.new
172
+ current_dfa.set_initial @initial_state
173
+ @final_states.each do |state|
174
+ current_dfa.add_final state
175
+ end
176
+ @transition.each do |state1, value|
177
+ value.each do |input, state2|
178
+ case input
179
+ when 'a'
180
+ current_dfa.add_trans state1, AbstInt::OrSet.new(1), state2
181
+ when 'b'
182
+ current_dfa.add_trans state1, AbstInt::OrSet.new(-1), state2
183
+ end
184
+ end
185
+ end
186
+
187
+ remove_states = @states - ([@initial_state] + @final_states.to_a)
188
+
189
+ remove_states.each do |state|
190
+ current_dfa = self_remain_state_for_ofset_dfa! state, current_dfa
191
+ end
192
+
193
+ removed_dfa_without_final = current_dfa
194
+ current_dfa = nil
195
+ current_orset = nil
196
+
197
+ @final_states.each do |final|
198
+ current_dfa = removed_dfa_without_final
199
+ remove_states = current_dfa.states - [@initial_state, final]
200
+ remove_states.each do |state|
201
+ current_dfa = self_remain_state_for_ofset_dfa! state, current_dfa
202
+ end
203
+ if @initial_state == final then
204
+ current_dfa.transition[@initial_state].each do |input, state2|
205
+ if state2 == final
206
+ current_orset = current_orset | input.star
207
+ end
208
+ end
209
+ else
210
+ trans_cache = {}
211
+ current_dfa.transition.each do |state1, value|
212
+ value.each do |input, state2|
213
+ trans_cache[state1] ||= {}
214
+ trans_cache[state1][state2] = input
215
+ end
216
+ end
217
+ match_orset = (trans_cache[@initial_state][@initial_state] | (trans_cache[@initial_state][final] + trans_cache[final][final].try(:star) + trans_cache[final][@initial_state])).try(:star) + trans_cache[@initial_state][final] + trans_cache[final][final].try(:star)
218
+ current_orset = current_orset | match_orset
219
+ end
220
+ end
221
+
222
+ return current_orset
223
+ end
224
+
225
+ def cache_check start, goal, pass_states
226
+ return @cache.try(:[], start).try(:[], goal).try(:[], pass_states)
227
+ end
228
+
229
+ def save_cache start, goal, pass_states, result
230
+ @cache ||= {}
231
+ @cache[start] ||= {}
232
+ @cache[start][goal] ||= {}
233
+ @cache[start][goal][pass_states] = result
234
+ return nil
235
+ end
236
+
237
+ protected
238
+ def self_remain_state_for_ofset_dfa! state, current_dfa
239
+ new_dfa = AbstInt::CalculusModel::Dfa.new
240
+ in_states, out_states = Set.new, Set.new
241
+ current_dfa.transition.each do |state1, value|
242
+ value.each do |input, state2|
243
+ (out_states << state2; next) if state1 == state && state2 != state
244
+ (in_states << state1; next) if state1 != state && state2 == state
245
+ new_dfa.add_trans state1, input, state2
246
+ end
247
+ end
248
+
249
+ trans_cache = {}
250
+ current_dfa.transition.each do |state1, value|
251
+ value.each do |input, state2|
252
+ if in_states.include?(state1) ||
253
+ out_states.include?(state2) ||
254
+ state == state1 ||
255
+ state == state2 then
256
+ trans_cache[state1] ||= {}
257
+ trans_cache[state1][state2] = input
258
+ end
259
+ end
260
+ end
261
+
262
+ in_states.each do |in_state|
263
+ out_states.each do |out_state|
264
+ right_orset = nil
265
+ if trans_cache.try(:[], in_state).try(:[], state) && trans_cache.try(:[], state).try(:[], out_state) then
266
+ if trans_cache.try(:[], state).try(:[], state) then
267
+ right_orset = trans_cache[in_state][state] + trans_cache[state][out_state].star + trans_cache[state][out_state]
268
+ else
269
+ right_orset = trans_cache[in_state][state] + trans_cache[state][out_state]
270
+ end
271
+ end
272
+ if trans_cache.try(:[], in_state).try(:[], out_state) && right_orset then
273
+ new_orset = trans_cache[in_state][out_state] | right_orset
274
+ elsif right_orset then
275
+ new_orset = right_orset
276
+ else
277
+ new_orset = trans_cache[in_state][out_state]
278
+ end
279
+
280
+ new_dfa.add_trans in_state, new_orset, out_state if new_orset
281
+ end
282
+ end
283
+
284
+ return new_dfa
285
+ end
286
+ end
287
+ end