rbs 0.13.1 → 0.18.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +9 -5
- data/.gitignore +0 -1
- data/CHANGELOG.md +38 -2
- data/Gemfile +4 -0
- data/README.md +9 -3
- data/Rakefile +19 -1
- data/Steepfile +1 -1
- data/bin/annotate-with-rdoc +1 -1
- data/bin/setup +0 -2
- data/bin/test_runner.rb +15 -1
- data/{stdlib/builtin → core}/array.rbs +124 -120
- data/{stdlib/builtin → core}/basic_object.rbs +54 -54
- data/{stdlib/builtin → core}/binding.rbs +42 -42
- data/core/builtin.rbs +70 -0
- data/{stdlib/builtin → core}/class.rbs +33 -33
- data/{stdlib/builtin → core}/comparable.rbs +0 -0
- data/{stdlib/builtin → core}/complex.rbs +90 -90
- data/{stdlib/builtin → core}/constants.rbs +0 -0
- data/{stdlib/builtin → core}/data.rbs +0 -0
- data/{stdlib/builtin → core}/deprecated.rbs +0 -0
- data/{stdlib/builtin → core}/dir.rbs +1 -1
- data/{stdlib/builtin → core}/encoding.rbs +33 -33
- data/{stdlib/builtin → core}/enumerable.rbs +99 -92
- data/{stdlib/builtin → core}/enumerator.rbs +40 -40
- data/{stdlib/builtin → core}/errno.rbs +0 -0
- data/{stdlib/builtin → core}/errors.rbs +2 -2
- data/{stdlib/builtin → core}/exception.rbs +50 -50
- data/{stdlib/builtin → core}/false_class.rbs +6 -6
- data/{stdlib/builtin → core}/fiber.rbs +14 -14
- data/{stdlib/builtin → core}/fiber_error.rbs +1 -1
- data/{stdlib/builtin → core}/file.rbs +0 -0
- data/{stdlib/builtin → core}/file_test.rbs +0 -0
- data/{stdlib/builtin → core}/float.rbs +161 -161
- data/{stdlib/builtin → core}/gc.rbs +3 -3
- data/{stdlib/builtin → core}/hash.rbs +15 -15
- data/{stdlib/builtin → core}/integer.rbs +0 -0
- data/{stdlib/builtin → core}/io.rbs +89 -89
- data/{stdlib/builtin → core}/kernel.rbs +70 -154
- data/{stdlib/builtin → core}/marshal.rbs +0 -0
- data/{stdlib/builtin → core}/match_data.rbs +1 -1
- data/{stdlib/builtin → core}/math.rbs +0 -0
- data/{stdlib/builtin → core}/method.rbs +19 -19
- data/{stdlib/builtin → core}/module.rbs +13 -13
- data/{stdlib/builtin → core}/nil_class.rbs +20 -20
- data/{stdlib/builtin → core}/numeric.rbs +101 -101
- data/{stdlib/builtin → core}/object.rbs +173 -173
- data/core/object_space.rbs +98 -0
- data/{stdlib/builtin → core}/proc.rbs +91 -91
- data/{stdlib/builtin → core}/process.rbs +0 -0
- data/{stdlib/builtin → core}/random.rbs +1 -1
- data/{stdlib/builtin → core}/range.rbs +4 -6
- data/{stdlib/builtin → core}/rational.rbs +83 -83
- data/{stdlib/builtin → core}/rb_config.rbs +0 -0
- data/{stdlib/builtin → core}/regexp.rbs +0 -0
- data/{stdlib/builtin → core}/ruby_vm.rbs +0 -0
- data/{stdlib/builtin → core}/signal.rbs +7 -7
- data/{stdlib/builtin → core}/string.rbs +10 -10
- data/{stdlib/builtin → core}/string_io.rbs +8 -8
- data/{stdlib/builtin → core}/struct.rbs +2 -2
- data/{stdlib/builtin → core}/symbol.rbs +1 -1
- data/{stdlib/builtin → core}/thread.rbs +189 -189
- data/{stdlib/builtin → core}/thread_group.rbs +2 -2
- data/{stdlib/builtin → core}/time.rbs +0 -0
- data/{stdlib/builtin → core}/trace_point.rbs +0 -0
- data/{stdlib/builtin → core}/true_class.rbs +10 -10
- data/{stdlib/builtin → core}/unbound_method.rbs +0 -0
- data/{stdlib/builtin → core}/warning.rbs +1 -1
- data/docs/CONTRIBUTING.md +1 -0
- data/docs/repo.md +125 -0
- data/docs/syntax.md +50 -6
- data/goodcheck.yml +22 -5
- data/lib/rbs.rb +1 -0
- data/lib/rbs/ast/comment.rb +1 -1
- data/lib/rbs/cli.rb +105 -103
- data/lib/rbs/definition.rb +9 -4
- data/lib/rbs/definition_builder.rb +54 -22
- data/lib/rbs/environment.rb +1 -1
- data/lib/rbs/environment_loader.rb +79 -105
- data/lib/rbs/environment_walker.rb +58 -29
- data/lib/rbs/namespace.rb +1 -1
- data/lib/rbs/parser.rb +3153 -0
- data/lib/rbs/parser.y +25 -11
- data/lib/rbs/prototype/rb.rb +186 -25
- data/lib/rbs/prototype/runtime.rb +18 -7
- data/lib/rbs/repository.rb +121 -0
- data/lib/rbs/test/hook.rb +27 -15
- data/lib/rbs/test/setup.rb +5 -3
- data/lib/rbs/test/setup_helper.rb +4 -4
- data/lib/rbs/test/tester.rb +4 -1
- data/lib/rbs/test/type_check.rb +18 -7
- data/lib/rbs/type_name.rb +3 -2
- data/lib/rbs/variance_calculator.rb +1 -1
- data/lib/rbs/vendorer.rb +38 -16
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/writer.rb +1 -1
- data/sig/cli.rbs +58 -0
- data/sig/constant.rbs +2 -2
- data/sig/constant_table.rbs +11 -11
- data/sig/declarations.rbs +2 -2
- data/sig/definition.rbs +10 -5
- data/sig/definition_builder.rbs +4 -1
- data/sig/environment_loader.rbs +92 -46
- data/sig/members.rbs +2 -2
- data/sig/method_types.rbs +1 -1
- data/sig/namespace.rbs +4 -4
- data/sig/parser.rbs +25 -0
- data/sig/polyfill.rbs +42 -0
- data/sig/rbs.rbs +8 -0
- data/sig/repository.rbs +79 -0
- data/sig/substitution.rbs +3 -3
- data/sig/typename.rbs +1 -1
- data/sig/types.rbs +1 -1
- data/sig/vendorer.rbs +44 -0
- data/sig/writer.rbs +15 -15
- data/stdlib/abbrev/{abbrev.rbs → 0/abbrev.rbs} +0 -0
- data/stdlib/base64/{base64.rbs → 0/base64.rbs} +1 -1
- data/stdlib/benchmark/{benchmark.rbs → 0/benchmark.rbs} +2 -2
- data/stdlib/{bigdecimal/math → bigdecimal-math/0}/big_math.rbs +0 -0
- data/stdlib/bigdecimal/{big_decimal.rbs → 0/big_decimal.rbs} +0 -0
- data/stdlib/coverage/{coverage.rbs → 0/coverage.rbs} +2 -2
- data/stdlib/csv/{csv.rbs → 0/csv.rbs} +4 -4
- data/stdlib/date/{date.rbs → 0/date.rbs} +4 -4
- data/stdlib/date/{date_time.rbs → 0/date_time.rbs} +1 -1
- data/stdlib/dbm/0/dbm.rbs +277 -0
- data/stdlib/erb/{erb.rbs → 0/erb.rbs} +0 -0
- data/stdlib/fiber/{fiber.rbs → 0/fiber.rbs} +0 -0
- data/stdlib/find/{find.rbs → 0/find.rbs} +12 -12
- data/stdlib/forwardable/{forwardable.rbs → 0/forwardable.rbs} +0 -0
- data/stdlib/ipaddr/{ipaddr.rbs → 0/ipaddr.rbs} +0 -0
- data/stdlib/json/{json.rbs → 0/json.rbs} +0 -0
- data/stdlib/logger/{formatter.rbs → 0/formatter.rbs} +0 -0
- data/stdlib/logger/{log_device.rbs → 0/log_device.rbs} +1 -1
- data/stdlib/logger/{logger.rbs → 0/logger.rbs} +1 -1
- data/stdlib/logger/{period.rbs → 0/period.rbs} +0 -0
- data/stdlib/logger/{severity.rbs → 0/severity.rbs} +0 -0
- data/stdlib/mutex_m/{mutex_m.rbs → 0/mutex_m.rbs} +0 -0
- data/stdlib/pathname/{pathname.rbs → 0/pathname.rbs} +40 -40
- data/stdlib/prime/{integer-extension.rbs → 0/integer-extension.rbs} +0 -0
- data/stdlib/prime/{prime.rbs → 0/prime.rbs} +1 -1
- data/stdlib/pstore/0/pstore.rbs +287 -0
- data/stdlib/pty/{pty.rbs → 0/pty.rbs} +1 -1
- data/stdlib/securerandom/{securerandom.rbs → 0/securerandom.rbs} +0 -0
- data/stdlib/set/{set.rbs → 0/set.rbs} +10 -10
- data/stdlib/singleton/0/singleton.rbs +111 -0
- data/stdlib/tmpdir/{tmpdir.rbs → 0/tmpdir.rbs} +12 -12
- data/stdlib/tsort/0/cyclic.rbs +4 -0
- data/stdlib/tsort/0/interfaces.rbs +19 -0
- data/stdlib/tsort/0/tsort.rbs +363 -0
- data/stdlib/uri/{file.rbs → 0/file.rbs} +0 -0
- data/stdlib/uri/{generic.rbs → 0/generic.rbs} +1 -1
- data/stdlib/uri/{http.rbs → 0/http.rbs} +0 -0
- data/stdlib/uri/{https.rbs → 0/https.rbs} +0 -0
- data/stdlib/uri/{ldap.rbs → 0/ldap.rbs} +0 -0
- data/stdlib/uri/{ldaps.rbs → 0/ldaps.rbs} +0 -0
- data/stdlib/yaml/0/dbm.rbs +221 -0
- data/stdlib/yaml/0/store.rbs +53 -0
- data/stdlib/zlib/{zlib.rbs → 0/zlib.rbs} +0 -0
- data/steep/Gemfile.lock +9 -9
- metadata +109 -94
- data/stdlib/builtin/builtin.rbs +0 -42
@@ -66,7 +66,7 @@ module PTY
|
|
66
66
|
# : If `true` and the process identified by `pid` is no longer alive a
|
67
67
|
# PTY::ChildExited is raised.
|
68
68
|
#
|
69
|
-
def self.check: (Integer pid, ?
|
69
|
+
def self.check: (Integer pid, ?boolish raise) -> Process::Status?
|
70
70
|
|
71
71
|
alias self.getpty self.spawn
|
72
72
|
|
File without changes
|
@@ -51,8 +51,8 @@ class Set[A]
|
|
51
51
|
# Set.new(1..5) #=> #<Set: {1, 2, 3, 4, 5}>
|
52
52
|
# Set.new([1, 2, 3]) { |x| x * x } #=> #<Set: {1, 4, 9}>
|
53
53
|
#
|
54
|
-
def initialize: (_Each[A
|
55
|
-
| [X] (_Each[X
|
54
|
+
def initialize: (_Each[A]) -> untyped
|
55
|
+
| [X] (_Each[X]) { (X) -> A } -> untyped
|
56
56
|
| (?nil) -> untyped
|
57
57
|
|
58
58
|
# Creates a new set containing the given objects.
|
@@ -69,7 +69,7 @@ class Set[A]
|
|
69
69
|
# Set[1, 3, 5] & Set[3, 2, 1] #=> #<Set: {3, 1}>
|
70
70
|
# Set['a', 'b', 'z'] & ['a', 'b', 'c'] #=> #<Set: {"a", "b"}>
|
71
71
|
#
|
72
|
-
def &: (_Each[A
|
72
|
+
def &: (_Each[A]) -> self
|
73
73
|
|
74
74
|
alias intersection &
|
75
75
|
|
@@ -79,7 +79,7 @@ class Set[A]
|
|
79
79
|
# Set[1, 2, 3] | Set[2, 4, 5] #=> #<Set: {1, 2, 3, 4, 5}>
|
80
80
|
# Set[1, 5, 'z'] | (1..6) #=> #<Set: {1, 5, "z", 2, 3, 4, 6}>
|
81
81
|
#
|
82
|
-
def |: (_Each[A
|
82
|
+
def |: (_Each[A]) -> self
|
83
83
|
|
84
84
|
alias union |
|
85
85
|
|
@@ -91,7 +91,7 @@ class Set[A]
|
|
91
91
|
# Set[1, 3, 5] - Set[1, 5] #=> #<Set: {3}>
|
92
92
|
# Set['a', 'b', 'z'] - ['a', 'c'] #=> #<Set: {"b", "z"}>
|
93
93
|
#
|
94
|
-
def -: (_Each[A
|
94
|
+
def -: (_Each[A]) -> self
|
95
95
|
|
96
96
|
alias difference -
|
97
97
|
|
@@ -133,7 +133,7 @@ class Set[A]
|
|
133
133
|
# Set[1, 2] ^ Set[2, 3] #=> #<Set: {3, 1}>
|
134
134
|
# Set[1, 'b', 'c'] ^ ['b', 'd'] #=> #<Set: {"d", 1, "c"}>
|
135
135
|
#
|
136
|
-
def ^: (_Each[A
|
136
|
+
def ^: (_Each[A]) -> self
|
137
137
|
|
138
138
|
# Classifies the set by the return value of the given block and returns a hash
|
139
139
|
# of {value => set of elements} pairs. The block is called once for each
|
@@ -248,7 +248,7 @@ class Set[A]
|
|
248
248
|
# Merges the elements of the given enumerable object to the set and returns
|
249
249
|
# self.
|
250
250
|
#
|
251
|
-
def merge: (_Each[A
|
251
|
+
def merge: (_Each[A]) -> self
|
252
252
|
|
253
253
|
# Returns true if the set is a subset of the given set.
|
254
254
|
#
|
@@ -271,7 +271,7 @@ class Set[A]
|
|
271
271
|
# set.replace([1, 2]) #=> #<Set: {1, 2}>
|
272
272
|
# set #=> #<Set: {1, 2}>
|
273
273
|
#
|
274
|
-
def replace: (_Each[A
|
274
|
+
def replace: (_Each[A]) -> self
|
275
275
|
|
276
276
|
# Resets the internal state after modification to existing elements and returns
|
277
277
|
# self.
|
@@ -288,7 +288,7 @@ class Set[A]
|
|
288
288
|
# Deletes every element that appears in the given enumerable object and returns
|
289
289
|
# self.
|
290
290
|
#
|
291
|
-
def subtract: (_Each[A
|
291
|
+
def subtract: (_Each[A]) -> self
|
292
292
|
|
293
293
|
# Converts the set to an array. The order of elements is uncertain.
|
294
294
|
#
|
@@ -297,5 +297,5 @@ class Set[A]
|
|
297
297
|
#
|
298
298
|
def to_a: () -> Array[A]
|
299
299
|
|
300
|
-
include Enumerable[A
|
300
|
+
include Enumerable[A]
|
301
301
|
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# The Singleton module implements the Singleton pattern.
|
2
|
+
#
|
3
|
+
# ## Usage
|
4
|
+
#
|
5
|
+
# To use Singleton, include the module in your class.
|
6
|
+
#
|
7
|
+
# class Klass
|
8
|
+
# include Singleton
|
9
|
+
# # ...
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# This ensures that only one instance of Klass can be created.
|
13
|
+
#
|
14
|
+
# a,b = Klass.instance, Klass.instance
|
15
|
+
#
|
16
|
+
# a == b
|
17
|
+
# # => true
|
18
|
+
#
|
19
|
+
# Klass.new
|
20
|
+
# # => NoMethodError - new is private ...
|
21
|
+
#
|
22
|
+
# The instance is created at upon the first call of Klass.instance().
|
23
|
+
#
|
24
|
+
# class OtherKlass
|
25
|
+
# include Singleton
|
26
|
+
# # ...
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# ObjectSpace.each_object(OtherKlass){}
|
30
|
+
# # => 0
|
31
|
+
#
|
32
|
+
# OtherKlass.instance
|
33
|
+
# ObjectSpace.each_object(OtherKlass){}
|
34
|
+
# # => 1
|
35
|
+
#
|
36
|
+
# This behavior is preserved under inheritance and cloning.
|
37
|
+
#
|
38
|
+
# ## Implementation
|
39
|
+
#
|
40
|
+
# This above is achieved by:
|
41
|
+
#
|
42
|
+
# * Making Klass.new and Klass.allocate private.
|
43
|
+
#
|
44
|
+
# * Overriding Klass.inherited(sub_klass) and Klass.clone() to ensure that the
|
45
|
+
# Singleton properties are kept when inherited and cloned.
|
46
|
+
#
|
47
|
+
# * Providing the Klass.instance() method that returns the same object each
|
48
|
+
# time it is called.
|
49
|
+
#
|
50
|
+
# * Overriding Klass._load(str) to call Klass.instance().
|
51
|
+
#
|
52
|
+
# * Overriding Klass#clone and Klass#dup to raise TypeErrors to prevent
|
53
|
+
# cloning or duping.
|
54
|
+
#
|
55
|
+
#
|
56
|
+
# ## Singleton and Marshal
|
57
|
+
#
|
58
|
+
# By default Singleton's #_dump(depth) returns the empty string. Marshalling by
|
59
|
+
# default will strip state information, e.g. instance variables from the
|
60
|
+
# instance. Classes using Singleton can provide custom _load(str) and
|
61
|
+
# _dump(depth) methods to retain some of the previous state of the instance.
|
62
|
+
#
|
63
|
+
# require 'singleton'
|
64
|
+
#
|
65
|
+
# class Example
|
66
|
+
# include Singleton
|
67
|
+
# attr_accessor :keep, :strip
|
68
|
+
# def _dump(depth)
|
69
|
+
# # this strips the @strip information from the instance
|
70
|
+
# Marshal.dump(@keep, depth)
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
# def self._load(str)
|
74
|
+
# instance.keep = Marshal.load(str)
|
75
|
+
# instance
|
76
|
+
# end
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
# a = Example.instance
|
80
|
+
# a.keep = "keep this"
|
81
|
+
# a.strip = "get rid of this"
|
82
|
+
#
|
83
|
+
# stored_state = Marshal.dump(a)
|
84
|
+
#
|
85
|
+
# a.keep = nil
|
86
|
+
# a.strip = nil
|
87
|
+
# b = Marshal.load(stored_state)
|
88
|
+
# p a == b # => true
|
89
|
+
# p a.keep # => "keep this"
|
90
|
+
# p a.strip # => nil
|
91
|
+
module Singleton
|
92
|
+
def self.__init__: (Class klass) -> Class
|
93
|
+
|
94
|
+
def self.instance: () -> instance
|
95
|
+
|
96
|
+
public
|
97
|
+
|
98
|
+
# By default, do not retain any state when marshalling.
|
99
|
+
#
|
100
|
+
def _dump: (?Integer depth) -> String
|
101
|
+
|
102
|
+
# Raises a TypeError to prevent cloning.
|
103
|
+
#
|
104
|
+
def clone: () -> bot
|
105
|
+
|
106
|
+
# Raises a TypeError to prevent duping.
|
107
|
+
#
|
108
|
+
def dup: () -> bot
|
109
|
+
end
|
110
|
+
|
111
|
+
Singleton::VERSION: String
|
@@ -1,13 +1,13 @@
|
|
1
1
|
class Dir
|
2
2
|
# Returns the operating system's temporary file path.
|
3
|
-
#
|
3
|
+
#
|
4
4
|
def self.tmpdir: () -> String
|
5
5
|
|
6
6
|
# Dir.mktmpdir creates a temporary directory.
|
7
|
-
#
|
7
|
+
#
|
8
8
|
# The directory is created with 0700 permission. Application should not change
|
9
9
|
# the permission to make the temporary directory accessible from other users.
|
10
|
-
#
|
10
|
+
#
|
11
11
|
# The prefix and suffix of the name of the directory is specified by the
|
12
12
|
# optional first argument, *prefix_suffix*.
|
13
13
|
# * If it is not specified or nil, "d" is used as the prefix and no suffix is
|
@@ -15,30 +15,30 @@ class Dir
|
|
15
15
|
# * If it is a string, it is used as the prefix and no suffix is used.
|
16
16
|
# * If it is an array, first element is used as the prefix and second element
|
17
17
|
# is used as a suffix.
|
18
|
-
#
|
19
|
-
#
|
18
|
+
#
|
19
|
+
#
|
20
20
|
# Dir.mktmpdir {|dir| dir is ".../d..." }
|
21
21
|
# Dir.mktmpdir("foo") {|dir| dir is ".../foo..." }
|
22
22
|
# Dir.mktmpdir(["foo", "bar"]) {|dir| dir is ".../foo...bar" }
|
23
|
-
#
|
23
|
+
#
|
24
24
|
# The directory is created under Dir.tmpdir or the optional second argument
|
25
25
|
# *tmpdir* if non-nil value is given.
|
26
|
-
#
|
26
|
+
#
|
27
27
|
# Dir.mktmpdir {|dir| dir is "#{Dir.tmpdir}/d..." }
|
28
28
|
# Dir.mktmpdir(nil, "/var/tmp") {|dir| dir is "/var/tmp/d..." }
|
29
|
-
#
|
29
|
+
#
|
30
30
|
# If a block is given, it is yielded with the path of the directory. The
|
31
31
|
# directory and its contents are removed using FileUtils.remove_entry before
|
32
32
|
# Dir.mktmpdir returns. The value of the block is returned.
|
33
|
-
#
|
33
|
+
#
|
34
34
|
# Dir.mktmpdir {|dir|
|
35
35
|
# # use the directory...
|
36
36
|
# open("#{dir}/foo", "w") { ... }
|
37
37
|
# }
|
38
|
-
#
|
38
|
+
#
|
39
39
|
# If a block is not given, The path of the directory is returned. In this case,
|
40
40
|
# Dir.mktmpdir doesn't remove the directory.
|
41
|
-
#
|
41
|
+
#
|
42
42
|
# dir = Dir.mktmpdir
|
43
43
|
# begin
|
44
44
|
# # use the directory...
|
@@ -47,7 +47,7 @@ class Dir
|
|
47
47
|
# # remove the directory.
|
48
48
|
# FileUtils.remove_entry dir
|
49
49
|
# end
|
50
|
-
#
|
50
|
+
#
|
51
51
|
def self.mktmpdir: (?(String | [ String, String ] | nil), ?String?, ?max_try: Integer?) -> String
|
52
52
|
| [X] (?(String | [ String, String ] | nil), ?String?, ?max_try: Integer?) { (String) -> X } -> X
|
53
53
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module TSort[Node]
|
2
|
+
interface _Sortable[Node]
|
3
|
+
# #tsort_each_node is used to iterate for all nodes over a graph.
|
4
|
+
#
|
5
|
+
def tsort_each_node: () { (Node) -> void } -> void
|
6
|
+
|
7
|
+
# #tsort_each_child is used to iterate for child nodes of node.
|
8
|
+
#
|
9
|
+
def tsort_each_child: (Node) { (Node) -> void } -> void
|
10
|
+
end
|
11
|
+
|
12
|
+
interface _EachNode[Node]
|
13
|
+
def call: () { (Node) -> void } -> void
|
14
|
+
end
|
15
|
+
|
16
|
+
interface _EachChild[Node]
|
17
|
+
def call: (Node) { (Node) -> void } -> void
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,363 @@
|
|
1
|
+
# TSort implements topological sorting using Tarjan's algorithm for strongly
|
2
|
+
# connected components.
|
3
|
+
#
|
4
|
+
# TSort is designed to be able to be used with any object which can be
|
5
|
+
# interpreted as a directed graph.
|
6
|
+
#
|
7
|
+
# TSort requires two methods to interpret an object as a graph, tsort_each_node
|
8
|
+
# and tsort_each_child.
|
9
|
+
#
|
10
|
+
# * tsort_each_node is used to iterate for all nodes over a graph.
|
11
|
+
# * tsort_each_child is used to iterate for child nodes of a given node.
|
12
|
+
#
|
13
|
+
#
|
14
|
+
# The equality of nodes are defined by eql? and hash since TSort uses Hash
|
15
|
+
# internally.
|
16
|
+
#
|
17
|
+
# ## A Simple Example
|
18
|
+
#
|
19
|
+
# The following example demonstrates how to mix the TSort module into an
|
20
|
+
# existing class (in this case, Hash). Here, we're treating each key in the hash
|
21
|
+
# as a node in the graph, and so we simply alias the required #tsort_each_node
|
22
|
+
# method to Hash's #each_key method. For each key in the hash, the associated
|
23
|
+
# value is an array of the node's child nodes. This choice in turn leads to our
|
24
|
+
# implementation of the required #tsort_each_child method, which fetches the
|
25
|
+
# array of child nodes and then iterates over that array using the user-supplied
|
26
|
+
# block.
|
27
|
+
#
|
28
|
+
# require 'tsort'
|
29
|
+
#
|
30
|
+
# class Hash
|
31
|
+
# include TSort
|
32
|
+
# alias tsort_each_node each_key
|
33
|
+
# def tsort_each_child(node, &block)
|
34
|
+
# fetch(node).each(&block)
|
35
|
+
# end
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# {1=>[2, 3], 2=>[3], 3=>[], 4=>[]}.tsort
|
39
|
+
# #=> [3, 2, 1, 4]
|
40
|
+
#
|
41
|
+
# {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}.strongly_connected_components
|
42
|
+
# #=> [[4], [2, 3], [1]]
|
43
|
+
#
|
44
|
+
# ## A More Realistic Example
|
45
|
+
#
|
46
|
+
# A very simple `make' like tool can be implemented as follows:
|
47
|
+
#
|
48
|
+
# require 'tsort'
|
49
|
+
#
|
50
|
+
# class Make
|
51
|
+
# def initialize
|
52
|
+
# @dep = {}
|
53
|
+
# @dep.default = []
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# def rule(outputs, inputs=[], &block)
|
57
|
+
# triple = [outputs, inputs, block]
|
58
|
+
# outputs.each {|f| @dep[f] = [triple]}
|
59
|
+
# @dep[triple] = inputs
|
60
|
+
# end
|
61
|
+
#
|
62
|
+
# def build(target)
|
63
|
+
# each_strongly_connected_component_from(target) {|ns|
|
64
|
+
# if ns.length != 1
|
65
|
+
# fs = ns.delete_if {|n| Array === n}
|
66
|
+
# raise TSort::Cyclic.new("cyclic dependencies: #{fs.join ', '}")
|
67
|
+
# end
|
68
|
+
# n = ns.first
|
69
|
+
# if Array === n
|
70
|
+
# outputs, inputs, block = n
|
71
|
+
# inputs_time = inputs.map {|f| File.mtime f}.max
|
72
|
+
# begin
|
73
|
+
# outputs_time = outputs.map {|f| File.mtime f}.min
|
74
|
+
# rescue Errno::ENOENT
|
75
|
+
# outputs_time = nil
|
76
|
+
# end
|
77
|
+
# if outputs_time == nil ||
|
78
|
+
# inputs_time != nil && outputs_time <= inputs_time
|
79
|
+
# sleep 1 if inputs_time != nil && inputs_time.to_i == Time.now.to_i
|
80
|
+
# block.call
|
81
|
+
# end
|
82
|
+
# end
|
83
|
+
# }
|
84
|
+
# end
|
85
|
+
#
|
86
|
+
# def tsort_each_child(node, &block)
|
87
|
+
# @dep[node].each(&block)
|
88
|
+
# end
|
89
|
+
# include TSort
|
90
|
+
# end
|
91
|
+
#
|
92
|
+
# def command(arg)
|
93
|
+
# print arg, "\n"
|
94
|
+
# system arg
|
95
|
+
# end
|
96
|
+
#
|
97
|
+
# m = Make.new
|
98
|
+
# m.rule(%w[t1]) { command 'date > t1' }
|
99
|
+
# m.rule(%w[t2]) { command 'date > t2' }
|
100
|
+
# m.rule(%w[t3]) { command 'date > t3' }
|
101
|
+
# m.rule(%w[t4], %w[t1 t3]) { command 'cat t1 t3 > t4' }
|
102
|
+
# m.rule(%w[t5], %w[t4 t2]) { command 'cat t4 t2 > t5' }
|
103
|
+
# m.build('t5')
|
104
|
+
#
|
105
|
+
# ## Bugs
|
106
|
+
#
|
107
|
+
# * 'tsort.rb' is wrong name because this library uses Tarjan's algorithm for
|
108
|
+
# strongly connected components. Although 'strongly_connected_components.rb'
|
109
|
+
# is correct but too long.
|
110
|
+
#
|
111
|
+
#
|
112
|
+
# ## References
|
113
|
+
#
|
114
|
+
# 1. Tarjan, "Depth First Search and Linear Graph Algorithms",
|
115
|
+
#
|
116
|
+
#
|
117
|
+
# *SIAM Journal on Computing*, Vol. 1, No. 2, pp. 146-160, June 1972.
|
118
|
+
module TSort[Node] : TSort::_Sortable[Node]
|
119
|
+
# The iterator version of the TSort.strongly_connected_components method.
|
120
|
+
#
|
121
|
+
# The graph is represented by *each_node* and *each_child*. *each_node* should
|
122
|
+
# have `call` method which yields for each node in the graph. *each_child*
|
123
|
+
# should have `call` method which takes a node argument and yields for each
|
124
|
+
# child node.
|
125
|
+
#
|
126
|
+
# g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]}
|
127
|
+
# each_node = lambda {|&b| g.each_key(&b) }
|
128
|
+
# each_child = lambda {|n, &b| g[n].each(&b) }
|
129
|
+
# TSort.each_strongly_connected_component(each_node, each_child) {|scc| p scc }
|
130
|
+
# #=> [4]
|
131
|
+
# # [2]
|
132
|
+
# # [3]
|
133
|
+
# # [1]
|
134
|
+
#
|
135
|
+
# g = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}
|
136
|
+
# each_node = lambda {|&b| g.each_key(&b) }
|
137
|
+
# each_child = lambda {|n, &b| g[n].each(&b) }
|
138
|
+
# TSort.each_strongly_connected_component(each_node, each_child) {|scc| p scc }
|
139
|
+
# #=> [4]
|
140
|
+
# # [2, 3]
|
141
|
+
# # [1]
|
142
|
+
#
|
143
|
+
def self.each_strongly_connected_component: [T] (_EachNode[T] each_node, _EachChild[T] each_child) { (Array[T]) -> void } -> void
|
144
|
+
| [T] (_EachNode[T] each_node, _EachChild[T] each_child) -> Enumerator[Array[T], void]
|
145
|
+
|
146
|
+
# Iterates over strongly connected components in a graph. The graph is
|
147
|
+
# represented by *node* and *each_child*.
|
148
|
+
#
|
149
|
+
# *node* is the first node. *each_child* should have `call` method which takes a
|
150
|
+
# node argument and yields for each child node.
|
151
|
+
#
|
152
|
+
# Return value is unspecified.
|
153
|
+
#
|
154
|
+
# #TSort.each_strongly_connected_component_from is a class method and it doesn't
|
155
|
+
# need a class to represent a graph which includes TSort.
|
156
|
+
#
|
157
|
+
# graph = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}
|
158
|
+
# each_child = lambda {|n, &b| graph[n].each(&b) }
|
159
|
+
# TSort.each_strongly_connected_component_from(1, each_child) {|scc|
|
160
|
+
# p scc
|
161
|
+
# }
|
162
|
+
# #=> [4]
|
163
|
+
# # [2, 3]
|
164
|
+
# # [1]
|
165
|
+
#
|
166
|
+
def self.each_strongly_connected_component_from: [T] (T node, _EachChild[T] each_child, ?untyped id_map, ?untyped stack) { (Array[T]) -> void } -> void
|
167
|
+
| [T] (T node, _EachChild[T] each_child, ?untyped id_map, ?untyped stack) -> Enumerator[Array[T], void]
|
168
|
+
|
169
|
+
# Returns strongly connected components as an array of arrays of nodes. The
|
170
|
+
# array is sorted from children to parents. Each elements of the array
|
171
|
+
# represents a strongly connected component.
|
172
|
+
#
|
173
|
+
# The graph is represented by *each_node* and *each_child*. *each_node* should
|
174
|
+
# have `call` method which yields for each node in the graph. *each_child*
|
175
|
+
# should have `call` method which takes a node argument and yields for each
|
176
|
+
# child node.
|
177
|
+
#
|
178
|
+
# g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]}
|
179
|
+
# each_node = lambda {|&b| g.each_key(&b) }
|
180
|
+
# each_child = lambda {|n, &b| g[n].each(&b) }
|
181
|
+
# p TSort.strongly_connected_components(each_node, each_child)
|
182
|
+
# #=> [[4], [2], [3], [1]]
|
183
|
+
#
|
184
|
+
# g = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}
|
185
|
+
# each_node = lambda {|&b| g.each_key(&b) }
|
186
|
+
# each_child = lambda {|n, &b| g[n].each(&b) }
|
187
|
+
# p TSort.strongly_connected_components(each_node, each_child)
|
188
|
+
# #=> [[4], [2, 3], [1]]
|
189
|
+
#
|
190
|
+
def self.strongly_connected_components: [T] (_EachNode[T] each_node, _EachChild[T] each_child) -> Array[Array[T]]
|
191
|
+
|
192
|
+
# Returns a topologically sorted array of nodes. The array is sorted from
|
193
|
+
# children to parents, i.e. the first element has no child and the last node has
|
194
|
+
# no parent.
|
195
|
+
#
|
196
|
+
# The graph is represented by *each_node* and *each_child*. *each_node* should
|
197
|
+
# have `call` method which yields for each node in the graph. *each_child*
|
198
|
+
# should have `call` method which takes a node argument and yields for each
|
199
|
+
# child node.
|
200
|
+
#
|
201
|
+
# If there is a cycle, TSort::Cyclic is raised.
|
202
|
+
#
|
203
|
+
# g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]}
|
204
|
+
# each_node = lambda {|&b| g.each_key(&b) }
|
205
|
+
# each_child = lambda {|n, &b| g[n].each(&b) }
|
206
|
+
# p TSort.tsort(each_node, each_child) #=> [4, 2, 3, 1]
|
207
|
+
#
|
208
|
+
# g = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}
|
209
|
+
# each_node = lambda {|&b| g.each_key(&b) }
|
210
|
+
# each_child = lambda {|n, &b| g[n].each(&b) }
|
211
|
+
# p TSort.tsort(each_node, each_child) # raises TSort::Cyclic
|
212
|
+
#
|
213
|
+
def self.tsort: [T] (_EachNode[T] each_node, _EachChild[T] each_child) -> Array[T]
|
214
|
+
|
215
|
+
# The iterator version of the TSort.tsort method.
|
216
|
+
#
|
217
|
+
# The graph is represented by *each_node* and *each_child*. *each_node* should
|
218
|
+
# have `call` method which yields for each node in the graph. *each_child*
|
219
|
+
# should have `call` method which takes a node argument and yields for each
|
220
|
+
# child node.
|
221
|
+
#
|
222
|
+
# g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]}
|
223
|
+
# each_node = lambda {|&b| g.each_key(&b) }
|
224
|
+
# each_child = lambda {|n, &b| g[n].each(&b) }
|
225
|
+
# TSort.tsort_each(each_node, each_child) {|n| p n }
|
226
|
+
# #=> 4
|
227
|
+
# # 2
|
228
|
+
# # 3
|
229
|
+
# # 1
|
230
|
+
#
|
231
|
+
def self.tsort_each: [T] (_EachNode[T] each_node, _EachChild[T] each_child) { (T) -> void } -> void
|
232
|
+
| [T] (_EachNode[T] each_node, _EachChild[T] each_child) -> Enumerator[T, void]
|
233
|
+
|
234
|
+
# The iterator version of the #strongly_connected_components method.
|
235
|
+
# *`obj*.each_strongly_connected_component` is similar to
|
236
|
+
# *`obj*.strongly_connected_components.each`, but modification of *obj* during
|
237
|
+
# the iteration may lead to unexpected results.
|
238
|
+
#
|
239
|
+
# #each_strongly_connected_component returns `nil`.
|
240
|
+
#
|
241
|
+
# class G
|
242
|
+
# include TSort
|
243
|
+
# def initialize(g)
|
244
|
+
# @g = g
|
245
|
+
# end
|
246
|
+
# def tsort_each_child(n, &b) @g[n].each(&b) end
|
247
|
+
# def tsort_each_node(&b) @g.each_key(&b) end
|
248
|
+
# end
|
249
|
+
#
|
250
|
+
# graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
|
251
|
+
# graph.each_strongly_connected_component {|scc| p scc }
|
252
|
+
# #=> [4]
|
253
|
+
# # [2]
|
254
|
+
# # [3]
|
255
|
+
# # [1]
|
256
|
+
#
|
257
|
+
# graph = G.new({1=>[2], 2=>[3, 4], 3=>[2], 4=>[]})
|
258
|
+
# graph.each_strongly_connected_component {|scc| p scc }
|
259
|
+
# #=> [4]
|
260
|
+
# # [2, 3]
|
261
|
+
# # [1]
|
262
|
+
#
|
263
|
+
def each_strongly_connected_component: () { (Array[Node]) -> void } -> void
|
264
|
+
| () -> Enumerator[Array[Node], void]
|
265
|
+
|
266
|
+
# Iterates over strongly connected component in the subgraph reachable from
|
267
|
+
# *node*.
|
268
|
+
#
|
269
|
+
# Return value is unspecified.
|
270
|
+
#
|
271
|
+
# #each_strongly_connected_component_from doesn't call #tsort_each_node.
|
272
|
+
#
|
273
|
+
# class G
|
274
|
+
# include TSort
|
275
|
+
# def initialize(g)
|
276
|
+
# @g = g
|
277
|
+
# end
|
278
|
+
# def tsort_each_child(n, &b) @g[n].each(&b) end
|
279
|
+
# def tsort_each_node(&b) @g.each_key(&b) end
|
280
|
+
# end
|
281
|
+
#
|
282
|
+
# graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
|
283
|
+
# graph.each_strongly_connected_component_from(2) {|scc| p scc }
|
284
|
+
# #=> [4]
|
285
|
+
# # [2]
|
286
|
+
#
|
287
|
+
# graph = G.new({1=>[2], 2=>[3, 4], 3=>[2], 4=>[]})
|
288
|
+
# graph.each_strongly_connected_component_from(2) {|scc| p scc }
|
289
|
+
# #=> [4]
|
290
|
+
# # [2, 3]
|
291
|
+
#
|
292
|
+
def each_strongly_connected_component_from: (Node, ?untyped id_map, ?untyped stack) { (Array[Node]) -> void } -> void
|
293
|
+
| (Node, ?untyped id_map, ?untyped stack) -> Enumerator[Array[Node], void]
|
294
|
+
|
295
|
+
# Returns strongly connected components as an array of arrays of nodes. The
|
296
|
+
# array is sorted from children to parents. Each elements of the array
|
297
|
+
# represents a strongly connected component.
|
298
|
+
#
|
299
|
+
# class G
|
300
|
+
# include TSort
|
301
|
+
# def initialize(g)
|
302
|
+
# @g = g
|
303
|
+
# end
|
304
|
+
# def tsort_each_child(n, &b) @g[n].each(&b) end
|
305
|
+
# def tsort_each_node(&b) @g.each_key(&b) end
|
306
|
+
# end
|
307
|
+
#
|
308
|
+
# graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
|
309
|
+
# p graph.strongly_connected_components #=> [[4], [2], [3], [1]]
|
310
|
+
#
|
311
|
+
# graph = G.new({1=>[2], 2=>[3, 4], 3=>[2], 4=>[]})
|
312
|
+
# p graph.strongly_connected_components #=> [[4], [2, 3], [1]]
|
313
|
+
#
|
314
|
+
def strongly_connected_components: () -> Array[Array[Node]]
|
315
|
+
|
316
|
+
# Returns a topologically sorted array of nodes. The array is sorted from
|
317
|
+
# children to parents, i.e. the first element has no child and the last node has
|
318
|
+
# no parent.
|
319
|
+
#
|
320
|
+
# If there is a cycle, TSort::Cyclic is raised.
|
321
|
+
#
|
322
|
+
# class G
|
323
|
+
# include TSort
|
324
|
+
# def initialize(g)
|
325
|
+
# @g = g
|
326
|
+
# end
|
327
|
+
# def tsort_each_child(n, &b) @g[n].each(&b) end
|
328
|
+
# def tsort_each_node(&b) @g.each_key(&b) end
|
329
|
+
# end
|
330
|
+
#
|
331
|
+
# graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
|
332
|
+
# p graph.tsort #=> [4, 2, 3, 1]
|
333
|
+
#
|
334
|
+
# graph = G.new({1=>[2], 2=>[3, 4], 3=>[2], 4=>[]})
|
335
|
+
# p graph.tsort # raises TSort::Cyclic
|
336
|
+
#
|
337
|
+
def tsort: () -> Array[Node]
|
338
|
+
|
339
|
+
# The iterator version of the #tsort method. *`obj*.tsort_each` is similar to
|
340
|
+
# *`obj*.tsort.each`, but modification of *obj* during the iteration may lead to
|
341
|
+
# unexpected results.
|
342
|
+
#
|
343
|
+
# #tsort_each returns `nil`. If there is a cycle, TSort::Cyclic is raised.
|
344
|
+
#
|
345
|
+
# class G
|
346
|
+
# include TSort
|
347
|
+
# def initialize(g)
|
348
|
+
# @g = g
|
349
|
+
# end
|
350
|
+
# def tsort_each_child(n, &b) @g[n].each(&b) end
|
351
|
+
# def tsort_each_node(&b) @g.each_key(&b) end
|
352
|
+
# end
|
353
|
+
#
|
354
|
+
# graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]})
|
355
|
+
# graph.tsort_each {|n| p n }
|
356
|
+
# #=> 4
|
357
|
+
# # 2
|
358
|
+
# # 3
|
359
|
+
# # 1
|
360
|
+
#
|
361
|
+
def tsort_each: () { (Node) -> void } -> void
|
362
|
+
| () -> Enumerator[Node, void]
|
363
|
+
end
|