Skip to content

Commit

Permalink
Slightly Faster Algorithm
Browse files Browse the repository at this point in the history
All `trie.find().isEmpty`'s were replaced with their hasChildren counterparts. Some repeated operations were replaced with `TrieNode.walk()`.
  • Loading branch information
taibeled committed Dec 18, 2024
1 parent e2c2f1d commit e1bdb27
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ class _AlgorithmShowerState extends State<AlgorithmShower> {
void _initializeAlgorithm() {
// I extrapolated this out of build just to be unnecessarily safe (turns out it was a good idea)

if (widget.dictionaryTrie.find(widget.path).isEmpty ||
if (!widget.dictionaryTrie.hasChildren(widget.path) ||
widget.dictionaryTrie.has(widget.path)) {
return;
}
Expand All @@ -315,8 +315,10 @@ class _AlgorithmShowerState extends State<AlgorithmShower> {
sortedLetters.clear();

if (isTurn) {
var temporaryDictionary = widget.dictionaryTrie.root.walk(widget.path)!;

for (var letter in letters) {
if (widget.dictionaryTrie.find(widget.path + letter).isEmpty) {
if (!temporaryDictionary.hasChild(letter)) {
continue;
}
optimalGame[letter] = determinePercentage(
Expand All @@ -341,7 +343,7 @@ class _AlgorithmShowerState extends State<AlgorithmShower> {
);
}

if (widget.dictionaryTrie.find(widget.path).isEmpty) {
if (!widget.dictionaryTrie.hasChildren(widget.path)) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0),
child: Column(
Expand All @@ -352,7 +354,7 @@ class _AlgorithmShowerState extends State<AlgorithmShower> {
textAlign: TextAlign.center,
),
const SizedBox(height: 24),
if (fullScrabbleTrie.find(widget.path).isNotEmpty)
if (fullScrabbleTrie.hasChildren(widget.path))
const Text(
"Note: Words exist in the full Scrabble dictionary. Change the dictionary type to view them.",
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w300),
Expand Down

0 comments on commit e1bdb27

Please sign in to comment.