-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtries.c
More file actions
164 lines (145 loc) · 4.45 KB
/
tries.c
File metadata and controls
164 lines (145 loc) · 4.45 KB
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
#include <stdio.h>
#include <string.h>
#include "HsFFI.h"
#ifdef __GLASGOW_HASKELL__
#include "CTries_stub.h"
#endif
///
/// This simply calls `hs_init` with the appropriate parameters.
///
HsBool tries_init(void){
int argc = 2;
char *argv[] = { "+RTS", "-A32m", NULL };
char **pargv = argv;
// Initialize Haskell runtime
hs_init(&argc, &pargv);
return HS_BOOL_TRUE;
}
///
/// This simply calls `hs_exit`.
///
void tries_end(void){
hs_exit();
}
///
/// A wrapper that calls `showC` which is a C wrapper around the `show` function
/// and prints the resulting string using `printf`.
void print_trie(HsStablePtr trie)
{
printf("%s\n\n", showC(trie));
}
///
/// A wrapper that calls `emptyTrieC` to get a stable pointer to an `EmptyTrie`.
///
HsStablePtr get_empty_trie(void)
{
return emptyTrieC();
}
///
/// A wrapper that calls `rootNodeC` to get a stable pointer to a root node.
///
HsStablePtr get_root_node(void)
{
return rootNodeC();
}
///
/// A wrapper that calls `getNextNodeC` with a string and a stable pointer and
/// returns a stable pointer to the resulting `Trie`
///
HsStablePtr get_next_node(char* prefix, HsStablePtr trie)
{
return getNextNodeC(prefix, trie);
}
///
/// A wrapper that calls `getNodeC` with a string and a stable pointer to a `Trie`
/// and returns a stable pointer to the resulting `Trie`.
///
HsStablePtr get_node(char* prefix, HsStablePtr trie)
{
return getNodeC(prefix, trie);
}
///
/// A wrapper that calls `isInTrieC` with a string and a stable pointer to a `Trie`
/// and returns an integer which is 1 if True and 0 if False.
///
int is_in_trie(char* prefix, HsStablePtr trie)
{
return isInTrieC(prefix, trie);
}
///
/// A wrapper that calls `getMaxWeightC` with a stable pointer to a `Trie` and
/// return the maximum weight in that `Trie` as a `double`.
///
double get_max_weight(HsStablePtr trie)
{
return getMaxWeightC(trie);
}
///
/// A wrapper that calls `insertPrefixC` with a double, a string, and a stable
/// pointer to a `Trie` and returns a stable pointer to the resulting
/// `Trie`.
///
HsStablePtr insert_prefix(double weight, char* word, HsStablePtr trie)
{
return insertPrefixC(weight, word, trie);
}
///
/// A wrapper that calls `insertWordsC` with an array of doubles, an array of
/// strings, an integer, and a stable pointer to a `Trie`, and returns a stable
/// pointer to the constructed `Trie`.
///
HsStablePtr insert_words(double weights[], char** words,
int num_words, HsStablePtr trie)
{
return insertWordsC(weights, words, num_words, trie);
}
///
/// A wrapper that calls `trieFromListC` with an array of doubles, an array of
/// strings, an integer, and returns a stable pointer to the constructed `Trie`.
///
HsStablePtr trie_from_arrays(double weights[], char** words,
int num_words)
{
return trieFromListC(weights, words, num_words);
}
///
/// A wrapper that calls `removePrefixC` with a string and a stable pointer to a
/// `Trie` and returns a stable pointer to the `Trie` with the given node removed.
///
HsStablePtr remove_prefix(char* prefix, HsStablePtr trie)
{
return removePrefixC(prefix, trie);
}
///
/// A wrapper that calls `readTermsC` with a string and returns a stable pointer to the
/// `Trie` built from the specified file.
///
HsStablePtr read_terms(char* file_path)
{
return readTermsC(file_path);
}
///
/// A wrapper that calls `autocompleteByReferenceC` with an array of doubles
/// to be modified, an array of strings to be modified, a string, a stable pointer
/// to a `Trie`, and an integer. This function modifies the arguments `weights_out`
/// and `words_out` by reference to include the results of `autocomplete`.
///
void autocomplete(double* weights_out, char** words_out,
char* word, HsStablePtr trie, int k)
{
autocompleteByReferenceC(weights_out, words_out, word, trie, k);
}
///
/// A wrapper that calls `slowcompleteByReferenceC` with an array of doubles
/// to be modified, an array of strings to be modified, an integer, a string, an
/// array of doubles, an array of strings, and an integer.
/// This function modifies the arguments `weights_out` and `words_out` by
/// reference to include the results of `slowcomplete`.
///
void slowcomplete(double* weights_out, char** words_out,
int num_words, char* prefix, double* weights_in,
char** words_in, int k)
{
slowCompleteByReferenceC(weights_out, words_out, num_words,
prefix, weights_in, words_in, k);
}