| 13479 | } |
| 13480 | |
| 13481 | inline void |
| 13482 | Dictionary::init(const char* fn) { |
| 13483 | if (fn == nullptr) { |
| 13484 | // Initialize from predefined dictionary |
| 13485 | |
| 13486 | // Set up information |
| 13487 | max_len = ::max_word_len; |
| 13488 | n_all_words = 0; |
| 13489 | |
| 13490 | size_t sz = 0; |
| 13491 | for (int l=0; l<=max_len; l++) { |
| 13492 | n_words[l]=::n_words[l]; |
| 13493 | n_all_words += n_words[l]; |
| 13494 | sz += n_words[l] * (l+1); // Leave room for terminating zero |
| 13495 | } |
| 13496 | chunk = static_cast<char*>(Gecode::heap.ralloc(sz)); |
| 13497 | |
| 13498 | // Copy words |
| 13499 | char* c = chunk; |
| 13500 | for (int l=0; l<=max_len; l++) { |
| 13501 | s_words[l] = c; |
| 13502 | for (int i=0; i<n_words[l]; i++) { |
| 13503 | for (int j=0; j<l; j++) |
| 13504 | *c++ = ::s_words[l][i][j]; |
| 13505 | *c++ = 0; |
| 13506 | } |
| 13507 | } |
| 13508 | } else { |
| 13509 | // Initialize from file |
| 13510 | |
| 13511 | // Set up information |
| 13512 | max_len = 0; |
| 13513 | n_all_words = 0; |
| 13514 | |
| 13515 | size_t sz = 0; |
| 13516 | |
| 13517 | { |
| 13518 | std::string s; |
| 13519 | std::ifstream f; |
| 13520 | f.open(fn); |
| 13521 | |
| 13522 | if (!f.is_open()) |
| 13523 | throw Gecode::Exception("Dictionary", |
| 13524 | "Unable to open file"); |
| 13525 | while (!f.eof()) { |
| 13526 | getline(f,s); |
| 13527 | if (s.size() >= static_cast<size_t>(limit_len)) |
| 13528 | goto skip1; |
| 13529 | { |
| 13530 | int n = static_cast<int>(s.size()); |
| 13531 | for (int i=0; i<n; i++) |
| 13532 | if (!isalpha(s[static_cast<unsigned int>(i)]) || |
| 13533 | !islower(s[static_cast<unsigned int>(i)])) |
| 13534 | goto skip1; |
| 13535 | // Found a legal word |
| 13536 | n_all_words++; |
| 13537 | n_words[n]++; |
| 13538 | sz += n+1; |
no test coverage detected