Class: SortedSet

Inherits:
Set show all
Defined in:
lib/extensions/set/set.rb,
lib/extensions/rhoxml/rexml/set.rb

Overview

SortedSet implements a set which elements are sorted in order. See Set.

Constant Summary collapse

@@setup =
false

Constants inherited from Set

Set::InspectKey

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Set

#&, #-, #==, #^, #add, #add?, #classify, #clear, #collect!, #delete, #delete?, #delete_if, #divide, #each, #empty?, #eql?, #flatten, #flatten!, #freeze, #hash, #include?, #initialize_copy, #inspect, #keep_if, #merge, #pretty_print, #pretty_print_cycle, #proper_subset?, #proper_superset?, #reject!, #replace, #select!, #size, #subset?, #subtract, #superset?, #taint, #to_a, #untaint, #|

Methods included from Enumerable

#all?, #any?, #collect, #detect, #each_with_index, #find_all, #group_by, #inject, #max, #member?, #min, #sort, #sort_by, #to_a, #to_set

Constructor Details

#initialize(*args, &block) ⇒ SortedSet

:nodoc:



613
614
615
616
# File 'lib/extensions/set/set.rb', line 613

def initialize(*args, &block)	# :nodoc:
  SortedSet.setup
  initialize(*args, &block)
end

Class Method Details

.[](*ary) ⇒ Object

:nodoc:



518
519
520
# File 'lib/extensions/set/set.rb', line 518

def [](*ary)	# :nodoc:
  new(ary)
end

.setupObject

:nodoc:



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'lib/extensions/set/set.rb', line 522

def setup	# :nodoc:
  @@setup and return

  module_eval {
    # a hack to shut up warning
    alias old_init initialize
    remove_method :old_init
  }
  begin
	require 'rbtree'

	module_eval %{
	  def initialize(*args, &block)
 @hash = RBTree.new
 super
	  end
	  
	  def add(o)
 o.respond_to?(:<=>) or raise ArgumentError, "value must respond to <=>"
 super
	  end
	  alias << add
	}
  rescue LoadError
	module_eval %{
	  def initialize(*args, &block)
 @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__)
 n = @hash.size
 super
 @keys = nil if @hash.size != n
 self
	  end

	  def keep_if
 block_given? or return enum_for(__method__)
 n = @hash.size
 super
 @keys = nil if @hash.size != n
 self
	  end

	  def merge(enum)
 @keys = nil
 super
	  end

	  def each
 block_given? or return enum_for(__method__)
 to_a.each { |o| yield(o) }
 self
	  end

	  def to_a
 (@keys = @hash.keys).sort! unless @keys
 @keys
	  end
	}
  end

  @@setup = true
end