pax_global_header00006660000000000000000000000064140117555700014517gustar00rootroot0000000000000052 comment=3be5eb2c1a33cb78603abbfe423d188d355a240a sorted_set-1.0.3/000077500000000000000000000000001401175557000136735ustar00rootroot00000000000000sorted_set-1.0.3/.github/000077500000000000000000000000001401175557000152335ustar00rootroot00000000000000sorted_set-1.0.3/.github/workflows/000077500000000000000000000000001401175557000172705ustar00rootroot00000000000000sorted_set-1.0.3/.github/workflows/test.yml000066400000000000000000000011251401175557000207710ustar00rootroot00000000000000name: test on: [push, pull_request] jobs: test: strategy: fail-fast: false matrix: ruby: [ 2.7, 3.0, head, jruby ] os: [ ubuntu ] name: >- ${{matrix.os}}-ruby-${{matrix.ruby}} runs-on: ${{matrix.os}}-latest continue-on-error: ${{matrix.ruby == 'head'}} steps: - name: Check out uses: actions/checkout@master - name: Set up ruby and bundle uses: ruby/setup-ruby@v1 with: ruby-version: ${{matrix.ruby}} bundler-cache: true - name: Run test run: bundle exec rake test sorted_set-1.0.3/.gitignore000066400000000000000000000001261401175557000156620ustar00rootroot00000000000000/.bundle/ /.yardoc /_yardoc/ /coverage/ /doc/ /pkg/ /spec/reports/ /tmp/ Gemfile.lock sorted_set-1.0.3/CHANGELOG.md000066400000000000000000000012751401175557000155110ustar00rootroot00000000000000# SortedSet Changelog ## 1.0.3 (2021-02-13) * Enhancements * Switch back to the original [rbtree](https://rubygems.org/gems/rbtree) gem, which added support for Ruby >=3.0. ## 1.0.2 (2020-12-24) * Enhancements * Switch to the [rbtree3](https://github.com/kyrylo/rbtree3) gem to support Ruby >=3.0. ## 1.0.1 (2020-12-22) * Enhancements * Be a no-op library for JRuby, as it has its own version of Set and SortedSet. ## 1.0.0 (2020-12-22) This is the first release of sorted_set as a gem. Here lists the changes since the version bundled with Ruby 2.7. * Breaking Changes * The pure-ruby fallback implementation has been removed. It now requires an RBTree library to install and run. sorted_set-1.0.3/Gemfile000066400000000000000000000002061401175557000151640ustar00rootroot00000000000000source "https://rubygems.org" # Specify your gem's dependencies in sorted_set.gemspec gemspec gem "rake", "~> 12.0" gem "test-unit" sorted_set-1.0.3/LICENSE.txt000066400000000000000000000024061401175557000155200ustar00rootroot00000000000000Copyright (c) 2002-2020 Akinori MUSHA All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. sorted_set-1.0.3/README.md000066400000000000000000000025761401175557000151640ustar00rootroot00000000000000# SortedSet SortedSet implements a Set whose elements are sorted in ascending order (according to the return values of their `<=>` methods) when iterating over them. Every element in SortedSet must be *mutually comparable* to every other: comparison with `<=>` must not return nil for any pair of elements. Otherwise ArgumentError will be raised. Currently this library does nothing for JRuby, as it has its own version of Set and SortedSet. ## Installation Add this line to your application's Gemfile: ```ruby gem 'sorted_set' ``` And then execute: $ bundle install Or install it yourself as: $ gem install sorted_set ## Usage ```ruby require "sorted_set" set = SortedSet.new([2, 1, 5, 6, 4, 5, 3, 3, 3]) ary = [] set.each do |obj| ary << obj end p ary # => [1, 2, 3, 4, 5, 6] set2 = SortedSet.new([1, 2, "3"]) set2.each { |obj| } # => raises ArgumentError: comparison of Fixnum with String failed ``` ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/knu/sorted_set. ## License The gem is available as open source under either the terms of the [2-Clause BSD License](https://opensource.org/licenses/BSD-2-Clause). sorted_set-1.0.3/Rakefile000066400000000000000000000003061401175557000153370ustar00rootroot00000000000000require "bundler/gem_tasks" require "rake/testtask" Rake::TestTask.new(:test) do |t| t.libs << "test" t.libs << "lib" t.test_files = FileList["test/**/test_*.rb"] end task :default => :test sorted_set-1.0.3/bin/000077500000000000000000000000001401175557000144435ustar00rootroot00000000000000sorted_set-1.0.3/bin/console000077500000000000000000000005311401175557000160320ustar00rootroot00000000000000#!/usr/bin/env ruby require "bundler/setup" require "sorted_set" # You can add fixtures and/or initialization code here to make experimenting # with your gem easier. You can also use a different console, if you like. # (If you use this, don't forget to add pry to your Gemfile!) # require "pry" # Pry.start require "irb" IRB.start(__FILE__) sorted_set-1.0.3/bin/setup000077500000000000000000000002031401175557000155240ustar00rootroot00000000000000#!/usr/bin/env bash set -euo pipefail IFS=$'\n\t' set -vx bundle install # Do any other automated setup that you need to do here sorted_set-1.0.3/lib/000077500000000000000000000000001401175557000144415ustar00rootroot00000000000000sorted_set-1.0.3/lib/sorted_set.rb000066400000000000000000000026261401175557000171470ustar00rootroot00000000000000# :markup: markdown require 'set' return if defined?(JRUBY_VERSION) require 'rbtree' Object.instance_exec do # Undefine SortedSet for two reasons. # # 1. We should wipe out an existing implementation if Ruby 2.x loads # this library after loading the standard set library which # defines SortedSet. # # 2. Ruby >=3.0 (set >=1.0.0) sets autoload on SortedSet, and we # shouldn't let the following reference to SortedSet fire it # because it would end up requiring this library, leading to # circular require. # remove_const :SortedSet if const_defined?(:SortedSet) end ## # SortedSet implements a Set whose elements are sorted in ascending # order (according to the return values of their `<=>` methods) when # iterating over them. # # Every element in SortedSet must be *mutually comparable* to every # other: comparison with `<=>` must not return nil for any pair of # elements. Otherwise ArgumentError will be raised. # # ## Example # # ```ruby # require "sorted_set" # # set = SortedSet.new([2, 1, 5, 6, 4, 5, 3, 3, 3]) # ary = [] # # set.each do |obj| # ary << obj # end # # p ary # => [1, 2, 3, 4, 5, 6] # # set2 = SortedSet.new([1, 2, "3"]) # set2.each { |obj| } # => raises ArgumentError: comparison of Fixnum with String failed # ``` class SortedSet < Set # Creates a SortedSet. See Set.new for details. def initialize(*args) @hash = RBTree.new super end end sorted_set-1.0.3/sorted_set.gemspec000066400000000000000000000025361401175557000174210ustar00rootroot00000000000000Gem::Specification.new do |spec| spec.name = "sorted_set" spec.version = "1.0.3" spec.authors = ["Akinori MUSHA"] spec.email = ["knu@idaemons.org"] spec.summary = %q{Implements a variant of Set whose elements are sorted in ascending order} spec.description = %q{Implements a variant of Set whose elements are sorted in ascending order} spec.homepage = "https://github.com/knu/sorted_set" spec.license = "BSD-2-Clause" spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0") spec.metadata["homepage_uri"] = spec.homepage spec.metadata["source_code_uri"] = spec.homepage spec.metadata["changelog_uri"] = "https://github.com/knu/sorted_set/blob/v#{spec.version}/CHANGELOG.md" # Specify which files should be added to the gem when it is released. # The `git ls-files -z` loads the files in the RubyGem that have been added into git. spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } end spec.bindir = "exe" spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] if defined?(JRUBY_VERSION) spec.platform = "java" else spec.add_runtime_dependency "set", "~> 1.0" spec.add_runtime_dependency "rbtree" end end sorted_set-1.0.3/test/000077500000000000000000000000001401175557000146525ustar00rootroot00000000000000sorted_set-1.0.3/test/test_sorted_set.rb000066400000000000000000000054501401175557000204150ustar00rootroot00000000000000require 'test/unit' require 'sorted_set' class TC_SortedSet < Test::Unit::TestCase def test_sortedset s = SortedSet[4,5,3,1,2] a = s.to_a assert_equal([1,2,3,4,5], a) a << -1 assert_equal([1,2,3,4,5], s.to_a) prev = nil s.each { |o| assert(prev < o) if prev; prev = o } assert_not_nil(prev) s.map! { |o| -2 * o } assert_equal([-10,-8,-6,-4,-2], s.to_a) prev = nil ret = s.each { |o| assert(prev < o) if prev; prev = o } assert_not_nil(prev) assert_same(s, ret) s = SortedSet.new([2,1,3]) { |o| o * -2 } assert_equal([-6,-4,-2], s.to_a) s = SortedSet.new(['one', 'two', 'three', 'four']) a = [] ret = s.delete_if { |o| a << o; o.start_with?('t') } assert_same(s, ret) assert_equal(['four', 'one'], s.to_a) assert_equal(['four', 'one', 'three', 'two'], a) s = SortedSet.new(['one', 'two', 'three', 'four']) a = [] ret = s.reject! { |o| a << o; o.start_with?('t') } assert_same(s, ret) assert_equal(['four', 'one'], s.to_a) assert_equal(['four', 'one', 'three', 'two'], a) s = SortedSet.new(['one', 'two', 'three', 'four']) a = [] ret = s.reject! { |o| a << o; false } assert_same(nil, ret) assert_equal(['four', 'one', 'three', 'two'], s.to_a) assert_equal(['four', 'one', 'three', 'two'], a) end def test_each ary = [1,3,5,7,10,20] set = SortedSet.new(ary) ret = set.each { |o| } assert_same(set, ret) e = set.each assert_instance_of(Enumerator, e) assert_nothing_raised { set.each { |o| ary.delete(o) or raise "unexpected element: #{o}" } ary.empty? or raise "forgotten elements: #{ary.join(', ')}" } assert_equal(6, e.size) set << 42 assert_equal(7, e.size) end def test_freeze orig = set = SortedSet[3,2,1] assert_equal false, set.frozen? set << 4 assert_same orig, set.freeze assert_equal true, set.frozen? assert_raise(FrozenError) { set << 5 } assert_equal 4, set.size # https://bugs.ruby-lang.org/issues/12091 assert_nothing_raised { assert_equal [1,2,3,4], set.to_a } end def test_freeze_dup set1 = SortedSet[1,2,3] set1.freeze set2 = set1.dup assert_not_predicate set2, :frozen? assert_nothing_raised { set2.add 4 } end def test_freeze_clone set1 = SortedSet[1,2,3] set1.freeze set2 = set1.clone assert_predicate set2, :frozen? assert_raise(FrozenError) { set2.add 5 } end def test_enumerable_to_set ary = [2,5,4,3,2,1,3] set = ary.to_set(SortedSet) assert_instance_of(SortedSet, set) assert_equal([1,2,3,4,5], set.to_a) set = ary.to_set(SortedSet) { |o| o * -2 } assert_instance_of(SortedSet, set) assert_equal([-10,-8,-6,-4,-2], set.to_a) end end