2003-09-19 10:53:02 +04:00
|
|
|
#--
|
2015-11-16 10:41:30 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
#
|
2003-09-19 10:53:02 +04:00
|
|
|
# set.rb - defines the Set class
|
|
|
|
#++
|
2016-11-05 12:23:14 +03:00
|
|
|
# Copyright (c) 2002-2016 Akinori MUSHA <knu@iDaemons.org>
|
2002-08-30 17:47:49 +04:00
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
# Documentation by Akinori MUSHA and Gavin Sinclair.
|
2002-08-30 17:47:49 +04:00
|
|
|
#
|
2003-09-19 10:53:02 +04:00
|
|
|
# All rights reserved. You can redistribute and/or modify it under the same
|
|
|
|
# terms as Ruby.
|
2002-08-30 17:47:49 +04:00
|
|
|
#
|
2003-09-19 10:53:02 +04:00
|
|
|
# $Id$
|
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
# == Overview
|
|
|
|
#
|
2003-10-17 17:16:03 +04:00
|
|
|
# This library provides the Set class, which deals with a collection
|
|
|
|
# of unordered values with no duplicates. It is a hybrid of Array's
|
|
|
|
# intuitive inter-operation facilities and Hash's fast lookup. If you
|
2013-07-31 09:58:09 +04:00
|
|
|
# need to keep values sorted in some order, use the SortedSet class.
|
2002-12-24 08:29:04 +03:00
|
|
|
#
|
2003-10-17 17:16:03 +04:00
|
|
|
# The method +to_set+ is added to Enumerable for convenience.
|
2002-12-24 08:29:04 +03:00
|
|
|
#
|
2009-05-01 11:46:23 +04:00
|
|
|
# See the Set and SortedSet documentation for examples of usage.
|
2002-08-30 17:47:49 +04:00
|
|
|
|
2003-09-19 10:53:02 +04:00
|
|
|
|
|
|
|
#
|
2002-12-24 08:29:04 +03:00
|
|
|
# Set implements a collection of unordered values with no duplicates.
|
|
|
|
# This is a hybrid of Array's intuitive inter-operation facilities and
|
|
|
|
# Hash's fast lookup.
|
|
|
|
#
|
2007-03-20 05:09:10 +03:00
|
|
|
# Set is easy to use with Enumerable objects (implementing +each+).
|
|
|
|
# Most of the initializer methods and binary operators accept generic
|
|
|
|
# Enumerable objects besides sets and arrays. An Enumerable object
|
|
|
|
# can be converted to Set using the +to_set+ method.
|
2003-09-19 10:53:02 +04:00
|
|
|
#
|
2013-07-31 09:58:17 +04:00
|
|
|
# Set uses Hash as storage, so you must note the following points:
|
|
|
|
#
|
|
|
|
# * Equality of elements is determined according to Object#eql? and
|
2016-11-05 12:23:14 +03:00
|
|
|
# Object#hash. Use Set#compare_by_identity to make a set compare
|
|
|
|
# its elements by their identity.
|
2013-07-31 09:58:17 +04:00
|
|
|
# * Set assumes that the identity of each element does not change
|
|
|
|
# while it is stored. Modifying an element of a set will render the
|
|
|
|
# set to an unreliable state.
|
|
|
|
# * When a string is to be stored, a frozen copy of the string is
|
|
|
|
# stored instead unless the original string is already frozen.
|
|
|
|
#
|
2012-08-30 08:01:58 +04:00
|
|
|
# == Comparison
|
|
|
|
#
|
2017-11-22 23:58:24 +03:00
|
|
|
# The comparison operators <, >, <=, and >= are implemented as
|
2012-08-30 08:01:58 +04:00
|
|
|
# shorthand for the {proper_,}{subset?,superset?} methods. However,
|
|
|
|
# the <=> operator is intentionally left out because not every pair of
|
2017-11-22 23:58:24 +03:00
|
|
|
# sets is comparable ({x, y} vs. {x, z} for example).
|
2012-08-30 08:01:58 +04:00
|
|
|
#
|
2003-09-19 10:53:02 +04:00
|
|
|
# == Example
|
|
|
|
#
|
|
|
|
# require 'set'
|
2017-11-22 23:58:24 +03:00
|
|
|
# s1 = Set[1, 2] #=> #<Set: {1, 2}>
|
2017-11-17 12:48:47 +03:00
|
|
|
# s2 = [1, 2].to_set #=> #<Set: {1, 2}>
|
|
|
|
# s1 == s2 #=> true
|
|
|
|
# s1.add("foo") #=> #<Set: {1, 2, "foo"}>
|
|
|
|
# s1.merge([2, 6]) #=> #<Set: {1, 2, "foo", 6}>
|
|
|
|
# s1.subset?(s2) #=> false
|
|
|
|
# s2.subset?(s1) #=> true
|
2003-09-19 10:53:02 +04:00
|
|
|
#
|
2008-03-21 15:15:06 +03:00
|
|
|
# == Contact
|
|
|
|
#
|
|
|
|
# - Akinori MUSHA <knu@iDaemons.org> (current maintainer)
|
|
|
|
#
|
2002-08-30 17:47:49 +04:00
|
|
|
class Set
|
|
|
|
include Enumerable
|
|
|
|
|
2002-12-24 08:29:04 +03:00
|
|
|
# Creates a new set containing the given objects.
|
2017-11-17 12:48:47 +03:00
|
|
|
#
|
|
|
|
# Set[1, 2] # => #<Set: {1, 2}>
|
2017-11-22 23:58:24 +03:00
|
|
|
# Set[1, 2, 1] # => #<Set: {1, 2}>
|
2017-11-17 12:48:47 +03:00
|
|
|
# Set[1, 'c', :s] # => #<Set: {1, "c", :s}>
|
2002-08-30 17:47:49 +04:00
|
|
|
def self.[](*ary)
|
|
|
|
new(ary)
|
|
|
|
end
|
|
|
|
|
2002-12-24 08:29:04 +03:00
|
|
|
# Creates a new set containing the elements of the given enumerable
|
|
|
|
# object.
|
|
|
|
#
|
|
|
|
# If a block is given, the elements of enum are preprocessed by the
|
|
|
|
# given block.
|
2017-11-17 12:48:47 +03:00
|
|
|
#
|
|
|
|
# Set.new([1, 2]) #=> #<Set: {1, 2}>
|
2017-11-22 23:58:24 +03:00
|
|
|
# Set.new([1, 2, 1]) #=> #<Set: {1, 2}>
|
2017-11-17 12:48:47 +03:00
|
|
|
# Set.new([1, 'c', :s]) #=> #<Set: {1, "c", :s}>
|
2017-11-22 23:58:24 +03:00
|
|
|
# Set.new(1..5) #=> #<Set: {1, 2, 3, 4, 5}>
|
2017-11-17 12:48:47 +03:00
|
|
|
# Set.new([1, 2, 3]) { |x| x * x } #=> #<Set: {1, 4, 9}>
|
2003-01-21 19:38:42 +03:00
|
|
|
def initialize(enum = nil, &block) # :yields: o
|
2015-02-11 22:04:16 +03:00
|
|
|
@hash ||= Hash.new(false)
|
2002-08-30 17:47:49 +04:00
|
|
|
|
2002-09-07 14:48:14 +04:00
|
|
|
enum.nil? and return
|
|
|
|
|
2002-09-20 14:46:52 +04:00
|
|
|
if block
|
2010-02-11 20:38:05 +03:00
|
|
|
do_with_enum(enum) { |o| add(block[o]) }
|
2002-09-20 14:46:52 +04:00
|
|
|
else
|
|
|
|
merge(enum)
|
|
|
|
end
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
|
|
|
|
2016-11-05 12:23:14 +03:00
|
|
|
# Makes the set compare its elements by their identity and returns
|
|
|
|
# self. This method may not be supported by all subclasses of Set.
|
|
|
|
def compare_by_identity
|
|
|
|
if @hash.respond_to?(:compare_by_identity)
|
|
|
|
@hash.compare_by_identity
|
|
|
|
self
|
|
|
|
else
|
|
|
|
raise NotImplementedError, "#{self.class.name}\##{__method__} is not implemented"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if the set will compare its elements by their
|
|
|
|
# identity. Also see Set#compare_by_identity.
|
|
|
|
def compare_by_identity?
|
|
|
|
@hash.respond_to?(:compare_by_identity?) && @hash.compare_by_identity?
|
|
|
|
end
|
|
|
|
|
2011-05-12 03:17:52 +04:00
|
|
|
def do_with_enum(enum, &block) # :nodoc:
|
2010-02-11 20:38:05 +03:00
|
|
|
if enum.respond_to?(:each_entry)
|
2014-08-06 15:28:21 +04:00
|
|
|
enum.each_entry(&block) if block
|
2010-02-11 20:38:05 +03:00
|
|
|
elsif enum.respond_to?(:each)
|
2014-08-06 15:28:21 +04:00
|
|
|
enum.each(&block) if block
|
2010-02-11 20:38:05 +03:00
|
|
|
else
|
|
|
|
raise ArgumentError, "value must be enumerable"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
private :do_with_enum
|
|
|
|
|
2014-08-06 14:13:34 +04:00
|
|
|
# Dup internal hash.
|
|
|
|
def initialize_dup(orig)
|
|
|
|
super
|
2012-09-01 13:51:48 +04:00
|
|
|
@hash = orig.instance_variable_get(:@hash).dup
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
|
|
|
|
2014-08-06 14:13:34 +04:00
|
|
|
# Clone internal hash.
|
2019-08-26 07:11:46 +03:00
|
|
|
def initialize_clone(orig, freeze: true)
|
2014-08-06 14:13:34 +04:00
|
|
|
super
|
2019-08-26 07:11:46 +03:00
|
|
|
@hash = orig.instance_variable_get(:@hash).clone(freeze: freeze)
|
2014-08-06 14:13:34 +04:00
|
|
|
end
|
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
def freeze # :nodoc:
|
2007-03-20 05:09:10 +03:00
|
|
|
@hash.freeze
|
2013-06-01 13:01:16 +04:00
|
|
|
super
|
2007-03-20 05:09:10 +03:00
|
|
|
end
|
|
|
|
|
2002-12-24 08:29:04 +03:00
|
|
|
# Returns the number of elements.
|
2002-08-30 17:47:49 +04:00
|
|
|
def size
|
|
|
|
@hash.size
|
|
|
|
end
|
|
|
|
alias length size
|
|
|
|
|
2002-12-24 08:29:04 +03:00
|
|
|
# Returns true if the set contains no elements.
|
2002-08-30 17:47:49 +04:00
|
|
|
def empty?
|
|
|
|
@hash.empty?
|
|
|
|
end
|
|
|
|
|
2002-12-24 08:29:04 +03:00
|
|
|
# Removes all elements and returns self.
|
2017-11-17 12:48:47 +03:00
|
|
|
#
|
|
|
|
# set = Set[1, 'c', :s] #=> #<Set: {1, "c", :s}>
|
|
|
|
# set.clear #=> #<Set: {}>
|
|
|
|
# set #=> #<Set: {}>
|
2002-08-30 17:47:49 +04:00
|
|
|
def clear
|
|
|
|
@hash.clear
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2002-12-24 08:29:04 +03:00
|
|
|
# Replaces the contents of the set with the contents of the given
|
|
|
|
# enumerable object and returns self.
|
2017-11-22 23:58:24 +03:00
|
|
|
#
|
|
|
|
# set = Set[1, 'c', :s] #=> #<Set: {1, "c", :s}>
|
|
|
|
# set.replace([1, 2]) #=> #<Set: {1, 2}>
|
|
|
|
# set #=> #<Set: {1, 2}>
|
2002-08-30 17:47:49 +04:00
|
|
|
def replace(enum)
|
2010-10-10 13:45:36 +04:00
|
|
|
if enum.instance_of?(self.class)
|
|
|
|
@hash.replace(enum.instance_variable_get(:@hash))
|
2014-08-06 15:28:21 +04:00
|
|
|
self
|
2002-09-20 14:46:52 +04:00
|
|
|
else
|
2015-11-16 10:41:18 +03:00
|
|
|
do_with_enum(enum) # make sure enum is enumerable before calling clear
|
2002-09-20 14:46:52 +04:00
|
|
|
clear
|
2010-02-11 20:38:05 +03:00
|
|
|
merge(enum)
|
2002-09-20 14:46:52 +04:00
|
|
|
end
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
|
|
|
|
2003-01-21 18:09:12 +03:00
|
|
|
# Converts the set to an array. The order of elements is uncertain.
|
2017-11-17 12:48:47 +03:00
|
|
|
#
|
|
|
|
# Set[1, 2].to_a #=> [1, 2]
|
|
|
|
# Set[1, 'c', :s].to_a #=> [1, "c", :s]
|
2002-08-30 17:47:49 +04:00
|
|
|
def to_a
|
|
|
|
@hash.keys
|
|
|
|
end
|
|
|
|
|
2013-07-19 06:22:11 +04:00
|
|
|
# Returns self if no arguments are given. Otherwise, converts the
|
|
|
|
# set to another with klass.new(self, *args, &block).
|
|
|
|
#
|
|
|
|
# In subclasses, returns klass.new(self, *args, &block) unless
|
|
|
|
# overridden.
|
|
|
|
def to_set(klass = Set, *args, &block)
|
|
|
|
return self if instance_of?(Set) && klass == Set && block.nil? && args.empty?
|
|
|
|
klass.new(self, *args, &block)
|
|
|
|
end
|
|
|
|
|
2011-05-12 03:17:52 +04:00
|
|
|
def flatten_merge(set, seen = Set.new) # :nodoc:
|
2002-09-07 14:32:23 +04:00
|
|
|
set.each { |e|
|
|
|
|
if e.is_a?(Set)
|
2011-05-19 01:19:18 +04:00
|
|
|
if seen.include?(e_id = e.object_id)
|
|
|
|
raise ArgumentError, "tried to flatten recursive Set"
|
|
|
|
end
|
2002-08-30 17:47:49 +04:00
|
|
|
|
2011-05-19 01:19:18 +04:00
|
|
|
seen.add(e_id)
|
|
|
|
flatten_merge(e, seen)
|
|
|
|
seen.delete(e_id)
|
2002-08-30 17:47:49 +04:00
|
|
|
else
|
2011-05-19 01:19:18 +04:00
|
|
|
add(e)
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2002-09-07 14:32:23 +04:00
|
|
|
self
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
2002-09-07 14:32:23 +04:00
|
|
|
protected :flatten_merge
|
2002-08-30 17:47:49 +04:00
|
|
|
|
2002-12-24 08:29:04 +03:00
|
|
|
# Returns a new set that is a copy of the set, flattening each
|
|
|
|
# containing set recursively.
|
2002-08-30 17:47:49 +04:00
|
|
|
def flatten
|
2002-10-02 20:45:35 +04:00
|
|
|
self.class.new.flatten_merge(self)
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
|
|
|
|
2002-12-24 08:29:04 +03:00
|
|
|
# Equivalent to Set#flatten, but replaces the receiver with the
|
|
|
|
# result in place. Returns nil if no modifications were made.
|
2002-08-30 17:47:49 +04:00
|
|
|
def flatten!
|
2015-11-16 10:29:37 +03:00
|
|
|
replace(flatten()) if any? { |e| e.is_a?(Set) }
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
|
|
|
|
2002-12-24 08:29:04 +03:00
|
|
|
# Returns true if the set contains the given object.
|
2015-02-17 04:47:28 +03:00
|
|
|
#
|
|
|
|
# Note that <code>include?</code> and <code>member?</code> do not test member
|
|
|
|
# equality using <code>==</code> as do other Enumerables.
|
|
|
|
#
|
|
|
|
# See also Enumerable#include?
|
2002-08-30 17:47:49 +04:00
|
|
|
def include?(o)
|
2015-02-11 22:04:16 +03:00
|
|
|
@hash[o]
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
|
|
|
alias member? include?
|
|
|
|
|
2003-01-21 18:15:26 +03:00
|
|
|
# Returns true if the set is a superset of the given set.
|
2002-11-09 21:52:04 +03:00
|
|
|
def superset?(set)
|
2015-11-16 09:43:43 +03:00
|
|
|
case
|
2017-10-21 19:28:52 +03:00
|
|
|
when set.instance_of?(self.class) && @hash.respond_to?(:>=)
|
2015-11-16 09:43:43 +03:00
|
|
|
@hash >= set.instance_variable_get(:@hash)
|
|
|
|
when set.is_a?(Set)
|
|
|
|
size >= set.size && set.all? { |o| include?(o) }
|
|
|
|
else
|
|
|
|
raise ArgumentError, "value must be a set"
|
|
|
|
end
|
2002-11-09 21:52:04 +03:00
|
|
|
end
|
2012-08-30 08:01:58 +04:00
|
|
|
alias >= superset?
|
2002-11-09 21:52:04 +03:00
|
|
|
|
2003-01-21 18:15:26 +03:00
|
|
|
# Returns true if the set is a proper superset of the given set.
|
2002-11-09 21:52:04 +03:00
|
|
|
def proper_superset?(set)
|
2015-11-16 09:43:43 +03:00
|
|
|
case
|
2017-10-21 19:28:52 +03:00
|
|
|
when set.instance_of?(self.class) && @hash.respond_to?(:>)
|
2015-11-16 09:43:43 +03:00
|
|
|
@hash > set.instance_variable_get(:@hash)
|
|
|
|
when set.is_a?(Set)
|
|
|
|
size > set.size && set.all? { |o| include?(o) }
|
|
|
|
else
|
|
|
|
raise ArgumentError, "value must be a set"
|
|
|
|
end
|
2002-11-09 21:52:04 +03:00
|
|
|
end
|
2012-08-30 08:01:58 +04:00
|
|
|
alias > proper_superset?
|
2002-11-09 21:52:04 +03:00
|
|
|
|
2003-01-21 18:15:26 +03:00
|
|
|
# Returns true if the set is a subset of the given set.
|
2002-11-09 21:52:04 +03:00
|
|
|
def subset?(set)
|
2015-11-16 09:43:43 +03:00
|
|
|
case
|
2017-10-21 19:28:52 +03:00
|
|
|
when set.instance_of?(self.class) && @hash.respond_to?(:<=)
|
2015-11-16 09:43:43 +03:00
|
|
|
@hash <= set.instance_variable_get(:@hash)
|
|
|
|
when set.is_a?(Set)
|
|
|
|
size <= set.size && all? { |o| set.include?(o) }
|
|
|
|
else
|
|
|
|
raise ArgumentError, "value must be a set"
|
|
|
|
end
|
2002-11-09 21:52:04 +03:00
|
|
|
end
|
2012-08-30 08:01:58 +04:00
|
|
|
alias <= subset?
|
2002-11-09 21:52:04 +03:00
|
|
|
|
2002-12-24 08:29:04 +03:00
|
|
|
# Returns true if the set is a proper subset of the given set.
|
2002-11-09 21:52:04 +03:00
|
|
|
def proper_subset?(set)
|
2015-11-16 09:43:43 +03:00
|
|
|
case
|
2017-10-21 19:28:52 +03:00
|
|
|
when set.instance_of?(self.class) && @hash.respond_to?(:<)
|
2015-11-16 09:43:43 +03:00
|
|
|
@hash < set.instance_variable_get(:@hash)
|
|
|
|
when set.is_a?(Set)
|
|
|
|
size < set.size && all? { |o| set.include?(o) }
|
|
|
|
else
|
|
|
|
raise ArgumentError, "value must be a set"
|
|
|
|
end
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
2012-08-30 08:01:58 +04:00
|
|
|
alias < proper_subset?
|
2002-08-30 17:47:49 +04:00
|
|
|
|
2013-07-30 13:58:13 +04:00
|
|
|
# Returns true if the set and the given set have at least one
|
|
|
|
# element in common.
|
2014-01-28 10:19:19 +04:00
|
|
|
#
|
2017-11-22 23:58:24 +03:00
|
|
|
# Set[1, 2, 3].intersect? Set[4, 5] #=> false
|
|
|
|
# Set[1, 2, 3].intersect? Set[3, 4] #=> true
|
2013-07-30 13:58:13 +04:00
|
|
|
def intersect?(set)
|
|
|
|
set.is_a?(Set) or raise ArgumentError, "value must be a set"
|
|
|
|
if size < set.size
|
|
|
|
any? { |o| set.include?(o) }
|
|
|
|
else
|
|
|
|
set.any? { |o| include?(o) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if the set and the given set have no element in
|
|
|
|
# common. This method is the opposite of +intersect?+.
|
2014-01-28 10:19:19 +04:00
|
|
|
#
|
2017-11-22 23:58:24 +03:00
|
|
|
# Set[1, 2, 3].disjoint? Set[3, 4] #=> false
|
|
|
|
# Set[1, 2, 3].disjoint? Set[4, 5] #=> true
|
2013-07-30 13:58:13 +04:00
|
|
|
def disjoint?(set)
|
|
|
|
!intersect?(set)
|
|
|
|
end
|
|
|
|
|
2002-12-24 08:29:04 +03:00
|
|
|
# Calls the given block once for each element in the set, passing
|
2008-04-23 06:58:46 +04:00
|
|
|
# the element as parameter. Returns an enumerator if no block is
|
|
|
|
# given.
|
2012-08-31 12:43:09 +04:00
|
|
|
def each(&block)
|
2015-06-15 08:37:38 +03:00
|
|
|
block or return enum_for(__method__) { size }
|
2012-08-31 12:43:09 +04:00
|
|
|
@hash.each_key(&block)
|
2003-07-27 22:10:54 +04:00
|
|
|
self
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
|
|
|
|
2003-09-19 10:53:02 +04:00
|
|
|
# Adds the given object to the set and returns self. Use +merge+ to
|
2007-03-20 05:09:10 +03:00
|
|
|
# add many elements at once.
|
2017-11-17 12:48:47 +03:00
|
|
|
#
|
|
|
|
# Set[1, 2].add(3) #=> #<Set: {1, 2, 3}>
|
2017-11-22 23:58:24 +03:00
|
|
|
# Set[1, 2].add([3, 4]) #=> #<Set: {1, 2, [3, 4]}>
|
2017-11-17 12:48:47 +03:00
|
|
|
# Set[1, 2].add(2) #=> #<Set: {1, 2}>
|
2002-08-30 17:47:49 +04:00
|
|
|
def add(o)
|
2005-06-25 10:22:05 +04:00
|
|
|
@hash[o] = true
|
2002-08-30 17:47:49 +04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
alias << add
|
|
|
|
|
2003-09-19 05:51:17 +04:00
|
|
|
# Adds the given object to the set and returns self. If the
|
2002-12-24 08:29:04 +03:00
|
|
|
# object is already in the set, returns nil.
|
2017-11-17 12:48:47 +03:00
|
|
|
#
|
|
|
|
# Set[1, 2].add?(3) #=> #<Set: {1, 2, 3}>
|
2017-11-22 23:58:24 +03:00
|
|
|
# Set[1, 2].add?([3, 4]) #=> #<Set: {1, 2, [3, 4]}>
|
2017-11-17 12:48:47 +03:00
|
|
|
# Set[1, 2].add?(2) #=> nil
|
2002-09-20 14:46:52 +04:00
|
|
|
def add?(o)
|
2015-11-16 10:29:37 +03:00
|
|
|
add(o) unless include?(o)
|
2002-09-20 14:46:52 +04:00
|
|
|
end
|
|
|
|
|
2003-09-19 11:47:46 +04:00
|
|
|
# Deletes the given object from the set and returns self. Use +subtract+ to
|
2007-03-20 05:09:10 +03:00
|
|
|
# delete many items at once.
|
2002-08-30 17:47:49 +04:00
|
|
|
def delete(o)
|
2002-09-20 14:46:52 +04:00
|
|
|
@hash.delete(o)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2002-12-24 08:29:04 +03:00
|
|
|
# Deletes the given object from the set and returns self. If the
|
|
|
|
# object is not in the set, returns nil.
|
2002-09-20 14:46:52 +04:00
|
|
|
def delete?(o)
|
2015-11-16 10:29:37 +03:00
|
|
|
delete(o) if include?(o)
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
|
|
|
|
2002-12-24 08:29:04 +03:00
|
|
|
# Deletes every element of the set for which block evaluates to
|
2016-05-04 12:07:01 +03:00
|
|
|
# true, and returns self. Returns an enumerator if no block is
|
|
|
|
# given.
|
2002-08-30 17:47:49 +04:00
|
|
|
def delete_if
|
2016-05-04 12:06:59 +03:00
|
|
|
block_given? or return enum_for(__method__) { size }
|
2013-05-19 12:33:27 +04:00
|
|
|
# @hash.delete_if should be faster, but using it breaks the order
|
|
|
|
# of enumeration in subclasses.
|
2013-05-20 17:28:32 +04:00
|
|
|
select { |o| yield o }.each { |o| @hash.delete(o) }
|
2002-08-30 17:47:49 +04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2010-05-30 17:19:09 +04:00
|
|
|
# Deletes every element of the set for which block evaluates to
|
2016-05-04 12:07:01 +03:00
|
|
|
# false, and returns self. Returns an enumerator if no block is
|
|
|
|
# given.
|
2010-05-30 17:19:09 +04:00
|
|
|
def keep_if
|
2016-05-04 12:06:59 +03:00
|
|
|
block_given? or return enum_for(__method__) { size }
|
2013-05-19 12:33:27 +04:00
|
|
|
# @hash.keep_if should be faster, but using it breaks the order of
|
|
|
|
# enumeration in subclasses.
|
2013-05-20 17:28:32 +04:00
|
|
|
reject { |o| yield o }.each { |o| @hash.delete(o) }
|
2010-05-30 17:19:09 +04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2008-06-03 10:01:29 +04:00
|
|
|
# Replaces the elements with ones returned by collect().
|
2016-05-04 12:07:01 +03:00
|
|
|
# Returns an enumerator if no block is given.
|
2008-06-03 08:42:32 +04:00
|
|
|
def collect!
|
2016-05-04 12:06:59 +03:00
|
|
|
block_given? or return enum_for(__method__) { size }
|
2017-10-21 20:03:40 +03:00
|
|
|
set = self.class.new
|
|
|
|
each { |o| set << yield(o) }
|
|
|
|
replace(set)
|
2002-09-20 14:46:52 +04:00
|
|
|
end
|
|
|
|
alias map! collect!
|
|
|
|
|
2002-12-24 08:29:04 +03:00
|
|
|
# Equivalent to Set#delete_if, but returns nil if no changes were
|
2016-05-04 12:07:01 +03:00
|
|
|
# made. Returns an enumerator if no block is given.
|
2012-08-31 12:43:09 +04:00
|
|
|
def reject!(&block)
|
2016-05-04 12:06:59 +03:00
|
|
|
block or return enum_for(__method__) { size }
|
2002-09-20 14:46:52 +04:00
|
|
|
n = size
|
2012-08-31 12:43:09 +04:00
|
|
|
delete_if(&block)
|
2015-11-16 10:29:37 +03:00
|
|
|
self if size != n
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
|
|
|
|
2010-05-30 17:19:09 +04:00
|
|
|
# Equivalent to Set#keep_if, but returns nil if no changes were
|
2016-05-04 12:07:01 +03:00
|
|
|
# made. Returns an enumerator if no block is given.
|
2012-08-31 12:43:09 +04:00
|
|
|
def select!(&block)
|
2016-05-04 12:06:59 +03:00
|
|
|
block or return enum_for(__method__) { size }
|
2010-05-30 17:19:09 +04:00
|
|
|
n = size
|
2012-08-31 12:43:09 +04:00
|
|
|
keep_if(&block)
|
2015-11-16 10:29:37 +03:00
|
|
|
self if size != n
|
2010-05-30 17:19:09 +04:00
|
|
|
end
|
|
|
|
|
2018-02-25 16:52:07 +03:00
|
|
|
# Equivalent to Set#select!
|
|
|
|
alias filter! select!
|
|
|
|
|
2002-12-24 08:29:04 +03:00
|
|
|
# Merges the elements of the given enumerable object to the set and
|
|
|
|
# returns self.
|
2002-08-30 17:47:49 +04:00
|
|
|
def merge(enum)
|
2009-05-01 11:52:09 +04:00
|
|
|
if enum.instance_of?(self.class)
|
|
|
|
@hash.update(enum.instance_variable_get(:@hash))
|
2002-09-20 14:46:52 +04:00
|
|
|
else
|
2010-02-11 20:38:05 +03:00
|
|
|
do_with_enum(enum) { |o| add(o) }
|
2002-09-20 14:46:52 +04:00
|
|
|
end
|
|
|
|
|
2002-08-30 17:47:49 +04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2002-12-24 08:29:04 +03:00
|
|
|
# Deletes every element that appears in the given enumerable object
|
|
|
|
# and returns self.
|
2002-08-30 17:47:49 +04:00
|
|
|
def subtract(enum)
|
2010-02-11 20:38:05 +03:00
|
|
|
do_with_enum(enum) { |o| delete(o) }
|
2002-08-30 17:47:49 +04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2002-12-24 08:29:04 +03:00
|
|
|
# Returns a new set built by merging the set and the elements of the
|
|
|
|
# given enumerable object.
|
2017-11-17 12:48:47 +03:00
|
|
|
#
|
2017-11-22 23:58:24 +03:00
|
|
|
# Set[1, 2, 3] | Set[2, 4, 5] #=> #<Set: {1, 2, 3, 4, 5}>
|
2017-11-17 12:48:47 +03:00
|
|
|
# Set[1, 5, 'z'] | (1..6) #=> #<Set: {1, 5, "z", 2, 3, 4, 6}>
|
2002-11-09 21:52:04 +03:00
|
|
|
def |(enum)
|
2002-09-20 14:46:52 +04:00
|
|
|
dup.merge(enum)
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
2017-11-23 00:13:51 +03:00
|
|
|
alias + |
|
|
|
|
alias union |
|
2002-08-30 17:47:49 +04:00
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
# Returns a new set built by duplicating the set, removing every
|
|
|
|
# element that appears in the given enumerable object.
|
2017-11-17 12:48:47 +03:00
|
|
|
#
|
|
|
|
# Set[1, 3, 5] - Set[1, 5] #=> #<Set: {3}>
|
|
|
|
# Set['a', 'b', 'z'] - ['a', 'c'] #=> #<Set: {"b", "z"}>
|
2011-05-19 04:07:25 +04:00
|
|
|
def -(enum)
|
|
|
|
dup.subtract(enum)
|
|
|
|
end
|
2017-11-23 00:13:51 +03:00
|
|
|
alias difference -
|
2011-05-19 01:19:18 +04:00
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
# Returns a new set containing elements common to the set and the
|
|
|
|
# given enumerable object.
|
2017-11-17 12:48:47 +03:00
|
|
|
#
|
|
|
|
# Set[1, 3, 5] & Set[3, 2, 1] #=> #<Set: {3, 1}>
|
|
|
|
# Set['a', 'b', 'z'] & ['a', 'b', 'c'] #=> #<Set: {"a", "b"}>
|
2011-05-19 04:07:25 +04:00
|
|
|
def &(enum)
|
|
|
|
n = self.class.new
|
2018-11-03 15:27:10 +03:00
|
|
|
if enum.is_a?(Set)
|
|
|
|
if enum.size > size
|
|
|
|
each { |o| n.add(o) if enum.include?(o) }
|
|
|
|
else
|
|
|
|
enum.each { |o| n.add(o) if include?(o) }
|
|
|
|
end
|
|
|
|
else
|
|
|
|
do_with_enum(enum) { |o| n.add(o) if include?(o) }
|
|
|
|
end
|
2011-05-19 04:07:25 +04:00
|
|
|
n
|
|
|
|
end
|
2017-11-23 00:13:51 +03:00
|
|
|
alias intersection &
|
2002-08-30 17:47:49 +04:00
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
# Returns a new set containing elements exclusive between the set
|
|
|
|
# and the given enumerable object. (set ^ enum) is equivalent to
|
|
|
|
# ((set | enum) - (set & enum)).
|
2017-11-17 12:48:47 +03:00
|
|
|
#
|
2017-11-22 23:58:24 +03:00
|
|
|
# Set[1, 2] ^ Set[2, 3] #=> #<Set: {3, 1}>
|
2017-11-17 12:48:47 +03:00
|
|
|
# Set[1, 'b', 'c'] ^ ['b', 'd'] #=> #<Set: {"d", 1, "c"}>
|
2011-05-19 04:07:25 +04:00
|
|
|
def ^(enum)
|
|
|
|
n = Set.new(enum)
|
2015-11-16 10:29:37 +03:00
|
|
|
each { |o| n.add(o) unless n.delete?(o) }
|
2011-05-19 04:07:25 +04:00
|
|
|
n
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if two sets are equal. The equality of each couple
|
|
|
|
# of elements is defined according to Object#eql?.
|
2017-11-17 12:48:47 +03:00
|
|
|
#
|
|
|
|
# Set[1, 2] == Set[2, 1] #=> true
|
|
|
|
# Set[1, 3, 5] == Set[1, 5] #=> false
|
|
|
|
# Set['a', 'b', 'c'] == Set['a', 'c', 'b'] #=> true
|
|
|
|
# Set['a', 'b', 'c'] == ['a', 'c', 'b'] #=> false
|
2011-05-19 04:07:25 +04:00
|
|
|
def ==(other)
|
|
|
|
if self.equal?(other)
|
|
|
|
true
|
|
|
|
elsif other.instance_of?(self.class)
|
|
|
|
@hash == other.instance_variable_get(:@hash)
|
|
|
|
elsif other.is_a?(Set) && self.size == other.size
|
|
|
|
other.all? { |o| @hash.include?(o) }
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
def hash # :nodoc:
|
|
|
|
@hash.hash
|
|
|
|
end
|
2002-08-30 17:47:49 +04:00
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
def eql?(o) # :nodoc:
|
|
|
|
return false unless o.is_a?(Set)
|
2012-09-01 13:51:48 +04:00
|
|
|
@hash.eql?(o.instance_variable_get(:@hash))
|
2011-05-19 04:07:25 +04:00
|
|
|
end
|
2002-08-30 17:47:49 +04:00
|
|
|
|
2017-10-22 15:25:34 +03:00
|
|
|
# Resets the internal state after modification to existing elements
|
|
|
|
# and returns self.
|
|
|
|
#
|
|
|
|
# Elements will be reindexed and deduplicated.
|
|
|
|
def reset
|
|
|
|
if @hash.respond_to?(:rehash)
|
|
|
|
@hash.rehash # This should perform frozenness check.
|
|
|
|
else
|
2019-01-20 07:44:25 +03:00
|
|
|
raise FrozenError, "can't modify frozen #{self.class.name}" if frozen?
|
2017-10-22 15:25:34 +03:00
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2017-10-29 23:59:33 +03:00
|
|
|
# Returns true if the given object is a member of the set,
|
|
|
|
# and false otherwise.
|
2017-09-19 11:45:12 +03:00
|
|
|
#
|
|
|
|
# Used in case statements:
|
|
|
|
#
|
2017-10-29 23:59:33 +03:00
|
|
|
# require 'set'
|
|
|
|
#
|
2017-09-19 11:45:12 +03:00
|
|
|
# case :apple
|
2017-10-29 23:59:33 +03:00
|
|
|
# when Set[:potato, :carrot]
|
|
|
|
# "vegetable"
|
|
|
|
# when Set[:apple, :banana]
|
|
|
|
# "fruit"
|
2017-09-19 11:45:12 +03:00
|
|
|
# end
|
2017-10-29 23:59:33 +03:00
|
|
|
# # => "fruit"
|
2017-09-19 11:45:12 +03:00
|
|
|
#
|
|
|
|
# Or by itself:
|
|
|
|
#
|
2017-11-22 23:58:24 +03:00
|
|
|
# Set[1, 2, 3] === 2 #=> true
|
|
|
|
# Set[1, 2, 3] === 4 #=> false
|
2017-09-19 11:45:12 +03:00
|
|
|
#
|
|
|
|
alias === include?
|
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
# Classifies the set by the return value of the given block and
|
|
|
|
# returns a hash of {value => set of elements} pairs. The block is
|
|
|
|
# called once for each element of the set, passing the element as
|
|
|
|
# parameter.
|
|
|
|
#
|
|
|
|
# require 'set'
|
|
|
|
# files = Set.new(Dir.glob("*.rb"))
|
|
|
|
# hash = files.classify { |f| File.mtime(f).year }
|
2017-11-22 23:58:24 +03:00
|
|
|
# hash #=> {2000=>#<Set: {"a.rb", "b.rb"}>,
|
|
|
|
# # 2001=>#<Set: {"c.rb", "d.rb", "e.rb"}>,
|
|
|
|
# # 2002=>#<Set: {"f.rb"}>}
|
2016-05-04 12:07:01 +03:00
|
|
|
#
|
|
|
|
# Returns an enumerator if no block is given.
|
2011-05-19 04:07:25 +04:00
|
|
|
def classify # :yields: o
|
2016-05-04 12:06:59 +03:00
|
|
|
block_given? or return enum_for(__method__) { size }
|
2002-08-30 17:47:49 +04:00
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
h = {}
|
2002-08-30 17:47:49 +04:00
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
each { |i|
|
2015-11-16 10:29:37 +03:00
|
|
|
(h[yield(i)] ||= self.class.new).add(i)
|
2011-05-19 01:19:18 +04:00
|
|
|
}
|
2002-08-30 17:47:49 +04:00
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
h
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
# Divides the set into a set of subsets according to the commonality
|
|
|
|
# defined by the given block.
|
|
|
|
#
|
|
|
|
# If the arity of the block is 2, elements o1 and o2 are in common
|
|
|
|
# if block.call(o1, o2) is true. Otherwise, elements o1 and o2 are
|
|
|
|
# in common if block.call(o1) == block.call(o2).
|
|
|
|
#
|
|
|
|
# require 'set'
|
|
|
|
# numbers = Set[1, 3, 4, 6, 9, 10, 11]
|
|
|
|
# set = numbers.divide { |i,j| (i - j).abs == 1 }
|
2017-11-22 23:58:24 +03:00
|
|
|
# set #=> #<Set: {#<Set: {1}>,
|
|
|
|
# # #<Set: {11, 9, 10}>,
|
|
|
|
# # #<Set: {3, 4}>,
|
|
|
|
# # #<Set: {6}>}>
|
2016-05-04 12:07:01 +03:00
|
|
|
#
|
|
|
|
# Returns an enumerator if no block is given.
|
2011-05-19 04:07:25 +04:00
|
|
|
def divide(&func)
|
2016-05-04 12:06:59 +03:00
|
|
|
func or return enum_for(__method__) { size }
|
2011-05-19 04:07:25 +04:00
|
|
|
|
|
|
|
if func.arity == 2
|
|
|
|
require 'tsort'
|
|
|
|
|
|
|
|
class << dig = {} # :nodoc:
|
|
|
|
include TSort
|
|
|
|
|
|
|
|
alias tsort_each_node each_key
|
|
|
|
def tsort_each_child(node, &block)
|
|
|
|
fetch(node).each(&block)
|
|
|
|
end
|
|
|
|
end
|
2002-08-30 17:47:49 +04:00
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
each { |u|
|
|
|
|
dig[u] = a = []
|
|
|
|
each{ |v| func.call(u, v) and a << v }
|
|
|
|
}
|
2002-08-30 17:47:49 +04:00
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
set = Set.new()
|
|
|
|
dig.each_strongly_connected_component { |css|
|
|
|
|
set.add(self.class.new(css))
|
|
|
|
}
|
|
|
|
set
|
|
|
|
else
|
|
|
|
Set.new(classify(&func).values)
|
|
|
|
end
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
InspectKey = :__inspect_key__ # :nodoc:
|
|
|
|
|
|
|
|
# Returns a string containing a human-readable representation of the
|
2017-11-22 23:58:24 +03:00
|
|
|
# set ("#<Set: {element1, element2, ...}>").
|
2011-05-19 04:07:25 +04:00
|
|
|
def inspect
|
|
|
|
ids = (Thread.current[InspectKey] ||= [])
|
|
|
|
|
|
|
|
if ids.include?(object_id)
|
|
|
|
return sprintf('#<%s: {...}>', self.class.name)
|
|
|
|
end
|
|
|
|
|
2015-11-16 10:41:30 +03:00
|
|
|
ids << object_id
|
2011-05-19 04:07:25 +04:00
|
|
|
begin
|
|
|
|
return sprintf('#<%s: {%s}>', self.class, to_a.inspect[1..-2])
|
|
|
|
ensure
|
|
|
|
ids.pop
|
|
|
|
end
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
|
|
|
|
2017-07-14 11:46:13 +03:00
|
|
|
alias to_s inspect
|
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
def pretty_print(pp) # :nodoc:
|
|
|
|
pp.text sprintf('#<%s: {', self.class.name)
|
|
|
|
pp.nest(1) {
|
|
|
|
pp.seplist(self) { |o|
|
|
|
|
pp.pp o
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pp.text "}>"
|
|
|
|
end
|
2011-05-19 01:19:18 +04:00
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
def pretty_print_cycle(pp) # :nodoc:
|
|
|
|
pp.text sprintf('#<%s: {%s}>', self.class.name, empty? ? '' : '...')
|
|
|
|
end
|
2002-08-30 17:47:49 +04:00
|
|
|
end
|
|
|
|
|
2011-05-15 15:55:52 +04:00
|
|
|
#
|
2014-02-05 04:33:38 +04:00
|
|
|
# SortedSet implements a Set that guarantees that its elements are
|
2009-05-01 11:46:23 +04:00
|
|
|
# yielded in sorted order (according to the return values of their
|
|
|
|
# #<=> methods) when iterating over them.
|
2011-05-15 15:55:52 +04:00
|
|
|
#
|
2009-05-07 21:32:48 +04:00
|
|
|
# All elements that are added to a SortedSet must respond to the <=>
|
|
|
|
# method for comparison.
|
2011-05-15 15:55:52 +04:00
|
|
|
#
|
2009-05-01 11:46:23 +04:00
|
|
|
# Also, all elements must be <em>mutually comparable</em>: <tt>el1 <=>
|
|
|
|
# el2</tt> must not return <tt>nil</tt> for any elements <tt>el1</tt>
|
|
|
|
# and <tt>el2</tt>, else an ArgumentError will be raised when
|
|
|
|
# iterating over the SortedSet.
|
|
|
|
#
|
|
|
|
# == Example
|
2011-05-15 15:55:52 +04:00
|
|
|
#
|
2009-05-01 11:46:23 +04:00
|
|
|
# require "set"
|
2011-05-15 15:55:52 +04:00
|
|
|
#
|
2009-05-02 17:54:09 +04:00
|
|
|
# set = SortedSet.new([2, 1, 5, 6, 4, 5, 3, 3, 3])
|
2009-05-01 11:46:23 +04:00
|
|
|
# ary = []
|
2011-05-15 15:55:52 +04:00
|
|
|
#
|
2009-05-01 11:46:23 +04:00
|
|
|
# set.each do |obj|
|
|
|
|
# ary << obj
|
|
|
|
# end
|
2011-05-15 15:55:52 +04:00
|
|
|
#
|
2009-05-01 11:46:23 +04:00
|
|
|
# p ary # => [1, 2, 3, 4, 5, 6]
|
2011-05-15 15:55:52 +04:00
|
|
|
#
|
2009-05-02 17:54:09 +04:00
|
|
|
# set2 = SortedSet.new([1, 2, "3"])
|
2009-05-01 11:46:23 +04:00
|
|
|
# set2.each { |obj| } # => raises ArgumentError: comparison of Fixnum with String failed
|
2011-05-15 15:55:52 +04:00
|
|
|
#
|
2002-09-20 14:46:52 +04:00
|
|
|
class SortedSet < Set
|
|
|
|
@@setup = false
|
2017-10-21 18:57:32 +03:00
|
|
|
@@mutex = Mutex.new
|
2002-09-20 14:46:52 +04:00
|
|
|
|
|
|
|
class << self
|
2011-05-19 04:07:25 +04:00
|
|
|
def [](*ary) # :nodoc:
|
2002-09-20 14:46:52 +04:00
|
|
|
new(ary)
|
|
|
|
end
|
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
def setup # :nodoc:
|
2002-09-20 14:46:52 +04:00
|
|
|
@@setup and return
|
|
|
|
|
2017-10-21 18:57:32 +03:00
|
|
|
@@mutex.synchronize do
|
|
|
|
# a hack to shut up warning
|
|
|
|
alias_method :old_init, :initialize
|
|
|
|
|
|
|
|
begin
|
|
|
|
require 'rbtree'
|
|
|
|
|
|
|
|
module_eval <<-END, __FILE__, __LINE__+1
|
|
|
|
def initialize(*args)
|
|
|
|
@hash = RBTree.new
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def add(o)
|
|
|
|
o.respond_to?(:<=>) or raise ArgumentError, "value must respond to <=>"
|
|
|
|
super
|
|
|
|
end
|
|
|
|
alias << add
|
|
|
|
END
|
|
|
|
rescue LoadError
|
|
|
|
module_eval <<-END, __FILE__, __LINE__+1
|
|
|
|
def initialize(*args)
|
|
|
|
@keys = nil
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear
|
|
|
|
@keys = nil
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def replace(enum)
|
|
|
|
@keys = nil
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def add(o)
|
|
|
|
o.respond_to?(:<=>) or raise ArgumentError, "value must respond to <=>"
|
|
|
|
@keys = nil
|
|
|
|
super
|
|
|
|
end
|
|
|
|
alias << add
|
|
|
|
|
|
|
|
def delete(o)
|
|
|
|
@keys = nil
|
|
|
|
@hash.delete(o)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_if
|
|
|
|
block_given? or return enum_for(__method__) { size }
|
|
|
|
n = @hash.size
|
|
|
|
super
|
|
|
|
@keys = nil if @hash.size != n
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def keep_if
|
|
|
|
block_given? or return enum_for(__method__) { size }
|
|
|
|
n = @hash.size
|
|
|
|
super
|
|
|
|
@keys = nil if @hash.size != n
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge(enum)
|
|
|
|
@keys = nil
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def each(&block)
|
|
|
|
block or return enum_for(__method__) { size }
|
|
|
|
to_a.each(&block)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_a
|
|
|
|
(@keys = @hash.keys).sort! unless @keys
|
|
|
|
@keys
|
|
|
|
end
|
|
|
|
|
|
|
|
def freeze
|
|
|
|
to_a
|
|
|
|
super
|
|
|
|
end
|
2017-10-22 15:25:34 +03:00
|
|
|
|
|
|
|
def rehash
|
|
|
|
@keys = nil
|
|
|
|
super
|
|
|
|
end
|
2017-10-21 18:57:32 +03:00
|
|
|
END
|
|
|
|
end
|
|
|
|
# a hack to shut up warning
|
|
|
|
remove_method :old_init
|
2002-09-20 14:46:52 +04:00
|
|
|
|
2017-10-21 18:57:32 +03:00
|
|
|
@@setup = true
|
|
|
|
end
|
2002-09-20 14:46:52 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
def initialize(*args, &block) # :nodoc:
|
2002-09-20 14:46:52 +04:00
|
|
|
SortedSet.setup
|
2019-06-05 05:08:54 +03:00
|
|
|
@keys = nil
|
|
|
|
super
|
2002-09-20 14:46:52 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Enumerable
|
2002-12-24 08:29:04 +03:00
|
|
|
# Makes a set from the enumerable object with given arguments.
|
2005-06-30 10:20:09 +04:00
|
|
|
# Needs to +require "set"+ to use this method.
|
2002-09-20 14:46:52 +04:00
|
|
|
def to_set(klass = Set, *args, &block)
|
|
|
|
klass.new(self, *args, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# =begin
|
|
|
|
# == RestricedSet class
|
|
|
|
# RestricedSet implements a set with restrictions defined by a given
|
|
|
|
# block.
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2002-09-20 14:46:52 +04:00
|
|
|
# === Super class
|
|
|
|
# Set
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2002-09-20 14:46:52 +04:00
|
|
|
# === Class Methods
|
|
|
|
# --- RestricedSet::new(enum = nil) { |o| ... }
|
|
|
|
# --- RestricedSet::new(enum = nil) { |rset, o| ... }
|
|
|
|
# Creates a new restricted set containing the elements of the given
|
|
|
|
# enumerable object. Restrictions are defined by the given block.
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2002-09-20 14:46:52 +04:00
|
|
|
# If the block's arity is 2, it is called with the RestrictedSet
|
|
|
|
# itself and an object to see if the object is allowed to be put in
|
|
|
|
# the set.
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2002-09-20 14:46:52 +04:00
|
|
|
# Otherwise, the block is called with an object to see if the object
|
|
|
|
# is allowed to be put in the set.
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2002-09-20 14:46:52 +04:00
|
|
|
# === Instance Methods
|
|
|
|
# --- restriction_proc
|
|
|
|
# Returns the restriction procedure of the set.
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2002-09-20 14:46:52 +04:00
|
|
|
# =end
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2002-09-20 14:46:52 +04:00
|
|
|
# class RestricedSet < Set
|
|
|
|
# def initialize(*args, &block)
|
|
|
|
# @proc = block or raise ArgumentError, "missing a block"
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2002-09-20 14:46:52 +04:00
|
|
|
# if @proc.arity == 2
|
|
|
|
# instance_eval %{
|
2011-05-19 04:07:25 +04:00
|
|
|
# def add(o)
|
|
|
|
# @hash[o] = true if @proc.call(self, o)
|
|
|
|
# self
|
|
|
|
# end
|
|
|
|
# alias << add
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2011-05-19 04:07:25 +04:00
|
|
|
# def add?(o)
|
|
|
|
# if include?(o) || !@proc.call(self, o)
|
|
|
|
# nil
|
|
|
|
# else
|
|
|
|
# @hash[o] = true
|
|
|
|
# self
|
|
|
|
# end
|
|
|
|
# end
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2011-05-19 04:07:25 +04:00
|
|
|
# def replace(enum)
|
|
|
|
# enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
|
|
|
|
# clear
|
|
|
|
# enum.each_entry { |o| add(o) }
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2011-05-19 04:07:25 +04:00
|
|
|
# self
|
|
|
|
# end
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2011-05-19 04:07:25 +04:00
|
|
|
# def merge(enum)
|
|
|
|
# enum.respond_to?(:each) or raise ArgumentError, "value must be enumerable"
|
|
|
|
# enum.each_entry { |o| add(o) }
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2011-05-19 04:07:25 +04:00
|
|
|
# self
|
|
|
|
# end
|
2002-09-20 14:46:52 +04:00
|
|
|
# }
|
|
|
|
# else
|
|
|
|
# instance_eval %{
|
2011-05-19 04:07:25 +04:00
|
|
|
# def add(o)
|
2005-06-25 10:22:05 +04:00
|
|
|
# if @proc.call(o)
|
2011-05-19 04:07:25 +04:00
|
|
|
# @hash[o] = true
|
2005-06-25 10:22:05 +04:00
|
|
|
# end
|
2011-05-19 04:07:25 +04:00
|
|
|
# self
|
|
|
|
# end
|
|
|
|
# alias << add
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2011-05-19 04:07:25 +04:00
|
|
|
# def add?(o)
|
|
|
|
# if include?(o) || !@proc.call(o)
|
|
|
|
# nil
|
|
|
|
# else
|
|
|
|
# @hash[o] = true
|
|
|
|
# self
|
|
|
|
# end
|
|
|
|
# end
|
2002-09-20 14:46:52 +04:00
|
|
|
# }
|
|
|
|
# end
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2002-09-20 14:46:52 +04:00
|
|
|
# super(*args)
|
|
|
|
# end
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2002-09-20 14:46:52 +04:00
|
|
|
# def restriction_proc
|
|
|
|
# @proc
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
2012-11-24 22:51:45 +04:00
|
|
|
# Tests have been moved to test/test_set.rb.
|