vec.c - bmf - bmf (Bayesian Mail Filter) 0.9.4 fork + patches | |
git clone git://git.codemadness.org/bmf | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
vec.c (2589B) | |
--- | |
1 /* $Id: vec.c,v 1.4 2002/10/20 18:19:17 tommy Exp $ */ | |
2 | |
3 /* | |
4 * Copyright (c) 2002 Tom Marshall <[email protected]> | |
5 * | |
6 * This program is free software. It may be distributed under the terms | |
7 * in the file LICENSE, found in the top level of the distribution. | |
8 * | |
9 * vec.c: vector functions for bmf. | |
10 * Vectors are used to hold token lists for input data and flatfile da… | |
11 * entries in standalone mode. They dramatically reduce the number of… | |
12 * mallocs and, if used properly, have no performance penalty over fan… | |
13 * data structures. | |
14 */ | |
15 | |
16 #include "config.h" | |
17 #include "dbg.h" | |
18 #include "str.h" | |
19 #include "lex.h" | |
20 #include "vec.h" | |
21 | |
22 /***********************************************************************… | |
23 * vector | |
24 */ | |
25 | |
26 void | |
27 vec_create(vec_t * pthis) | |
28 { | |
29 pthis->nalloc = VEC_INITIAL_SIZE; | |
30 pthis->nitems = 0; | |
31 /* TODO: check malloc() */ | |
32 pthis->pitems = malloc(VEC_INITIAL_SIZE * sizeof(str_t)); | |
33 } | |
34 | |
35 void | |
36 vec_destroy(vec_t * pthis) | |
37 { | |
38 pthis->nitems = 0; | |
39 free(pthis->pitems); | |
40 pthis->pitems = NULL; | |
41 } | |
42 | |
43 static void | |
44 vec_setsize(vec_t * pthis, uint nsize) | |
45 { | |
46 uint nnewalloc; | |
47 str_t *pnewitems; | |
48 uint n; | |
49 | |
50 if (nsize > pthis->nalloc) { | |
51 nnewalloc = pthis->nalloc * 2; | |
52 if (nnewalloc < nsize) | |
53 nnewalloc = nsize; | |
54 pnewitems = (str_t *) realloc(pthis->pitems, nnewalloc *… | |
55 if (pnewitems == NULL) { | |
56 perror("realloc()"); | |
57 exit(2); | |
58 } | |
59 for (n = pthis->nitems; n < nsize; n++) { | |
60 str_create(&pnewitems[n]); | |
61 } | |
62 pthis->pitems = pnewitems; | |
63 pthis->nalloc = nnewalloc; | |
64 } | |
65 } | |
66 | |
67 void | |
68 vec_addtail(vec_t * pthis, str_t * pstr) | |
69 { | |
70 vec_setsize(pthis, pthis->nitems + 1); | |
71 pthis->pitems[pthis->nitems] = *pstr; | |
72 pthis->nitems++; | |
73 } | |
74 | |
75 void | |
76 vec_first(vec_t * pthis, veciter_t * piter) | |
77 { | |
78 piter->plist = pthis; | |
79 piter->index = 0; | |
80 } | |
81 | |
82 /***********************************************************************… | |
83 * sorted vector | |
84 */ | |
85 | |
86 static int | |
87 svec_compare(const void *p1, const void *p2) | |
88 { | |
89 return str_casecmp((const str_t *) p1, (const str_t *) p2); | |
90 } | |
91 | |
92 void | |
93 svec_sort(vec_t * pthis) | |
94 { | |
95 if (pthis->nitems > 1) { | |
96 qsort(pthis->pitems, pthis->nitems, sizeof(str_t), svec_… | |
97 } | |
98 } | |
99 | |
100 /***********************************************************************… | |
101 * vector iterator | |
102 */ | |
103 | |
104 void | |
105 veciter_destroy(veciter_t * pthis) | |
106 { | |
107 /* empty */ | |
108 } | |
109 | |
110 str_t * | |
111 veciter_get(veciter_t * pthis) | |
112 { | |
113 if (pthis->plist == NULL || pthis->index >= pthis->plist->nitems… | |
114 return NULL; | |
115 } | |
116 return &pthis->plist->pitems[pthis->index]; | |
117 } | |
118 | |
119 bool_t | |
120 veciter_next(veciter_t * pthis) | |
121 { | |
122 pthis->index++; | |
123 if (pthis->index == pthis->plist->nitems) { | |
124 return false; | |
125 } | |
126 return true; | |
127 } |