aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/containers/str_hash/str_hash.cpp
blob: 12986385334e2b57bb8c9a33ded1683932cb1138 (plain) (blame)
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
#include "str_hash.h"

#include <library/cpp/charset/ci_string.h>
#include <util/stream/output.h>
#include <util/stream/input.h>

HashSet::HashSet(const char** array, size_type size) {
    Resize(size);
    while (*array && **array)
        AddPermanent(*array++);
}

void HashSet::Read(IInputStream* input) {
    TString s;

    while (input->ReadLine(s)) {
        AddUniq(TCiString(s).c_str());
    }
}

void HashSet::Write(IOutputStream* output) const {
    for (const auto& it : *this) {
        *output << it.first << "\n";
    }
}

#ifdef TEST_STRHASH
#include <ctime>
#include <fstream>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main(int argc, char* argv[]) {
    if (argc < 2) {
        printf("usage: stoplist <stop-words file ...\n");
        exit(EXIT_FAILURE); // FreeBSD: EX_USAGE
    }
    Hash hash;
    hash.Read(cin);
    for (--argc, ++argv; argc > 0; --argc, ++argv) {
        ifstream input(argv[0]);
        if (!input.good()) {
            perror(argv[0]);
            continue;
        }
        TCiString s;
        while (input >> s) {
            if (!hash.Has(s))
                cout << s << "\n";
            else
                cout << "[[" << s << "]]"
                     << "\n";
        }
    }
    return EXIT_SUCCESS; // EX_OK
}

#endif