laser 0.7.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +14 -0
- data/LICENSE +661 -0
- data/README.md +158 -0
- data/Rakefile +104 -0
- data/VERSION +1 -0
- data/bin/laser +7 -0
- data/design_docs/goals.md +57 -0
- data/design_docs/object_regex.md +426 -0
- data/design_docs/type_annotations.md +80 -0
- data/ext/laser/BasicBlock.cpp +572 -0
- data/ext/laser/BasicBlock.h +118 -0
- data/ext/laser/extconf.rb +3 -0
- data/features/laser.feature +25 -0
- data/features/step_definitions/laser_steps.rb +39 -0
- data/features/support/env.rb +14 -0
- data/features/support/testdata/1_input +1 -0
- data/features/support/testdata/1_output +1 -0
- data/features/support/testdata/2_input +4 -0
- data/features/support/testdata/2_output +4 -0
- data/features/support/testdata/3_input +8 -0
- data/features/support/testdata/3_output +11 -0
- data/features/support/testdata/4_input +5 -0
- data/features/support/testdata/4_output +5 -0
- data/features/support/testdata/5_input +13 -0
- data/laser.gemspec +382 -0
- data/lib/laser.rb +98 -0
- data/lib/laser/analysis/annotations.rb +95 -0
- data/lib/laser/analysis/annotations/annotation_config.yaml +3 -0
- data/lib/laser/analysis/annotations/comment_attachment_annotation.rb +66 -0
- data/lib/laser/analysis/annotations/node_pointers_annotation.rb +36 -0
- data/lib/laser/analysis/annotations/runtime_annotation.rb +55 -0
- data/lib/laser/analysis/argument_expansion.rb +132 -0
- data/lib/laser/analysis/arity.rb +34 -0
- data/lib/laser/analysis/bindings.rb +144 -0
- data/lib/laser/analysis/bootstrap/bootstrap.rb +298 -0
- data/lib/laser/analysis/bootstrap/laser_class.rb +106 -0
- data/lib/laser/analysis/bootstrap/laser_method.rb +255 -0
- data/lib/laser/analysis/bootstrap/laser_module.rb +403 -0
- data/lib/laser/analysis/bootstrap/laser_module_copy.rb +74 -0
- data/lib/laser/analysis/bootstrap/laser_object.rb +69 -0
- data/lib/laser/analysis/bootstrap/laser_proc.rb +150 -0
- data/lib/laser/analysis/bootstrap/laser_singleton_class.rb +44 -0
- data/lib/laser/analysis/comments.rb +35 -0
- data/lib/laser/analysis/control_flow.rb +28 -0
- data/lib/laser/analysis/control_flow/alias_analysis.rb +31 -0
- data/lib/laser/analysis/control_flow/basic_block.rb +105 -0
- data/lib/laser/analysis/control_flow/cfg_builder.rb +2505 -0
- data/lib/laser/analysis/control_flow/cfg_instruction.rb +190 -0
- data/lib/laser/analysis/control_flow/constant_propagation.rb +742 -0
- data/lib/laser/analysis/control_flow/control_flow_graph.rb +370 -0
- data/lib/laser/analysis/control_flow/lifetime_analysis.rb +91 -0
- data/lib/laser/analysis/control_flow/method_call_search.rb +26 -0
- data/lib/laser/analysis/control_flow/raise_properties.rb +25 -0
- data/lib/laser/analysis/control_flow/simulation.rb +385 -0
- data/lib/laser/analysis/control_flow/static_single_assignment.rb +185 -0
- data/lib/laser/analysis/control_flow/unreachability_analysis.rb +57 -0
- data/lib/laser/analysis/control_flow/unused_variables.rb +91 -0
- data/lib/laser/analysis/control_flow/yield_properties.rb +103 -0
- data/lib/laser/analysis/errors.rb +131 -0
- data/lib/laser/analysis/laser_utils.rb +18 -0
- data/lib/laser/analysis/lexical_analysis.rb +172 -0
- data/lib/laser/analysis/method_call.rb +68 -0
- data/lib/laser/analysis/protocol_registry.rb +30 -0
- data/lib/laser/analysis/scope.rb +118 -0
- data/lib/laser/analysis/sexp.rb +159 -0
- data/lib/laser/analysis/sexp_analysis.rb +40 -0
- data/lib/laser/analysis/sexp_extensions/constant_extraction.rb +115 -0
- data/lib/laser/analysis/sexp_extensions/source_location.rb +164 -0
- data/lib/laser/analysis/sexp_extensions/type_inference.rb +47 -0
- data/lib/laser/analysis/signature.rb +76 -0
- data/lib/laser/analysis/special_methods/send.rb +67 -0
- data/lib/laser/analysis/unused_methods.rb +21 -0
- data/lib/laser/analysis/visitor.rb +141 -0
- data/lib/laser/annotation_parser/annotations.treetop +126 -0
- data/lib/laser/annotation_parser/annotations_parser.rb +748 -0
- data/lib/laser/annotation_parser/class_annotations.treetop +82 -0
- data/lib/laser/annotation_parser/class_annotations_parser.rb +654 -0
- data/lib/laser/annotation_parser/overload.treetop +24 -0
- data/lib/laser/annotation_parser/overload_parser.rb +167 -0
- data/lib/laser/annotation_parser/parsers.rb +6 -0
- data/lib/laser/annotation_parser/structural.treetop +37 -0
- data/lib/laser/annotation_parser/structural_parser.rb +406 -0
- data/lib/laser/annotation_parser/useful_parsers.treetop +47 -0
- data/lib/laser/annotation_parser/useful_parsers_parser.rb +674 -0
- data/lib/laser/rake/task.rb +46 -0
- data/lib/laser/runner.rb +189 -0
- data/lib/laser/scanner.rb +169 -0
- data/lib/laser/standard_library/_thread.rb +110 -0
- data/lib/laser/standard_library/abbrev.rb +103 -0
- data/lib/laser/standard_library/array.rb +418 -0
- data/lib/laser/standard_library/base64.rb +91 -0
- data/lib/laser/standard_library/basic_object.rb +55 -0
- data/lib/laser/standard_library/benchmark.rb +556 -0
- data/lib/laser/standard_library/bignum.rb +185 -0
- data/lib/laser/standard_library/cgi.rb +275 -0
- data/lib/laser/standard_library/cgi/cookie.rb +147 -0
- data/lib/laser/standard_library/cgi/core.rb +791 -0
- data/lib/laser/standard_library/cgi/html.rb +1021 -0
- data/lib/laser/standard_library/cgi/session.rb +537 -0
- data/lib/laser/standard_library/cgi/session/pstore.rb +111 -0
- data/lib/laser/standard_library/cgi/util.rb +188 -0
- data/lib/laser/standard_library/class_definitions.rb +333 -0
- data/lib/laser/standard_library/comparable.rb +125 -0
- data/lib/laser/standard_library/complex.rb +162 -0
- data/lib/laser/standard_library/enumerable.rb +178 -0
- data/lib/laser/standard_library/exceptions.rb +135 -0
- data/lib/laser/standard_library/fixnum.rb +188 -0
- data/lib/laser/standard_library/float.rb +180 -0
- data/lib/laser/standard_library/hash.rb +237 -0
- data/lib/laser/standard_library/integer.rb +123 -0
- data/lib/laser/standard_library/laser_magic.rb +7 -0
- data/lib/laser/standard_library/nil_false_true.rb +113 -0
- data/lib/laser/standard_library/numbers.rb +192 -0
- data/lib/laser/standard_library/proc.rb +31 -0
- data/lib/laser/standard_library/set.rb +1348 -0
- data/lib/laser/standard_library/string.rb +666 -0
- data/lib/laser/standard_library/stringio.rb +2 -0
- data/lib/laser/standard_library/symbol.rb +125 -0
- data/lib/laser/standard_library/tsort.rb +242 -0
- data/lib/laser/support/acts_as_struct.rb +66 -0
- data/lib/laser/support/frequency.rb +55 -0
- data/lib/laser/support/inheritable_attributes.rb +145 -0
- data/lib/laser/support/module_extensions.rb +94 -0
- data/lib/laser/support/placeholder_object.rb +13 -0
- data/lib/laser/third_party/rgl/adjacency.rb +221 -0
- data/lib/laser/third_party/rgl/base.rb +228 -0
- data/lib/laser/third_party/rgl/bidirectional.rb +39 -0
- data/lib/laser/third_party/rgl/condensation.rb +47 -0
- data/lib/laser/third_party/rgl/connected_components.rb +138 -0
- data/lib/laser/third_party/rgl/control_flow.rb +170 -0
- data/lib/laser/third_party/rgl/depth_first_spanning_tree.rb +37 -0
- data/lib/laser/third_party/rgl/dominators.rb +124 -0
- data/lib/laser/third_party/rgl/dot.rb +93 -0
- data/lib/laser/third_party/rgl/graphxml.rb +51 -0
- data/lib/laser/third_party/rgl/implicit.rb +174 -0
- data/lib/laser/third_party/rgl/mutable.rb +117 -0
- data/lib/laser/third_party/rgl/rdot.rb +445 -0
- data/lib/laser/third_party/rgl/topsort.rb +72 -0
- data/lib/laser/third_party/rgl/transitivity.rb +180 -0
- data/lib/laser/third_party/rgl/traversal.rb +348 -0
- data/lib/laser/types/types.rb +433 -0
- data/lib/laser/version.rb +14 -0
- data/lib/laser/warning.rb +149 -0
- data/lib/laser/warning_sets/default.yml +13 -0
- data/lib/laser/warnings/assignment_in_condition.rb +20 -0
- data/lib/laser/warnings/comment_spacing.rb +31 -0
- data/lib/laser/warnings/extra_blank_lines.rb +30 -0
- data/lib/laser/warnings/extra_whitespace.rb +16 -0
- data/lib/laser/warnings/hash_symbol_18_warning.rb +63 -0
- data/lib/laser/warnings/hash_symbol_19_warning.rb +29 -0
- data/lib/laser/warnings/line_length.rb +115 -0
- data/lib/laser/warnings/misaligned_unindentation.rb +17 -0
- data/lib/laser/warnings/operator_spacing.rb +68 -0
- data/lib/laser/warnings/parens_on_declaration.rb +30 -0
- data/lib/laser/warnings/rescue_exception.rb +42 -0
- data/lib/laser/warnings/semicolon.rb +25 -0
- data/lib/laser/warnings/sexp_errors.rb +24 -0
- data/lib/laser/warnings/uncalled_method_warning.rb +7 -0
- data/lib/laser/warnings/useless_double_quotes.rb +38 -0
- data/spec/analysis_specs/annotations_spec.rb +47 -0
- data/spec/analysis_specs/annotations_specs/comment_attachment_spec.rb +68 -0
- data/spec/analysis_specs/annotations_specs/node_pointers_annotation_spec.rb +90 -0
- data/spec/analysis_specs/annotations_specs/runtime_annotation_spec.rb +135 -0
- data/spec/analysis_specs/annotations_specs/spec_helper.rb +33 -0
- data/spec/analysis_specs/argument_expansion_spec.rb +113 -0
- data/spec/analysis_specs/bindings_spec.rb +36 -0
- data/spec/analysis_specs/comment_spec.rb +93 -0
- data/spec/analysis_specs/control_flow_specs/cfg_instruction_spec.rb +111 -0
- data/spec/analysis_specs/control_flow_specs/constant_propagation_spec.rb +560 -0
- data/spec/analysis_specs/control_flow_specs/control_flow_graph_spec.rb +5 -0
- data/spec/analysis_specs/control_flow_specs/raise_properties_spec.rb +310 -0
- data/spec/analysis_specs/control_flow_specs/raise_type_inference_spec.rb +301 -0
- data/spec/analysis_specs/control_flow_specs/return_type_inference_spec.rb +431 -0
- data/spec/analysis_specs/control_flow_specs/simulation_spec.rb +158 -0
- data/spec/analysis_specs/control_flow_specs/spec_helper.rb +110 -0
- data/spec/analysis_specs/control_flow_specs/tuple_misuse_inference_spec.rb +125 -0
- data/spec/analysis_specs/control_flow_specs/unreachability_analysis_spec.rb +76 -0
- data/spec/analysis_specs/control_flow_specs/unused_variable_spec.rb +99 -0
- data/spec/analysis_specs/control_flow_specs/yield_properties_spec.rb +372 -0
- data/spec/analysis_specs/error_spec.rb +30 -0
- data/spec/analysis_specs/laser_class_spec.rb +322 -0
- data/spec/analysis_specs/lexical_analysis_spec.rb +184 -0
- data/spec/analysis_specs/protocol_registry_spec.rb +63 -0
- data/spec/analysis_specs/scope_annotation_spec.rb +1013 -0
- data/spec/analysis_specs/scope_spec.rb +126 -0
- data/spec/analysis_specs/sexp_analysis_spec.rb +30 -0
- data/spec/analysis_specs/sexp_extension_specs/constant_extraction_spec.rb +309 -0
- data/spec/analysis_specs/sexp_extension_specs/source_location_spec.rb +231 -0
- data/spec/analysis_specs/sexp_extension_specs/spec_helper.rb +1 -0
- data/spec/analysis_specs/sexp_extension_specs/type_inference_spec.rb +252 -0
- data/spec/analysis_specs/sexp_spec.rb +167 -0
- data/spec/analysis_specs/spec_helper.rb +27 -0
- data/spec/analysis_specs/unused_methods_spec.rb +65 -0
- data/spec/analysis_specs/visitor_spec.rb +64 -0
- data/spec/annotation_parser_specs/annotations_parser_spec.rb +89 -0
- data/spec/annotation_parser_specs/class_annotation_parser_spec.rb +120 -0
- data/spec/annotation_parser_specs/overload_parser_spec.rb +39 -0
- data/spec/annotation_parser_specs/parsers_spec.rb +14 -0
- data/spec/annotation_parser_specs/spec_helper.rb +1 -0
- data/spec/annotation_parser_specs/structural_parser_spec.rb +67 -0
- data/spec/laser_spec.rb +14 -0
- data/spec/rake_specs/spec_helper.rb +1 -0
- data/spec/rake_specs/task_spec.rb +67 -0
- data/spec/runner_spec.rb +207 -0
- data/spec/scanner_spec.rb +75 -0
- data/spec/spec_helper.rb +121 -0
- data/spec/standard_library/exceptions_spec.rb +19 -0
- data/spec/standard_library/globals_spec.rb +14 -0
- data/spec/standard_library/set_spec.rb +31 -0
- data/spec/standard_library/spec_helper.rb +1 -0
- data/spec/standard_library/standard_library_spec.rb +302 -0
- data/spec/support_specs/acts_as_struct_spec.rb +94 -0
- data/spec/support_specs/frequency_spec.rb +23 -0
- data/spec/support_specs/module_extensions_spec.rb +117 -0
- data/spec/support_specs/spec_helper.rb +1 -0
- data/spec/type_specs/spec_helper.rb +1 -0
- data/spec/type_specs/types_spec.rb +133 -0
- data/spec/warning_spec.rb +95 -0
- data/spec/warning_specs/assignment_in_condition_spec.rb +68 -0
- data/spec/warning_specs/comment_spacing_spec.rb +65 -0
- data/spec/warning_specs/extra_blank_lines_spec.rb +70 -0
- data/spec/warning_specs/extra_whitespace_spec.rb +33 -0
- data/spec/warning_specs/hash_symbol_18_warning_spec.rb +89 -0
- data/spec/warning_specs/hash_symbol_19_warning_spec.rb +63 -0
- data/spec/warning_specs/line_length_spec.rb +173 -0
- data/spec/warning_specs/misaligned_unindentation_spec.rb +35 -0
- data/spec/warning_specs/operator_spacing_spec.rb +104 -0
- data/spec/warning_specs/parens_on_declaration_spec.rb +57 -0
- data/spec/warning_specs/rescue_exception_spec.rb +105 -0
- data/spec/warning_specs/semicolon_spec.rb +58 -0
- data/spec/warning_specs/spec_helper.rb +1 -0
- data/spec/warning_specs/useless_double_quotes_spec.rb +74 -0
- data/status_reports/2010/12/2010-12-14.md +163 -0
- data/status_reports/2010/12/2010-12-23.md +298 -0
- data/status_reports/2010/12/2010-12-24.md +6 -0
- data/test/third_party_tests/rgl_tests/TestComponents.rb +65 -0
- data/test/third_party_tests/rgl_tests/TestCycles.rb +61 -0
- data/test/third_party_tests/rgl_tests/TestDirectedGraph.rb +125 -0
- data/test/third_party_tests/rgl_tests/TestDot.rb +18 -0
- data/test/third_party_tests/rgl_tests/TestEdge.rb +34 -0
- data/test/third_party_tests/rgl_tests/TestGraph.rb +71 -0
- data/test/third_party_tests/rgl_tests/TestGraphXML.rb +57 -0
- data/test/third_party_tests/rgl_tests/TestImplicit.rb +52 -0
- data/test/third_party_tests/rgl_tests/TestRdot.rb +863 -0
- data/test/third_party_tests/rgl_tests/TestTransitivity.rb +129 -0
- data/test/third_party_tests/rgl_tests/TestTraversal.rb +220 -0
- data/test/third_party_tests/rgl_tests/TestUnDirectedGraph.rb +102 -0
- data/test/third_party_tests/rgl_tests/examples/north/Graph.log +128 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.0.graphml +28 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.1.graphml +28 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.11.graphml +31 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.12.graphml +27 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.13.graphml +27 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.14.graphml +27 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.15.graphml +26 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.16.graphml +26 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.17.graphml +26 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.19.graphml +37 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.2.graphml +28 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.20.graphml +38 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.22.graphml +43 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.24.graphml +30 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.25.graphml +45 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.27.graphml +38 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.28.graphml +30 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.29.graphml +38 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.3.graphml +26 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.30.graphml +34 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.31.graphml +42 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.34.graphml +42 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.37.graphml +28 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.38.graphml +38 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.39.graphml +36 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.4.graphml +26 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.40.graphml +37 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.41.graphml +37 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.42.graphml +26 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.45.graphml +28 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.46.graphml +32 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.5.graphml +31 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.50.graphml +30 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.56.graphml +29 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.57.graphml +32 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.58.graphml +32 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.6.graphml +26 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.60.graphml +32 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.61.graphml +34 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.62.graphml +34 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.68.graphml +30 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.69.graphml +32 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.7.graphml +29 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.70.graphml +26 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.71.graphml +27 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.72.graphml +28 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.74.graphml +29 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.75.graphml +29 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.78.graphml +27 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.79.graphml +34 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.8.graphml +29 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.80.graphml +34 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.82.graphml +35 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.83.graphml +32 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.85.graphml +34 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.86.graphml +34 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.88.graphml +37 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.89.graphml +29 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.9.graphml +26 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.90.graphml +32 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.91.graphml +31 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.92.graphml +26 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.93.graphml +32 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.10.94.graphml +34 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.12.8.graphml +40 -0
- data/test/third_party_tests/rgl_tests/examples/north/g.14.9.graphml +36 -0
- data/test/third_party_tests/rgl_tests/test_helper.rb +7 -0
- data/test/third_party_tests/test_inheritable_attributes.rb +187 -0
- metadata +470 -0
@@ -0,0 +1,192 @@
|
|
1
|
+
class Numeric
|
2
|
+
include Comparable
|
3
|
+
# pure: true
|
4
|
+
# builtin: true
|
5
|
+
def %(other)
|
6
|
+
end
|
7
|
+
# pure: true
|
8
|
+
# builtin: true
|
9
|
+
def +@
|
10
|
+
end
|
11
|
+
# pure: true
|
12
|
+
# builtin: true
|
13
|
+
def -@
|
14
|
+
end
|
15
|
+
# pure: true
|
16
|
+
# builtin: true
|
17
|
+
def <=>(other)
|
18
|
+
end
|
19
|
+
# pure: true
|
20
|
+
# builtin: true
|
21
|
+
def abs
|
22
|
+
end
|
23
|
+
# pure: true
|
24
|
+
# builtin: true
|
25
|
+
def abs2
|
26
|
+
end
|
27
|
+
# pure: true
|
28
|
+
# builtin: true
|
29
|
+
def arg
|
30
|
+
end
|
31
|
+
# pure: true
|
32
|
+
# builtin: true
|
33
|
+
def angle
|
34
|
+
arg
|
35
|
+
end
|
36
|
+
# pure: true
|
37
|
+
# builtin: true
|
38
|
+
def ceil
|
39
|
+
end
|
40
|
+
# pure: true
|
41
|
+
# builtin: true
|
42
|
+
def coerce(num)
|
43
|
+
end
|
44
|
+
# pure: true
|
45
|
+
# builtin: true
|
46
|
+
def conj
|
47
|
+
end
|
48
|
+
# pure: true
|
49
|
+
# builtin: true
|
50
|
+
def conjugate
|
51
|
+
conj
|
52
|
+
end
|
53
|
+
# pure: true
|
54
|
+
# builtin: true
|
55
|
+
def denominator
|
56
|
+
end
|
57
|
+
# pure: true
|
58
|
+
# builtin: true
|
59
|
+
def div(numeric)
|
60
|
+
end
|
61
|
+
# pure: true
|
62
|
+
# builtin: true
|
63
|
+
def divmod(numeric)
|
64
|
+
end
|
65
|
+
# pure: true
|
66
|
+
# builtin: true
|
67
|
+
# returns: Boolean
|
68
|
+
def eql?(numeric)
|
69
|
+
end
|
70
|
+
# pure: true
|
71
|
+
# builtin: true
|
72
|
+
def fdiv(numeric)
|
73
|
+
end
|
74
|
+
# pure: true
|
75
|
+
# builtin: true
|
76
|
+
def floor
|
77
|
+
end
|
78
|
+
# pure: true
|
79
|
+
# builtin: true
|
80
|
+
def i
|
81
|
+
end
|
82
|
+
# pure: true
|
83
|
+
# builtin: true
|
84
|
+
def imag
|
85
|
+
end
|
86
|
+
# pure: true
|
87
|
+
# builtin: true
|
88
|
+
def imaginary
|
89
|
+
end
|
90
|
+
# pure: true
|
91
|
+
# builtin: true
|
92
|
+
# returns: Boolean
|
93
|
+
def integer?
|
94
|
+
end
|
95
|
+
# pure: true
|
96
|
+
# builtin: true
|
97
|
+
def magnitude
|
98
|
+
abs
|
99
|
+
end
|
100
|
+
# pure: true
|
101
|
+
# builtin: true
|
102
|
+
def modulo(numeric)
|
103
|
+
end
|
104
|
+
# pure: true
|
105
|
+
# builtin: true
|
106
|
+
# returns: Boolean
|
107
|
+
def nonzero?
|
108
|
+
end
|
109
|
+
# pure: true
|
110
|
+
# builtin: true
|
111
|
+
def numerator
|
112
|
+
end
|
113
|
+
# pure: true
|
114
|
+
# builtin: true
|
115
|
+
def phase
|
116
|
+
arg
|
117
|
+
end
|
118
|
+
# pure: true
|
119
|
+
# builtin: true
|
120
|
+
def polar
|
121
|
+
end
|
122
|
+
# pure: true
|
123
|
+
# builtin: true
|
124
|
+
def pretty_print
|
125
|
+
end
|
126
|
+
# pure: true
|
127
|
+
# builtin: true
|
128
|
+
def pretty_print_cycle
|
129
|
+
end
|
130
|
+
# pure: true
|
131
|
+
# builtin: true
|
132
|
+
def quo(numeric)
|
133
|
+
end
|
134
|
+
# pure: true
|
135
|
+
# builtin: true
|
136
|
+
def real
|
137
|
+
end
|
138
|
+
# pure: true
|
139
|
+
# builtin: true
|
140
|
+
# returns: Boolean
|
141
|
+
def real?
|
142
|
+
end
|
143
|
+
# pure: true
|
144
|
+
# builtin: true
|
145
|
+
def rect
|
146
|
+
end
|
147
|
+
# pure: true
|
148
|
+
# builtin: true
|
149
|
+
def rectangular
|
150
|
+
rect
|
151
|
+
end
|
152
|
+
# pure: true
|
153
|
+
# builtin: true
|
154
|
+
def remainder(numeric)
|
155
|
+
end
|
156
|
+
# pure: true
|
157
|
+
# builtin: true
|
158
|
+
def round(numdigits=0)
|
159
|
+
end
|
160
|
+
# pure: true
|
161
|
+
# builtin: true
|
162
|
+
# raise: always
|
163
|
+
def singleton_method_added(p1)
|
164
|
+
raise TypeError.new
|
165
|
+
end
|
166
|
+
# pure: true
|
167
|
+
# builtin: true
|
168
|
+
# yield_usage: optional
|
169
|
+
def step(limit, step=1)
|
170
|
+
end
|
171
|
+
# pure: true
|
172
|
+
# builtin: true
|
173
|
+
def to_c
|
174
|
+
end
|
175
|
+
# pure: true
|
176
|
+
# builtin: true
|
177
|
+
def to_int
|
178
|
+
end
|
179
|
+
# pure: true
|
180
|
+
# builtin: true
|
181
|
+
def truncate
|
182
|
+
end
|
183
|
+
# pure: true
|
184
|
+
# builtin: true
|
185
|
+
# returns: Boolean
|
186
|
+
def zero?
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
require 'integer'
|
191
|
+
require 'float'
|
192
|
+
require 'complex'
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class Proc
|
2
|
+
# pure: true
|
3
|
+
# builtin: true
|
4
|
+
# raises: never
|
5
|
+
# yield_usage: optional
|
6
|
+
def self.new(&blk)
|
7
|
+
end
|
8
|
+
|
9
|
+
# builtin: true
|
10
|
+
def call(*args)
|
11
|
+
end
|
12
|
+
alias yield call
|
13
|
+
alias [] call
|
14
|
+
|
15
|
+
def ===(other)
|
16
|
+
call(other)
|
17
|
+
end
|
18
|
+
|
19
|
+
# pure: true
|
20
|
+
# raises: never
|
21
|
+
def to_proc
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
# pure: true
|
26
|
+
# builtin: true
|
27
|
+
# raises: never
|
28
|
+
# returns: empty
|
29
|
+
def lexical_self=(val)
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,1348 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#--
|
3
|
+
# set.rb - defines the Set class
|
4
|
+
#++
|
5
|
+
# Copyright (c) 2002-2008 Akinori MUSHA <knu@iDaemons.org>
|
6
|
+
#
|
7
|
+
# Documentation by Akinori MUSHA and Gavin Sinclair.
|
8
|
+
#
|
9
|
+
# All rights reserved. You can redistribute and/or modify it under the same
|
10
|
+
# terms as Ruby.
|
11
|
+
#
|
12
|
+
# $Id: set.rb 29434 2010-10-10 09:45:36Z knu $
|
13
|
+
#
|
14
|
+
# == Overview
|
15
|
+
#
|
16
|
+
# This library provides the Set class, which deals with a collection
|
17
|
+
# of unordered values with no duplicates. It is a hybrid of Array's
|
18
|
+
# intuitive inter-operation facilities and Hash's fast lookup. If you
|
19
|
+
# need to keep values ordered, use the SortedSet class.
|
20
|
+
#
|
21
|
+
# The method +to_set+ is added to Enumerable for convenience.
|
22
|
+
#
|
23
|
+
# See the Set and SortedSet documentation for examples of usage.
|
24
|
+
|
25
|
+
|
26
|
+
#
|
27
|
+
# Set implements a collection of unordered values with no duplicates.
|
28
|
+
# This is a hybrid of Array's intuitive inter-operation facilities and
|
29
|
+
# Hash's fast lookup.
|
30
|
+
#
|
31
|
+
# The equality of each couple of elements is determined according to
|
32
|
+
# Object#eql? and Object#hash, since Set uses Hash as storage.
|
33
|
+
#
|
34
|
+
# Set is easy to use with Enumerable objects (implementing +each+).
|
35
|
+
# Most of the initializer methods and binary operators accept generic
|
36
|
+
# Enumerable objects besides sets and arrays. An Enumerable object
|
37
|
+
# can be converted to Set using the +to_set+ method.
|
38
|
+
#
|
39
|
+
# == Example
|
40
|
+
#
|
41
|
+
# require 'set'
|
42
|
+
# s1 = Set.new [1, 2] # -> #<Set: {1, 2}>
|
43
|
+
# s2 = [1, 2].to_set # -> #<Set: {1, 2}>
|
44
|
+
# s1 == s2 # -> true
|
45
|
+
# s1.add("foo") # -> #<Set: {1, 2, "foo"}>
|
46
|
+
# s1.merge([2, 6]) # -> #<Set: {6, 1, 2, "foo"}>
|
47
|
+
# s1.subset? s2 # -> false
|
48
|
+
# s2.subset? s1 # -> true
|
49
|
+
#
|
50
|
+
# == Contact
|
51
|
+
#
|
52
|
+
# - Akinori MUSHA <knu@iDaemons.org> (current maintainer)
|
53
|
+
#
|
54
|
+
class Set
|
55
|
+
include Enumerable
|
56
|
+
# Creates a new set containing the given objects.
|
57
|
+
def self.[](*ary)
|
58
|
+
new(ary)
|
59
|
+
end
|
60
|
+
|
61
|
+
# Creates a new set containing the elements of the given enumerable
|
62
|
+
# object.
|
63
|
+
#
|
64
|
+
# If a block is given, the elements of enum are preprocessed by the
|
65
|
+
# given block.
|
66
|
+
def initialize(enum = nil, &block) # :yields: o
|
67
|
+
@hash ||= Hash.new
|
68
|
+
|
69
|
+
enum.nil? and return
|
70
|
+
|
71
|
+
if block
|
72
|
+
do_with_enum(enum) { |o| add(block[o]) }
|
73
|
+
else
|
74
|
+
merge(enum)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def do_with_enum(enum, &block)
|
79
|
+
if enum.respond_to?(:each_entry)
|
80
|
+
enum.each_entry(&block)
|
81
|
+
elsif enum.respond_to?(:each)
|
82
|
+
enum.each(&block)
|
83
|
+
else
|
84
|
+
raise ArgumentError, "value must be enumerable"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
private :do_with_enum
|
88
|
+
|
89
|
+
# Copy internal hash.
|
90
|
+
def initialize_copy(orig)
|
91
|
+
@hash = orig.instance_eval{@hash}.dup
|
92
|
+
end
|
93
|
+
|
94
|
+
def freeze # :nodoc:
|
95
|
+
super
|
96
|
+
@hash.freeze
|
97
|
+
self
|
98
|
+
end
|
99
|
+
|
100
|
+
def taint # :nodoc:
|
101
|
+
super
|
102
|
+
@hash.taint
|
103
|
+
self
|
104
|
+
end
|
105
|
+
|
106
|
+
def untaint # :nodoc:
|
107
|
+
super
|
108
|
+
@hash.untaint
|
109
|
+
self
|
110
|
+
end
|
111
|
+
|
112
|
+
# Returns the number of elements.
|
113
|
+
def size
|
114
|
+
@hash.size
|
115
|
+
end
|
116
|
+
alias length size
|
117
|
+
|
118
|
+
# Returns true if the set contains no elements.
|
119
|
+
def empty?
|
120
|
+
@hash.empty?
|
121
|
+
end
|
122
|
+
|
123
|
+
# Removes all elements and returns self.
|
124
|
+
def clear
|
125
|
+
@hash.clear
|
126
|
+
self
|
127
|
+
end
|
128
|
+
|
129
|
+
# Replaces the contents of the set with the contents of the given
|
130
|
+
# enumerable object and returns self.
|
131
|
+
def replace(enum)
|
132
|
+
if enum.instance_of?(self.class)
|
133
|
+
@hash.replace(enum.instance_variable_get(:@hash))
|
134
|
+
else
|
135
|
+
clear
|
136
|
+
merge(enum)
|
137
|
+
end
|
138
|
+
|
139
|
+
self
|
140
|
+
end
|
141
|
+
|
142
|
+
# Converts the set to an array. The order of elements is uncertain.
|
143
|
+
def to_a
|
144
|
+
@hash.keys
|
145
|
+
end
|
146
|
+
|
147
|
+
def flatten_merge(set, seen = Set.new)
|
148
|
+
set.each { |e|
|
149
|
+
if e.is_a?(Set)
|
150
|
+
if seen.include?(e_id = e.object_id)
|
151
|
+
raise ArgumentError, "tried to flatten recursive Set"
|
152
|
+
end
|
153
|
+
|
154
|
+
seen.add(e_id)
|
155
|
+
flatten_merge(e, seen)
|
156
|
+
seen.delete(e_id)
|
157
|
+
else
|
158
|
+
add(e)
|
159
|
+
end
|
160
|
+
}
|
161
|
+
|
162
|
+
self
|
163
|
+
end
|
164
|
+
protected :flatten_merge
|
165
|
+
|
166
|
+
# Returns a new set that is a copy of the set, flattening each
|
167
|
+
# containing set recursively.
|
168
|
+
def flatten
|
169
|
+
self.class.new.flatten_merge(self)
|
170
|
+
end
|
171
|
+
|
172
|
+
# Equivalent to Set#flatten, but replaces the receiver with the
|
173
|
+
# result in place. Returns nil if no modifications were made.
|
174
|
+
def flatten!
|
175
|
+
if detect { |e| e.is_a?(Set) }
|
176
|
+
replace(flatten())
|
177
|
+
else
|
178
|
+
nil
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
# Returns true if the set contains the given object.
|
183
|
+
def include?(o)
|
184
|
+
@hash.include?(o)
|
185
|
+
end
|
186
|
+
alias member? include?
|
187
|
+
|
188
|
+
# Returns true if the set is a superset of the given set.
|
189
|
+
def superset?(set)
|
190
|
+
set.is_a?(Set) or raise ArgumentError, "value must be a set"
|
191
|
+
return false if size < set.size
|
192
|
+
set.all? { |o| include?(o) }
|
193
|
+
end
|
194
|
+
|
195
|
+
# Returns true if the set is a proper superset of the given set.
|
196
|
+
def proper_superset?(set)
|
197
|
+
set.is_a?(Set) or raise ArgumentError, "value must be a set"
|
198
|
+
return false if size <= set.size
|
199
|
+
set.all? { |o| include?(o) }
|
200
|
+
end
|
201
|
+
|
202
|
+
# Returns true if the set is a subset of the given set.
|
203
|
+
def subset?(set)
|
204
|
+
set.is_a?(Set) or raise ArgumentError, "value must be a set"
|
205
|
+
return false if set.size < size
|
206
|
+
all? { |o| set.include?(o) }
|
207
|
+
end
|
208
|
+
|
209
|
+
# Returns true if the set is a proper subset of the given set.
|
210
|
+
def proper_subset?(set)
|
211
|
+
set.is_a?(Set) or raise ArgumentError, "value must be a set"
|
212
|
+
return false if set.size <= size
|
213
|
+
all? { |o| set.include?(o) }
|
214
|
+
end
|
215
|
+
|
216
|
+
# Calls the given block once for each element in the set, passing
|
217
|
+
# the element as parameter. Returns an enumerator if no block is
|
218
|
+
# given.
|
219
|
+
def each
|
220
|
+
block_given? or return enum_for(__method__)
|
221
|
+
@hash.each_key { |o| yield(o) }
|
222
|
+
self
|
223
|
+
end
|
224
|
+
|
225
|
+
# Adds the given object to the set and returns self. Use +merge+ to
|
226
|
+
# add many elements at once.
|
227
|
+
def add(o)
|
228
|
+
@hash[o] = true
|
229
|
+
self
|
230
|
+
end
|
231
|
+
alias << add
|
232
|
+
|
233
|
+
# Adds the given object to the set and returns self. If the
|
234
|
+
# object is already in the set, returns nil.
|
235
|
+
def add?(o)
|
236
|
+
if include?(o)
|
237
|
+
nil
|
238
|
+
else
|
239
|
+
add(o)
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
# Deletes the given object from the set and returns self. Use +subtract+ to
|
244
|
+
# delete many items at once.
|
245
|
+
def delete(o)
|
246
|
+
@hash.delete(o)
|
247
|
+
self
|
248
|
+
end
|
249
|
+
|
250
|
+
# Deletes the given object from the set and returns self. If the
|
251
|
+
# object is not in the set, returns nil.
|
252
|
+
def delete?(o)
|
253
|
+
if include?(o)
|
254
|
+
delete(o)
|
255
|
+
else
|
256
|
+
nil
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
# Deletes every element of the set for which block evaluates to
|
261
|
+
# true, and returns self.
|
262
|
+
def delete_if
|
263
|
+
block_given? or return enum_for(__method__)
|
264
|
+
to_a.each { |o| @hash.delete(o) if yield(o) }
|
265
|
+
self
|
266
|
+
end
|
267
|
+
|
268
|
+
# Deletes every element of the set for which block evaluates to
|
269
|
+
# false, and returns self.
|
270
|
+
def keep_if
|
271
|
+
block_given? or return enum_for(__method__)
|
272
|
+
to_a.each { |o| @hash.delete(o) unless yield(o) }
|
273
|
+
self
|
274
|
+
end
|
275
|
+
|
276
|
+
# Replaces the elements with ones returned by collect().
|
277
|
+
def collect!
|
278
|
+
block_given? or return enum_for(__method__)
|
279
|
+
set = self.class.new
|
280
|
+
each { |o| set << yield(o) }
|
281
|
+
replace(set)
|
282
|
+
end
|
283
|
+
alias map! collect!
|
284
|
+
|
285
|
+
# Equivalent to Set#delete_if, but returns nil if no changes were
|
286
|
+
# made.
|
287
|
+
def reject!
|
288
|
+
block_given? or return enum_for(__method__)
|
289
|
+
n = size
|
290
|
+
delete_if { |o| yield(o) }
|
291
|
+
size == n ? nil : self
|
292
|
+
end
|
293
|
+
|
294
|
+
# Equivalent to Set#keep_if, but returns nil if no changes were
|
295
|
+
# made.
|
296
|
+
def select!
|
297
|
+
block_given? or return enum_for(__method__)
|
298
|
+
n = size
|
299
|
+
keep_if { |o| yield(o) }
|
300
|
+
size == n ? nil : self
|
301
|
+
end
|
302
|
+
|
303
|
+
# Merges the elements of the given enumerable object to the set and
|
304
|
+
# returns self.
|
305
|
+
def merge(enum)
|
306
|
+
if enum.instance_of?(self.class)
|
307
|
+
@hash.update(enum.instance_variable_get(:@hash))
|
308
|
+
else
|
309
|
+
do_with_enum(enum) { |o| add(o) }
|
310
|
+
end
|
311
|
+
|
312
|
+
self
|
313
|
+
end
|
314
|
+
|
315
|
+
# Deletes every element that appears in the given enumerable object
|
316
|
+
# and returns self.
|
317
|
+
def subtract(enum)
|
318
|
+
do_with_enum(enum) { |o| delete(o) }
|
319
|
+
self
|
320
|
+
end
|
321
|
+
|
322
|
+
# Returns a new set built by merging the set and the elements of the
|
323
|
+
# given enumerable object.
|
324
|
+
def |(enum)
|
325
|
+
dup.merge(enum)
|
326
|
+
end
|
327
|
+
alias + | ##
|
328
|
+
alias union | ##
|
329
|
+
|
330
|
+
# Returns a new set built by duplicating the set, removing every
|
331
|
+
# element that appears in the given enumerable object.
|
332
|
+
def -(enum)
|
333
|
+
dup.subtract(enum)
|
334
|
+
end
|
335
|
+
alias difference - ##
|
336
|
+
|
337
|
+
# Returns a new set containing elements common to the set and the
|
338
|
+
# given enumerable object.
|
339
|
+
def &(enum)
|
340
|
+
n = self.class.new
|
341
|
+
do_with_enum(enum) { |o| n.add(o) if include?(o) }
|
342
|
+
n
|
343
|
+
end
|
344
|
+
alias intersection & ##
|
345
|
+
|
346
|
+
# Returns a new set containing elements exclusive between the set
|
347
|
+
# and the given enumerable object. (set ^ enum) is equivalent to
|
348
|
+
# ((set | enum) - (set & enum)).
|
349
|
+
def ^(enum)
|
350
|
+
n = Set.new(enum)
|
351
|
+
each { |o| if n.include?(o) then n.delete(o) else n.add(o) end }
|
352
|
+
n
|
353
|
+
end
|
354
|
+
|
355
|
+
# Returns true if two sets are equal. The equality of each couple
|
356
|
+
# of elements is defined according to Object#eql?.
|
357
|
+
def ==(other)
|
358
|
+
if self.equal?(other)
|
359
|
+
true
|
360
|
+
elsif other.instance_of?(self.class)
|
361
|
+
@hash == other.instance_variable_get(:@hash)
|
362
|
+
elsif other.is_a?(Set) && self.size == other.size
|
363
|
+
other.all? { |o| @hash.include?(o) }
|
364
|
+
else
|
365
|
+
false
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
369
|
+
def hash # :nodoc:
|
370
|
+
@hash.hash
|
371
|
+
end
|
372
|
+
|
373
|
+
def eql?(o) # :nodoc:
|
374
|
+
return false unless o.is_a?(Set)
|
375
|
+
@hash.eql?(o.instance_eval{@hash})
|
376
|
+
end
|
377
|
+
|
378
|
+
# Classifies the set by the return value of the given block and
|
379
|
+
# returns a hash of {value => set of elements} pairs. The block is
|
380
|
+
# called once for each element of the set, passing the element as
|
381
|
+
# parameter.
|
382
|
+
#
|
383
|
+
# e.g.:
|
384
|
+
#
|
385
|
+
# require 'set'
|
386
|
+
# files = Set.new(Dir.glob("*.rb"))
|
387
|
+
# hash = files.classify { |f| File.mtime(f).year }
|
388
|
+
# p hash # => {2000=>#<Set: {"a.rb", "b.rb"}>,
|
389
|
+
# # 2001=>#<Set: {"c.rb", "d.rb", "e.rb"}>,
|
390
|
+
# # 2002=>#<Set: {"f.rb"}>}
|
391
|
+
def classify # :yields: o
|
392
|
+
block_given? or return enum_for(__method__)
|
393
|
+
|
394
|
+
h = {}
|
395
|
+
|
396
|
+
each { |i|
|
397
|
+
x = yield(i)
|
398
|
+
(h[x] ||= self.class.new).add(i)
|
399
|
+
}
|
400
|
+
|
401
|
+
h
|
402
|
+
end
|
403
|
+
|
404
|
+
# Divides the set into a set of subsets according to the commonality
|
405
|
+
# defined by the given block.
|
406
|
+
#
|
407
|
+
# If the arity of the block is 2, elements o1 and o2 are in common
|
408
|
+
# if block.call(o1, o2) is true. Otherwise, elements o1 and o2 are
|
409
|
+
# in common if block.call(o1) == block.call(o2).
|
410
|
+
#
|
411
|
+
# e.g.:
|
412
|
+
#
|
413
|
+
# require 'set'
|
414
|
+
# numbers = Set[1, 3, 4, 6, 9, 10, 11]
|
415
|
+
# set = numbers.divide { |i,j| (i - j).abs == 1 }
|
416
|
+
# p set # => #<Set: {#<Set: {1}>,
|
417
|
+
# # #<Set: {11, 9, 10}>,
|
418
|
+
# # #<Set: {3, 4}>,
|
419
|
+
# # #<Set: {6}>}>
|
420
|
+
def divide(&func)
|
421
|
+
func or return enum_for(__method__)
|
422
|
+
|
423
|
+
if func.arity == 2
|
424
|
+
require 'tsort'
|
425
|
+
|
426
|
+
class << dig = {} # :nodoc:
|
427
|
+
include TSort
|
428
|
+
|
429
|
+
alias tsort_each_node each_key
|
430
|
+
def tsort_each_child(node, &block)
|
431
|
+
fetch(node).each(&block)
|
432
|
+
end
|
433
|
+
end
|
434
|
+
|
435
|
+
each { |u|
|
436
|
+
dig[u] = a = []
|
437
|
+
each{ |v| func.call(u, v) and a << v }
|
438
|
+
}
|
439
|
+
|
440
|
+
set = Set.new()
|
441
|
+
dig.each_strongly_connected_component { |css|
|
442
|
+
set.add(self.class.new(css))
|
443
|
+
}
|
444
|
+
set
|
445
|
+
else
|
446
|
+
Set.new(classify(&func).values)
|
447
|
+
end
|
448
|
+
end
|
449
|
+
|
450
|
+
InspectKey = :__inspect_key__ # :nodoc:
|
451
|
+
|
452
|
+
# Returns a string containing a human-readable representation of the
|
453
|
+
# set. ("#<Set: {element1, element2, ...}>")
|
454
|
+
def inspect
|
455
|
+
ids = (Thread.current[InspectKey] ||= [])
|
456
|
+
|
457
|
+
if ids.include?(object_id)
|
458
|
+
return sprintf('#<%s: {...}>', self.class.name)
|
459
|
+
end
|
460
|
+
|
461
|
+
begin
|
462
|
+
ids << object_id
|
463
|
+
return sprintf('#<%s: {%s}>', self.class, to_a.inspect[1..-2])
|
464
|
+
ensure
|
465
|
+
ids.pop
|
466
|
+
end
|
467
|
+
end
|
468
|
+
|
469
|
+
def pretty_print(pp) # :nodoc:
|
470
|
+
pp.text sprintf('#<%s: {', self.class.name)
|
471
|
+
pp.nest(1) {
|
472
|
+
pp.seplist(self) { |o|
|
473
|
+
pp.pp o
|
474
|
+
}
|
475
|
+
}
|
476
|
+
pp.text "}>"
|
477
|
+
end
|
478
|
+
|
479
|
+
def pretty_print_cycle(pp) # :nodoc:
|
480
|
+
pp.text sprintf('#<%s: {%s}>', self.class.name, empty? ? '' : '...')
|
481
|
+
end
|
482
|
+
end
|
483
|
+
|
484
|
+
#
|
485
|
+
# SortedSet implements a Set that guarantees that it's element are
|
486
|
+
# yielded in sorted order (according to the return values of their
|
487
|
+
# #<=> methods) when iterating over them.
|
488
|
+
#
|
489
|
+
# All elements that are added to a SortedSet must respond to the <=>
|
490
|
+
# method for comparison.
|
491
|
+
#
|
492
|
+
# Also, all elements must be <em>mutually comparable</em>: <tt>el1 <=>
|
493
|
+
# el2</tt> must not return <tt>nil</tt> for any elements <tt>el1</tt>
|
494
|
+
# and <tt>el2</tt>, else an ArgumentError will be raised when
|
495
|
+
# iterating over the SortedSet.
|
496
|
+
#
|
497
|
+
# == Example
|
498
|
+
#
|
499
|
+
# require "set"
|
500
|
+
#
|
501
|
+
# set = SortedSet.new([2, 1, 5, 6, 4, 5, 3, 3, 3])
|
502
|
+
# ary = []
|
503
|
+
#
|
504
|
+
# set.each do |obj|
|
505
|
+
# ary << obj
|
506
|
+
# end
|
507
|
+
#
|
508
|
+
# p ary # => [1, 2, 3, 4, 5, 6]
|
509
|
+
#
|
510
|
+
# set2 = SortedSet.new([1, 2, "3"])
|
511
|
+
# set2.each { |obj| } # => raises ArgumentError: comparison of Fixnum with String failed
|
512
|
+
#
|
513
|
+
class SortedSet < Set
|
514
|
+
@@setup = false
|
515
|
+
|
516
|
+
class << self
|
517
|
+
def [](*ary) # :nodoc:
|
518
|
+
new(ary)
|
519
|
+
end
|
520
|
+
|
521
|
+
def setup # :nodoc:
|
522
|
+
@@setup and return
|
523
|
+
|
524
|
+
module_eval {
|
525
|
+
# a hack to shut up warning
|
526
|
+
alias old_init initialize
|
527
|
+
remove_method :old_init
|
528
|
+
}
|
529
|
+
begin
|
530
|
+
require 'rbtree'
|
531
|
+
|
532
|
+
module_eval %{
|
533
|
+
def initialize(*args, &block)
|
534
|
+
@hash = RBTree.new
|
535
|
+
super
|
536
|
+
end
|
537
|
+
|
538
|
+
def add(o)
|
539
|
+
o.respond_to?(:<=>) or raise ArgumentError, "value must respond to <=>"
|
540
|
+
super
|
541
|
+
end
|
542
|
+
alias << add
|
543
|
+
}
|
544
|
+
rescue LoadError
|
545
|
+
module_eval %{
|
546
|
+
def initialize(*args, &block)
|
547
|
+
@keys = nil
|
548
|
+
super
|
549
|
+
end
|
550
|
+
|
551
|
+
def clear
|
552
|
+
@keys = nil
|
553
|
+
super
|
554
|
+
end
|
555
|
+
|
556
|
+
def replace(enum)
|
557
|
+
@keys = nil
|
558
|
+
super
|
559
|
+
end
|
560
|
+
|
561
|
+
def add(o)
|
562
|
+
o.respond_to?(:<=>) or raise ArgumentError, "value must respond to <=>"
|
563
|
+
@keys = nil
|
564
|
+
super
|
565
|
+
end
|
566
|
+
alias << add
|
567
|
+
|
568
|
+
def delete(o)
|
569
|
+
@keys = nil
|
570
|
+
@hash.delete(o)
|
571
|
+
self
|
572
|
+
end
|
573
|
+
|
574
|
+
def delete_if
|
575
|
+
block_given? or return enum_for(__method__)
|
576
|
+
n = @hash.size
|
577
|
+
super
|
578
|
+
@keys = nil if @hash.size != n
|
579
|
+
self
|
580
|
+
end
|
581
|
+
|
582
|
+
def keep_if
|
583
|
+
block_given? or return enum_for(__method__)
|
584
|
+
n = @hash.size
|
585
|
+
super
|
586
|
+
@keys = nil if @hash.size != n
|
587
|
+
self
|
588
|
+
end
|
589
|
+
|
590
|
+
def merge(enum)
|
591
|
+
@keys = nil
|
592
|
+
super
|
593
|
+
end
|
594
|
+
|
595
|
+
def each
|
596
|
+
block_given? or return enum_for(__method__)
|
597
|
+
to_a.each { |o| yield(o) }
|
598
|
+
self
|
599
|
+
end
|
600
|
+
|
601
|
+
def to_a
|
602
|
+
(@keys = @hash.keys).sort! unless @keys
|
603
|
+
@keys
|
604
|
+
end
|
605
|
+
}
|
606
|
+
end
|
607
|
+
|
608
|
+
@@setup = true
|
609
|
+
end
|
610
|
+
end
|
611
|
+
|
612
|
+
def initialize(*args, &block) # :nodoc:
|
613
|
+
SortedSet.setup
|
614
|
+
initialize(*args, &block)
|
615
|
+
end
|
616
|
+
end
|
617
|
+
|
618
|
+
module Enumerable
|
619
|
+
# Makes a set from the enumerable object with given arguments.
|
620
|
+
# Needs to +require "set"+ to use this method.
|
621
|
+
def to_set(klass = Set, *args, &block)
|
622
|
+
klass.new(self, *args, &block)
|
623
|
+
end
|
624
|
+
end
|
625
|
+
|
626
|
+
# =begin
|
627
|
+
# == RestricedSet class
|
628
|
+
# RestricedSet implements a set with restrictions defined by a given
|
629
|
+
# block.
|
630
|
+
#
|
631
|
+
# === Super class
|
632
|
+
# Set
|
633
|
+
#
|
634
|
+
# === Class Methods
|
635
|
+
# --- RestricedSet::new(enum = nil) { |o| ... }
|
636
|
+
# --- RestricedSet::new(enum = nil) { |rset, o| ... }
|
637
|
+
# Creates a new restricted set containing the elements of the given
|
638
|
+
# enumerable object. Restrictions are defined by the given block.
|
639
|
+
#
|
640
|
+
# If the block's arity is 2, it is called with the RestrictedSet
|
641
|
+
# itself and an object to see if the object is allowed to be put in
|
642
|
+
# the set.
|
643
|
+
#
|
644
|
+
# Otherwise, the block is called with an object to see if the object
|
645
|
+
# is allowed to be put in the set.
|
646
|
+
#
|
647
|
+
# === Instance Methods
|
648
|
+
# --- restriction_proc
|
649
|
+
# Returns the restriction procedure of the set.
|
650
|
+
#
|
651
|
+
# =end
|
652
|
+
#
|
653
|
+
# class RestricedSet < Set
|
654
|
+
# def initialize(*args, &block)
|
655
|
+
# @proc = block or raise ArgumentError, "missing a block"
|
656
|
+
#
|
657
|
+
# if @proc.arity == 2
|
658
|
+
# instance_eval %{
|
659
|
+
# def add(o)
|
660
|
+
# @hash[o] = true if @proc.call(self, o)
|
661
|
+
# self
|
662
|
+
# end
|
663
|
+
# alias << add
|
664
|
+
#
|
665
|
+
# def add?(o)
|
666
|
+
# if include?(o) || !@proc.call(self, o)
|
667
|
+
# nil
|
668
|
+
# else
|
669
|
+
# @hash[o] = true
|
670
|
+
# self
|
671
|
+
# end
|
672
|
+
# end
|
673
|
+
#
|
674
|
+
# def replace(enum)
|
675
|
+
# enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
|
676
|
+
# clear
|
677
|
+
# enum.each_entry { |o| add(o) }
|
678
|
+
#
|
679
|
+
# self
|
680
|
+
# end
|
681
|
+
#
|
682
|
+
# def merge(enum)
|
683
|
+
# enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
|
684
|
+
# enum.each_entry { |o| add(o) }
|
685
|
+
#
|
686
|
+
# self
|
687
|
+
# end
|
688
|
+
# }
|
689
|
+
# else
|
690
|
+
# instance_eval %{
|
691
|
+
# def add(o)
|
692
|
+
# if @proc.call(o)
|
693
|
+
# @hash[o] = true
|
694
|
+
# end
|
695
|
+
# self
|
696
|
+
# end
|
697
|
+
# alias << add
|
698
|
+
#
|
699
|
+
# def add?(o)
|
700
|
+
# if include?(o) || !@proc.call(o)
|
701
|
+
# nil
|
702
|
+
# else
|
703
|
+
# @hash[o] = true
|
704
|
+
# self
|
705
|
+
# end
|
706
|
+
# end
|
707
|
+
# }
|
708
|
+
# end
|
709
|
+
#
|
710
|
+
# super(*args)
|
711
|
+
# end
|
712
|
+
#
|
713
|
+
# def restriction_proc
|
714
|
+
# @proc
|
715
|
+
# end
|
716
|
+
# end
|
717
|
+
|
718
|
+
if $0 == __FILE__
|
719
|
+
eval DATA.read, nil, $0, __LINE__+4
|
720
|
+
end
|
721
|
+
|
722
|
+
__END__
|
723
|
+
|
724
|
+
require 'test/unit'
|
725
|
+
|
726
|
+
class TC_Set < Test::Unit::TestCase
|
727
|
+
def test_aref
|
728
|
+
assert_nothing_raised {
|
729
|
+
Set[]
|
730
|
+
Set[nil]
|
731
|
+
Set[1,2,3]
|
732
|
+
}
|
733
|
+
|
734
|
+
assert_equal(0, Set[].size)
|
735
|
+
assert_equal(1, Set[nil].size)
|
736
|
+
assert_equal(1, Set[[]].size)
|
737
|
+
assert_equal(1, Set[[nil]].size)
|
738
|
+
|
739
|
+
set = Set[2,4,6,4]
|
740
|
+
assert_equal(Set.new([2,4,6]), set)
|
741
|
+
end
|
742
|
+
|
743
|
+
def test_s_new
|
744
|
+
assert_nothing_raised {
|
745
|
+
Set.new()
|
746
|
+
Set.new(nil)
|
747
|
+
Set.new([])
|
748
|
+
Set.new([1,2])
|
749
|
+
Set.new('a'..'c')
|
750
|
+
}
|
751
|
+
assert_raises(ArgumentError) {
|
752
|
+
Set.new(false)
|
753
|
+
}
|
754
|
+
assert_raises(ArgumentError) {
|
755
|
+
Set.new(1)
|
756
|
+
}
|
757
|
+
assert_raises(ArgumentError) {
|
758
|
+
Set.new(1,2)
|
759
|
+
}
|
760
|
+
|
761
|
+
assert_equal(0, Set.new().size)
|
762
|
+
assert_equal(0, Set.new(nil).size)
|
763
|
+
assert_equal(0, Set.new([]).size)
|
764
|
+
assert_equal(1, Set.new([nil]).size)
|
765
|
+
|
766
|
+
ary = [2,4,6,4]
|
767
|
+
set = Set.new(ary)
|
768
|
+
ary.clear
|
769
|
+
assert_equal(false, set.empty?)
|
770
|
+
assert_equal(3, set.size)
|
771
|
+
|
772
|
+
ary = [1,2,3]
|
773
|
+
|
774
|
+
s = Set.new(ary) { |o| o * 2 }
|
775
|
+
assert_equal([2,4,6], s.sort)
|
776
|
+
end
|
777
|
+
|
778
|
+
def test_clone
|
779
|
+
set1 = Set.new
|
780
|
+
set2 = set1.clone
|
781
|
+
set1 << 'abc'
|
782
|
+
assert_equal(Set.new, set2)
|
783
|
+
end
|
784
|
+
|
785
|
+
def test_dup
|
786
|
+
set1 = Set[1,2]
|
787
|
+
set2 = set1.dup
|
788
|
+
|
789
|
+
assert_not_same(set1, set2)
|
790
|
+
|
791
|
+
assert_equal(set1, set2)
|
792
|
+
|
793
|
+
set1.add(3)
|
794
|
+
|
795
|
+
assert_not_equal(set1, set2)
|
796
|
+
end
|
797
|
+
|
798
|
+
def test_size
|
799
|
+
assert_equal(0, Set[].size)
|
800
|
+
assert_equal(2, Set[1,2].size)
|
801
|
+
assert_equal(2, Set[1,2,1].size)
|
802
|
+
end
|
803
|
+
|
804
|
+
def test_empty?
|
805
|
+
assert_equal(true, Set[].empty?)
|
806
|
+
assert_equal(false, Set[1, 2].empty?)
|
807
|
+
end
|
808
|
+
|
809
|
+
def test_clear
|
810
|
+
set = Set[1,2]
|
811
|
+
ret = set.clear
|
812
|
+
|
813
|
+
assert_same(set, ret)
|
814
|
+
assert_equal(true, set.empty?)
|
815
|
+
end
|
816
|
+
|
817
|
+
def test_replace
|
818
|
+
set = Set[1,2]
|
819
|
+
ret = set.replace('a'..'c')
|
820
|
+
|
821
|
+
assert_same(set, ret)
|
822
|
+
assert_equal(Set['a','b','c'], set)
|
823
|
+
end
|
824
|
+
|
825
|
+
def test_to_a
|
826
|
+
set = Set[1,2,3,2]
|
827
|
+
ary = set.to_a
|
828
|
+
|
829
|
+
assert_equal([1,2,3], ary.sort)
|
830
|
+
end
|
831
|
+
|
832
|
+
def test_flatten
|
833
|
+
# test1
|
834
|
+
set1 = Set[
|
835
|
+
1,
|
836
|
+
Set[
|
837
|
+
5,
|
838
|
+
Set[7,
|
839
|
+
Set[0]
|
840
|
+
],
|
841
|
+
Set[6,2],
|
842
|
+
1
|
843
|
+
],
|
844
|
+
3,
|
845
|
+
Set[3,4]
|
846
|
+
]
|
847
|
+
|
848
|
+
set2 = set1.flatten
|
849
|
+
set3 = Set.new(0..7)
|
850
|
+
|
851
|
+
assert_not_same(set2, set1)
|
852
|
+
assert_equal(set3, set2)
|
853
|
+
|
854
|
+
# test2; destructive
|
855
|
+
orig_set1 = set1
|
856
|
+
set1.flatten!
|
857
|
+
|
858
|
+
assert_same(orig_set1, set1)
|
859
|
+
assert_equal(set3, set1)
|
860
|
+
|
861
|
+
# test3; multiple occurrences of a set in an set
|
862
|
+
set1 = Set[1, 2]
|
863
|
+
set2 = Set[set1, Set[set1, 4], 3]
|
864
|
+
|
865
|
+
assert_nothing_raised {
|
866
|
+
set2.flatten!
|
867
|
+
}
|
868
|
+
|
869
|
+
assert_equal(Set.new(1..4), set2)
|
870
|
+
|
871
|
+
# test4; recursion
|
872
|
+
set2 = Set[]
|
873
|
+
set1 = Set[1, set2]
|
874
|
+
set2.add(set1)
|
875
|
+
|
876
|
+
assert_raises(ArgumentError) {
|
877
|
+
set1.flatten!
|
878
|
+
}
|
879
|
+
|
880
|
+
# test5; miscellaneous
|
881
|
+
empty = Set[]
|
882
|
+
set = Set[Set[empty, "a"],Set[empty, "b"]]
|
883
|
+
|
884
|
+
assert_nothing_raised {
|
885
|
+
set.flatten
|
886
|
+
}
|
887
|
+
|
888
|
+
set1 = empty.merge(Set["no_more", set])
|
889
|
+
|
890
|
+
assert_nil(Set.new(0..31).flatten!)
|
891
|
+
|
892
|
+
x = Set[Set[],Set[1,2]].flatten!
|
893
|
+
y = Set[1,2]
|
894
|
+
|
895
|
+
assert_equal(x, y)
|
896
|
+
end
|
897
|
+
|
898
|
+
def test_include?
|
899
|
+
set = Set[1,2,3]
|
900
|
+
|
901
|
+
assert_equal(true, set.include?(1))
|
902
|
+
assert_equal(true, set.include?(2))
|
903
|
+
assert_equal(true, set.include?(3))
|
904
|
+
assert_equal(false, set.include?(0))
|
905
|
+
assert_equal(false, set.include?(nil))
|
906
|
+
|
907
|
+
set = Set["1",nil,"2",nil,"0","1",false]
|
908
|
+
assert_equal(true, set.include?(nil))
|
909
|
+
assert_equal(true, set.include?(false))
|
910
|
+
assert_equal(true, set.include?("1"))
|
911
|
+
assert_equal(false, set.include?(0))
|
912
|
+
assert_equal(false, set.include?(true))
|
913
|
+
end
|
914
|
+
|
915
|
+
def test_superset?
|
916
|
+
set = Set[1,2,3]
|
917
|
+
|
918
|
+
assert_raises(ArgumentError) {
|
919
|
+
set.superset?()
|
920
|
+
}
|
921
|
+
|
922
|
+
assert_raises(ArgumentError) {
|
923
|
+
set.superset?(2)
|
924
|
+
}
|
925
|
+
|
926
|
+
assert_raises(ArgumentError) {
|
927
|
+
set.superset?([2])
|
928
|
+
}
|
929
|
+
|
930
|
+
assert_equal(true, set.superset?(Set[]))
|
931
|
+
assert_equal(true, set.superset?(Set[1,2]))
|
932
|
+
assert_equal(true, set.superset?(Set[1,2,3]))
|
933
|
+
assert_equal(false, set.superset?(Set[1,2,3,4]))
|
934
|
+
assert_equal(false, set.superset?(Set[1,4]))
|
935
|
+
|
936
|
+
assert_equal(true, Set[].superset?(Set[]))
|
937
|
+
end
|
938
|
+
|
939
|
+
def test_proper_superset?
|
940
|
+
set = Set[1,2,3]
|
941
|
+
|
942
|
+
assert_raises(ArgumentError) {
|
943
|
+
set.proper_superset?()
|
944
|
+
}
|
945
|
+
|
946
|
+
assert_raises(ArgumentError) {
|
947
|
+
set.proper_superset?(2)
|
948
|
+
}
|
949
|
+
|
950
|
+
assert_raises(ArgumentError) {
|
951
|
+
set.proper_superset?([2])
|
952
|
+
}
|
953
|
+
|
954
|
+
assert_equal(true, set.proper_superset?(Set[]))
|
955
|
+
assert_equal(true, set.proper_superset?(Set[1,2]))
|
956
|
+
assert_equal(false, set.proper_superset?(Set[1,2,3]))
|
957
|
+
assert_equal(false, set.proper_superset?(Set[1,2,3,4]))
|
958
|
+
assert_equal(false, set.proper_superset?(Set[1,4]))
|
959
|
+
|
960
|
+
assert_equal(false, Set[].proper_superset?(Set[]))
|
961
|
+
end
|
962
|
+
|
963
|
+
def test_subset?
|
964
|
+
set = Set[1,2,3]
|
965
|
+
|
966
|
+
assert_raises(ArgumentError) {
|
967
|
+
set.subset?()
|
968
|
+
}
|
969
|
+
|
970
|
+
assert_raises(ArgumentError) {
|
971
|
+
set.subset?(2)
|
972
|
+
}
|
973
|
+
|
974
|
+
assert_raises(ArgumentError) {
|
975
|
+
set.subset?([2])
|
976
|
+
}
|
977
|
+
|
978
|
+
assert_equal(true, set.subset?(Set[1,2,3,4]))
|
979
|
+
assert_equal(true, set.subset?(Set[1,2,3]))
|
980
|
+
assert_equal(false, set.subset?(Set[1,2]))
|
981
|
+
assert_equal(false, set.subset?(Set[]))
|
982
|
+
|
983
|
+
assert_equal(true, Set[].subset?(Set[1]))
|
984
|
+
assert_equal(true, Set[].subset?(Set[]))
|
985
|
+
end
|
986
|
+
|
987
|
+
def test_proper_subset?
|
988
|
+
set = Set[1,2,3]
|
989
|
+
|
990
|
+
assert_raises(ArgumentError) {
|
991
|
+
set.proper_subset?()
|
992
|
+
}
|
993
|
+
|
994
|
+
assert_raises(ArgumentError) {
|
995
|
+
set.proper_subset?(2)
|
996
|
+
}
|
997
|
+
|
998
|
+
assert_raises(ArgumentError) {
|
999
|
+
set.proper_subset?([2])
|
1000
|
+
}
|
1001
|
+
|
1002
|
+
assert_equal(true, set.proper_subset?(Set[1,2,3,4]))
|
1003
|
+
assert_equal(false, set.proper_subset?(Set[1,2,3]))
|
1004
|
+
assert_equal(false, set.proper_subset?(Set[1,2]))
|
1005
|
+
assert_equal(false, set.proper_subset?(Set[]))
|
1006
|
+
|
1007
|
+
assert_equal(false, Set[].proper_subset?(Set[]))
|
1008
|
+
end
|
1009
|
+
|
1010
|
+
def test_each
|
1011
|
+
ary = [1,3,5,7,10,20]
|
1012
|
+
set = Set.new(ary)
|
1013
|
+
|
1014
|
+
ret = set.each { |o| }
|
1015
|
+
assert_same(set, ret)
|
1016
|
+
|
1017
|
+
e = set.each
|
1018
|
+
assert_instance_of(Enumerator, e)
|
1019
|
+
|
1020
|
+
assert_nothing_raised {
|
1021
|
+
set.each { |o|
|
1022
|
+
ary.delete(o) or raise "unexpected element: #{o}"
|
1023
|
+
}
|
1024
|
+
|
1025
|
+
ary.empty? or raise "forgotten elements: #{ary.join(', ')}"
|
1026
|
+
}
|
1027
|
+
end
|
1028
|
+
|
1029
|
+
def test_add
|
1030
|
+
set = Set[1,2,3]
|
1031
|
+
|
1032
|
+
ret = set.add(2)
|
1033
|
+
assert_same(set, ret)
|
1034
|
+
assert_equal(Set[1,2,3], set)
|
1035
|
+
|
1036
|
+
ret = set.add?(2)
|
1037
|
+
assert_nil(ret)
|
1038
|
+
assert_equal(Set[1,2,3], set)
|
1039
|
+
|
1040
|
+
ret = set.add(4)
|
1041
|
+
assert_same(set, ret)
|
1042
|
+
assert_equal(Set[1,2,3,4], set)
|
1043
|
+
|
1044
|
+
ret = set.add?(5)
|
1045
|
+
assert_same(set, ret)
|
1046
|
+
assert_equal(Set[1,2,3,4,5], set)
|
1047
|
+
end
|
1048
|
+
|
1049
|
+
def test_delete
|
1050
|
+
set = Set[1,2,3]
|
1051
|
+
|
1052
|
+
ret = set.delete(4)
|
1053
|
+
assert_same(set, ret)
|
1054
|
+
assert_equal(Set[1,2,3], set)
|
1055
|
+
|
1056
|
+
ret = set.delete?(4)
|
1057
|
+
assert_nil(ret)
|
1058
|
+
assert_equal(Set[1,2,3], set)
|
1059
|
+
|
1060
|
+
ret = set.delete(2)
|
1061
|
+
assert_equal(set, ret)
|
1062
|
+
assert_equal(Set[1,3], set)
|
1063
|
+
|
1064
|
+
ret = set.delete?(1)
|
1065
|
+
assert_equal(set, ret)
|
1066
|
+
assert_equal(Set[3], set)
|
1067
|
+
end
|
1068
|
+
|
1069
|
+
def test_delete_if
|
1070
|
+
set = Set.new(1..10)
|
1071
|
+
ret = set.delete_if { |i| i > 10 }
|
1072
|
+
assert_same(set, ret)
|
1073
|
+
assert_equal(Set.new(1..10), set)
|
1074
|
+
|
1075
|
+
set = Set.new(1..10)
|
1076
|
+
ret = set.delete_if { |i| i % 3 == 0 }
|
1077
|
+
assert_same(set, ret)
|
1078
|
+
assert_equal(Set[1,2,4,5,7,8,10], set)
|
1079
|
+
end
|
1080
|
+
|
1081
|
+
def test_collect!
|
1082
|
+
set = Set[1,2,3,'a','b','c',-1..1,2..4]
|
1083
|
+
|
1084
|
+
ret = set.collect! { |i|
|
1085
|
+
case i
|
1086
|
+
when Numeric
|
1087
|
+
i * 2
|
1088
|
+
when String
|
1089
|
+
i.upcase
|
1090
|
+
else
|
1091
|
+
nil
|
1092
|
+
end
|
1093
|
+
}
|
1094
|
+
|
1095
|
+
assert_same(set, ret)
|
1096
|
+
assert_equal(Set[2,4,6,'A','B','C',nil], set)
|
1097
|
+
end
|
1098
|
+
|
1099
|
+
def test_reject!
|
1100
|
+
set = Set.new(1..10)
|
1101
|
+
|
1102
|
+
ret = set.reject! { |i| i > 10 }
|
1103
|
+
assert_nil(ret)
|
1104
|
+
assert_equal(Set.new(1..10), set)
|
1105
|
+
|
1106
|
+
ret = set.reject! { |i| i % 3 == 0 }
|
1107
|
+
assert_same(set, ret)
|
1108
|
+
assert_equal(Set[1,2,4,5,7,8,10], set)
|
1109
|
+
end
|
1110
|
+
|
1111
|
+
def test_merge
|
1112
|
+
set = Set[1,2,3]
|
1113
|
+
|
1114
|
+
ret = set.merge([2,4,6])
|
1115
|
+
assert_same(set, ret)
|
1116
|
+
assert_equal(Set[1,2,3,4,6], set)
|
1117
|
+
end
|
1118
|
+
|
1119
|
+
def test_subtract
|
1120
|
+
set = Set[1,2,3]
|
1121
|
+
|
1122
|
+
ret = set.subtract([2,4,6])
|
1123
|
+
assert_same(set, ret)
|
1124
|
+
assert_equal(Set[1,3], set)
|
1125
|
+
end
|
1126
|
+
|
1127
|
+
def test_plus
|
1128
|
+
set = Set[1,2,3]
|
1129
|
+
|
1130
|
+
ret = set + [2,4,6]
|
1131
|
+
assert_not_same(set, ret)
|
1132
|
+
assert_equal(Set[1,2,3,4,6], ret)
|
1133
|
+
end
|
1134
|
+
|
1135
|
+
def test_minus
|
1136
|
+
set = Set[1,2,3]
|
1137
|
+
|
1138
|
+
ret = set - [2,4,6]
|
1139
|
+
assert_not_same(set, ret)
|
1140
|
+
assert_equal(Set[1,3], ret)
|
1141
|
+
end
|
1142
|
+
|
1143
|
+
def test_and
|
1144
|
+
set = Set[1,2,3,4]
|
1145
|
+
|
1146
|
+
ret = set & [2,4,6]
|
1147
|
+
assert_not_same(set, ret)
|
1148
|
+
assert_equal(Set[2,4], ret)
|
1149
|
+
end
|
1150
|
+
|
1151
|
+
def test_xor
|
1152
|
+
set = Set[1,2,3,4]
|
1153
|
+
ret = set ^ [2,4,5,5]
|
1154
|
+
assert_not_same(set, ret)
|
1155
|
+
assert_equal(Set[1,3,5], ret)
|
1156
|
+
end
|
1157
|
+
|
1158
|
+
def test_eq
|
1159
|
+
set1 = Set[2,3,1]
|
1160
|
+
set2 = Set[1,2,3]
|
1161
|
+
|
1162
|
+
assert_equal(set1, set1)
|
1163
|
+
assert_equal(set1, set2)
|
1164
|
+
assert_not_equal(Set[1], [1])
|
1165
|
+
|
1166
|
+
set1 = Class.new(Set)["a", "b"]
|
1167
|
+
set2 = Set["a", "b", set1]
|
1168
|
+
set1 = set1.add(set1.clone)
|
1169
|
+
|
1170
|
+
# assert_equal(set1, set2)
|
1171
|
+
# assert_equal(set2, set1)
|
1172
|
+
assert_equal(set2, set2.clone)
|
1173
|
+
assert_equal(set1.clone, set1)
|
1174
|
+
|
1175
|
+
assert_not_equal(Set[Exception.new,nil], Set[Exception.new,Exception.new], "[ruby-dev:26127]")
|
1176
|
+
end
|
1177
|
+
|
1178
|
+
# def test_hash
|
1179
|
+
# end
|
1180
|
+
|
1181
|
+
# def test_eql?
|
1182
|
+
# end
|
1183
|
+
|
1184
|
+
def test_classify
|
1185
|
+
set = Set.new(1..10)
|
1186
|
+
ret = set.classify { |i| i % 3 }
|
1187
|
+
|
1188
|
+
assert_equal(3, ret.size)
|
1189
|
+
assert_instance_of(Hash, ret)
|
1190
|
+
ret.each_value { |value| assert_instance_of(Set, value) }
|
1191
|
+
assert_equal(Set[3,6,9], ret[0])
|
1192
|
+
assert_equal(Set[1,4,7,10], ret[1])
|
1193
|
+
assert_equal(Set[2,5,8], ret[2])
|
1194
|
+
end
|
1195
|
+
|
1196
|
+
def test_divide
|
1197
|
+
set = Set.new(1..10)
|
1198
|
+
ret = set.divide { |i| i % 3 }
|
1199
|
+
|
1200
|
+
assert_equal(3, ret.size)
|
1201
|
+
n = 0
|
1202
|
+
ret.each { |s| n += s.size }
|
1203
|
+
assert_equal(set.size, n)
|
1204
|
+
assert_equal(set, ret.flatten)
|
1205
|
+
|
1206
|
+
set = Set[7,10,5,11,1,3,4,9,0]
|
1207
|
+
ret = set.divide { |a,b| (a - b).abs == 1 }
|
1208
|
+
|
1209
|
+
assert_equal(4, ret.size)
|
1210
|
+
n = 0
|
1211
|
+
ret.each { |s| n += s.size }
|
1212
|
+
assert_equal(set.size, n)
|
1213
|
+
assert_equal(set, ret.flatten)
|
1214
|
+
ret.each { |s|
|
1215
|
+
if s.include?(0)
|
1216
|
+
assert_equal(Set[0,1], s)
|
1217
|
+
elsif s.include?(3)
|
1218
|
+
assert_equal(Set[3,4,5], s)
|
1219
|
+
elsif s.include?(7)
|
1220
|
+
assert_equal(Set[7], s)
|
1221
|
+
elsif s.include?(9)
|
1222
|
+
assert_equal(Set[9,10,11], s)
|
1223
|
+
else
|
1224
|
+
raise "unexpected group: #{s.inspect}"
|
1225
|
+
end
|
1226
|
+
}
|
1227
|
+
end
|
1228
|
+
|
1229
|
+
def test_inspect
|
1230
|
+
set1 = Set[1]
|
1231
|
+
|
1232
|
+
assert_equal('#<Set: {1}>', set1.inspect)
|
1233
|
+
|
1234
|
+
set2 = Set[Set[0], 1, 2, set1]
|
1235
|
+
assert_equal(false, set2.inspect.include?('#<Set: {...}>'))
|
1236
|
+
|
1237
|
+
set1.add(set2)
|
1238
|
+
assert_equal(true, set1.inspect.include?('#<Set: {...}>'))
|
1239
|
+
end
|
1240
|
+
|
1241
|
+
# def test_pretty_print
|
1242
|
+
# end
|
1243
|
+
|
1244
|
+
# def test_pretty_print_cycle
|
1245
|
+
# end
|
1246
|
+
end
|
1247
|
+
|
1248
|
+
class TC_SortedSet < Test::Unit::TestCase
|
1249
|
+
def test_sortedset
|
1250
|
+
s = SortedSet[4,5,3,1,2]
|
1251
|
+
|
1252
|
+
assert_equal([1,2,3,4,5], s.to_a)
|
1253
|
+
|
1254
|
+
prev = nil
|
1255
|
+
s.each { |o| assert(prev < o) if prev; prev = o }
|
1256
|
+
assert_not_nil(prev)
|
1257
|
+
|
1258
|
+
s.map! { |o| -2 * o }
|
1259
|
+
|
1260
|
+
assert_equal([-10,-8,-6,-4,-2], s.to_a)
|
1261
|
+
|
1262
|
+
prev = nil
|
1263
|
+
ret = s.each { |o| assert(prev < o) if prev; prev = o }
|
1264
|
+
assert_not_nil(prev)
|
1265
|
+
assert_same(s, ret)
|
1266
|
+
|
1267
|
+
s = SortedSet.new([2,1,3]) { |o| o * -2 }
|
1268
|
+
assert_equal([-6,-4,-2], s.to_a)
|
1269
|
+
|
1270
|
+
s = SortedSet.new(['one', 'two', 'three', 'four'])
|
1271
|
+
a = []
|
1272
|
+
ret = s.delete_if { |o| a << o; o.start_with?('t') }
|
1273
|
+
assert_same(s, ret)
|
1274
|
+
assert_equal(['four', 'one'], s.to_a)
|
1275
|
+
assert_equal(['four', 'one', 'three', 'two'], a)
|
1276
|
+
|
1277
|
+
s = SortedSet.new(['one', 'two', 'three', 'four'])
|
1278
|
+
a = []
|
1279
|
+
ret = s.reject! { |o| a << o; o.start_with?('t') }
|
1280
|
+
assert_same(s, ret)
|
1281
|
+
assert_equal(['four', 'one'], s.to_a)
|
1282
|
+
assert_equal(['four', 'one', 'three', 'two'], a)
|
1283
|
+
|
1284
|
+
s = SortedSet.new(['one', 'two', 'three', 'four'])
|
1285
|
+
a = []
|
1286
|
+
ret = s.reject! { |o| a << o; false }
|
1287
|
+
assert_same(nil, ret)
|
1288
|
+
assert_equal(['four', 'one', 'three', 'two'], s.to_a)
|
1289
|
+
assert_equal(['four', 'one', 'three', 'two'], a)
|
1290
|
+
end
|
1291
|
+
end
|
1292
|
+
|
1293
|
+
class TC_Enumerable < Test::Unit::TestCase
|
1294
|
+
def test_to_set
|
1295
|
+
ary = [2,5,4,3,2,1,3]
|
1296
|
+
|
1297
|
+
set = ary.to_set
|
1298
|
+
assert_instance_of(Set, set)
|
1299
|
+
assert_equal([1,2,3,4,5], set.sort)
|
1300
|
+
|
1301
|
+
set = ary.to_set { |o| o * -2 }
|
1302
|
+
assert_instance_of(Set, set)
|
1303
|
+
assert_equal([-10,-8,-6,-4,-2], set.sort)
|
1304
|
+
|
1305
|
+
set = ary.to_set(SortedSet)
|
1306
|
+
assert_instance_of(SortedSet, set)
|
1307
|
+
assert_equal([1,2,3,4,5], set.to_a)
|
1308
|
+
|
1309
|
+
set = ary.to_set(SortedSet) { |o| o * -2 }
|
1310
|
+
assert_instance_of(SortedSet, set)
|
1311
|
+
assert_equal([-10,-8,-6,-4,-2], set.sort)
|
1312
|
+
end
|
1313
|
+
end
|
1314
|
+
|
1315
|
+
# class TC_RestricedSet < Test::Unit::TestCase
|
1316
|
+
# def test_s_new
|
1317
|
+
# assert_raises(ArgumentError) { RestricedSet.new }
|
1318
|
+
#
|
1319
|
+
# s = RestricedSet.new([-1,2,3]) { |o| o > 0 }
|
1320
|
+
# assert_equal([2,3], s.sort)
|
1321
|
+
# end
|
1322
|
+
#
|
1323
|
+
# def test_restriction_proc
|
1324
|
+
# s = RestricedSet.new([-1,2,3]) { |o| o > 0 }
|
1325
|
+
#
|
1326
|
+
# f = s.restriction_proc
|
1327
|
+
# assert_instance_of(Proc, f)
|
1328
|
+
# assert(f[1])
|
1329
|
+
# assert(!f[0])
|
1330
|
+
# end
|
1331
|
+
#
|
1332
|
+
# def test_replace
|
1333
|
+
# s = RestricedSet.new(-3..3) { |o| o > 0 }
|
1334
|
+
# assert_equal([1,2,3], s.sort)
|
1335
|
+
#
|
1336
|
+
# s.replace([-2,0,3,4,5])
|
1337
|
+
# assert_equal([3,4,5], s.sort)
|
1338
|
+
# end
|
1339
|
+
#
|
1340
|
+
# def test_merge
|
1341
|
+
# s = RestricedSet.new { |o| o > 0 }
|
1342
|
+
# s.merge(-5..5)
|
1343
|
+
# assert_equal([1,2,3,4,5], s.sort)
|
1344
|
+
#
|
1345
|
+
# s.merge([10,-10,-8,8])
|
1346
|
+
# assert_equal([1,2,3,4,5,8,10], s.sort)
|
1347
|
+
# end
|
1348
|
+
# end
|