Type casting fixes and bounding array access

This commit is contained in:
Imbus 2024-11-21 08:46:55 +01:00
parent 8c8930f5c5
commit 94d807fc67

View file

@ -1,7 +1,6 @@
#include "dictionary.h"
#include "word.h"
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
@ -13,7 +12,7 @@ using std::vector;
Dictionary::Dictionary() {}
bool Dictionary::contains(const string &word) const {
int l = word.length();
auto l = word.length();
Word w = Word(word);
if (std::find(this->words[l].begin(), this->words[l].end(), w) !=
std::end(this->words[l])) {
@ -60,9 +59,9 @@ int Dictionary::slurp(path p) {
std::string line;
while (std::getline(file, line)) {
if (line.size() > MAXLEN)
continue;
words[line.size()].push_back(Word(line));
// Words larger than max gets placed in the topmost bucket
words[std::min(line.size(), static_cast<size_t>(MAXLEN) - 1)].push_back(
Word(line));
}
file.close();