File: triebench.cpp

package info (click to toggle)
libime 1.1.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,612 kB
  • sloc: cpp: 40,794; ansic: 952; python: 130; sh: 32; makefile: 11
file content (97 lines) | stat: -rw-r--r-- 2,883 bytes parent folder | download
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
/*
 * SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */
#include <unistd.h>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <fcitx-utils/log.h>
#include "libime/core/datrie.h"

using namespace libime;

int main() {
    typedef DATrie<int32_t> TestTrie;
    TestTrie tree;
    std::string key;
    std::unordered_map<std::string, int32_t> map;

    int count = 1;
    // key can be same as other
    while (std::cin >> key) {
        map[key] = count;
        tree.update(key,
                    [count, &map,
                     &key](TestTrie::value_type v) -> TestTrie::value_type {
                        if (v != 0) {
                            // this is a key inserted twice
                            FCITX_ASSERT(map.find(key) != map.end());
                        }
                        // std::cout << key << " " << v << " " << count <<
                        // std::endl;
                        return count;
                    });
        FCITX_ASSERT(tree.exactMatchSearch(key) == count);
        count++;
    }

    std::vector<TestTrie::value_type> d;
    d.resize(tree.size());
    tree.dump(d.data(), d.size());

    FCITX_ASSERT(tree.size() == map.size());
    for (auto &p : map) {
        // std::cout << p.first << " " << tree.exactMatchSearch(p.first) << " "
        // << p.second <<
        // std::endl;
        FCITX_ASSERT(tree.exactMatchSearch(p.first) == p.second);
    }

    std::string tempKey;
    size_t foreach_count = 0;
    tree.foreach([&tree, &map, &tempKey, &foreach_count](
                     TestTrie::value_type value, size_t len, uint64_t pos) {
        (void)value;
        tree.suffix(tempKey, len, pos);
        FCITX_ASSERT(map.find(tempKey) != map.end());
        FCITX_ASSERT(tree.exactMatchSearch(tempKey) == value);
        FCITX_ASSERT(map[tempKey] == value);
        tree.update(tempKey, [](int32_t v) { return v + 1; });
        foreach_count++;
        return true;
    });

    tree.erase(map.begin()->first);
    FCITX_ASSERT(tree.size() == foreach_count - 1);

    tree.save("trie_data");

    tree.clear();

    FCITX_ASSERT(!tree.erase(map.begin()->first));
    FCITX_ASSERT(tree.empty());
    decltype(tree) trie2("trie_data");
    using std::swap;
    swap(tree, trie2);

    foreach_count = 0;
    tree.foreach([&tree, &map, &tempKey,
                  &foreach_count](int32_t value, size_t len, uint64_t pos) {
        (void)value;
        tree.suffix(tempKey, len, pos);
        FCITX_ASSERT(map.find(tempKey) != map.end());
        FCITX_ASSERT(tree.exactMatchSearch(tempKey) == value);
        FCITX_ASSERT(map[tempKey] + 1 == value);
        foreach_count++;
        return true;
    });

    FCITX_ASSERT(tree.size() == foreach_count);

    return 0;
}