-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
168 lines (146 loc) · 4.84 KB
/
utils.c
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* libdatrie - Double-Array Trie Library
* Copyright (C) 2013 Theppitak Karoonboonyanan <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* utils.c - Utility functions for datrie test cases
* Created: 2013-10-16
* Author: Theppitak Karoonboonyanan <[email protected]>
*/
#include <datrie/trie.h>
#include "utils.h"
/*---------------------*
* Debugging helpers *
*---------------------*/
void
msg_step (const char *msg)
{
printf ("=> %s...\n", msg);
}
/*-------------------------*
* Trie creation helpers *
*-------------------------*/
static AlphaMap *
en_alpha_map_new (void)
{
AlphaMap *en_map;
en_map = alpha_map_new ();
if (!en_map)
goto err_map_not_created;
if (alpha_map_add_range (en_map, 0x0061, 0x007a) != 0)
goto err_map_created;
return en_map;
err_map_created:
alpha_map_free (en_map);
err_map_not_created:
return NULL;
}
Trie *
en_trie_new (void)
{
AlphaMap *en_map;
Trie *en_trie;
en_map = en_alpha_map_new ();
if (!en_map)
goto err_map_not_created;
en_trie = trie_new (en_map);
if (!en_trie)
goto err_map_created;
alpha_map_free (en_map);
return en_trie;
err_map_created:
alpha_map_free (en_map);
err_map_not_created:
return NULL;
}
/*---------------------------*
* Dict source for testing *
*---------------------------*/
DictRec dict_src[] = {
{(AlphaChar *)L"a", TRIE_DATA_UNREAD},
{(AlphaChar *)L"abacus", TRIE_DATA_UNREAD},
{(AlphaChar *)L"abandon", TRIE_DATA_UNREAD},
{(AlphaChar *)L"accident", TRIE_DATA_UNREAD},
{(AlphaChar *)L"accredit", TRIE_DATA_UNREAD},
{(AlphaChar *)L"algorithm", TRIE_DATA_UNREAD},
{(AlphaChar *)L"ammonia", TRIE_DATA_UNREAD},
{(AlphaChar *)L"angel", TRIE_DATA_UNREAD},
{(AlphaChar *)L"angle", TRIE_DATA_UNREAD},
{(AlphaChar *)L"azure", TRIE_DATA_UNREAD},
{(AlphaChar *)L"bat", TRIE_DATA_UNREAD},
{(AlphaChar *)L"bet", TRIE_DATA_UNREAD},
{(AlphaChar *)L"best", TRIE_DATA_UNREAD},
{(AlphaChar *)L"home", TRIE_DATA_UNREAD},
{(AlphaChar *)L"house", TRIE_DATA_UNREAD},
{(AlphaChar *)L"hut", TRIE_DATA_UNREAD},
{(AlphaChar *)L"king", TRIE_DATA_UNREAD},
{(AlphaChar *)L"kite", TRIE_DATA_UNREAD},
{(AlphaChar *)L"name", TRIE_DATA_UNREAD},
{(AlphaChar *)L"net", TRIE_DATA_UNREAD},
{(AlphaChar *)L"network", TRIE_DATA_UNREAD},
{(AlphaChar *)L"nut", TRIE_DATA_UNREAD},
{(AlphaChar *)L"nutshell", TRIE_DATA_UNREAD},
{(AlphaChar *)L"quality", TRIE_DATA_UNREAD},
{(AlphaChar *)L"quantum", TRIE_DATA_UNREAD},
{(AlphaChar *)L"quantity", TRIE_DATA_UNREAD},
{(AlphaChar *)L"quartz", TRIE_DATA_UNREAD},
{(AlphaChar *)L"quick", TRIE_DATA_UNREAD},
{(AlphaChar *)L"quiz", TRIE_DATA_UNREAD},
{(AlphaChar *)L"run", TRIE_DATA_UNREAD},
{(AlphaChar *)L"tape", TRIE_DATA_UNREAD},
{(AlphaChar *)L"test", TRIE_DATA_UNREAD},
{(AlphaChar *)L"what", TRIE_DATA_UNREAD},
{(AlphaChar *)L"when", TRIE_DATA_UNREAD},
{(AlphaChar *)L"where", TRIE_DATA_UNREAD},
{(AlphaChar *)L"which", TRIE_DATA_UNREAD},
{(AlphaChar *)L"who", TRIE_DATA_UNREAD},
{(AlphaChar *)L"why", TRIE_DATA_UNREAD},
{(AlphaChar *)L"zebra", TRIE_DATA_UNREAD},
{(AlphaChar *)NULL, TRIE_DATA_ERROR},
};
int
dict_src_n_entries (void)
{
return sizeof (dict_src) / sizeof (dict_src[0]) - 1;
}
TrieData
dict_src_get_data (const AlphaChar *key)
{
const DictRec *dict_p;
for (dict_p = dict_src; dict_p->key; dict_p++) {
if (alpha_char_strcmp (dict_p->key, key) == 0) {
return dict_p->data;
}
}
return TRIE_DATA_ERROR;
}
int
dict_src_set_data (const AlphaChar *key, TrieData data)
{
DictRec *dict_p;
for (dict_p = dict_src; dict_p->key; dict_p++) {
if (alpha_char_strcmp (dict_p->key, key) == 0) {
dict_p->data = data;
return 0;
}
}
return -1;
}
/*
vi:ts=4:ai:expandtab
*/