• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/examples/spell_check/spell_check_file.cpp
1
////////////////////////////////////////////////////////////////////////////////
2
//  Copyright (c) 2012 Andrew Kemp
3
//
4
//  SPDX-License-Identifier: BSL-1.0
5
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
6
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
////////////////////////////////////////////////////////////////////////////////
8

9
#include <hpx/config.hpp>
10
#if !defined(HPX_COMPUTE_DEVICE_CODE)
11
#include <hpx/hpx_init.hpp>
12
#include <hpx/include/actions.hpp>
13
#include <hpx/include/async.hpp>
14
#include <hpx/include/lcos.hpp>
15
#include <hpx/include/runtime.hpp>
16
#include <hpx/include/util.hpp>
17

18
#include <cstddef>
19
#include <fstream>
20
#include <iostream>
21
#include <string>
22
#include <vector>
23

24
std::vector<std::string> words;
25

26
std::string search(int start, int end, std::string const& word);
27

×
28
HPX_PLAIN_ACTION(search, search_action)
29

×
30
std::string search(int start, int end, std::string const& word)
31
{
32
    //highest value is 'z' at 122
33
    //lowest value is 'a' at 97
34
    //start is where our word check is located.
35
    //end is where the end if this thread's search is.
×
36
    int mid = (start + end);
×
37
    std::string check = words[mid / 2];
38
    //first we check if there is no definitive match
×
39
    if (start == end && word != check)
40
    {
41
        //just a quick check, because the list is not perfectly symmetrical
42
        {
43
            int pos = mid / 2;
44

45
            std::size_t size;
46
            //if our value is lower than start, we disregard it.
47
            if (word.length() >= check.length())
48
                size = check.length();
49
            else
50
                size = word.length();
51
            std::string part;
52
            bool sub = true;
×
53
            for (std::size_t i = 0; i < size; i++)
54
            {
×
55
                char check_char = static_cast<char>(tolower(check[i]));
×
56
                char word_char = word[i];
×
57
                if (word_char != check_char)
58
                {
×
59
                    if (word_char >= check_char)
60
                        sub = false;
61
                    break;
62
                }
63
                else
×
64
                    part.push_back(word_char);
65
            }
×
66
            while (check != word)
67
            {
×
68
                check = words[pos];
×
69
                if (sub)
×
70
                    pos--;
71
                else
×
72
                    pos++;
×
73
                if (check.find(part) == std::string::npos)
×
74
                    return word + " was not found in this dictionary, " +
75
                        check + " was the closest match.\n";
76
            }
×
77
            return "";
78
        }
79
    }
80
    std::size_t size;
81
    //if our value is lower than start, we disregard it.
82
    if (word.length() >= check.length())
83
        size = check.length();
84
    else
85
        size = word.length();
×
86
    for (std::size_t i = 0; i < size; i++)
87
    {
×
88
        char check_char = static_cast<char>(tolower(check[i]));
×
89
        char word_char = word[i];
×
90
        if (check_char != word_char)
91
        {
×
92
            if (word_char > check_char)
×
93
                return search((mid + 1) / 2, end, word);
94
            else
×
95
                return search(start, (mid - 1) / 2, word);
96
        }
97
    }
×
98
    if (check.length() == word.length())
×
99
        return "";
100
    else
×
101
        return search((start + end + 1) / 2, end, word);
102
}
×
103
int hpx_main()
104
{
105
    {
106
        using namespace std;
×
107
        ifstream fin;
×
108
        string path = __FILE__;
109
        string wordlist_path;
×
110
        string remove = "spell_check_file.cpp";
×
111
        for (string::size_type i = 0; i < path.length() - remove.length(); i++)
112
        {
×
113
            wordlist_path.push_back(path[i]);
×
114
            if (path[i] == '\\')
115
            {
×
116
                wordlist_path.push_back(path[i]);
117
            }
118
        }
119
        // list of American English words in alphabetical order.
120
        // Provided by Kevin at http://wordlist.sourceforge.net/
121

122
        string word;
×
123
        cout << "Loading file: example_text.txt\n";
124
        hpx::chrono::high_resolution_timer t;
125

126
        int fileLines = 0;
127
        ;
×
128
        std::string example_path = wordlist_path + "example_text.txt";
×
129
        fin.open(example_path);
×
130
        if (fin.is_open())
131
        {
132
            string temp;
×
133
            while (fin.good())
134
            {
×
135
                fin >> temp;
×
136
                for (string::size_type i = 0; i < temp.size(); ++i)
×
137
                    word.push_back(temp[i]);
×
138
                word.push_back(' ');
×
139
                fileLines++;
140
            }
×
141
            cout << fileLines << " words loaded in " << t.elapsed() << "s.\n";
142
        }
143
        else
144
        {
×
145
            cout << "Error: Unable to open file.\n";
146
            return hpx::finalize();
147
        }
×
148
        vector<bool> contraction;
×
149
        vector<string> strs;
150
        {
×
151
            vector<string> temp;
×
152
            hpx::string_util::split(
×
153
                temp, word, hpx::string_util::is_any_of("\n\t -"));
×
154
            for (string::size_type i = 0; i < temp.size(); i++)
155
            {
156
                bool isContraction = false;
157
                string holder;
×
158
                for (string::size_type j = 0; j < temp[i].size(); j++)
159
                {
160
                    //a size check to avoid errors
×
161
                    if (temp[i].size() - j - 1 == 2)
162
                    {
163
                        //if this is a contraction, ignore the rest of it...
×
164
                        if (temp[i][j + 1] == '\'' && temp[i][j] == 'n' &&
×
165
                            temp[i][j + 2] == 't')
166
                        {
167
                            //but label this as a contraction
168
                            isContraction = true;
169
                            break;
170
                        }
171
                    }
172
                    //remove any garbage characters
×
173
                    if (toupper(temp[i][j]) >= 'A' &&
174
                        toupper(temp[i][j]) <= 'Z')
×
175
                        holder.push_back(
×
176
                            static_cast<char>(tolower(temp[i][j])));
177
                }
×
178
                if (holder.size() > 0)
179
                {
×
180
                    contraction.push_back(isContraction);
×
181
                    strs.push_back(holder);
182
                }
183
            }
×
184
        }
×
185
        fin.close();
×
186
        wordlist_path = wordlist_path + "5desk.txt";
×
187
        fin.open(wordlist_path);
×
188
        int wordcount = 0;
×
189
        cout << "Reading dictionary file to memory...\n";
190
        t.restart();
×
191
        if (fin.is_open())
192
        {
193
            string temp;
×
194
            while (fin.good())
195
            {
×
196
                getline(fin, temp);
×
197
                for (string::size_type i = 0; i < temp.length(); i++)
×
198
                    temp[i] = static_cast<char>(tolower(temp[i]));
×
199
                words.push_back(temp);
×
200
                wordcount++;
201
            }
×
202
            cout << wordcount << " words loaded in " << t.elapsed() << "s.\n";
203
        }
204
        else
205
        {
×
206
            cout << "Error: Unable to open file.\n";
207
            return hpx::finalize();
208
        }
×
209
        fin.close();
210
        t.restart();
211
        {
212
            using hpx::async;
213
            using hpx::future;
214
            using hpx::wait_all;
×
215
            vector<search_action> sAct;    //[sizeX * sizeY];
×
216
            vector<future<string>> wordRun;
×
217
            wordRun.reserve(strs.size());
×
218
            for (string::size_type i = 0; i < strs.size(); ++i)
219
            {
220
                string& single = strs[i];
×
221
                int start = 0;
×
222
                hpx::id_type const locality_id = hpx::find_here();
223
                search_action temp;
224
                wordRun.push_back(
×
225
                    hpx::async(temp, locality_id, start, wordcount, single));
226
                sAct.push_back(temp);
227
                //cout << search(0, wordcount, single) << endl;
228
            }
229
            hpx::wait_all(wordRun);
×
230
            cout << "Search completed in " << t.elapsed() << "s.\n";
×
231
            for (string::size_type i = 0; i < strs.size(); i++)
232
            {
×
233
                string get = wordRun[i].get();
×
234
                if (get.size() > 0)
235
                {
×
236
                    cout << "Word number " << i + 1 << ":\n";
×
237
                    if (contraction[i])
238
                        cout << "Note: This word seems to be a "
239
                                "contraction.\nThe last two letters have been "
×
240
                                "ignored.\n";
241
                    cout << get;
242
                }
243
            }
×
244
        }
×
245
    }
×
246
    return hpx::finalize();    // Handles HPX shutdown
247
}
×
248
int main()
249
{
×
250
    int code = hpx::init();
251
    std::cout << std::endl;
252
    return code;
253
}
254
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc