rbs 0.13.1 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +9 -5
  3. data/.gitignore +0 -1
  4. data/CHANGELOG.md +38 -2
  5. data/Gemfile +4 -0
  6. data/README.md +9 -3
  7. data/Rakefile +19 -1
  8. data/Steepfile +1 -1
  9. data/bin/annotate-with-rdoc +1 -1
  10. data/bin/setup +0 -2
  11. data/bin/test_runner.rb +15 -1
  12. data/{stdlib/builtin → core}/array.rbs +124 -120
  13. data/{stdlib/builtin → core}/basic_object.rbs +54 -54
  14. data/{stdlib/builtin → core}/binding.rbs +42 -42
  15. data/core/builtin.rbs +70 -0
  16. data/{stdlib/builtin → core}/class.rbs +33 -33
  17. data/{stdlib/builtin → core}/comparable.rbs +0 -0
  18. data/{stdlib/builtin → core}/complex.rbs +90 -90
  19. data/{stdlib/builtin → core}/constants.rbs +0 -0
  20. data/{stdlib/builtin → core}/data.rbs +0 -0
  21. data/{stdlib/builtin → core}/deprecated.rbs +0 -0
  22. data/{stdlib/builtin → core}/dir.rbs +1 -1
  23. data/{stdlib/builtin → core}/encoding.rbs +33 -33
  24. data/{stdlib/builtin → core}/enumerable.rbs +99 -92
  25. data/{stdlib/builtin → core}/enumerator.rbs +40 -40
  26. data/{stdlib/builtin → core}/errno.rbs +0 -0
  27. data/{stdlib/builtin → core}/errors.rbs +2 -2
  28. data/{stdlib/builtin → core}/exception.rbs +50 -50
  29. data/{stdlib/builtin → core}/false_class.rbs +6 -6
  30. data/{stdlib/builtin → core}/fiber.rbs +14 -14
  31. data/{stdlib/builtin → core}/fiber_error.rbs +1 -1
  32. data/{stdlib/builtin → core}/file.rbs +0 -0
  33. data/{stdlib/builtin → core}/file_test.rbs +0 -0
  34. data/{stdlib/builtin → core}/float.rbs +161 -161
  35. data/{stdlib/builtin → core}/gc.rbs +3 -3
  36. data/{stdlib/builtin → core}/hash.rbs +15 -15
  37. data/{stdlib/builtin → core}/integer.rbs +0 -0
  38. data/{stdlib/builtin → core}/io.rbs +89 -89
  39. data/{stdlib/builtin → core}/kernel.rbs +70 -154
  40. data/{stdlib/builtin → core}/marshal.rbs +0 -0
  41. data/{stdlib/builtin → core}/match_data.rbs +1 -1
  42. data/{stdlib/builtin → core}/math.rbs +0 -0
  43. data/{stdlib/builtin → core}/method.rbs +19 -19
  44. data/{stdlib/builtin → core}/module.rbs +13 -13
  45. data/{stdlib/builtin → core}/nil_class.rbs +20 -20
  46. data/{stdlib/builtin → core}/numeric.rbs +101 -101
  47. data/{stdlib/builtin → core}/object.rbs +173 -173
  48. data/core/object_space.rbs +98 -0
  49. data/{stdlib/builtin → core}/proc.rbs +91 -91
  50. data/{stdlib/builtin → core}/process.rbs +0 -0
  51. data/{stdlib/builtin → core}/random.rbs +1 -1
  52. data/{stdlib/builtin → core}/range.rbs +4 -6
  53. data/{stdlib/builtin → core}/rational.rbs +83 -83
  54. data/{stdlib/builtin → core}/rb_config.rbs +0 -0
  55. data/{stdlib/builtin → core}/regexp.rbs +0 -0
  56. data/{stdlib/builtin → core}/ruby_vm.rbs +0 -0
  57. data/{stdlib/builtin → core}/signal.rbs +7 -7
  58. data/{stdlib/builtin → core}/string.rbs +10 -10
  59. data/{stdlib/builtin → core}/string_io.rbs +8 -8
  60. data/{stdlib/builtin → core}/struct.rbs +2 -2
  61. data/{stdlib/builtin → core}/symbol.rbs +1 -1
  62. data/{stdlib/builtin → core}/thread.rbs +189 -189
  63. data/{stdlib/builtin → core}/thread_group.rbs +2 -2
  64. data/{stdlib/builtin → core}/time.rbs +0 -0
  65. data/{stdlib/builtin → core}/trace_point.rbs +0 -0
  66. data/{stdlib/builtin → core}/true_class.rbs +10 -10
  67. data/{stdlib/builtin → core}/unbound_method.rbs +0 -0
  68. data/{stdlib/builtin → core}/warning.rbs +1 -1
  69. data/docs/CONTRIBUTING.md +1 -0
  70. data/docs/repo.md +125 -0
  71. data/docs/syntax.md +50 -6
  72. data/goodcheck.yml +22 -5
  73. data/lib/rbs.rb +1 -0
  74. data/lib/rbs/ast/comment.rb +1 -1
  75. data/lib/rbs/cli.rb +105 -103
  76. data/lib/rbs/definition.rb +9 -4
  77. data/lib/rbs/definition_builder.rb +54 -22
  78. data/lib/rbs/environment.rb +1 -1
  79. data/lib/rbs/environment_loader.rb +79 -105
  80. data/lib/rbs/environment_walker.rb +58 -29
  81. data/lib/rbs/namespace.rb +1 -1
  82. data/lib/rbs/parser.rb +3153 -0
  83. data/lib/rbs/parser.y +25 -11
  84. data/lib/rbs/prototype/rb.rb +186 -25
  85. data/lib/rbs/prototype/runtime.rb +18 -7
  86. data/lib/rbs/repository.rb +121 -0
  87. data/lib/rbs/test/hook.rb +27 -15
  88. data/lib/rbs/test/setup.rb +5 -3
  89. data/lib/rbs/test/setup_helper.rb +4 -4
  90. data/lib/rbs/test/tester.rb +4 -1
  91. data/lib/rbs/test/type_check.rb +18 -7
  92. data/lib/rbs/type_name.rb +3 -2
  93. data/lib/rbs/variance_calculator.rb +1 -1
  94. data/lib/rbs/vendorer.rb +38 -16
  95. data/lib/rbs/version.rb +1 -1
  96. data/lib/rbs/writer.rb +1 -1
  97. data/sig/cli.rbs +58 -0
  98. data/sig/constant.rbs +2 -2
  99. data/sig/constant_table.rbs +11 -11
  100. data/sig/declarations.rbs +2 -2
  101. data/sig/definition.rbs +10 -5
  102. data/sig/definition_builder.rbs +4 -1
  103. data/sig/environment_loader.rbs +92 -46
  104. data/sig/members.rbs +2 -2
  105. data/sig/method_types.rbs +1 -1
  106. data/sig/namespace.rbs +4 -4
  107. data/sig/parser.rbs +25 -0
  108. data/sig/polyfill.rbs +42 -0
  109. data/sig/rbs.rbs +8 -0
  110. data/sig/repository.rbs +79 -0
  111. data/sig/substitution.rbs +3 -3
  112. data/sig/typename.rbs +1 -1
  113. data/sig/types.rbs +1 -1
  114. data/sig/vendorer.rbs +44 -0
  115. data/sig/writer.rbs +15 -15
  116. data/stdlib/abbrev/{abbrev.rbs → 0/abbrev.rbs} +0 -0
  117. data/stdlib/base64/{base64.rbs → 0/base64.rbs} +1 -1
  118. data/stdlib/benchmark/{benchmark.rbs → 0/benchmark.rbs} +2 -2
  119. data/stdlib/{bigdecimal/math → bigdecimal-math/0}/big_math.rbs +0 -0
  120. data/stdlib/bigdecimal/{big_decimal.rbs → 0/big_decimal.rbs} +0 -0
  121. data/stdlib/coverage/{coverage.rbs → 0/coverage.rbs} +2 -2
  122. data/stdlib/csv/{csv.rbs → 0/csv.rbs} +4 -4
  123. data/stdlib/date/{date.rbs → 0/date.rbs} +4 -4
  124. data/stdlib/date/{date_time.rbs → 0/date_time.rbs} +1 -1
  125. data/stdlib/dbm/0/dbm.rbs +277 -0
  126. data/stdlib/erb/{erb.rbs → 0/erb.rbs} +0 -0
  127. data/stdlib/fiber/{fiber.rbs → 0/fiber.rbs} +0 -0
  128. data/stdlib/find/{find.rbs → 0/find.rbs} +12 -12
  129. data/stdlib/forwardable/{forwardable.rbs → 0/forwardable.rbs} +0 -0
  130. data/stdlib/ipaddr/{ipaddr.rbs → 0/ipaddr.rbs} +0 -0
  131. data/stdlib/json/{json.rbs → 0/json.rbs} +0 -0
  132. data/stdlib/logger/{formatter.rbs → 0/formatter.rbs} +0 -0
  133. data/stdlib/logger/{log_device.rbs → 0/log_device.rbs} +1 -1
  134. data/stdlib/logger/{logger.rbs → 0/logger.rbs} +1 -1
  135. data/stdlib/logger/{period.rbs → 0/period.rbs} +0 -0
  136. data/stdlib/logger/{severity.rbs → 0/severity.rbs} +0 -0
  137. data/stdlib/mutex_m/{mutex_m.rbs → 0/mutex_m.rbs} +0 -0
  138. data/stdlib/pathname/{pathname.rbs → 0/pathname.rbs} +40 -40
  139. data/stdlib/prime/{integer-extension.rbs → 0/integer-extension.rbs} +0 -0
  140. data/stdlib/prime/{prime.rbs → 0/prime.rbs} +1 -1
  141. data/stdlib/pstore/0/pstore.rbs +287 -0
  142. data/stdlib/pty/{pty.rbs → 0/pty.rbs} +1 -1
  143. data/stdlib/securerandom/{securerandom.rbs → 0/securerandom.rbs} +0 -0
  144. data/stdlib/set/{set.rbs → 0/set.rbs} +10 -10
  145. data/stdlib/singleton/0/singleton.rbs +111 -0
  146. data/stdlib/tmpdir/{tmpdir.rbs → 0/tmpdir.rbs} +12 -12
  147. data/stdlib/tsort/0/cyclic.rbs +4 -0
  148. data/stdlib/tsort/0/interfaces.rbs +19 -0
  149. data/stdlib/tsort/0/tsort.rbs +363 -0
  150. data/stdlib/uri/{file.rbs → 0/file.rbs} +0 -0
  151. data/stdlib/uri/{generic.rbs → 0/generic.rbs} +1 -1
  152. data/stdlib/uri/{http.rbs → 0/http.rbs} +0 -0
  153. data/stdlib/uri/{https.rbs → 0/https.rbs} +0 -0
  154. data/stdlib/uri/{ldap.rbs → 0/ldap.rbs} +0 -0
  155. data/stdlib/uri/{ldaps.rbs → 0/ldaps.rbs} +0 -0
  156. data/stdlib/yaml/0/dbm.rbs +221 -0
  157. data/stdlib/yaml/0/store.rbs +53 -0
  158. data/stdlib/zlib/{zlib.rbs → 0/zlib.rbs} +0 -0
  159. data/steep/Gemfile.lock +9 -9
  160. metadata +109 -94
  161. 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, ?bool raise) -> Process::Status?
69
+ def self.check: (Integer pid, ?boolish raise) -> Process::Status?
70
70
 
71
71
  alias self.getpty self.spawn
72
72
 
@@ -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, untyped]) -> untyped
55
- | [X] (_Each[X, untyped]) { (X) -> A } -> untyped
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, untyped]) -> self
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, untyped]) -> self
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, untyped]) -> self
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, untyped]) -> self
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, untyped]) -> self
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, untyped]) -> self
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, untyped]) -> self
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, self]
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,4 @@
1
+ module TSort[Node]
2
+ class Cyclic < StandardError
3
+ end
4
+ 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