Class: I8::Set

Inherits:
Hamster::Set show all
Defined in:
lib/nrser/labs/i8.rb

Class Method Summary collapse

Methods inherited from Hamster::Set

#as_json, #to_h, #to_mutable, #to_mutable_array, #to_yaml

Class Method Details

.emptyI8::Set

Override to build our empty set through alloc so that we can return it in new.

Returns:



82
83
84
# File 'lib/nrser/labs/i8.rb', line 82

def self.empty
  @empty ||= alloc Hamster::Trie.new( 0 )
end

.new(items = []) ⇒ I8::Set

Get a I8::Set containing ‘items`.

Overridden to…

  1. Return ‘items` if items is already a I8::Set… sort of like a copy constructor that doesn’t actually copy because the instances are immutable.

  2. Return an instance of ‘self` pointing to `items`’s Hamster::Trie if ‘items` is a Hamster::Set; this way you get an instance of the correct class but don’t do any additional instantiation.

  3. Returns empty if ‘items` responds to `#empty?` truthfully.

Otherwise, defers to ‘super`.

Parameters:

  • items (#each) (defaults to: [])

    Items for the new set to contain.

Returns:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/nrser/labs/i8.rb', line 62

def self.new items = []
  case items
  when self
    items
  when Hamster::Set
    alloc items.instance_variable_get( :@trie )
  else
    if items.respond_to?( :empty? ) && items.empty?
      self.empty
    else
      super items
    end
  end
end