File: kui_tree.cpp

package info (click to toggle)
cgdb 0.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,468 kB
  • sloc: cpp: 12,169; ansic: 10,042; sh: 4,383; exp: 640; makefile: 197
file content (257 lines) | stat: -rw-r--r-- 6,042 bytes parent folder | download | duplicates (2)
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
#include <stdlib.h>
#include <string.h>

#include <memory>
#include <map>
#include "kui_tree.h"

/* struct kui_tree_node {{{ */

struct kui_tree_node;
typedef std::shared_ptr<kui_tree_node> KuiTreeNodeSPtr;
typedef std::map<int, KuiTreeNodeSPtr> KuiTreeNodeMap;

/**
 * A node in a kui tree.
 */
struct kui_tree_node {

    kui_tree_node() : key(0), macro_value(NULL) {}

    /* The keyboard key this node represents. */
    int key;

    /* If non-null, this node represents the macro value reached.
     * Otherwise, this node is not the end of the macro.
     */
    void *macro_value;

    /**
     * The children macros from this nodes perspective.
     */
    KuiTreeNodeMap children;

    private:
        kui_tree_node(const kui_tree_node &other);
        kui_tree_node &operator=(const kui_tree_node &other);
};

/**
 * Inserts a map into the tree.
 *
 * @param node
 * The node to insert the map into
 *
 * @param klist
 * The key sequence that will trigger the key mapping substitution
 * The last item in the list will have a 0 value
 *
 * @param data
 * The substitution that should take place when the key mapping is reached.
 *
 * @return
 * 0 on success or -1 on error
 */
static int kui_tree_node_insert(KuiTreeNodeSPtr node, int *klist, void *data)
{
    if (!node) {
        return -1;
    }

    // If at the end of the list, done
    if (klist[0] == 0) {
        node->macro_value = data;
        return 0;
    } else {
        KuiTreeNodeSPtr new_node;
        KuiTreeNodeMap::const_iterator iter = node->children.find(klist[0]);

        if (iter == node->children.end()) {
            new_node = KuiTreeNodeSPtr(new kui_tree_node());
            new_node->key = klist[0];

            node->children.insert(std::make_pair(klist[0], new_node));
        } else {
            new_node = iter->second;
        }

        return kui_tree_node_insert(new_node, &klist[1], data);
    }
}

/**
 * Delete a map from the tree.
 *
 * @param node
 * The node to remove the map from
 *
 * @param klist
 * The key sequence to delete.
 * The last item in the list will have a 0 value
 */
static int kui_tree_node_delete(KuiTreeNodeSPtr node, int *klist)
{
    auto iter = node->children.find(klist[0]);
    if (iter == node->children.end()) {
        return -1;
    } else {
        // Delete the requested mapping
        if (klist[1] == 0) {
            iter->second->macro_value = NULL;

            // The found node (iter->second) is the leaf node
            // for this mapping. However, other mappings may be require
            // this node to continue existing. For example,
            //   imap u1 a
            //   imap u12 b
            //   iunmap u1
            // When unmapping u1, you'll want to keep the node for 1
            // because the u12 mapping still requires it.
            if (iter->second->children.size() == 0) {
                node->children.erase(iter);
            }
        } else {
            kui_tree_node_delete(iter->second, &klist[1]);
            if (iter->second->children.size() == 0 &&
                iter->second->macro_value == NULL) {
                node->children.erase(iter);
            }
        }
    }

    return 0;
}

/* }}} */

/* struct kui_tree {{{ */

/**
 * This data structure is capable of storing a set of maps internally in such a
 * way that it is easy to see what maps are active if a char at a time is fed
 * to this structure.
 *
 * Also, it can determine what mapping was reached if one was found.
 */
struct kui_tree {

    /* The root of the tree */
    KuiTreeNodeSPtr root;

    /* The current position pointing into the tree (while looking for a map) */
    KuiTreeNodeSPtr cur;

    /* The last node found while looking for a map. */
    /* This happens because maps can be subsets of other maps. */
    KuiTreeNodeSPtr found_node;

    /* The internal state of the tree ( still looking, map found, not found ) */
    enum kui_tree_state state;
    /* If a map was found at all, this is set to 1 while looking, otherwise 0. */
    int found;
};

int kui_tree_destroy(struct kui_tree *ktree)
{
    delete ktree;
    return 0;
}

struct kui_tree *kui_tree_create(void)
{
    struct kui_tree *ktree = new kui_tree();;
    ktree->root = KuiTreeNodeSPtr(new kui_tree_node());

    return ktree;
}

int kui_tree_insert(struct kui_tree *ktree, int *klist, void *data)
{
    return kui_tree_node_insert(ktree->root, klist, data);
}

int kui_tree_delete(struct kui_tree *ktree, int *klist)
{
    kui_tree_node_delete(ktree->root, klist);
    return 0;
}

int kui_tree_reset_state(struct kui_tree *ktree)
{
    if (!ktree)
        return -1;

    ktree->cur = ktree->root;
    ktree->state = KUI_TREE_MATCHING;
    ktree->found = 0;
    ktree->found_node = NULL;

    return 0;
}

int kui_tree_finalize_state(struct kui_tree *ktree)
{
    if (!ktree)
        return -1;

    if (ktree->found)
        ktree->state = KUI_TREE_FOUND;

    return 0;
}

int kui_tree_get_state(struct kui_tree *ktree, enum kui_tree_state *state)
{
    if (!ktree)
        return -1;

    *state = ktree->state;

    return 0;
}

int kui_tree_get_data(struct kui_tree *ktree, void *data)
{
    if (!ktree)
        return -1;

    if (!ktree->found)
        return -1;

    memcpy(data, &ktree->found_node->macro_value, sizeof (void *));

    return 0;
}

int kui_tree_push_key(struct kui_tree *ktree, int key, int *map_found)
{
    *map_found = 0;

    if (ktree->state != KUI_TREE_MATCHING)
        return -1;

    /* Check to see if this key matches */
    auto iter = ktree->cur->children.find(key);

    /* Not found */
    if (iter == ktree->cur->children.end()) {
        ktree->state = KUI_TREE_NOT_FOUND;
        ktree->cur = NULL;
    } else {
        ktree->cur = iter->second;

        if (iter->second->children.size() == 0) {
            ktree->state = KUI_TREE_FOUND;
        }

        if (iter->second->macro_value) {
            ktree->found = 1;
            ktree->found_node = iter->second;
            *map_found = 1;
        }
    }

    return 0;
}

/* }}} */