Class: WordNode

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

Overview

Represents a node in the dictionary graph. As the graph is built, each node is given links to possible letters that follow in this string. A node represents a character in a word so far - i.e. There might be a node for the character a in the string ca, which would have pointers to nodes containing t, n, etc for the words cat, can, and so on. There will be multiple a nodes in the entire graph for different string beginnings. The nodes have no notion of the string that came before them; this is managed by the calling program.

Instance Method Summary collapse

Constructor Details

#initialize(letter) ⇒ WordNode

Returns a new instance of WordNode.



10
11
12
13
14
15
# File 'lib/dict_map.rb', line 10

def initialize(letter)
    @ends_word = false
    @successors = Hash.new
    @letter = letter
    @exhausted = false
end

Instance Method Details

#end_word!Object



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

def end_word!
    @ends_word = true
end

#ends_word?Boolean

If true, this letter is the end of a word in the dictionary.

Returns:

  • (Boolean)


18
19
20
# File 'lib/dict_map.rb', line 18

def ends_word?
    @ends_word
end

#exhausted=(val) ⇒ Object



43
44
45
# File 'lib/dict_map.rb', line 43

def exhausted= (val)
    @exhausted = val
end

#exhausted?Boolean

A node is exhausted if all solutions of the string beyond its point have been found in a grid. This is useful in situations where you only want to find a word once and allows the search to be cut off early.

Returns:

  • (Boolean)


39
40
41
# File 'lib/dict_map.rb', line 39

def exhausted?
    @exhausted
end

#letterObject

the letter represented by this node



27
28
29
# File 'lib/dict_map.rb', line 27

def letter
    @letter
end

#successorsObject

successors are nodes that can follow the string so far.



32
33
34
# File 'lib/dict_map.rb', line 32

def successors
    @successors
end

#successors_exhausted?Boolean

Checks the exhausted status of the successor nodes. Ideally, this should be private… but there may be cases where we actually exhaust a longer word but not this one, or vice versa. As a result, there is no implication between this word being “used” and its successors being used.

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dict_map.rb', line 50

def successors_exhausted?
    if @successors.empty?
        return true
    end
    @successors.each_value do |node|
        if !node.exhausted?
            return false
        end
    end
    return true
end