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
->destroying
= 0;
40 twalk_stringdata_destroy (const void *nodep
,
46 mala_string_data_free (*(MalaString_ref
)nodep
);
51 mala_stringbucket_erase (MalaStringBucket bucket
)
53 bucket
->destroying
= 1;
55 /* it makes the string destructor a bit easier when we destroy data on a single pass */
56 twalk (bucket
->bucket
, twalk_stringdata_destroy
);
58 tdestroy (bucket
->bucket
, (void(*)(void*)) mala_string_free
);
59 bucket
->bucket
= NULL
;
60 bucket
->destroying
= 0;
65 mala_stringbucket_exists_cstr (MalaStringBucket bucket
, const char * cstr
)
67 mala_string tmp
= MALA_STRING_AUTO (cstr
);
68 return !!tfind (&tmp
, &bucket
->bucket
, bucket
->cmpfn
);
72 * remove string from bucket
75 mala_stringbucket_string_remove (MalaString_ref self
)
82 /* self is the last reference in the bucket */
83 if ((*self
)->refcnt
<= 8)
85 if ((*self
)->user
&& (*self
)->bucket
->freefn
)
86 (*self
)->bucket
->freefn ((*self
)->user
);
89 tdelete (*self
, &(*self
)->bucket
->bucket
, (*self
)->bucket
->cmpfn
);
90 (*self
)->bucket
= NULL
;
92 /* buckets counted as reference too */
99 /* make a new string */
100 (*self
)->refcnt
-= 4;
101 return *self
= mala_string_new ((*self
)->str
, NULL
);
107 mala_stringbucket_insert (MalaStringBucket bucket
, MalaString_ref s
)
115 /* already in this bucket */
116 if (bucket
&& (*s
)->bucket
== bucket
)
119 /* in another bucket, remove it */
122 mala_stringbucket_string_remove (s
);
126 mala_string tmp
= MALA_STRING_AUTO ((*s
)->str
);
129 loc
= (MalaString_ref
) tsearch (&tmp
, &bucket
->bucket
, bucket
->cmpfn
);
136 (*s
)->bucket
= bucket
;
141 /*string already in bucket*/
142 mala_string_free (*s
);
154 // c-file-style: "gnu"
156 // arch-tag: 84fbd8ab-4e90-4d04-ab70-d9b955dde41c