Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm out of practice with VBA, but IIRC, iterating the Collection using For Each should be a bit faster than going numerically:</p> <pre><code>Dim i As Variant For Each i In current.next_nodes If i.letter = char Then Set current = i match = True Exit For End If Next node </code></pre> <p>You're also not using the full capabilities of Collection. It's a Key-Value map, not just a resizeable array. You might get better performance if you use the letter as a key, though looking up a key that isn't present throws an error, so you have to use an ugly error workaround to check for each node. The inside of the b loop would look like:</p> <pre><code>Dim char As String char = Mid(wordlist.Item(a), b, 1) Dim node As Node On Error Resume Next Set node = Nothing Set node = current.next_nodes.Item(char) On Error Goto 0 If node Is Nothing Then Set node = New Node current.next_nodes.add node, char Endif Set current = node </code></pre> <p>You won't need the letter variable on class Node that way.</p> <p>I didn't test this. I hope it's all right...</p> <p>Edit: Fixed the For Each loop.</p> <hr> <p>Another thing you can do which will possibly be slower but will use less memory is use an array instead of a collection, and resize with each added element. Arrays can't be public on classes, so you have to add methods to the class to deal with it:</p> <pre><code>Public letter As String Private next_nodes() As Node Public is_word As Boolean Public Sub addNode(new_node As Node) Dim current_size As Integer On Error Resume Next current_size = UBound(next_nodes) 'ubound throws an error if the array is not yet allocated On Error GoTo 0 ReDim next_nodes(0 To current_size) As Node Set next_nodes(current_size) = new_node End Sub Public Function getNode(letter As String) As Node Dim n As Variant On Error Resume Next For Each n In next_nodes If n.letter = letter Then Set getNode = n Exit Function End If Next End Function </code></pre> <hr> <p>Edit: And a final optimization strategy, get the Integer char value with the Asc function and store that instead of a String.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload