[ create a new paste ] login | about

Link: http://codepad.org/3bzfp0dq    [ raw code | fork ]

C++, pasted on Dec 28:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

#include <boost/exception/all.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>

#include <boost/lexical_cast.hpp>

#include "Serialization.hpp"

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems)
{
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim))
    {
        elems.push_back(item);
    }
    return elems;
}


std::vector<std::string> split(const std::string &s, char delim)
{
    std::vector<std::string> elems;
    split(s, delim, elems);
    return elems;
}

struct SSentence
{
    std::vector<unsigned short> Words;
    unsigned char* Save(unsigned char* pFile);
    unsigned char* Load(unsigned char* pFile);
};

unsigned char* SSentence::Save(unsigned char* pFile)
{
    return pFile;
}

unsigned char* SSentence::Load(unsigned char* pFile)
{
    return pFile;
}

struct SWord
{
    std::string Text;
    std::vector<SSentence> Usages;
    unsigned char* Save(unsigned char* pFile);
    unsigned char* Load(unsigned char* pFile);
};

unsigned char* SWord::Save(unsigned char* pFile)
{
    return pFile;
}

unsigned char* SWord::Load(unsigned char* pFile)
{
    return pFile;
}

// 2 byte unsigned	0 to 65,535
// 1 byte unsigned	0 to 255
struct SPermutation
{
    std::vector<unsigned short> WordIndices;
    std::vector<unsigned char> UsageIndices;
    unsigned char* Save(unsigned char* pFile);
    unsigned char* Load(unsigned char* pFile);
};

unsigned char* SPermutation::Save(unsigned char* pFile)
{
    return pFile;
}

unsigned char* SPermutation::Load(unsigned char* pFile)
{
    return pFile;
}

std::vector<SWord> g_words;
std::vector<SPermutation> g_sentences;

void SaveAll(std::string strFileName)
{
    std::ofstream ofs(strFileName.c_str());
    if(!ofs)
    {
        return;
    }

    unsigned int nWords = g_words.size();
    ofs.write((char*)&nWords, sizeof(unsigned int));
    for (unsigned int iii = 0; iii < nWords; ++iii)
    {
        SWord& refWord = g_words[iii];
        unsigned int nWordLength = refWord.Text.size();
        ofs.write((char*)&nWordLength, sizeof(unsigned int));
        ofs.write((char*)refWord.Text.c_str(), sizeof(unsigned char) * nWordLength);
        unsigned int nUsages = refWord.Usages.size();
        ofs.write((char*)&nUsages, sizeof(unsigned int));

        for (unsigned int jjj = 0; jjj < nUsages; ++jjj)
        {
            SSentence& refSentence = refWord.Usages[jjj];
            unsigned int nSentenceWords = refSentence.Words.size();
            ofs.write((char*)&nSentenceWords, sizeof(unsigned int));
            for (unsigned int lll = 0; lll < nSentenceWords; ++lll)
            {
                ofs.write((char*)&refSentence.Words[lll], sizeof(unsigned short));
            }
        }

    }

    unsigned int nPermutations = g_sentences.size();
    ofs.write((char*)&nPermutations, sizeof(unsigned int));
    for (unsigned int iii = 0; iii < nPermutations; ++iii)
    {
        SPermutation& refPerm = g_sentences[iii];

        unsigned int nWordIndices = refPerm.WordIndices.size();
        ofs.write((char*)&nWordIndices, sizeof(unsigned int));
        for (unsigned int jjj = 0; jjj < nWordIndices; ++jjj)
        {
            ofs.write((char*)&refPerm.WordIndices[jjj], sizeof(unsigned short));
        }

        unsigned int nUsageIndices = refPerm.UsageIndices.size();
        ofs.write((char*)&nUsageIndices, sizeof(unsigned int));
        for (unsigned int jjj = 0; jjj < nUsageIndices; ++jjj)
        {
            ofs.write((char*)&refPerm.UsageIndices[jjj], sizeof(unsigned char));
        }
    }


    ofs.close();
}

void LoadAll(std::string strFileName)
{
    std::ifstream ifs(strFileName.c_str());

    if(!ifs)
    {
        return;
    }


    unsigned int nWords = 0;
    ifs.read((char*)&nWords, sizeof(unsigned int));
    for (unsigned int iii = 0; iii < nWords; ++iii)
    {
        SWord word ;
        unsigned int nWordLength = 0;
        ifs.read((char*)&nWordLength, sizeof(unsigned int));
        unsigned char *pWord = new unsigned char[nWordLength + 1];
        pWord[nWordLength] = '\0';
        ifs.read((char*)pWord, sizeof(unsigned char) * nWordLength);
        word.Text.append((char*)pWord);
        delete [] pWord;
        unsigned int nUsages = 0;
        ifs.read((char*)&nUsages, sizeof(unsigned int));

        for (unsigned int jjj = 0; jjj < nUsages; ++jjj)
        {
            SSentence sentence ;
            unsigned int nSentenceWords = 0;
            ifs.read((char*)&nSentenceWords, sizeof(unsigned int));
            for (unsigned int lll = 0; lll < nSentenceWords; ++lll)
            {
                unsigned short shWord = 0;
                ifs.read((char*)&shWord, sizeof(unsigned short));
                sentence.Words.push_back(shWord);
            }

            word.Usages.push_back(sentence);
        }

        g_words.push_back(word);

    }

    unsigned int nPermutations = 0;
    ifs.read((char*)&nPermutations, sizeof(unsigned int));
    for (unsigned int iii = 0; iii < nPermutations; ++iii)
    {
        SPermutation perm = g_sentences[iii];

        unsigned int nWordIndices = 0;
        ifs.read((char*)&nWordIndices, sizeof(unsigned int));
        for (unsigned int jjj = 0; jjj < nWordIndices; ++jjj)
        {
            unsigned short shWordIndex = 0;
            ifs.read((char*)&shWordIndex, sizeof(unsigned short));
            perm.WordIndices.push_back(shWordIndex);
        }

        unsigned int nUsageIndices = 0;
        ifs.read((char*)&nUsageIndices, sizeof(unsigned int));
        for (unsigned int jjj = 0; jjj < nUsageIndices; ++jjj)
        {
            unsigned char chUsageIndex = 0;
            ifs.read((char*)&chUsageIndex, sizeof(unsigned char));
            perm.UsageIndices.push_back(chUsageIndex);
        }
    }

    ifs.close();
}

bool WordExists(std::string strName)
{
    bool bVal = false;
    for (unsigned int iii = 0; iii < g_words.size(); ++iii)
    {
        if (g_words[iii].Text == strName)
        {
            bVal = true;
            break;
        }
    }
    return bVal;
}

unsigned short GetWordIndex(std::string strName)
{
    unsigned short shVal = 0;
    for (unsigned int iii = 0; iii < g_words.size(); ++iii)
    {
        if (g_words[iii].Text == strName)
        {
            shVal = iii;
            break;
        }
    }
    return shVal;
}

std::string GetInput();
void Test();

int main()
{
    Test();
    return 0;
}

std::string GetInput()
{
    std::string strInput;
    getline(cin, strInput);
    return strInput;
}

void Test()
{

    LoadAll("language.bin");

    unsigned short nCurrentPermutation = 0;
    unsigned short nCurrentWord = 0 ;



    std::string strInput;
    while (strInput != "exit")
    {
        strInput.clear();
        cout << "Waiting for input: " << endl;
        strInput = GetInput();

        std::vector<std::string> chunks = split(strInput, ' ');
        if (chunks[0] == "help")
        {
            cout << "help helps." << endl;
            cout << "create word textForNameHere" << endl;
            cout << "create permutation word word word word etc. " << endl;
            cout << "add word usage word word word word etc." << endl;
            cout << "add permutation usages index index index index etc." << endl;
            cout << "set word textForNameHere" << endl;
            cout << "set permutation word word word word etc." << endl;
            cout << "edit word name textForNewName" << endl;
            cout << "edit word usage usageIndex word word word word etc." << endl;
            cout << "list word" << endl;
        }
        else if (chunks[0] == "create")
        {
            if (chunks.size() < 2)
            {
                continue;
            }
            if (chunks[1] == "word")
            {
                if (chunks.size() < 3)
                {
                    continue;
                }
                if (WordExists(chunks[2]) == true)
                {
                    cout << "That word exists already." << endl;
                    continue;
                }

                SWord word;
                word.Text = chunks[2];
                g_words.push_back(word);
                nCurrentWord = g_words.size() - 1;
            }
            else if(chunks[1] == "permutation")
            {
                if (chunks.size() < 3)
                {
                    continue;
                }
                SPermutation permutation;
                for (unsigned int iii = 2; iii < chunks.size(); ++iii)
                {
                    unsigned short wordIndex = GetWordIndex(chunks[iii]);
                    permutation.WordIndices.push_back(wordIndex);
                }

                g_sentences.push_back(permutation);
                nCurrentPermutation = g_sentences.size() - 1;
            }
        }
        else if (chunks[0] == "add")
        {
            if (chunks.size() < 3)
            {
                continue;
            }
            if (chunks[1] == "word")
            {
                if (chunks[2] == "usage")
                {
                    if (chunks.size() < 4)
                    {
                        continue;
                    }

                    SSentence sentence;
                    for (unsigned int iii = 3; iii < chunks.size(); ++iii)
                    {
                        unsigned short wordIndex = GetWordIndex(chunks[iii]);
                        sentence.Words.push_back(wordIndex);
                    }

                    g_words[nCurrentWord].Usages.push_back(sentence);

                }
            }
            else if (chunks[1] == "permutation")
            {
                if (chunks.size() < 3)
                {
                    continue;
                }
                if (chunks[2] == "usages")
                {
                    if (chunks.size() < 4)
                    {
                        continue;
                    }
                    for (unsigned int iii = 3; iii < chunks.size(); ++iii)
                    {
                        unsigned char chVal = boost::lexical_cast<unsigned char>(chunks[iii]);
                        g_sentences[nCurrentPermutation].UsageIndices.push_back(chVal);
                    }
                }
            }
        }
        else if (chunks[0] == "set")
        {
            if (chunks.size() < 3)
            {
                continue;
            }
            if (chunks[1] == "word")
            {
                nCurrentWord = GetWordIndex(chunks[2]);
            }
            else if (chunks[1] == "permutation")
            {
                std::vector<unsigned short> indices;
                for (unsigned int iii = 2; iii < chunks.size(); ++iii)
                {
                    unsigned short wordIndex = GetWordIndex(chunks[iii]);
                    indices.push_back(wordIndex);
                }

                for (unsigned int iii = 0; iii < g_sentences.size(); ++iii)
                {
                    SPermutation& refPermutation = g_sentences[iii];
                    if (refPermutation.WordIndices == indices)
                    {
                        nCurrentPermutation = iii;
                        break;
                    }
                }
            }
        }
        else if (chunks[0] == "edit")
        {
            if (chunks.size() < 3)
            {
                continue;
            }

            if (chunks[1] == "word")
            {
                if (chunks[2] == "name")
                {
                    if (chunks.size() < 4)
                    {
                        continue;
                    }

                    g_words[nCurrentWord].Text = chunks[3];
                }
                else if (chunks[2] == "usage")
                {
                    if (chunks.size() < 5)
                    {
                        continue;
                    }
                    unsigned int nUsageIndex = boost::lexical_cast<unsigned int>(chunks[3]);
                    if (nUsageIndex >= g_words[nCurrentWord].Usages.size())
                    {
                        continue;
                    }

                    SSentence sentence;
                    for (unsigned int iii = 4; iii < chunks.size(); ++iii)
                    {
                        unsigned short wordIndex = GetWordIndex(chunks[iii]);
                        sentence.Words.push_back(wordIndex);
                    }
                    g_words[nCurrentWord].Usages[nUsageIndex] = sentence;
                }
            }
        }
        else if (chunks[0] == "list")
        {
            if (chunks.size() < 2)
            {
                continue;
            }
            if (chunks[1] == "word")
            {
                cout << "Name: " << g_words[nCurrentWord].Text << endl;
                cout << "Sentences:" << endl;
                for (unsigned int iii = 0; iii < g_words[nCurrentWord].Usages.size(); ++iii)
                {
                    cout << "#: " << iii << endl;
                    SSentence& refSentence = g_words[nCurrentWord].Usages[iii];
                    unsigned int nUsageWords = refSentence.Words.size();
                    for (unsigned int jjj = 0; jjj < nUsageWords; ++jjj)
                    {
                        cout << " " << g_words[refSentence.Words[jjj]].Text;
                    }
                    cout << endl;
                }
            }
        }
    }

    SaveAll("language.bin");
}


Create a new paste based on this one


Comments: