tests update
[mala.git] / engine / stringbucket.h
blob0f14d20a2db34c9b451eb03e597480cd9c0dd3f8
1 /*
2 stringbucket.h - MaLa stringbuckets
4 Copyright (C) 2004, 2005, 2006, 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.
20 #ifndef MALA_STRINGBUCKET_H
21 #define MALA_STRINGBUCKET_H
23 #include "acogc.h"
24 #include "mala_types.h"
25 #include "strings.h"
27 typedef int (*mala_string_cmp_fn) (const void*, const void*);
29 #define MALA_STRING_FWD (mala_string_cmp_fn) mala_string_compare
30 #define MALA_STRING_REV (mala_string_cmp_fn) mala_string_compare_rev
33 strings are looked up in a weak-splay tree, that is a splay tree
34 which contains weak-pointers to the strings,
35 when a splay tree node to a expired string is encountered it will be instantly removed
37 Note that the acogc allows caching and reinstantiating of swept strings
39 struct mala_stringbucketnode_struct
41 acogc_weakref weak_string;
42 MalaStringBucketNode left;
43 MalaStringBucketNode right;
44 MalaStringBucketNode parent; //makes splaying more efficent/easier
48 struct mala_stringbucket_struct
50 MalaStringBucketNode tree;
51 AcogcRoot gcroot;
52 AcogcFactory gcfactory_strings;
53 AcogcFactory gcfactory_splaynodes;
54 mala_string_cmp_fn cmpfn;
59 * StringBucket
62 typedef
63 enum
65 MALA_STRINGBUCKET_FIND, // try to find string
66 MALA_STRINGBUCKET_SEARCH, // search string, create it if not already existing
67 MALA_STRINGBUCKET_ATTACH, // attach string which must be allocated by the caller,
68 // freed by stringbucket when already in the tree or at garbage collection
69 MALA_STRINGBUCKET_REFERENCE // reference string, will not be freed, reference will not be used if string already exists
70 } mala_stringbucket_mode;
73 #ifdef EBUG_ACOGC
74 void
75 mala_stringbucketnode_dump (const char* name, MalaStringBucketNode_ref n, unsigned depth);
76 #else
77 #define mala_stringbucketnode_dump(s,n,d)
78 #endif
80 void
81 mala_stringbucketnode_simpleremove (MalaStringBucketNode_ref n, MalaStringBucket bucket);
83 acogc_mark_result
84 mala_stringbucketnode_acogc_mark (void * p);
86 void
87 mala_stringbucketnode_acogc_initize (void * p);
89 void
90 mala_stringbucketnode_acogc_finalize (void * p);
92 acogc_mark_result
93 mala_stringbucket_acogc_mark (void * bucket);
95 void
96 mala_stringbucket_acogc_initize (void * p);
98 MalaStringBucketNode
99 mala_stringbucket_node_find (MalaStringBucket bucket,
100 const char * what,
101 mala_stringbucket_mode mode);
103 void
104 mala_stringbucketnode_splay (MalaStringBucketNode n, MalaStringBucket bucket);
106 MalaStringBucket
107 mala_stringbucket_new (mala_string_cmp_fn cmpfn,
108 AcogcFactory bucketfactory,
109 AcogcFactory gcfactory_strings,
110 AcogcFactory gcfactory_splaynodes);
112 void
113 mala_stringbucket_init (MalaStringBucket bucket,
114 mala_string_cmp_fn cmpfn,
115 AcogcRoot gcroot,
116 AcogcFactory gcfactory_strings,
117 AcogcFactory gcfactory_splaynodes);
119 static inline MalaString
120 mala_stringbucket_find (MalaStringBucket bucket, const char * what)
122 MalaStringBucketNode ret = mala_stringbucket_node_find (bucket, what, MALA_STRINGBUCKET_FIND);
123 return (MalaString) ret ? acogc_weakref_reinstget (&ret->weak_string) : NULL;
126 static inline MalaString
127 mala_stringbucket_search (MalaStringBucket bucket, const char * what)
129 return (MalaString) acogc_weakref_reinstget
130 (&mala_stringbucket_node_find (bucket, what, MALA_STRINGBUCKET_SEARCH)->weak_string);
133 static inline MalaString
134 mala_stringbucket_attach (MalaStringBucket bucket, const char * what)
136 return (MalaString) acogc_weakref_reinstget
137 (&mala_stringbucket_node_find (bucket, what, MALA_STRINGBUCKET_ATTACH)->weak_string);
140 static inline MalaString
141 mala_stringbucket_reference (MalaStringBucket bucket, const char * what)
143 return (MalaString) acogc_weakref_reinstget
144 (&mala_stringbucket_node_find (bucket, what, MALA_STRINGBUCKET_REFERENCE)->weak_string);
147 #endif /* MALA_STRINGBUCKET_H */
150 // Local Variables:
151 // mode: C
152 // c-file-style: "gnu"
153 // End:
154 // arch-tag: 09e98f6d-4992-4014-a18f-a5e532bdeb27
155 // end_of_file