Class: Rux::Set

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rux/set.rb

Overview

A set of strings backed by a radix tree

Instance Method Summary collapse

Constructor Details

#initialize(members = []) ⇒ Set

Returns a new instance of Set.

Parameters:

  • members (Array<String>) (defaults to: [])

7
8
9
10
# File 'lib/rux/set.rb', line 7

def initialize(members = [])
  @tree = Rux::Tree.new
  members.each { |m| add(m) }
end

Instance Method Details

#add(member) ⇒ Boolean Also known as: <<, push

Returns true if member was added (false if already present).

Parameters:

  • member (String)

Returns:

  • (Boolean)

    true if member was added (false if already present)


14
15
16
# File 'lib/rux/set.rb', line 14

def add(member)
  !!@tree.set(member, nil, :absent)
end

#delete(member) ⇒ Boolean

Returns true if member was deleted (false if was not present).

Parameters:

  • member (String)

Returns:

  • (Boolean)

    true if member was deleted (false if was not present)


20
21
22
# File 'lib/rux/set.rb', line 20

def delete(member)
  !@tree.delete(member, :absent)
end

#eachObject


40
41
42
# File 'lib/rux/set.rb', line 40

def each
  @tree.each { |k, v| yield(k) }
end

#include?(member) ⇒ Boolean

Returns true if member is present.

Parameters:

  • member (String)

Returns:

  • (Boolean)

    true if member is present


26
27
28
# File 'lib/rux/set.rb', line 26

def include?(member)
  !@tree.get(member, :absent)
end

#sizeInteger Also known as: length, count

Returns:

  • (Integer)

31
32
33
# File 'lib/rux/set.rb', line 31

def size
  @tree.size
end