• 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_simple.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 word + " was found in this dictionary.\n";
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 word + " was found in this dictionary.\n";
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.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
        wordlist_path = wordlist_path + "5desk.txt";
×
122
        fin.open(wordlist_path);
×
123
        int wordcount = 0;
×
124
        cout << "Reading dictionary file to memory...\n";
125
        hpx::chrono::high_resolution_timer t;
×
126
        if (fin.is_open())
127
        {
128
            string temp;
×
129
            while (fin.good())
130
            {
×
131
                getline(fin, temp);
×
132
                for (string::size_type i = 0; i < temp.length(); i++)
×
133
                    temp[i] = static_cast<char>(tolower(temp[i]));
×
134
                words.push_back(temp);
×
135
                wordcount++;
136
            }
×
137
            cout << wordcount << " words loaded in " << t.elapsed() << "s.\n";
138
        }
139
        else
140
        {
×
141
            cout << "Error: Unable to open file.\n";
142
            return hpx::finalize();
143
        }
×
144
        fin.close();
×
145
        char* word = new char[1024];
146
        cout << "Enter the words you would like to spellcheck, separated by a "
×
147
                "\"Space\", and then press \"Enter\".\n";
×
148
        cin.getline(word, 1024, '\n');
×
149
        vector<bool> contraction;
×
150
        vector<string> strs;
151
        {
×
152
            vector<string> temp;
×
153
            hpx::string_util::split(
×
154
                temp, word, hpx::string_util::is_any_of("\n\t -"));
×
155
            for (string::size_type i = 0; i < temp.size(); i++)
156
            {
157
                bool isContraction = false;
158
                string holder;
×
159
                for (string::size_type j = 0; j < temp[i].size(); j++)
160
                {
161
                    //a size check to avoid errors
×
162
                    if (temp[i].size() - j - 1 == 2)
163
                    {
164
                        //if this is a contraction, ignore the rest of it...
×
165
                        if (temp[i][j + 1] == '\'' && temp[i][j] == 'n' &&
×
166
                            temp[i][j + 2] == 't')
167
                        {
168
                            //but label this as a contraction
169
                            isContraction = true;
170
                            break;
171
                        }
172
                    }
173
                    //remove any garbage characters
×
174
                    if (toupper(temp[i][j]) >= 'A' &&
175
                        toupper(temp[i][j]) <= 'Z')
×
176
                        holder.push_back(
×
177
                            static_cast<char>(tolower(temp[i][j])));
178
                }
×
179
                if (holder.size() > 0)
180
                {
×
181
                    contraction.push_back(isContraction);
×
182
                    strs.push_back(holder);
183
                }
184
            }
×
185
        }
186
        t.restart();
187
        {
188
            using hpx::async;
189
            using hpx::future;
×
190
            vector<search_action> sAct;    //[sizeX * sizeY];
×
191
            vector<future<string>> wordRun;
×
192
            wordRun.reserve(strs.size());
×
193
            for (string::size_type i = 0; i < strs.size(); ++i)
194
            {
195
                string& single = strs[i];
×
196
                int start = 0;
×
197
                hpx::id_type const locality_id = hpx::find_here();
198
                search_action temp;
199
                wordRun.push_back(
×
200
                    hpx::async(temp, locality_id, start, wordcount, single));
201
                sAct.push_back(temp);
202
                //cout << search(0, wordcount, single) << endl;
203
            }
204
            hpx::wait_all(wordRun);
×
205
            cout << "Search completed in " << t.elapsed() << "s.\n";
×
206
            for (string::size_type i = 0; i < strs.size(); i++)
207
            {
×
208
                cout << "Word number " << i + 1 << ":\n";
×
209
                if (contraction[i])
210
                    cout << "Note: This word seems to be a contraction.\n"
×
211
                            "The last two letters have been ignored.\n";
×
212
                cout << wordRun[i].get();
213
            }
×
214
        }
×
215
    }
×
216
    return hpx::finalize();    // Handles HPX shutdown
×
217
}
×
218
int main()
219
{
×
220
    int code = hpx::init();
221
    std::cout << std::endl;
222
    return code;
223
}
224
#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