2 stringbucket.c - MaLa stringbucket handling
4 Copyright (C) 2004, 2005, Christian Thaeter <chth@gmx.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, contact me.
24 #include "stringbucket.h"
28 void mala_stringbucket_init (MalaStringBucket bucket
,
29 mala_string_cmp_fn cmpfn
,
30 void (*freefn
)(void*))
32 bucket
->bucket
= NULL
;
33 bucket
->cmpfn
= cmpfn
;
34 bucket
->freefn
= freefn
;
35 bucket
->gc_trace
= MALA_NOTRACE
;
39 mala_stringbucket_erase (MalaStringBucket bucket
)
41 tdestroy (bucket
->bucket
, (void(*)(void*)) mala_string_free_forced
);
42 bucket
->bucket
= NULL
;
47 mala_stringbucket_exists_cstr (MalaStringBucket bucket
, const char * cstr
)
49 mala_string tmp
= MALA_STRING_AUTO (cstr
);
50 return !!tfind (&tmp
, &bucket
->bucket
, bucket
->cmpfn
);
54 * remove string from bucket
57 mala_stringbucket_string_remove (MalaString_ref self
)
64 /* self is the last reference in the bucket */
65 if ((*self
)->refcnt
<= 8)
67 if ((*self
)->user
&& (*self
)->bucket
->freefn
)
68 (*self
)->bucket
->freefn ((*self
)->user
);
71 tdelete (*self
, &(*self
)->bucket
->bucket
, (*self
)->bucket
->cmpfn
);
72 (*self
)->bucket
= NULL
;
74 /* buckets counted as reference too */
81 /* make a new string */
83 return *self
= mala_string_new ((*self
)->str
, NULL
);
89 mala_stringbucket_insert (MalaStringBucket bucket
, MalaString_ref s
)
97 /* already in this bucket */
98 if (bucket
&& (*s
)->bucket
== bucket
)
101 /* in another bucket, remove it */
104 mala_stringbucket_string_remove (s
);
108 mala_string tmp
= MALA_STRING_AUTO ((*s
)->str
);
111 loc
= (MalaString_ref
) tsearch (&tmp
, &bucket
->bucket
, bucket
->cmpfn
);
118 (*s
)->bucket
= bucket
;
123 /*string already in bucket*/
124 mala_string_free (*s
);
136 // c-file-style: "gnu"
138 // arch-tag: 84fbd8ab-4e90-4d04-ab70-d9b955dde41c