1 /*** dict.c -- generic dict (single valued mapping) service
3 * Copyright (C) 2007, 2008 Sebastian Freundt
5 * Author: Sebastian Freundt <hroptatyr@sxemacs.org>
7 * This file is part of SXEmacs.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the author nor the names of any contributors
21 * may be used to endorse or promote products derived from this
22 * software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #ifndef INCLUDED_dict_h_
42 #define INCLUDED_dict_h_
48 #ifdef ALL_DEBUG_FLAGS
49 #undef DICT_DEBUG_FLAG
50 #define DICT_DEBUG_FLAG
53 #define __DICT_DEBUG__(args...) fprintf(stderr, "DICT " args)
54 #ifndef DICT_DEBUG_FLAG
55 #define DICT_DEBUG(args...)
57 #define DICT_DEBUG(args...) __DICT_DEBUG__(args)
59 #define DICT_CRITICAL(args...) __DICT_DEBUG__("CRITICAL: " args)
61 typedef struct dict_s *dict_t;
62 typedef struct dict_iter_s *dict_iter_t;
63 typedef struct dict_impl_s *dict_impl_t;
65 typedef size_t(*dict_size_f)(dict_t);
66 typedef Lisp_Object(*dict_put_f)(dict_t, Lisp_Object, Lisp_Object);
67 typedef Lisp_Object(*dict_get_f)(dict_t, Lisp_Object, Lisp_Object);
68 typedef Lisp_Object(*dict_remove_f)(dict_t, Lisp_Object);
69 typedef void(*dict_iter_init_f)(dict_t, dict_iter_t);
70 typedef void(*dict_iter_next_f)(dict_iter_t, Lisp_Object*, Lisp_Object*);
71 typedef void(*dict_iter_fini_f)(dict_iter_t);
73 extern_inline dict_impl_t dict_impl(const void *obj);
74 extern_inline size_t dict_size(const dict_t);
75 extern_inline bool dict_empty_p(const dict_t);
76 extern_inline dict_t make_dict(void*);
77 extern_inline Lisp_Object put_dict(dict_t, Lisp_Object key, Lisp_Object val);
78 extern_inline Lisp_Object get_dict(dict_t, Lisp_Object key, Lisp_Object _default);
79 extern_inline Lisp_Object remove_dict(dict_t, Lisp_Object key);
81 extern_inline void dict_iter_init(dict_t, dict_iter_t);
82 extern_inline void dict_iter_next(dict_iter_t, Lisp_Object*, Lisp_Object*);
83 extern_inline void dict_iter_fini(dict_iter_t);
85 extern void dict_LTX_init(void);
86 extern void dict_LTX_reinit(void);
87 extern void dict_LTX_deinit(void);
94 dict_remove_f remove_f;
95 dict_iter_init_f iter_init_f;
96 dict_iter_next_f iter_next_f;
97 dict_iter_fini_f iter_fini_f;
101 struct lcrecord_header lhreader;
103 /* just an anonymous one for the seq implementation */
106 /* the dual approach */
117 extern_inline dict_impl_t
118 dict_impl(const void *obj)
120 /* a dict is an association set (aset) in category speak */
121 if (INTP((Lisp_Object)obj) || CHARP((Lisp_Object)obj)) {
124 return cat_morphism(obj, cat_mk_aset);
128 dict_size(const dict_t d)
130 dict_impl_t di = dict_impl(d);
133 return di->size_f(d);
135 /* is this right here? */
136 return wrong_type_argument(Qdictp, (Lisp_Object)d);
141 dict_empty_p(dict_t d)
143 return dict_size(d) == 0;
152 extern_inline Lisp_Object
153 put_dict(dict_t d, Lisp_Object key, Lisp_Object val)
155 return dict_impl(d)->put_f(d, key, val);
158 extern_inline Lisp_Object
159 get_dict(dict_t d, Lisp_Object key, Lisp_Object _default)
161 return dict_impl(d)->get_f(d, key, _default);
164 extern_inline Lisp_Object
165 remove_dict(dict_t d, Lisp_Object key)
167 return dict_impl(d)->remove_f(d, key);
171 dict_iter_init(dict_t d, dict_iter_t di)
173 dict_impl(d)->iter_init_f(d, di);
178 dict_iter_next(dict_iter_t di, Lisp_Object *key, Lisp_Object *val)
180 dict_impl(di->dict)->iter_next_f(di, key, val);
185 dict_iter_fini(dict_iter_t di)
187 dict_impl(di->dict)->iter_fini_f(di);
191 #endif /* INCLUDED_dict_h_ */