forked from pytries/datrie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie_state_get_terminal_data.patch
59 lines (55 loc) · 1.65 KB
/
trie_state_get_terminal_data.patch
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
diff --git a/libdatrie/datrie/trie.c b/libdatrie/datrie/trie.c
index 2fadd8a..37e95a6 100644
--- a/libdatrie/datrie/trie.c
+++ b/libdatrie/datrie/trie.c
@@ -1084,6 +1084,42 @@ trie_iterator_get_data (const TrieIterator *iter)
return tail_get_data (s->trie->tail, tail_index);
}
+/**
+ * @brief Get data from terminal state
+ *
+ * @param s : a terminal state
+ *
+ * @return the data associated with the terminal state @a s,
+ * or TRIE_DATA_ERROR if @a s is not a terminal state
+ *
+ * Not available in 'libdatrie'. Part of the 'datrie' Python package.
+ */
+TrieData
+trie_state_get_terminal_data (const TrieState *s)
+{
+ TrieIndex tail_index;
+ TrieIndex index = s->index;
+
+ if (!s)
+ return TRIE_DATA_ERROR;
+
+ if (!s->is_suffix){
+ if (!trie_da_is_separate(s->trie->da, index)) {
+ /* walk to a terminal char to get the data */
+ Bool ret = da_walk (s->trie->da, &index, TRIE_CHAR_TERM);
+ if (!ret) {
+ return TRIE_DATA_ERROR;
+ }
+ }
+ tail_index = trie_da_get_tail_index (s->trie->da, index);
+ }
+ else {
+ tail_index = s->index;
+ }
+
+ return tail_get_data (s->trie->tail, tail_index);
+}
+
/*
vi:ts=4:ai:expandtab
*/
diff --git a/libdatrie/datrie/trie.h b/libdatrie/datrie/trie.h
index 1daa0d9..da16483 100644
--- a/libdatrie/datrie/trie.h
+++ b/libdatrie/datrie/trie.h
@@ -222,6 +222,7 @@ AlphaChar * trie_iterator_get_key (const TrieIterator *iter);
TrieData trie_iterator_get_data (const TrieIterator *iter);
+TrieData trie_state_get_terminal_data (const TrieState *s);
#ifdef __cplusplus
}