Merge branch 'improve-dict-api'
This commit is contained in:
commit
d4f64815b4
|
@ -144,9 +144,9 @@ Symbol to Object table. Internally it is implemented on hash-table.
|
|||
|
||||
Note that dictionary is not a weak map; if you are going to make a highly memory-consuming program with dictionaries, you should know that dictionaries keep their bound objects and never let them free until you explicitly deletes bindings.
|
||||
|
||||
- **(dictionary)**
|
||||
- **(dictionary . plist)**
|
||||
|
||||
Returns a newly allocated empty dictionary. In the future, it is planned to extend this function to take optional arguments for initial key/values.
|
||||
Returns a newly allocated empty dictionary. The dictionary is initialized with the content of plist.
|
||||
|
||||
- **(dictionary? obj)**
|
||||
|
||||
|
@ -168,6 +168,21 @@ Note that dictionary is not a weak map; if you are going to make a highly memory
|
|||
|
||||
Returns the number of registered elements in dict.
|
||||
|
||||
- **(dicitonary-map proc dict)**
|
||||
|
||||
Perform mapping action onto dictionary object. ``proc`` is called by a sequence ``(proc key val)``.
|
||||
|
||||
- **(dictionary-for-each proc dict)**
|
||||
|
||||
Similar to ``dictionary-map``, but discards the result.
|
||||
|
||||
- **(dictionary->plist dict)**
|
||||
- **(plist->dictionary plist)**
|
||||
- **(dictionary->alist dict)**
|
||||
- **(alist->dictionary alist)**
|
||||
|
||||
Conversion between dictionary and alist/plist.
|
||||
|
||||
|
||||
(picrin user)
|
||||
-------------
|
||||
|
|
|
@ -19,6 +19,11 @@ struct pic_dict {
|
|||
|
||||
struct pic_dict *pic_dict_new(pic_state *);
|
||||
|
||||
pic_value pic_dict_ref(pic_state *, struct pic_dict *, pic_sym);
|
||||
void pic_dict_set(pic_state *, struct pic_dict *, pic_sym, pic_value);
|
||||
void pic_dict_del(pic_state *, struct pic_dict *, pic_sym);
|
||||
size_t pic_dict_size(pic_state *, struct pic_dict *);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
list(APPEND PICLIB_SCHEME_LIBS
|
||||
${PROJECT_SOURCE_DIR}/piclib/built-in.scm
|
||||
${PROJECT_SOURCE_DIR}/piclib/picrin/dictionary.scm
|
||||
${PROJECT_SOURCE_DIR}/piclib/srfi/1.scm
|
||||
${PROJECT_SOURCE_DIR}/piclib/srfi/8.scm
|
||||
${PROJECT_SOURCE_DIR}/piclib/srfi/26.scm
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
(define-library (picrin dictionary)
|
||||
(import (scheme base))
|
||||
|
||||
(define (dictionary-map proc dict)
|
||||
(let ((kvs '()))
|
||||
(dictionary-for-each
|
||||
(lambda (key val)
|
||||
(set! kvs (cons (proc key val) kvs)))
|
||||
dict)
|
||||
(reverse kvs)))
|
||||
|
||||
(define (dictionary->plist dict)
|
||||
(let ((kvs '()))
|
||||
(dictionary-for-each
|
||||
(lambda (key val)
|
||||
(set! kvs (cons val (cons key kvs))))
|
||||
dict)
|
||||
(reverse kvs)))
|
||||
|
||||
(define (plist->dictionary plist)
|
||||
(let ((dict (make-dictionary)))
|
||||
(do ((kv plist (cddr kv)))
|
||||
((null? kv)
|
||||
dict)
|
||||
(dictionary-set! dict (car kv) (cadr kv)))))
|
||||
|
||||
(define (dictionary->alist dict)
|
||||
(dictionary-map
|
||||
(lambda (key val)
|
||||
(cons key val))
|
||||
dict))
|
||||
|
||||
(define (alist->dictionary alist)
|
||||
(let ((dict (make-dictionary)))
|
||||
(do ((kv alist (cdr kv)))
|
||||
((null? kv)
|
||||
dict)
|
||||
(dictionary-set! dict (car kv) (cdr kv)))))
|
||||
|
||||
(define (dictionary . plist)
|
||||
(plist->dictionary plist))
|
||||
|
||||
(export dictionary
|
||||
dictionary-map
|
||||
dictionary->plist
|
||||
plist->dictionary
|
||||
dictionary->alist
|
||||
alist->dictionary))
|
75
src/dict.c
75
src/dict.c
|
@ -16,6 +16,44 @@ pic_dict_new(pic_state *pic)
|
|||
return dict;
|
||||
}
|
||||
|
||||
pic_value
|
||||
pic_dict_ref(pic_state *pic, struct pic_dict *dict, pic_sym key)
|
||||
{
|
||||
xh_entry *e;
|
||||
|
||||
e = xh_get_int(&dict->hash, key);
|
||||
if (! e) {
|
||||
pic_errorf(pic, "element not found for a key: ~s", pic_sym_value(key));
|
||||
}
|
||||
return xh_val(e, pic_value);
|
||||
}
|
||||
|
||||
void
|
||||
pic_dict_set(pic_state *pic, struct pic_dict *dict, pic_sym key, pic_value val)
|
||||
{
|
||||
UNUSED(pic);
|
||||
|
||||
xh_put_int(&dict->hash, key, &val);
|
||||
}
|
||||
|
||||
size_t
|
||||
pic_dict_size(pic_state *pic, struct pic_dict *dict)
|
||||
{
|
||||
UNUSED(pic);
|
||||
|
||||
return dict->hash.count;
|
||||
}
|
||||
|
||||
void
|
||||
pic_dict_del(pic_state *pic, struct pic_dict *dict, pic_sym key)
|
||||
{
|
||||
if (xh_get_int(&dict->hash, key) == NULL) {
|
||||
pic_errorf(pic, "no slot named ~s found in dictionary", pic_sym_value(key));
|
||||
}
|
||||
|
||||
xh_del_int(&dict->hash, key);
|
||||
}
|
||||
|
||||
static pic_value
|
||||
pic_dict_dict(pic_state *pic)
|
||||
{
|
||||
|
@ -43,15 +81,10 @@ pic_dict_dict_ref(pic_state *pic)
|
|||
{
|
||||
struct pic_dict *dict;
|
||||
pic_sym key;
|
||||
xh_entry *e;
|
||||
|
||||
pic_get_args(pic, "dm", &dict, &key);
|
||||
|
||||
e = xh_get_int(&dict->hash, key);
|
||||
if (! e) {
|
||||
pic_errorf(pic, "element not found for a key: ~s", pic_sym_value(key));
|
||||
}
|
||||
return xh_val(e, pic_value);
|
||||
return pic_dict_ref(pic, dict , key);
|
||||
}
|
||||
|
||||
static pic_value
|
||||
|
@ -63,7 +96,7 @@ pic_dict_dict_set(pic_state *pic)
|
|||
|
||||
pic_get_args(pic, "dmo", &dict, &key, &val);
|
||||
|
||||
xh_put_int(&dict->hash, key, &val);
|
||||
pic_dict_set(pic, dict, key, val);
|
||||
|
||||
return pic_none_value();
|
||||
}
|
||||
|
@ -76,11 +109,7 @@ pic_dict_dict_del(pic_state *pic)
|
|||
|
||||
pic_get_args(pic, "dm", &dict, &key);
|
||||
|
||||
if (xh_get_int(&dict->hash, key) == NULL) {
|
||||
pic_errorf(pic, "no slot named ~s found in dictionary", pic_sym_value(key));
|
||||
}
|
||||
|
||||
xh_del_int(&dict->hash, key);
|
||||
pic_dict_del(pic, dict, key);
|
||||
|
||||
return pic_none_value();
|
||||
}
|
||||
|
@ -92,18 +121,36 @@ pic_dict_dict_size(pic_state *pic)
|
|||
|
||||
pic_get_args(pic, "d", &dict);
|
||||
|
||||
return pic_int_value(dict->hash.count);
|
||||
return pic_int_value(pic_dict_size(pic, dict));
|
||||
}
|
||||
|
||||
static pic_value
|
||||
pic_dict_dict_for_each(pic_state *pic)
|
||||
{
|
||||
struct pic_proc *proc;
|
||||
struct pic_dict *dict;
|
||||
xh_iter it;
|
||||
|
||||
pic_get_args(pic, "ld", &proc, &dict);
|
||||
|
||||
xh_begin(&it, &dict->hash);
|
||||
while (xh_next(&it)) {
|
||||
pic_apply2(pic, proc, pic_sym_value(xh_key(it.e, pic_sym)), xh_val(it.e, pic_value));
|
||||
}
|
||||
|
||||
return pic_none_value();
|
||||
}
|
||||
|
||||
void
|
||||
pic_init_dict(pic_state *pic)
|
||||
{
|
||||
pic_deflibrary ("(picrin dictionary)") {
|
||||
pic_defun(pic, "dictionary", pic_dict_dict);
|
||||
pic_defun(pic, "make-dictionary", pic_dict_dict);
|
||||
pic_defun(pic, "dictionary?", pic_dict_dict_p);
|
||||
pic_defun(pic, "dictionary-ref", pic_dict_dict_ref);
|
||||
pic_defun(pic, "dictionary-set!", pic_dict_dict_set);
|
||||
pic_defun(pic, "dictionary-delete", pic_dict_dict_del);
|
||||
pic_defun(pic, "dictionary-size", pic_dict_dict_size);
|
||||
pic_defun(pic, "dictionary-for-each", pic_dict_dict_for_each);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue