Skip to content

Commit

Permalink
DLang: Use Appender instead of ~ operator.
Browse files Browse the repository at this point in the history
No difference in performance.
  • Loading branch information
renatoathaydes committed Jan 14, 2024
1 parent 40cd423 commit f1364d0
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/d/src/dencoder.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import std.conv : to;
import std.outbuffer : OutBuffer;
import std.ascii : isAlpha, isDigit;
import std.algorithm.iteration : filter;
import std.array : Appender, appender;

@safe:

Expand Down Expand Up @@ -54,11 +55,11 @@ private bool lastItemIsDigit(string[] words)
}

void printTranslations(string[][BigInt] dict, ISolutionHandler shandler,
string number, string digits, string[] words)
string number, string digits, Appender!(string[]) words)
{
if (digits.length == 0)
{
shandler.put(number, words);
shandler.put(number, words[]);
return;
}
bool foundWord = false;
Expand All @@ -72,14 +73,18 @@ void printTranslations(string[][BigInt] dict, ISolutionHandler shandler,
foundWord = true;
foreach (word; *foundWords)
{
dict.printTranslations(shandler, number, digits[i + 1 .. $], words ~ word);
words.put(word);
dict.printTranslations(shandler, number, digits[i + 1 .. $], words);
words.shrinkTo(words[].length - 1);
}
}
}
if (!foundWord && !words.lastItemIsDigit)
if (!foundWord && !words[].lastItemIsDigit)
{
string digit = [digits[0]];
dict.printTranslations(shandler, number, digits[1 .. $], words ~ digit);
words.put(digit);
dict.printTranslations(shandler, number, digits[1 .. $], words);
words.shrinkTo(words[].length - 1);
}
}

Expand Down Expand Up @@ -170,13 +175,14 @@ void main(string[] args) @trusted
auto numbers = args.length > 3 ? args[3] : "tests/numbers.txt";
auto dict = loadDictionary(file);
auto numbersFile = File(numbers);
auto words = new string[16];
auto words = appender!(string[]);
words.reserve(16);
foreach (number; numbersFile.byLine)
{
auto num = number.idup;
string digits = num.filter!(c => c.isDigit)
.to!string;
dict.printTranslations(shandler, num, digits, words[0 .. 0]);
dict.printTranslations(shandler, num, digits, words);
}
shandler.flush();
}

0 comments on commit f1364d0

Please sign in to comment.