-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathvword.c
82 lines (64 loc) · 1.44 KB
/
vword.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
/* vword.c - Functions operating on a word
*
* Copyright (C) Navaneeth.K.N
*
* This is part of libvarnam. See LICENSE.txt for the license
*/
#include <assert.h>
#include <string.h>
#include "vword.h"
#include "vtypes.h"
#include "varray.h"
static void
initialize_word(varnam *handle, vword *word, const char *text, int confidence)
{
strbuf *w;
assert (word);
w = get_pooled_string (handle);
strbuf_add (w, text);
word->text = strbuf_to_s (w);
word->confidence = confidence;
}
vword*
Word(varnam *handle, const char *text, int confidence)
{
vword *word = xmalloc (sizeof (vword));
initialize_word (handle, word, text, confidence);
return word;
}
vword*
get_pooled_word(varnam *handle, const char *text, int confidence)
{
vword *word;
if (v_->words_pool == NULL)
v_->words_pool = vpool_init ();
word = vpool_get (v_->words_pool);
if (word == NULL)
{
word = Word (handle, text, confidence);
vpool_add (v_->words_pool, word);
}
else
initialize_word (handle, word, text, confidence);
return word;
}
bool
word_equals (void *left, void *right)
{
vword *lhs, *rhs;
if (left == NULL)
return false;
if (right == NULL)
return false;
lhs = (vword*) left;
rhs = (vword*) right;
return strcmp (lhs->text, rhs->text) == 0;
}
void
destroy_word(void *word)
{
if (word != NULL)
{
xfree((vword*) word);
}
}