Class: Wordlist::UniqueFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/wordlist/unique_filter.rb

Overview

Acts as a filter to filter out duplicate words.

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUniqueFilter

Creates a new unique filter.

Since:

  • 1.0.0



22
23
24
# File 'lib/wordlist/unique_filter.rb', line 22

def initialize
  @hashes = Set.new
end

Instance Attribute Details

#hashesSet<Integer> (readonly)

The seen String hashes

Returns:

  • (Set<Integer>)

Since:

  • 1.0.0



17
18
19
# File 'lib/wordlist/unique_filter.rb', line 17

def hashes
  @hashes
end

Instance Method Details

#add(word) ⇒ Object Also known as: <<

Adds the word to the unique filter.

Parameters:

  • word (String)

    The word to add.

Since:

  • 1.0.0



45
46
47
# File 'lib/wordlist/unique_filter.rb', line 45

def add(word)
  @hashes.add(word.hash)
end

#add?(word) ⇒ Boolean

Attempts to add the word to the unique filter.

Parameters:

  • word (String)

    The word to add.

Returns:

  • (Boolean)

    Returns true if the word does not yet exist in the unique filter. Returns false if the word already exists in the unique filter.

Since:

  • 1.0.0



61
62
63
# File 'lib/wordlist/unique_filter.rb', line 61

def add?(word)
  !@hashes.add?(word.hash).nil?
end

#clearObject

Clears the unique filter.

Since:

  • 1.0.0



77
78
79
# File 'lib/wordlist/unique_filter.rb', line 77

def clear
  @hashes.clear
end

#empty?Boolean

Determines if the unique filter is empty or not.

Returns:

  • (Boolean)

Since:

  • 1.0.0



70
71
72
# File 'lib/wordlist/unique_filter.rb', line 70

def empty?
  @hashes.empty?
end

#include?(word) ⇒ Boolean

Determines if the given word has been previously seen.

Parameters:

  • word (String)

    The word to check for.

Returns:

  • (Boolean)

    Specifies whether the word has been previously seen.

Since:

  • 1.0.0



35
36
37
# File 'lib/wordlist/unique_filter.rb', line 35

def include?(word)
  @hashes.include?(word.hash)
end

#sizeInteger

The size of the unique filter.

Returns:

  • (Integer)

    The number of unique words seen by the unique filter.

Since:

  • 1.0.0



87
88
89
# File 'lib/wordlist/unique_filter.rb', line 87

def size
  @hashes.size
end